Archived

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

xDS4Lx

glPush/PopMatrix

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 this post


Link to post
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.//
miscellaneous links

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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.//
miscellaneous links

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ZealousElixir
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.

<font color = "#A8A8A8" size = "1" face = "verdana, tahoma, arial, default-sans-serif">//<a href = "mailto:zealouselixir@yahoo.com">email me.</a>//<a href = "http://hosted.totalim.com/zealouselixir/">zealouselixir software.</a>//<a href = "http://msdn.microsoft.com/library/default.asp">msdn.</a>//<a href = "http://www.gamedev.net/reference/start_here/">n00biez.</a>//<br><a href = "http://www.ojuice.net/">m</a><a href = "http://www.nvidia.com/">i</a><a href = "http://www.gamasutra.com/">s</a><a href = "http://www.gdmag.com/">c</a><a href = "http://www.flipcode.com/">e</a><a href = "http://www.cprogramming.com/">l</a><a href = "http://www.bloodshed.net/">l</a><a href = "http://shilbert.cjb.net/">a</a><a href = "http://www.facehat.com/">n</a><a href = "http://nehe.gamedev.net/">e</a><a href = "http://nexe.gamedev.net/">o</a><a href = "http://www.gamedeveloper.com/">u</a><a href = "http://www.voxelsoft.com/sk/">s </a><a href = "http://www.gamespy.com/">l</a><a href = "http://www.codeproject.com/">i</a><a href = "http://www.sourceforge.com/">n</a><a href = "http://www.codeguru.com/">k</a><a href = "http://www.thinkgeek.com/">s</a><br></font>


Ok, so whats the fastest way to do it? (The code i posted is everything there is to it, they do not have any rotation or scaling or whatever).

Share this post


Link to post
Share on other sites