Adding BufferUtils.createColorBuffer()

We have createVector2Buffer(), createVector3Buffer() but no createColorBuffer()

It would be convenient and increase readability.



[patch]

Index: src/core/com/jme3/util/BufferUtils.java

===================================================================

— src/core/com/jme3/util/BufferUtils.java (revision 5887)

+++ src/core/com/jme3/util/BufferUtils.java (working copy)

@@ -108,6 +108,27 @@

buff.flip();

return buff;

}

+

  • /**
  • * Generate a new FloatBuffer using the given array of ColorRGBA objects.<br />
    
  • * The FloatBuffer will be 4 * data.length long and contain the vector data<br />
    
  • * as data[0].r, data[0].g, data[0].b, data[0].a, data[1].r... etc.<br />
    
  • *<br />
    
  • * @param data array of Vector3f objects to place into a new FloatBuffer<br />
    
  • */<br />
    
  • public static FloatBuffer createColorBuffer(ColorRGBA … data) {
  •    if (data == null) return null;<br />
    
  •    FloatBuffer buff = createColorBuffer(data.length);<br />
    
  •    for (int x = 0; x < data.length; x++) {<br />
    
  •        if (data[x] != null)<br />
    
  •            buff.put(data[x].getRed()).put(data[x].getGreen())<br />
    
  •                .put(data[x].getBlue()).put(data[x].getAlpha());<br />
    
  •        else<br />
    
  •            buff.put(0).put(0).put(0).put(0);<br />
    
  •    }<br />
    
  •    buff.flip();<br />
    
  •    return buff;<br />
    
  • }



    /**
  • Generate a new FloatBuffer using the given array of Quaternion objects.

    @@ -178,6 +199,41 @@

    }



    /**
  • * Create a new FloatBuffer of an appropriate size to hold the specified<br />
    
  • * number of ColorRGBA object data.<br />
    
  • *<br />
    
  • * @param vertices<br />
    
  • *            number of vertices that need to be held by the newly created<br />
    
  • *            buffer<br />
    
  • * @return the requested new FloatBuffer<br />
    
  • */<br />
    
  • public static FloatBuffer createColorBuffer(int vertices) {
  •   FloatBuffer vBuff = createFloatBuffer(4 * vertices);<br />
    
  •   return vBuff;<br />
    
  • }

    +
  • /**
  • * Create a new FloatBuffer of an appropriate size to hold the specified<br />
    
  • * number of ColorRGBA object data only if the given buffer if not already<br />
    
  • * the right size.<br />
    
  • *<br />
    
  • * @param buf<br />
    
  • *            the buffer to first check and rewind<br />
    
  • * @param vertices<br />
    
  • *            number of vertices that need to be held by the newly created<br />
    
  • *            buffer<br />
    
  • * @return the requested new FloatBuffer<br />
    
  • */<br />
    
  • public static FloatBuffer createColorBuffer(FloatBuffer buf, int vertices) {
  •   if (buf != null && buf.limit() == 4 * vertices) {<br />
    
  •   	buf.rewind();<br />
    
  •   	return buf;<br />
    
  •   }<br />
    

+

  •   return createFloatBuffer(4 * vertices);<br />
    
  • }

    +
  • /**
  • Sets the data contained in the given color into the FloatBuffer at the
  • specified index.

    *

    [/patch]

In jME3 it is actually preferable to use unsigned byte color buffers, rather than float color buffers.

Oh I see. Thank you for pointing it out.

Learning jme3 is fun.:wink: