Adding volume

In our previous example, we've seen how to draw a quad with one single color, but what about if each vertex has a completely different color? The process will not be very different from what we've already done, but let's see how we can do it.

First, let's change the color array to hold the color of the four vertices:

float color[] = { 
        1.0f, 0.2f, 0.2f, 1.0f, 
        0.2f, 1.0f, 0.2f, 1.0f, 
        0.2f, 0.2f, 1.0f, 1.0f, 
        1.0f, 1.0f, 1.0f, 1.0f, 
}; 

Now, in our initBuffers() method, let's initialize an additional Buffer for the color:

private FloatBuffer colorBuffer; ... ByteBuffer cbb = ByteBuffer.allocateDirect(color.length * (Float.SIZE / 8)); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(color); colorBuffer.position(0); ...

Get Building Android UIs with Custom Views now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.