Results 1 to 5 of 5

Thread: glPushMatrix() Efficiency

  1. #1
    Member Newbie
    Join Date
    Apr 2002
    Posts
    47

    glPushMatrix() Efficiency

    Which is more efficient?

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

    OR

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

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2001
    Location
    Holland
    Posts
    179

    Re: glPushMatrix() Efficiency

    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.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: glPushMatrix() Efficiency

    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.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Apr 2001
    Location
    Greece
    Posts
    477

    Re: glPushMatrix() Efficiency

    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?

  5. #5
    V-man
    Guest

    Re: glPushMatrix() Efficiency

    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

Similar Threads

  1. glPushMatrix and GL_PROJECTION
    By ajbiv in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-17-2009, 03:39 PM
  2. glPushMatrix()
    By kazmalak in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-06-2005, 04:44 AM
  3. glPushMatrix
    By Vlasko in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-22-2004, 11:34 AM
  4. glPushMatrix()???
    By fabianspring in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 08-04-2002, 01:08 PM
  5. glPushMatrix() and glPopMatrix
    By zzapatista in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-19-2001, 02:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean