Game Development Reference
In-Depth Information
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
);
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glMatrixMode(GL10.
GL_PROJECTION
);
gl.glLoadIdentity();
gl.glOrthof(−1, 1, -1, 1, 10, -10);
gl.glMatrixMode(GL10.
GL_MODELVIEW
);
gl.glLoadIdentity();
vertices.bind();
vertices.draw(GL10.
GL_TRIANGLES
, 0, 6);
vertices.unbind();
}
@Override
public void
update(
float
deltaTime) {
}
@Override
public void
pause() {
}
@Override
public void
resume() {
}
@Override
public void
dispose() {
}
}
}
As you can see, this is the complete source file. The following examples will show only the
relevant portions of this file, since the rest stays mostly the same, apart from the class names.
We have a
Vertices3
member in
Vertices3Screen
, which we initialize in the constructor. We
have six vertices in total, a color per vertex, and no texture coordinates. Since neither triangle
shares vertices with the other, we don't use indexed geometry. This information is passed to the
Vertices3
constructor. Next, we set the actual vertices with a call to
Vertices3.setVertices()
.
The first three lines specify the red triangle in the front, and the other three lines specify the
green triangle in the back, slightly offset to the right by 0.5 units. The third float on each line is
the z coordinate of the respective vertex.
In the
present()
method, we must first clear the screen and set the viewport, as always. Next,
we load an orthographic projection matrix, setting up a view frustum big enough to show our
entire scene. Finally, we just render the two triangles contained within the
Vertices3
instance.
Figure
10-2
shows the output of this program.