# 18. OpenGL Popping and Pushing Matrices

**You** may notice that when you assign a colour to an object, that colour will also

attach itself to every object you draw after the colour statement. Or the same for

rotations and translations. This is because OpenGL is based on a state machine.

If you want to really understand what these following commands do, then you need

to know about stacks and state machines, but if you only want to use these commands,

then read on. The core ingredient behind OpenGL, and any 3D rendering engine infact, is a transformation matrix.

Now every time you perform an action such as glTranslate or glRotate, then you are

modifying the transformation matrix, and once something has been done, it cannot be

easily undone, so OpenGL makes use of states and says, OK, you are in this state, so

we will apply these transformations and all previous transformations. States still means

that once an operation has been performed, it will affect everything to come, but it means

we can go back to a previous state and forget about our current one.

To do this, we only need to make use of two commands.

They are:

glPushMatrix(); //set where to start the current object transformations

and:

glPopMatrix(); //end the current object transformations

The first call, glPushMatrix(), tells OpenGL to store the current state that we are in.

This then allows us to perform a bunch of different effects.

Then when we want to go back to our previous state, we call glPopMatrix().

This works fine for rotations and translations, but OpenGL allows for many other commands,

so it gives us the ability to store attributes with glPushAttrib() and glPopAttrib(). Some of the attributes that we can store include, GL_LIGHT and GL_COLOUR. Of course there are many

more, but they are out of the scope of this tutorial.

If you have any queries, feel free to email me at swiftless@gmail.com

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. |
// when you set the glPushMatrix(); it distinguishes where to start the transformations for an object // to set where to stop these transformations you would call glPopMatrix(); // for instance, if you take the push and pop matrix code out of #include <GL/gl.h> GLfloat angle = 0.0; //angle for cube1 void cube (void) { void cube2 (void) { void display (void) { void reshape (int w, int h) { int main (int argc, char **argv) { |

Hi

I am currently studying openGL etc and there is a certain method to work out the CTM…i was hoping maybe someone knows how to do this method. I understand the individual Matrix for each (Translate, Scale and Rotate) but there is a certain easier method used when you put them all together to obtain your new point. I would appreciate the help!

Thanks

Very, very big fan of your tutorials Swiftless.

Thank you again. They are terrific.

-kropcke

I get how to use them, but about how they work: Does it add and subtract the matrices, or does it store on Push, and reset to identity, then transform to stored position / orientation? And can you have nested Push/Pops? ex:

push()

translate

push()

draw

pop()

pop()

push()

rotate

push()

draw

pop()

push()

draw

pop()

pop()

Hi Theo,

The way matrices work in OpenGL, are based on a Stack setup.

You start off with a matrix on the stack called the “Transformation Matrix” (there are also ones for textures and the projection), every time you do a call to glTranslate or glRotate, this changes the “Transformation Matrix”. If you call glPushMatrix, it takes an exact duplicate of the current “Transformation Matrix” and puts this on the top of the stack and this becomes the current matrix for all further glTranslate or glRotate calls. You can nest as many of these as you like within each other. Then once you call glPopMatrix, it pops the current “Transformation Matrix” off the stack and forgets it, and the previous “Transformation Matrix” becomes the current.

I hope this helps a little.

Cheers,

Swiftless

Thank you very much, Swiftless. This is the best explanation of the OpenGL Transformation Matrix Stack I have found.

=| thanks for trying to explain, but im still lost. Don’t feel bad, 4 people ahve tried to explain it to me… I am still clueless… you should be on ##opengl on freenode

Is this article not done yet? I don’t see anything.

It should be up now sorry, I have had a few posts suddenly stop showing, but I think I have fixed them all now.

Cheers,

Swiftless