PDA

View Full Version : Basic Question About Matrix Stack



Carmine
10-05-2012, 10:51 PM
Suppose this is the Display function for a very simple OpenGL, GLUT, program.
How many matrices would this put on the matrix stack?
I used to think it would be 4. However, now I'm leaning towards 1,
which would be the product of the (Identity matrix) x (the Translation Matrix) x
(the Rotation matrix) x (the Scale Matrix).


void Display (void)
{
glMatrixModel (GL_MODELVIEW);

glLoadIdentity ();

glTranslatef (5,3,1);
glRotatef (30, 1,0,0);
glScalef (2.1, 1.0, 2.1);

glBegin (GL_POINTS);
glVertex2d (5,2);
glEnd ();

}

Thanx.

Dan Bartlett
10-06-2012, 03:14 AM
Yeah, there's just 1 matrix manipulated by that code. All the transformation functions affect the matrix at the current stack depth. Matrices aren't put onto the stack unless you call glPushMatrix (duplicates current matrix + increases stack depth) + removed when you call glPopMatrix (reduces stack depth).
OpenGL only guarantees that GL_MODELVIEW will have a stack >=32 matrices, GL_TEXTURE has depth >=2, GL_COLOR has depth >=2, GL_MODELVIEW has depth >=2, so you shouldn't call glPushMatrix too many times without glPopMatrix calls.

thokra
10-06-2012, 05:08 AM
glLoadIdentity();
glTranslatef(5,3,1);
glRotatef(30, 1,0,0);
glScalef(2.1, 1.0, 2.1);


BTW, mathematically that translates to MModelview = MI* MT* MR* MS so as you can see, the current matrix is computed by matrix commands concatenated in reverse order, with the first(right-most) matrix being specified by the last command and the last(left-most) matrix being specified by the first command.

Carmine
10-06-2012, 05:11 PM
Thanks for the replies. This clarifies things quite a bit for me.