View Full Version : glPushMatrix() & glPopMatrix()

01-07-2001, 06:43 AM
Hi! I would like to know when should I use glPushMatrix() and glPopMatrix()? What is the main purpose of glPushMatrix() and glPopMatrix()? Thanks for help.

01-07-2001, 07:18 AM
The main idea is: when you use glTranslate(..) or glRotate(..) you affect the modelview matrix. This means that when you apply several transformations (translations & rotations) this matrix changes too.

For example if you want to transform two objects in a different manner, your code will look something like:

glPushMatrix(); // Set current matrix on the stack
glTranslatef(someX, someY, someZ); // transformation 1
glRotatef(someangle,someaxis);// transformation 2
glPopMatrix(); // Pop the old matrix without the transformations.

glPushMatrix(); // Set current matrix on the stack
// SomeTransformations
glPopMatrix(); // Pop the old matrix without the transformations.

To get the (modelview) matrix into the original state, you also can use glLoadIdentity();

To get more (structured and clear) information on this topic, you could look into the following online books:

OpenGL Programming Guide (http://heron.cc.ukans.edu/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/@Generic__BookView)
(The Red Book)

OpenGL Reference Manual (http://heron.cc.ukans.edu/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_RM/@Generic__BookView)
(The Blue Book)

OpenGL Super Bible (http://www.itknowledge.com/reference/archive/1571690735/ewtoc.html)

Or Nehe's Tutorials at http://nehe.gamedev.net .

I hope this helps,

Daniel Palomo van Es.

[This message has been edited by DPalomo (edited 01-07-2001).]

01-07-2001, 08:51 AM
The modelview matrix stack is also handy for hierarchical models.

01-07-2001, 10:39 AM
so its like "save" and "load", and you can only glPushMatrix a limited amount of times, 16 i think.

[This message has been edited by grady (edited 01-07-2001).]

01-07-2001, 12:10 PM
The actual number times you can push a matrix is dependent upon which matrix, and the particular OpenGL implementation. Using glGetInteger with GL_MAX_PROJECTION_STACK_DEPTH , GL_MAX_TEXTURE_STACK_DEPTH, or GL_MAX_MODELVIEW_STACK_DEPTH will return the maximum number of times the respective matrix can be pushed (without any pops inbetween).

[This message has been edited by DFrey (edited 01-07-2001).]

01-07-2001, 09:23 PM
Oh...i see....thanks for the helps..!
So the glPushMatrix() is saves the current screen to stack and glPopMatrix() is to load the data from the stack...Thanks a lot ! http://www.opengl.org/discussion_boards/ubb/smile.gif