Game Development Reference
In-Depth Information
multiply it by an orthographic projection matrix. We did something similar with the model-view
matrix:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -10);
gl.glRotatef(45, 0, 1, 0);
This snippet manipulates the model-view matrix. It first loads an identity matrix to clear whatever
was in the model-view matrix before that call. Next it multiplies the matrix with a translation
matrix and a rotation matrix. This order of multiplication is important, as it defines in what
order these transformations get applied to the vertices of the meshes. The last transformation
specified will be the first to be applied to the vertices. In the preceding case, we first rotate each
vertex by 45 degrees around the y axis. Then we move each vertex by −10 units along the z axis.
In both cases, all the transformations are encoded in a single matrix, in either the OpenGL ES
projection or the model-view matrix. But it turns out that for each matrix type, there's actually a
stack of matrices at our disposal.
For now, we're only using a single slot in this stack: the top of the stack (TOS). The TOS of a
matrix stack is the slot actually used by OpenGL ES to transform the vertices, be it with the
projection matrix or the model-view matrix. Any matrix below the TOS on the stack just sits there
idly, waiting to become the new TOS. So how can we manipulate this stack?
OpenGL ES has two methods we can use to push and pop the current TOS:
GL10.glPushMatrix();
GL10.glPopMatrix();
Like glTranslatef() and consorts, these methods always work on the currently active matrix
stack that we set via glMatrixMode() .
The glPushMatrix() method takes the current TOS, makes a copy of it, and pushes it on the
stack. The glPopMatrix() method takes the current TOS and pops it from the stack so that the
element below it becomes the new TOS.
Let's work through a little example:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslate(0,0,-10);
Up until this point, there has only been a single matrix on the model-view matrix stack. Let's
“save� this matrix:
gl.glPushMatrix();
Now we've made a copy of the current TOS and pushed down the old TOS. We have two
matrices on the stack now, each encoding a translation on the z axis by −10 units.
gl.glRotatef(45, 0, 1, 0);
gl.glScalef(1, 2, 1);
Search WWH ::




Custom Search