Game Development Reference
In-Depth Information
vertex attributes via the usual calls to glEnableClientState() and glVertexPointer() /
glTexCoordPointer() . The only difference is the method we call to draw the two triangles:
gl.glDrawElements(GL10. GL_TRIANGLES , 6, GL10. GL_UNSIGNED_SHORT , indices);
This method is actually very similar to glDrawArrays() . The first parameter specifies the type
of primitive we want to render—in this case, a list of triangles. The next parameter specifies
how many vertices we want to use, which equals six in our case. The third parameter specifies
what type the indices have—we specify unsigned short. Note that Java has no unsigned
types; however, given the one-complement encoding of signed numbers, it's OK to use a
ShortBuffer that actually holds signed shorts. The last parameter is our ShortBuffer holding
the six indices.
So, what will OpenGL ES do? It knows that we want to render triangles, and it knows that we
want to render two triangles, as we specified six vertices; but instead of fetching six vertices
sequentially from the vertices array, OpenGL ES goes sequentially through the index buffer and
uses the vertices it has indexed.
Putting It Together
When we put it all together, we arrive at the code in Listing 7-9.
Listing 7-9. Excerpt from IndexedTest.java; Drawing Two Indexed Triangles
class IndexedScreen extends Screen {
final int VERTEX_SIZE = (2 + 2) * 4;
GLGraphics glGraphics;
FloatBuffer vertices;
ShortBuffer indices;
Texture texture;
public IndexedScreen(Game game) {
super (game);
glGraphics = ((GLGame) game).getGLGraphics();
ByteBuffer byteBuffer = ByteBuffer. allocateDirect (4 * VERTEX_SIZE);
byteBuffer.order(ByteOrder. nativeOrder ());
vertices = byteBuffer.asFloatBuffer();
vertices.put( 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 });
vertices.flip();
byteBuffer = ByteBuffer. allocateDirect (6 * 2);
byteBuffer.order(ByteOrder. nativeOrder ());
indices = byteBuffer.asShortBuffer();
indices.put( new short [] { 0, 1, 2,
2, 3, 0 });
indices.flip();
 
Search WWH ::




Custom Search