View Full Version : How to use glPushMatrix and glPopMatrix?

11-22-2002, 03:28 AM
Simple question basically.
I seem to do it wrong for some reason since suddenly my scene partially stops "moving" relatively to the camera (object take a fixed position in the view, except the first drawn one).

When I want to keep my original centre of the world, do I have to puch my matrix after each time I pop it back?
draw thingy
draw thingy

Or should I do this differently?

Thank you!

***is still waiting for his book to arive***

11-22-2002, 03:44 AM
Well it depends.Think of it this way.You have a current matrix slot and a matrix stack.Push pushes the matrix in the slot on the stack(but the matrix still stays in the slot!) and pop pops the top of the stack into the slot.So after the first pop you have the initial matrix say M in the slot.M isn't on the stack though so if you want to get M after the second pop as well you'll have to repush as you do.If you don't you'll get a stack underflow as you push one item and try to pop back two.I'd say yoyr problems lies somewhere else.

11-22-2002, 04:13 AM
I think your on the right track.

// world view can be changed by the following

or use


// Object move/rotate
glPushMatrix() // Save current stack
glPopMatrix() // Restore stack

What happens is the object inside the Push/pop is effected by the past translate/rotate, plus the translate/rotate inside the push/pop. But after the stack is pop'd only the translate/rotate before the push will effect other objects after.

Hope this helps

11-22-2002, 04:43 AM
Thank you people,
I think I got the problem solved now... I THINK I forgot a pop at some point, thus causing a stack overflow after a number of frames.
I haven't been able to locate the problem in the code. Perhaps I forgot to un-comment something at that point http://www.opengl.org/discussion_boards/ubb/smile.gif

Anyway, I still don't know how to read out the error-values of an OpenGL application. I see in the references that push_matrix generates and GL_STACK_OVERFLOW error, but how can I show it on screen?