• ### Popular Now

• 9
• 9
• 10
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# glPush/PopMatrix

This topic is 5710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im wondering, when I call glPushMatrix() it preserves the state and pushes it on to the matrix stack right? Then if i cal glTranslate* and do other stuff, then glPopMatrix it moves me back to the position where I was before the glPush call right?

##### Share on other sites
It doesn''t preserve state, it "preserves" the current matrix (of whichever matrix mode you''re in, GL_TEXTURE, GL_MODELVIEW, etc) by pushing it onto a stack. Then, whatever changes you make are "erased" when you pop, because that old matrix is reloaded.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//

##### Share on other sites
ok thanx, by preserving state i meant the current matrix btw.

##### Share on other sites
You got it. Just making the clarification.

##### Share on other sites
So everytime i draw something like rendering a model or terrain i should call glPushMatrix, translate the terrain/model wherever i want it and rotate/scale it how i want, then when i finished drawing it call glPopMatrix and restore the modelview back to the original position?

##### Share on other sites
You can, but if you store the positions of your models in unrelative world space, you won''t have to. Most often you do though.

yes

##### Share on other sites
quote:
Original post by ZealousElixir
You can, but if you store the positions of your models in unrelative world space, you won''t have to. Most often you do though.

I''ve got 3 objects with the positions:

  Obj1X = 5; Obj1Y = 3;Obj2X = 1; Obj2Y = 2;Obj3X = 3; Obj3Y = 4;

And when i draw them I do like this:

  glPushMatrix();glTranslatef(Obj1X, Obj1Y, 0);// Draw Obj1;glPopMatrix();glPushMatrix();glTranslatef(Obj2X, Obj2Y, 0);// Draw Obj2;glPopMatrix();glPushMatrix();glTranslatef(Obj3X, Obj3Y, 0);// Draw Obj3;glPopMatrix();

Is there another way?

##### Share on other sites
That''s a good way to do it. I recommend reading the chapter about transformations in the red book(there''s an online version somewhere).

##### Share on other sites
Yeah there''s another way. If all those objects are relative to a single coordinate system (i.e. they have no additional rotations or translations required to draw them), you don''t have to call glPush/Pop for each item. Like I said though, most of the time you will want them drawn independently of each other and the current modelview coordinate system. So, that method is fine.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//