Game Development Reference
In-Depth Information
if (hasTexCoords) {
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRAY );
vertices.position(hasColor?6:2);
gl.glTexCoordPointer(2, GL10. GL_FLOAT , vertexSize, vertices);
}
if if(indices != null ) {
indices.position(offset);
gl.glDrawElements(primitiveType, numVertices, GL10. GL_UNSIGNED_SHORT , indices);
} else {
gl.glDrawArrays(primitiveType, offset, numVertices);
}
if (hasTexCoords)
gl.glDisableClientState(GL10. GL_TEXTURE_COORD_ARRAY );
if if(hasColor)
gl.glDisableClientState(GL10. GL_COLOR_ARRAY );
}
}
The final method of the Vertices class is draw() . It takes the type of the primitive (for example,
GL10.GL_TRIANGLES ), the offset into the vertices buffer (or the indices buffer if we use indices),
and the number of vertices to use for rendering. Depending on whether the vertices have colors
and texture coordinates, we enable the relevant OpenGL ES states and tell OpenGL ES where
to find the data. We do the same for the vertex positions, of course, which are always needed.
Depending on whether indices are used, we either call glDrawElements() or glDrawArrays()
with the parameters passed to the method. Note that the offset parameter can also be used in
case of indexed rendering: we simply set the position of the indices buffer accordingly so that
OpenGL ES starts reading the indices from that offset instead of the first index of the indices
buffer. The last thing we do in the draw() method is clean up the OpenGL ES state a little. We
call glDisableClientState() with either GL10.GL_COLOR_ARRAY or GL10.GL_TEXTURE_COORD_ARRAY
in case our vertices have these attributes. We need to do this, as another instance of Vertices
might not use those attributes. If we rendered that other Vertices instance, OpenGL ES would
still look for colors and/or texture coordinates.
We could replace all the tedious code in the constructor of our preceding example with the
following snippet:
Vertices vertices = new Vertices(glGraphics, 4, 6, false , true );
vertices.setVertices( new float [] { 100.0f, 100.0f, 0.0f, 1.0f,
228.0f, 100.0f, 1.0f, 1.0f,
228.0f, 228.0f, 1.0f, 0.0f,
100.0f, 228.0f, 0.0f, 0.0f }, 0, 16);
vertices.setIndices( new short[] { 0, 1, 2, 2, 3, 0 }, 0, 6);
Likewise, we could replace all the calls for setting up our vertex attribute arrays and rendering
with a single call to the following:
vertices.draw(GL10.GL_TRIANGLES, 0, 6);
Search WWH ::




Custom Search