Which is faster?

Hello

Which is faster? A glLoadIdentety() or a glPushMatrix()/glPopMatrix combination. The reason I wonder is that I load the identity matrix and then push the matrix before I draw anything and then pop it again, and I do this for each frame. Wouldn’t it be the same as loading the identity matrix at the beginning of each frrame instead?

Osku

I think when you do a glPushMatrix(), gl put it on the stack and loads a new identity matrix.

but on the other hand, glLoadIdentity() afaik clears up the stack…

a benchmark here would be nice

Bastian

glPushMatrix() only pushes the current matrix onto the stack and glPopMatrix() pops the current matrix from the stack. It doesn’t reload the matrix with identity after calling glPushMatrix()

just to for understanding
Osku you want to do something like

glMatrixMode(GL_MODELVIEW)
glLoadIdentiy();
glPushMatrix();
glTranslate(x,y,z);
glRotate(x,y,z,);
glBegin();
.
.
.
glEnd();
glPopMatrix();

i am not sure if that is a good way of doing it, since most cards don’t multiply with the identity matrix and i am not sure if the card recognizes a that the matrix you just popped out the stack is a ident matrix.

mcbestian:
glPushMatrix just puts the current matrix on the stack and doesn’t load the ident matrix and loading the identmatrix doesn’t do anything with your stack

Chris

ups…you’re right…I forgot that :slight_smile:
It won’t be funny if glPushMatrix loads an identity-matrix
sorry, my fault

Bastian

If you load an identity and then push it onto the stack, you should get it back when you pop it off. As I understand it, not multiplying with the identity matrix is a shortcut since it would be pointless - what you end up with is the matrix that you multiplied it by, so you could just bypass the multiplication step and say the answer is the matrix that you were multiplying the identity by (did that sentence make sense? ) This should have no effect on whether or not the identity matrix ended up on the stack - If the current matrix is the identity and you push it onto the stack, it ends up on the stack, no matter which matrix it happens to be. At least that’s near as I can tell from playing around with it a bit. Someone please let me know if I’m just on crack here.

Chris

Here is my understanding of it.

PushMatrix creates a new matrix with the current matrix’s state. PopMatrix will pop off the matrix and return you to the original state before you pushed. Loadidentity just changes the current matrix to the identity matrix. ( I guess that means 1’s in a diagonal and the rest zeroes in the matrix ) So, it just clears everything out.

So, if your keeping some sort of initial state in your matrix then calling load identity would erase it. Now if you wanted to keep that initial state then you could push…do stuff…pop and that would preserve the initial state. Otherwise, I guess you could call loadidentity if you were not keeping track of a state.

e.g.

//matrix A
PushMatrix();
//copy of matrix A
do stuff…
//matrix A is now altered…
glLoadIdentity()
//you cleared out altered matrix A to identity
PopMatrix();
//matrix A is restored to state before push


Or if you did not care about matrix A then

glLoadIdentiy();
do stuff…


Hmmm I bet glLoadIdentity might be faster because your not doing a push/pop but if you have to create some initial state each time then it could make it slower I suppose.

Hello

This is what I do :

GLvoid InitGL(GLvoid)
{


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

GLvoid display(GLvoid)
{
glPushMatrix();
glRotate…


glPopMatrix();
}

GLvoid Main(GLvoid)
{


glutCreateWindow(…);
InitGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Now I call the InitGL once and the identity matrix is loaded and i just push it and the pop it. Wouldn’t it be faster to just load the identity matrix each time instead of pushing and popping it?

Osku

The right answer is: Bench it!

glPushMatrix and glPopMatrix copy memory around on the matrix stacks.
glLoadIdentity just initializes the current matrix (and not to forget, there is no need to calculate an inverse of that). My guess is glLoadIdentity is faster in this case.

I think you’re wasting too many thoughts in this little detail. The main work will definitely be in another place, if you’ll have only those two calls.