I'm drawing some objects with OpenGL ES 2.0, and each vertex has several attributes which, as recommended for best performance, I'm storing interleaved in a single buffer. Some of these (like position) are represented with floats (GLES20.GL_FLOAT
), but I'm passing in a color, and it's wasteful of memory to pass the color attribute as four float components so I'd like to use GLES20.GL_UNSIGNED_BYTE
. Four of these will pack into one 32 bit field. However I can't figure out an easy way of storing these in ByteBuffer
s. E.g. using GL_FLOAT
for the colours I'd do the following:
void addVertex(float xpos, float ypos, int color) {
floatBuffer.put(xpos);
floatBuffer.put(ypos);
floatBuffer.put(Color.red(color) / 255f);
floatBuffer.put(Color.green(color) / 255f);
floatBuffer.put(Color.blue(color) / 255f);
floatBuffer.put(Color.alpha(color) / 255f);
}
This works, but I'm using 128 bits to represent a colour that could be represented in 32, which will also cause a performance hit. So what I'd like to do is create a ByteBuffer
and use asFloatBuffer()
to create an alias to it and use whichever is appropriate for the type being stored. Like so:
void addVertices() {
byteBuffer = ByteBuffer.allocateDirect(vertexCount * VERTEX_STRIDE).order(ByteOrder.nativeOrder());
floatBuffer = byteBuffer.asFloatBuffer();
for(Vertex v : vertices)
addVertex(v.x, v.y, v.color);
}
void addVertex(float xpos, float ypos, int color) {
floatBuffer.put(xpos);
floatBuffer.put(ypos);
byteBuffer.put(Color.red(color));
byteBuffer.put(Color.green(color));
byteBuffer.put(Color.blue(color));
byteBuffer.put(Color.alpha(color)); }
However this won't work because asFloatBuffer()
creates a FloatBuffer
that shares the underlying storage of the ByteBuffer
, but maintains its own position index. I'd have to manually track the position and update each one before storing anything, which is ugly.
I could also split the attributes into separate buffers, but the performance penalty for that would probably outweigh the gain.
Any ideas on how to do this in an elegant, efficient manner?