glPopMatrix, glPushMatrix??

another question i ahve is regarding the glPopMatrix and glPushMatrix…when exactly do i use these matrices…is there a rule to follow?
i understand they are used to save the state of the current matrix…but i am still sorta confused how to go about using them in coding…should i use it before i rotate a matrix, only?

You use glPushMatrix and glPopMatrix around a block of drawing code in which you temporarily want to do some additional translates/rotates/scales or whatever and then later want to undo these actions without having to keep exact track of what you did.
For example: when positioning the limbs of a character you would

  1. glTranslate/glRotate to the location of the body
  2. glPushMatrix
  3. glTranslate/glRotate to the left limb
  4. draw the left limb
  5. glPopMatrix
  6. glPushMatrix
  7. glTranslate/glRotate to the right limb
  8. draw the right limb
  9. glPopMatrix


Do keep a close look on the number of Push and Pops you do, they absolutely have to be balanced at all times!