need series of pop/push/matrixmode explained

Started by
3 comments, last by jpetrie 17 years, 8 months ago
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?
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.

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.
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.
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.

This topic is closed to new replies.

Advertisement