glPushMatrix() Efficiency

Which is more efficient?

glPushMatrix();
glTranslate(a, b, c);
drawObject();
glPopMatrix();

OR

glTranslate(a, b, c);
drawObject();
glTranslate(-a, -b, -c);

That simple case may be more efficient without the push/pop. However, soon you may be using lots of translates/rotates/scales etc. and then keeping track of the correct inversion of all of these operations becomes much more tedious than just issuing a push beforehand and a pop when you’re done…

Just my .02

Jean-Marc.

Not to forget the amount of round off error that accumulate when doing the inverse transformation.

Honestly, I don’t think that’s the largest problem though. The largest advantage of using the matrix stack is, as JML says, to make the code clean, and you don’t have to care about performing the inverse transformations in the correct order. As soom as you get some levels of transformations, you will easily get lost.

I was curious about this one as well.I’m not sure how the matrix stack is implemented but I’ve done similar things and pushing/poping is usually trivial(i.e. in this case more or less as expensive as copying all 16 components of the matrix),unless ofcourse this pushing or poping does other things as well wich I’m not aware of.Inverse operations on the other hand must first be computed(easy for translation but rotation need sines and cosines) and then multiplied by the current matrix.This must be more expensive although it might not make a big(or even noticeable)difference in modern hardware and for simple cases like the on above.What do you think?

Either there is a pointer to stack element or the top of the stack is always the current matrix:

//stack pointer
glPushMatrix()
{
for(i=0; i<16; i++)
stackpointer[16+i]=stackpointer[i];
if(stackpointer==upperlimit)
Error;
else
stackpointer+=16; //moving up
}
glPopMatrix()
{
if(stackpointer==lowerlimit)
Error
else
stackpointer-=16;
}
I think it was the second method used on some hardware.

V-man