Game Development Reference
In-Depth Information
GLU. gluPerspective (gl, 67,
glGraphics.getWidth() / ( float )glGraphics.getHeight(),
0.1f, 10f);
gl.glMatrixMode(GL10. GL_MODELVIEW );
gl.glLoadIdentity();
gl.glEnable(GL10. GL_DEPTH_TEST );
vertices.bind();
vertices.draw(GL10. GL_TRIANGLES , 0, 6);
vertices.unbind();
gl.glDisable(GL10. GL_DEPTH_TEST );
}
We first changed the arguments to the call to glClear() . Now both buffers are cleared instead of
just the framebuffer.
We also enable depth testing before we render the two triangles. After we are done with
rendering all of the 3D geometry, we disable depth testing again. Why? Imagine that we want
to render 2D UI elements on top of our 3D scene, such as the current score or buttons. Since
we'd use the SpriteBatcher for this, which only works in 2D, we wouldn't have any meaningful z
coordinates for the vertices of the 2D elements. We wouldn't need depth testing either, since we
would explicitly specify the order in which we want the vertices to be drawn to the screen.
The output of this example, shown in Figure 10-7 , looks as expected now.
Figure 10-7. The z-buffer in action, making the rendering order-independent
Finally, the green triangle in the middle is rendered correctly behind the red triangle, thanks
to our new best friend, the z-buffer. As with most friends, however, there are times when your
friendship suffers a little from minor issues. Let's examine some caveats when using the z-buffer.
Blending: There's Nothing Behind You
Assume that we want to enable blending for the red triangle at z = −3 in our scene. Say we set
each vertex color's alpha component to 0.5f, so that anything behind the triangle shines through.
 
Search WWH ::




Custom Search