Push and Pop Matrixes

Hi, I am a total beginner therefore i dont know much about OpenGL, but i am trying.

I am trying to make 4 cubes rotating in space, and someone told me that i have to include the functions glPushMatrix() and glPopMatrix() as in the example below:

glPushMatrix();
glLoadIdentity();
glTranslated(0.0f, 0.0f, -5.0f);
glRotated(rotVar, 20.0, 20.0, 10.0);
drawRGBCube();
glPopMatrix();

I understand that the “push” matrix puts something on the stack, and the “pop” removes something from the stack. I dont really understand the concept, as
i dont see any difference in my animation when i remove these functions from the code. The cube is still translated and rotated without any further complications.

I would be happy if someone could explain to me why i am supposed to use these functions, and what exactly they are supposed to do/solve.

Push and pop matrix operation are useful when there is a need for hierarchical transformations. The “inner” transformation are dependent or the “outer” one. For example take the arm of a robot then transformations call can be something like this:

glPushMatrix()
glMultMatrix(…)//Shoulder transform
glPushMatrix()
glMultMatrix(…)//elbow transform
glPushMatrix()
glMultMatrix(…)//finger transform
glPopMatrix()
glPopMatrix()
glPopMatrix()

In your animation you don’t see difference because you call glLoadIdentity just after glPushMatrix. So here all you are doing is to save a transformation matrix (push) then place a Identity matrix (void all previous transform) then multiply it with your transformations. Then you pop your transform matrix and you restore the previous transformation.

Hope that it’s clear.

dvwood: I would be happy if someone could explain to me why i am supposed to use these functions, and what exactly they are supposed to do/solve.

you don’t have to. the best way in using them is if you use OpenGL functions to rotate/scale/translate geometry or if you use nested GL lists. you can push (save) a matrix and pop (restore) it with those two calls.