Game Development Reference
In-Depth Information
ByteBuffer byteBuffer = ByteBuffer. allocateDirect (3 * VERTEX_SIZE);
byteBuffer.order(ByteOrder. nativeOrder ());
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();
}
@Override
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10. GL_COLOR_BUFFER_BIT );
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
gl.glEnableClientState(GL10. GL_COLOR_ARRAY );
vertices.position(0);
gl.glVertexPointer(2, GL10. GL_FLOAT , VERTEX_SIZE, vertices);
vertices.position(2);
gl.glColorPointer(4, GL10. GL_FLOAT , VERTEX_SIZE, vertices);
gl.glDrawArrays(GL10. GL_TRIANGLES , 0, 3);
}
Cool—that still looks pretty straightforward. Compared to the previous example, we simply
added the four color components to each vertex in our FloatBuffer and enabled the
GL10.GL_COLOR_ARRAY . The best thing about it is that any additional vertex attributes we add in
the following examples will work the same way. We just tell OpenGL ES not to use the default
value for that specific attribute; instead, we tell it to look up the attributes in our FloatBuffer ,
starting at a specific position and moving from vertex to vertex by VERTEX_SIZE bytes.
Now, we could also turn off the GL10.GL_COLOR_ARRAY so that OpenGL ES uses the default vertex
color again, which we can specify via glColor4f() as we did previously. For this we can call
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
OpenGL ES will just turn off the feature to read the colors from our FloatBuffer . If we already set
a color pointer via glColorPointer() , OpenGL ES will remember the pointer even though we just
told OpenGL ES to not use it.
To round our this example, let's have a look at the output of the preceding program. Figure 7-9
shows a screenshot.
Search WWH ::




Custom Search