glPushMatrix doubt

Started by
4 comments, last by kburkhart84 18 years, 8 months ago
lets say I have the following pseudo code: glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); .... drawing on this 1st matrix ... glPopMatrix(); ... some code ... glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); ... drawing on this 2nd matrix ... glPopMatrix(); whenever I call glPushMatrix I'm setting up a new matrix, is that correct? if so, are the attributes from the 1st matrix inherited by the second one or is the second matrix a completly new one? I did some testing on my own and I didn't understand when to use glPushMatrix and when not to use it thanks
Advertisement
The new matrix after a glPushMatrix is the same as what it was before, so yes, it inherits the old matrix. However, in your code you are doing glLoadIdentity after the push, which destroys any previous transform.
as you said, glLoadIdentity resets the matrix

thats why I dont understand why glPushMatrix is used if we can just reset the current matrix. maybe there's something I missed
Let's say you wanted to render multiple objects all contained in the same world. More specifically, let's say you wanted to render the SAME object in multiple places.

// Only need to set matrix mode once for multiple objects
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

..apply world transform (camera).. (this could be a glMultMatrix(), glLoadMatrix(), gluLookAt(), seperate glTranslate() and glRotate() calls, or any number of things)

// This has the effect of saving the world transform
glPushMatrix();
// Translate into object space
glTranslatef( x, y, z );

..render objectA..

// Here we revert to the original world transform
glPopMatrix();

// Save the world transform again
glPushMatrix();
// Translate to different object space
glTranslatef( x2, y2, z2 );

..render objectA..

// Revert to world transform again
glPopMatrix();

What the matrix stack allows you to do is save a copy of the current matrix so that further matrix operations can be 'undone'. The translate calls there modify the world transform, but since we have more than one object we need to revert to the world transform for each one.
I set the clouds in motion, turn up light and sound...Activate the window, and watch the world go 'round
If you are just loading the identity again in the 2nd group then you don't need the push and pop.

The reason for pushing and poping matrices is when you have a matrix manipulation that applys to multiple objects that you plan to draw such having your view oriented in 3d. You would use the appropriate functions to set up the modelview matrix to look wherever you want it to look. Then, for each object you wanted to render, you could pushmatrix, translate the object to its location in the world for rendering, render the object, then call popmatrix. At this point, your original camera view matrix is restored. It is the equivalent of undo-ing all transformations between push and pop.

Also, you do not need to call MatrixMode unles you are changing the mode. So unless you have some gl code in your "some code" section, then you don't need to include really either of the matrixmode calls. Chances are you will be working with the modelview matrix more than others so you might consider always assuming that the matrixmode is modelview and making sure to switch back to it after using a different mode
I'll explain a good use for the Push Pop functions. You are drawing a 3d model of a car. It is in parts, but for this example, let's say body, and each tire, 5 parts. Wherever the car goes, the tires do too, atleast I hope the do. So, the code might look like this.

glTranslate3f("Where the car is");
glRotatef("Same as above");
//Draw the Car's Body
glPushMatrix();
glTranslate3f("Where one tire is");
glRotatef("Rotation since the car is moving");
//Draw Tire
glPopMatrix();
glPushMatrix();
glTranslate3f("Where another tire is");
glRotatef("Rotation since the car is moving");
//Draw Tire
glPopMatrix();

It goes like this. You translate and rotate the whole car. Draw the car. Save the center position of the car by Push. Translate to where a tire is. Draw it. Pop to go back to the center of the car again. Push to save again. Again translate to another tire etc...

Because of this, no matter how you have the car rotated or translated, the tires always move in relation to the car. If the car moves, the tires follow, but you only have one translate to change, the first one since the rest follow.


This topic is closed to new replies.

Advertisement