Game Development Reference
In-Depth Information
gl.glMatrixMode(GL10.
GL_MODELVIEW
);
gl.glLoadIdentity();
gl.glEnable(GL10.
GL_DEPTH_TEST
);
gl.glEnable(GL10.
GL_BLEND
);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
vertices.bind();
vertices.draw(GL10.
GL_TRIANGLES
, 3, 3);
vertices.draw(GL10.
GL_TRIANGLES
, 0, 3);
vertices.unbind();
gl.glDisable(GL10.
GL_BLEND
);
gl.glDisable(GL10.
GL_DEPTH_TEST
);
}
In the constructor of the
ZBlendingScreen
class, we only change the alpha components of
the vertex colors of the first triangle to 0.5. This will make the first triangle transparent. In the
present()
method, we do the usual things, like clearing the buffers and setting up the matrices.
We also enable blending and set a proper blending function. The interesting bit is how we render
the two triangles now. We first render the green triangle, which is the second triangle in the
Vertices3
instance, as it is opaque. All opaque objects must be rendered before any transparent
objects are rendered. Next, we render the transparent triangle, which is the first triangle in the
Vertices3
instance. For both drawing calls, we simply use proper offsets and vertex counts as
the second and third arguments to the
vertices.draw()
method. Figure
10-8
shows the output
of this program.
Figure 10-8.
Blending with the z-buffer enabled
Let's reverse the order in which we draw the two triangles as follows:
vertices.draw(GL10.GL_TRIANGLES, 0, 3);
vertices.draw(GL10.GL_TRIANGLES, 3, 3);
So, we first draw the triangle starting from vertex 0 and then draw the second triangle starting
from vertex 3. This will render the red triangle in the front first and the green triangle in the back
second. Figure
10-9
shows the outcome.