Game Development Reference
In-Depth Information
A Helpful Snippet
Here's a small snippet that will be used in all of our examples in this chapter. It clears the screen
with black, sets the viewport to span the whole framebuffer, and sets up the projection matrix
(and thereby the view frustum) so that we can work in a comfortable coordinate system with the
origin in the lower-left corner of the screen and the y axis pointing upward.
gl.glClearColor(0,0,0,1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
Wait, what does glLoadIdentity() do in there? Well, most of the methods OpenGL ES offers
us to manipulate the active matrix don't actually set the matrix; instead, they construct
a temporary matrix from whatever parameters they take and multiply it with the current
matrix. The glOrthof() method is no exception. For example, if we called glOrthof() each
frame, we'd multiply the projection matrix to death with itself. Instead of doing that, we
make sure that we have a clean identity matrix in place before we multiply the projection
matrix. Remember, multiplying a matrix by the identity matrix will output the matrix itself
again, and that's what glLoadIdentity() is for. Think of it as first loading the value 1 and
then multiplying it with whatever we have—in our case, the projection matrix produced by
glOrthof() .
Note that our coordinate system now goes from (0,0,1) to (320,480,-1)—that's for portrait mode
rendering.
Specifying Triangles
Next, we have to figure out how we can tell OpenGL ES about the triangles we want it to render.
First, let's define what comprises a triangle:
ï?®
A triangle is comprised of three points.
ï?®
Each point is called a vertex.
ï?®
A vertex has a position in 3D space.
ï?®
A position in 3D space is given as three floats, specifying the x, y, and z
coordinates.
ï?®
A vertex can have additional attributes, such as a color or texture
coordinates (which we'll talk about later). These can be represented as floats
as well.
OpenGL ES expects to send our triangle definitions in the form of arrays; however, given that
OpenGL ES is actually a C API, we can't just use standard Java arrays. Instead, we have to use
Java NIO buffers, which are just memory blocks of consecutive bytes.
 
Search WWH ::




Custom Search