• Advertisement
Sign in to follow this  

need series of pop/push/matrixmode explained

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

now, i know what stack/heap is, it's just that i'm pretty confused by the following code (which works perfect for me though). here's the code that draws the scene:
(1)glMatrixMode(GL_MODELVIEW);
(2)glLoadIdentity();
(3)glPushMatrix();
//here's where the basic 3d scene drawing goes on
(4)glPopMatrix();
(5)glMatrixMode(GL_PROJECTION);
(6)glPushMatrix();
(7)glLoadIdentity();
(8)glOrtho(0,640,0,480,-1,1);
(9)glMatrixMode(GL_MODELVIEW);
//2d scene drawing on top of 3d scene (with depth test off)
(10)glMatrixMode(GL_PROJECTION);
(11)glPopMatrix();
i'm already confused by line (3). are we pushing the ID-matrix? if we change modes and don't push or LoadIdentity afterwards, does the current mode use the matrix from the mode before?

Share this post


Link to post
Share on other sites
Advertisement
OpenGL is a state machine. In general, unless you explicitly alter a state, it remains in that state. So when you call glMatrixMode(GL_PROJECTION), all subsequent actions affect the projection matrix stack, until you call glMatrixMode() again with GL_MODELVIEW or another valid value to change to a new stack. Et cetera.

When you change modes via glMatrixMode, you don't affect the contents of any matrix stack at all. You are just telling OpenGL which stack to apply subsequent matrix-related calls to.

Line (3) is pushing the modelview stack, which happens to contain the identity matrix at this point.

Share this post


Link to post
Share on other sites
ok then, here goes.
a) what's the point of pushing the ID in (3), if we later want the ID we could just do a LoadID, no? in this case: could i not replace (3) and (4) with a glLoadIdentity at (4)?
b) what are we pushing in (6)? we changed to projection but didn't load anything yet.

Share this post


Link to post
Share on other sites
Quote:
Original post by back2newbelf
ok then, here goes.
a) what's the point of pushing the ID in (3), if we later want the ID we could just do a LoadID, no? in this case: could i not replace (3) and (4) with a glLoadIdentity at (4)?
b) what are we pushing in (6)? we changed to projection but didn't load anything yet.


You might be pushing and popping the stack between (3) and (4), general system goes:


- set camera position
- for each object:
- push matrix
- set objects transformation matrix (multiply with current matrix)
- render object
- pop matrix back to camera matrix

and then there might be recursive camera matrices applied, like mirrored surfaces or TV screens etc..
But there's no real rule on how you have to do this and I do see a lot of extra push/pops in peoples code. If it works, it works and thats it!

ch.

Share this post


Link to post
Share on other sites
Pushing at (6) is to save the current projection matrix in preparation for altering the projection matrix for orthographic projection. Once rendering using the ortho projection is finished, we want to restore the original (perspective) projection matrix.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement