Game Development Reference
In-Depth Information
The call to glEnableClientState() is a bit of a relic. It tells OpenGL ES that the vertices we are
going to draw have a position. This is a bit silly, for two reasons:
GL10.GL_VERTEX_ARRAY , which is a bit confusing. It
would make more sense if it were called GL10.GL_POSITION_ARRAY .
There's no way to draw anything that has no position, so the call to this
ï?®
The constant is called
ï?®
method is a little bit superfluous. We do it anyway, however, to make
OpenGL ES happy.
In the call to glVertexPointer() , we tell OpenGL ES where it can find the vertex positions
and give it some additional information. The first parameter tells OpenGL ES that each vertex
position is composed of two coordinates, x and y. If we would have specified x, y, and z, we
would have passed three to the method. The second parameter tells OpenGL ES the data type
we used to store each coordinate. In this case, it's GL10.GL_FLOAT , indicating that we used floats
encoded as 4 bytes each. The third parameter, stride , tells OpenGL how far apart our vertex
positions are from each other in bytes. In the preceding case, stride is 0, as the positions are
tightly packed [vertex 1 (x,y), vertex 2 (x,y), and so on]. The final parameter is our FloatBuffer ,
for which there are two things to remember:
ï?® FloatBuffer represents a memory block in the native heap, and thus
has a starting address.
The
FloatBuffer is an offset from that starting address.
OpenGL ES will take the buffer's starting address and add the buffer's positions to arrive at
the float in the buffer from which it will start reading the vertices when we tell it to draw the
contents of the buffer. The vertex pointer (which again should be called the position pointer ) is a
state of OpenGL ES. As long as we don't change it (and the context isn't lost), OpenGL ES will
remember it and use it for all subsequent calls that need vertex positions.
Finally, there's the call to glDrawArrays() . It will draw our triangle. The first parameter specifies
what type of primitive we are going to draw. In this case, we say that we want to render a list of
triangles, which is specified via GL10.GL_TRIANGLES . The next parameter is an offset relative to
the first vertex to which the vertex pointer points. The offset is measured in vertices, not bytes or
floats. If we would have specified more than one triangle, we could use this offset to render only
a subset of our triangle list. The final argument tells OpenGL ES how many vertices it should use
for rendering. In our case, that's three vertices. Note that we always have to specify a multiple
of 3 if we draw GL10.GL_TRIANGLES . Each triangle is composed of three vertices, so that makes
sense. For other primitive types, the rules are a little different.
Once we issue the glVertexPointer() command, OpenGL ES will transfer the vertex positions
to the GPU and store them there for all subsequent rendering commands. Each time we tell
OpenGL ES to render vertices, it takes their positions from the data we last specified via
glVertexPointer() .
Each of our vertices might have more attributes than just a position. One other attribute might be
a vertex's color. We usually refer to those attributes as vertex attributes .
ï?®
The position of the
You might be wondering how OpenGL ES knows what color our triangle should have, as we
have only specified positions. It turns out that OpenGL ES has sensible defaults for any vertex
Search WWH ::




Custom Search