Matrix Commands

Could somebody explain what the glPushMatrix and glPopMatrix commands do? (and in what situation you use them) Additionally, what are the differences between the MODELVIEW and PROJECTION matrix? I mean, what does each manipulate? and which would you manipulate if you just wanted to move the camera (viewport) in relation to your scene without having to transform each and every object in the scene? I’m real beginner at this so if you’d humour me )

Thanks again.


I’m a beginner too (and my English is not very good) so I don’t know if I can help you but I’ll try…(I have a Red Book that explain OpenGL very well and if you want it my e-mail is
OpenGL use 44 matrix to do translation and rotation, the first 33 elements do the rotation (the 3 columns (or vectors) of 3 elements rappresent the 3 axis (x,y,z) in function of the old ones), the last column of 3 elements rappresents the trasnlation from the origin and the last row is composed by 4 elements (0 0 0 1).
When you draw a polygon and you want for example to rotate and translate it,openGL multiply the coordinates of the verteces of it by the current matrix that does the roto-translation. The command gl_PushMatrix and gl_PopMatrix put e pull the current matrix that does the roto-translation into the stack; so you can push the current matrix into the stack, make a roto-translation of one of your fragment, pop the matrix from the stack and make another roto-translation of another fragment in function of the same origin…on the contrary you can push the matrix into the stack, make roto-translation, make another roto-translation (in function of the new origin and of the new system of axis), and the pop the matrix from the stack fo another transformation in function fo the old origin…and so on…
While with glMatrixMode(GL_MODELVIEW) you make transformation on the model (your fragments) and on the view (your camera)… that is the SAME thing …infact you move and object you can have the same result moving the camera in the opposit way; with glMatrixMode(GL_PROJECTION) you make transformation on the kind of lenses that your camea suits…(large or small angle of view …)…to do it I use glPerspective(angle,aspect,near_plane,far_plane).

A simple example. 2 cubes rotations on a positif angle on X and the other on a negatif angle on X. Without, the Push&Pop, it’s doesn’t works.

The code …

float Angle = 0.0f;

// Your cube 1
Angle += 0.1f;
if (Angle == 360.0f) Angle = 0.0f;

// Your cube 2
Angle -= 0.1f;
if (Angle == 360.0f) Angle = 0.0f;

Try with and without the Push & Pop Matrix
and you’ll

Easiest way to mvoe your camera is with gluLookAt(). You basically give it the vertex where the camera is, the exact vertex it is looking at, and set the normal vector to 1.0 (a b and c represent the normal vector options). I use b in my 3D engine. Very simple, no major math needed to be done. Hope this helps!

gluLookAt(xpos, ypos, zpos, look-xpos, look-ypos, look-zpos, a, b, c);