Why use glTranslatef? Why not just change the rendering co-ords?

10,428

In legacy OpenGL matrix manipulation commands, such as glTranslate, alter the matrix that is currently top of the selected matrix stack.

For instance, when you select MODELVIEW as the current stack and call glTranslate, the current modelview matrix is replaced by M_current * M_translation.

Your code effectively performs an additional translation to vertices submitted to the GL but, in contrast to adding a constant offset to your x-coordinates, there may be additional transformations like rotations, scaling and possibly even one or more translation already encoded in your current MODELVIEW matrix.

So no, unless your current MODELVIEW matrix isn't the identity, the two are generally not equivalent.

However, if your only intent is to translate your vertices and never change the position again, you can add a constant offset.

If you ever only need a translation, you can do it without any problems inside a shader. This way you can alter the offset dynamically via a uniform. If you add a constant offset and upload your data to a VBO and need to change it again afterwards, you'll need to update the buffer, which simply is an unnecessary waste of bandwidth and memory transactions.

Share:
10,428
Matt Reynolds
Author by

Matt Reynolds

Mechatronics Engineering student at the University of Waterloo

Updated on June 14, 2022

Comments

  • Matt Reynolds
    Matt Reynolds almost 2 years

    I'm making a simple Pacman game in C++, using OpenGL and SDL. I was going to use the glTranslatef function, but it seemed simpler just to change the co-ords that the drawing function used. I'm wondering, why would you/should you use glTranslatef?

    Here's a quick example of both glTranslatef and simply changing the co-ords

    glPushMatrix()
    glTranslatef(10,0,0)
    draw()
    glPopMatrix()
    

    or

    //Assuming the draw function took co-ords
    draw(x+10 ,y ,z)
    

    My only guess is that they are effectively the same, it's solely preference. Can you enlighten me?

    EDIT

    Please note that I am only talking about the translate function. Everything about scaling and rotating is irrelevant, I am speaking ONLY about translations. (This is because translations are easy without the glTranslatef function, where as the other two are more complex without their functions)