18. OpenGL Popping and Pushing Matrices
Posted on : 25-03-2010 | By : Swiftless | In : OpenGL
Tags: matrices, OpenGL, pop, programming, push, tutorial
4
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. 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) { |
Related posts:







=| 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
[...] projections 18. OpenGL Popping and Pushing Matrices | SwiftlessWhen drawing objects in OpenGL, it can be annoying that you do a translation, and then before [...]