Game Development Reference
In-Depth Information
int VERTEX_SIZE = (2 + 4) * 4;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer vertices = byteBuffer.asFloatBuffer();
vertices.put( new float[] { 0.0f, 0.0f, 1, 0, 0, 1,
319.0f, 0.0f, 0, 1, 0, 1,
160.0f, 479.0f, 0, 0, 1, 1});
vertices.flip();
We first have to allocate a ByteBuffer for our three vertices. How big should that ByteBuffer
be? We have two coordinates and four (RGBA) color components per vertex, so that's six
floats in total. Each float value takes up 4 bytes, so a single vertex uses 24 bytes. We store
this information in VERTEX_SIZE . When we call ByteBuffer.allocateDirect() , we just multiply
VERTEX_SIZE by the number of vertices we want to store in the ByteBuffer . The rest is fairly
self-explanatory. We get a FloatBuffer view to our ByteBuffer and put() the vertices into the
ByteBuffer . Each row of the float array holds the x and y coordinates and the R, G, B, and A
components of a vertex, in that order.
If we want to render this, we have to tell OpenGL ES that our vertices not only have a position,
but also have a color attribute. We start off, as before, by calling glEnableClientState() :
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
Now that OpenGL ES knows that it can expect position and color information for each vertex,
we have to tell it where it can find that information:
vertices.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
vertices.position(2);
gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
We start by setting the position of our FloatBuffer , which holds our vertices to 0. The position
thus points to the x coordinate of our first vertex in the buffer. Next, we call glVertexPointer() .
The only difference from the previous example is that we now also specify the vertex size
(remember, it's given in bytes). OpenGL ES will then start reading in vertex positions from the
position in the buffer from which we told it to start. For the second vertex position, it will add
VERTEX_SIZE bytes to the first position's address, and so on.
Next, we set the position of the buffer to the R component of the first vertex and call
glColorPointer() , which tells OpenGL ES where it can find the colors of our vertices. The first
argument is the number of components per color. This is always four, as OpenGL ES demands
an R, G, B, and A component per vertex from us. The second parameter specifies the type of
each component. As with the vertex coordinates, we use GL10.GL_FLOAT again to indicate that
each color component is a float in the range between 0 and 1. The third parameter is the stride
between vertex colors. It's of course the same as the stride between vertex positions. The final
parameter is our vertices buffer again.
Since we called vertices.position(2) before the glColorPointer() call, OpenGL ES knows
that the first vertex color can be found starting from the third float in the buffer. If we wouldn't
have set the position of the buffer to 2, OpenGL ES would have started reading in the colors
Search WWH ::




Custom Search