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.

# glPushMatrix() & glPopMatrix()

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

DrawObject(ONE);

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

…

// SomeTransformations

…

DrawObject(TWO);

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

(The Red Book)

OpenGL Reference Manual

(The Blue Book)

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).]

The modelview matrix stack is also handy for hierarchical models.

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).]

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).]

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 !