PDA

View Full Version : glpushmatrix and glpopmatrix



jesh
03-18-2003, 01:06 AM
does anyone have a good example that illustrates the glPushMatrix and glPopMatrix.
what exactly does these two commands do? where should i use them?

nomad82
03-18-2003, 02:53 AM
glPushMatrix() saves the current matrix state.

glPopMatrix() gets the last saved (or "pushed") matrix state.

You use it so when you need to call glLoadIdentity(), you won't mess up the last matrix state (should you need it again).

I think there's a simple tutorial at:
http://www.gametutorials.com

nexusone
03-18-2003, 05:42 AM
Each time you call glTranslate/glRotate/etc they change matrix and each object after them is effected.

example

glRotatef(15, 1, 0, 0)
draw_object1() // Object one is drawn with a 15 degree rotation.

glRotatef(15, 1, 0, 0)
draw_object2() // Object two is drawn with a 30 degree rotation since the matrix is already rotated 15 degrees.

but with push/pop matrix you can save the matrix before a change has been made. Which keeps each object from effecting each other in the matrix moves.

glPushMatrix() // Save matrix
glTranslate // Move object
glRotate // Rotate object
Draw_object_1(); //Draw object
glPopMatrix() // Restore matrix to before changes
// We have now placed our first object and have our matrix back to before any changes had been made

// Draw second object
glPushMatrix() // Save matrix
glTranslate // Move object
glRotate // Rotate object
Draw_object_2(); // Draw Object
glPopMatrix() // Restore matrix to before changes

On my website the clock demo, is a good example of how push/pop can be used.
http://www.angelfire.com/linux/nexusone/


Originally posted by jesh:
does anyone have a good example that illustrates the glPushMatrix and glPopMatrix.
what exactly does these two commands do? where should i use them?



[This message has been edited by nexusone (edited 03-18-2003).]