18. OpenGL Popping and Pushing Matrices
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) { |