Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL transformations (glScale, glTranslate, etc)

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?

like image 672
Katianie Avatar asked Nov 29 '22 04:11

Katianie


2 Answers

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.)

like image 137
Martin Stone Avatar answered Dec 10 '22 04:12

Martin Stone


You have to think of the transformations taking place on a stack. In other words, the last transformation you specify takes place first. So,

glTranslatef(1,2,3);
glScalef(10,10,10);
glRotatef(45,1,0,0);
drawObject();

will first rotate 45 degrees about the x axis, then scale the object to (10,10,10), then translate to (1,2,3). However, you also have to remember that any transformation you apply affects transformations further down the line. If we reverse the order of the above transformations, the rotation will then rotate about a different point.

like image 30
Davido Avatar answered Dec 10 '22 03:12

Davido