Skip to main content
GameDev.net gamedev.net
🔒 Locked

glPushMatrix()/glPopMatrix() & glLoadMatrixf()

Started by fpsgamer Apr 8, 2008 at 1:52 AM 9 replies 18k views
Original Post
fpsgamer
fpsgamer
I am attempting to use glPushMatrix() and glPopMatrix() in conjunction with my own custom matrices via calls to glLoadMatrixf(). I executed the following steps: (1) Called glLoadMatrixf() to load a matrix which contains only tanslation. (2) Called glPushMatrix() (3) Called glLoadMatrixf() again to load a matrix which contains only rotation. I found that no matter what I did the translation portion is never applied. However If things work fine when I use glTranslatef() and glRotatef() in place of my own matrices -- please note I did check my matrices thoroughly and was able to verify my calculations.
Gage64
Gage64
It would help to see some code, but if you are not calling glPopMatrix() somewhere then of course the translation will never be applied. Edit: Sorry, that's completely incorrect. The translation is not applied because glLoadMatrix() replaces the current matrix with the one you give it, so your rotation matrix overwrites the translation matrix. You probably want glMultMatrix().

If that doesn't help, can you post the code?

[Edited by - Gage64 on April 8, 2008 2:16:44 AM]
fpsgamer
fpsgamer
Quote:
Original post by Gage64
The translation is not applied because glLoadMatrix() replaces the current matrix with the one you give it, so your rotation matrix overwrites the translation matrix. You probably want glMultMatrix().


Thats what I figured was happening.

But if a call to glLoadMatrix() makes a matrix active, how come glPushMatrix() fails to push the active matrix onto the stack?

Does this means that glPushMatrix() only works with the built in OpenGL matrix functions glTranslate/glRotate/etc?

PS. On a side note, are calls like glMultMatrix() ever implemented in hardware ... just curious.

[Edited by - fpsgamer on April 8, 2008 12:15:05 PM]
dmatter
dmatter
Quote:
Original post by fpsgamer
Once I make a matrix active through a call to glLoadMatrix(), how come glPushMatrix() fails to push the active matrix onto the stack?

Does this means that glPushMatrix() only works with the built in OpenGL matrix functions glTranslate/glRotate/etc?


glPushMatrix didn't fail to push onto the stack; it's job is to push a copy of the current matrix onto a stack of matrices. Those matrices on the stack don't interact at all. You only manipulate the current, top-most, matrix at any given time.

Example 1:
   command                 resultglLoadMatrixf(A)       stack = [A]glPushMatrix()         stack = [A, A]glLoadMatrixf(B)       stack = [B, A]glPopMatrix()          stack = [A]

Example 2:
   command                 resultglLoadMatrixf(A)       stack = [A]glPushMatrix()         stack = [A, A]glMultMatrixf(B)       stack = [AB, A]glPopMatrix()          stack = [A]


Notice that glLoadMatrixf replaced the top-most matrix, whilst glMultMatrixf just manipulated it.
Likewise, functions such as glTranslate* and glRotate* just manipulate (by multiplication) the top-most matrix.

[Edited by - dmatter on April 9, 2008 9:31:16 AM]
fpsgamer
fpsgamer
Wow, thanks dmatter. That was an awesome explanation.

I was previously under the impression OpenGL applied each matrix in order starting from the top of the stack ... thanks for clearing that up for me.
polymorphed
polymorphed
Quote:

Once I make a matrix active through a call to glLoadMatrix(), how come glPushMatrix() fails to push the active matrix onto the stack?


How do you know that it fails to push it on to the stack?

Quote:

Does this means that glPushMatrix() only works with the built in OpenGL matrix functions glTranslate/glRotate/etc?


Nope.
glTranslatef/glRotatef doesn't do anything you can't do with glMultMatrixf, so they're not special commands or anything.

Are you sure you have the correct matrixmode set? Try putting some glMatrixMode(GL_MODELVIEW); before you do matrix commands, and see if it helps.
while (tired) DrinkCoffee();
dmatter
dmatter
Quote:
Original post by fpsgamer
PS. On a side note, are calls like glMultMatrix() ever implemented in hardware ... just curious.

Almost definately no, not on modern cards anyway.
They might be implemented fairly efficiently in assembly though.
I believe they've been ditched in the upcoming OpenGL 3.0 specification too.
V-man
V-man
Quote:
Original post by dmatter
Quote:
Original post by fpsgamer
PS. On a side note, are calls like glMultMatrix() ever implemented in hardware ... just curious.

Almost definately no, not on modern cards anyway.
They might be implemented fairly efficiently in assembly though.
I believe they've been ditched in the upcoming OpenGL 3.0 specification too.


glPushMatrix and glPopMatrix aren't either. The driver keeps a matrix stack in RAM and it does glLoadMatrixf calls.
Hopefully, GL 3.0 will be released some day.
fpsgamer
fpsgamer
Quote:
Original post by dmatter
Example 2:
   command                 resultglLoadMatrixf(A)       stack = [A]glPushMatrix()         stack = [A, A]glMultMatrixf(B)       stack = [BA, A]glPopMatrix()          stack = [A]



After looking at the function, glMultMatrixf seems to right multiply its argument with the active matrix.

So shouldn't this:

glMultMatrixf(B) stack = [BA, A]

be this instead:

glMultMatrixf(B) stack = [AB, A]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.