Game Development Reference
In-Depth Information
public void
draw(
int
primitiveType,
int
offset,
int
numVertices) {
GL10 gl = glGraphics.getGL();
if
(indices !=
null
) {
indices.position(offset);
gl.glDrawElements(primitiveType, numVertices,
GL10.
GL_UNSIGNED_SHORT
, indices);
}
else
{
gl.glDrawArrays(primitiveType, offset, numVertices);
}
}
You can see that the
draw()
method is again unmodified; all of the magic happens in the
bind()
method.
public void
unbind() {
GL10 gl = glGraphics.getGL();
if
(hasTexCoords)
gl.glDisableClientState(GL10.
GL_TEXTURE_COORD_ARRAY
);
if
(hasColor)
gl.glDisableClientState(GL10.
GL_COLOR_ARRAY
);
if
(hasNormals)
gl.glDisableClientState(GL10.
GL_NORMAL_ARRAY
);
}
}
Finally, we also modify the
unbind()
method slightly. We disable the normal pointer if normals
were used, to clean up the OpenGL ES state properly.
Using this modified
Vertices3
version is as easy as before. Here's a small example:
float
[] vertices = { -0.5f, -0.5f, 0, 0, 0, 1,
0.5f, -0.5f, 0, 0, 0, 1,
0.0f, 0.5f, 0, 0, 0, 1 };
Vertices3 vertices =
new
Vertices3(glGraphics, 3, 0,
false
,
false
,
true
);
vertices.setVertices(vertices);
We create a float array to hold three vertices, each having a position (the first three floats on
each line) and a normal (the last three floats on each line). In this case, we have a triangle in the
x-y plane, with its normals pointing in the direction of the positive z axis. All that's left to do is
create the
Vertices3
instance and set the vertices. Easy, right? Binding, drawing, and unbinding
work is exactly the same as with the old version. We can, of course, also add vertex colors and
texture coordinates, as we did previously.
Putting It All Together
Let's put all this lighting knowledge together into an example. We want to draw a scene with a
global ambient light, a point light, and a directional light that all illuminate a cube centered at the
origin. For good measure, we'll also throw in a call to
gluLookAt()
to position our camera in the
world. Figure
11-6
shows the setup of our world.