Game Development Reference
In-Depth Information
Since matrix operations always work on the TOS, we now have a scaling operation, a rotation,
and a translation encoded in the top matrix. The matrix we pushed still only contains a
translation. When we now render a mesh given in model space, like our cube, it will first be
scaled on the y axis, then rotated around the y axis, and then translated by −10 units on the
z axis. Now let's pop the TOS:
gl.glPopMatrix();
This will remove the TOS and make the matrix below it the new TOS. In our example, that's the
original translation matrix. After this call, there's only one matrix on the stack again—the one
initialized in the beginning of the example. If we render an object now, it will only be translated
by −10 units on the z axis. The matrix containing the scaling, rotation, and translation is gone
because we popped it from the stack. Figure 10-14 shows what happens to the matrix stack
when we execute the preceding code.
glRotatef(45,0,1,0);
glScalef(1,2,1);
glLoadIdentity( );
glPushMatrix( );
glPopMatrix( );
glTranslatef(0,0,-10);
translate(0,0,-10)
rotate(45,0,1,0)
scale(1,2,1)
translate(0,0,-10)
identity
translate(0,0,-10)
translate(0,0,-10)
translate(0,0,-10)
translate(0,0,-10)
Figure 10-14. Manipulating the matrix stack
So what's this good for? The first thing we can use it for is to remember transformations that
should be applied to all the objects in our world. Say we want all objects in our world to be offset
by 10 units on each axis; we could do the following:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(10, 10, 10);
for( MyObject obj: myObjects) {
gl.glPushMatrix();
gl.glTranslatef(obj.x, obj.y, obj.z);
gl.glRotatef(obj.angle, 0, 1, 0);
// render model of object given in model space, e.g., the cube
gl.glPopMatrix();
}
We will use this pattern later in the chapter when we discuss how to create a camera system
in 3D. The camera position and orientation are usually encoded as a matrix. We will load
this camera matrix, which will transform all objects in such a way that we see them from the
camera's point of view. There's something even better we can use the matrix stack for, though.
 
Search WWH ::




Custom Search