OpenGL transformations (glScale, glTranslate, etc)

Katianie picture Katianie · Mar 19, 2011 · Viewed 47.4k times · Source

I'm learning about openGL and how to do transformations such as translating and scaling. I know you have to usually translate to the origin, then do whatever you want (lets say scale), then translate back. From my understanding this is done manually but you can do the same thing with glScale().

My question is do I still need to translate to the origin and back if I use the glScale function?

Answer

Martin Stone picture Martin Stone · Mar 19, 2011

You probably don't need to do any translation to the origin and back, just do the transformations in the required order. Remember that the last transformation applied takes place in the transformed space of the previous ones. For example:

// draw object centred on (1,2,3) and ten times bigger
glTranslatef(1,2,3);
glScalef(10,10,10);
drawObject();

versus

// draw object centred on (10,20,30) and ten times bigger
glScalef(10,10,10);
glTranslatef(1,2,3);
drawObject();

In the second example, both the translation and the object are scaled x10, because they're done after the scale. (This scheme allows drawObject() to include transformations and still behave like a single unit.)