Game Development Reference
In-Depth Information
z-buffer, the pixel passes the so-called depth test , or z-test . The pixel's color will be written to
the framebuffer, and its depth will overwrite the corresponding value in the z-buffer. If it fails
the test, neither the pixel's color nor its depth value will be written to the buffers. This is shown
in Figure 10-6 , where the second triangle is rendered. Some of the pixels have smaller depth
values and thus get rendered; other pixels don't pass the test.
Figure 10-6. Image in the framebuffer (left); z-buffer contents after rendering each of the two triangles (right)
As with the framebuffer, we also have to clear the z-buffer for each frame; otherwise, the depth
values from the last frame would still be in there. To do this, we can call glClear() , as per the
following:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
This will clear the framebuffer (or colorbuffer) and the z-buffer (or depthbuffer) all in one go.
Fixing the Previous Example
Let's fix the previous example's problems by using the z-buffer. Simply copy all the code over
to a new class called ZBufferTest and modify the present() method of the new ZBufferScreen
class, as shown in Listing 10-4.
Listing 10-4. Excerpt from ZBufferTest.java; Using the Z-buffer
@Override
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glClear(GL10. GL_COLOR_BUFFER_BIT | GL10. GL_DEPTH_BUFFER_BIT );
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
 
Search WWH ::




Custom Search