Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opengl

sdl

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)

like image 660
Matt Reynolds Avatar asked Aug 30 '13 17:08

Matt Reynolds


1 Answers

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.

like image 163
thokra Avatar answered Nov 14 '22 22:11

thokra