开发者

OpenGL transformations (glScale, glTranslate, etc)

开发者 https://www.devze.com 2023-02-17 15:42 出处:网络
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

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 c开发者_开发百科an 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?


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


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.


All glScale does is multiply the current matrix by a scale matrix. So yes, for what it sounds like you want to do, you would want to apply the scale matrix before applying a translation.

See also glScale reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消