Game Development Reference
In-Depth Information
In this case, the green triangle at z = −5 should shine through. Let's think about what OpenGL
ES will do and what else will happen:
ï?®
OpenGL ES will render the first triangle to the z-buffer and colorbuffer.
ï?®
Next OpenGL ES will render the green triangle because it comes after the
red triangle in our Vertices3 instance.
The portion of the green triangle behind the red triangle will not get shown
ï?®
on the screen because the pixels will be rejected by the depth test.
ï?®
Nothing will shine through the red triangle in the front since nothing was
there to shine through when it was rendered.
When using blending in combination with the z-buffer, you have to make sure that all transparent
objects are sorted by increasing distance from the camera position and are rendered from back
to front. All opaque objects must be rendered before any transparent objects. The opaque
objects don't have to be sorted, though.
Let's write a simple example that demonstrates this. We keep our current scene composed of
two triangles and set the alpha component of the vertex colors of the first triangle (z = −3) to
0.5f. According to our rule, we have to render the opaque objects first—in this case the green
triangle (z = −5)—and then render all the transparent objects, from furthest to closest. In our
scene, there's only one transparent object: the red triangle.
We copy over all the code from Listing 10-4 to a new class called ZBlendingTest and rename the
contained ZBufferScreen to ZBlendingScreen . We now simply need to change the vertex colors
of the first triangle and enable the blending and rendering of the two triangles in proper order in
the present() method. Listing 10-5 shows the two relevant methods.
Listing 10-5. Excerpt from ZBlendingTest.java; Blending with the Z-buffer Enabled
public ZBlendingScreen(Game game) {
super (game);
vertices = new Vertices3(glGraphics, 6, 0, true , false );
vertices.setVertices( new float [] { −0.5f, -0.5f, -3, 1, 0, 0, 0.5f,
0.5f, -0.5f, -3, 1, 0, 0, 0.5f,
0.0f, 0.5f, -3, 1, 0, 0, 0.5f,
0.0f, -0.5f, -5, 0, 1, 0, 1,
1.0f, -0.5f, -5, 0, 1, 0, 1,
0.5f, 0.5f, -5, 0, 1, 0, 1}, 0, 7 * 6);
}
@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();
GLU. gluPerspective (gl, 67,
glGraphics.getWidth() / ( float )glGraphics.getHeight(),
0.1f, 10f);
 
Search WWH ::




Custom Search