Ashika Umanga Umagiliya
Ashika Umanga Umagiliya

Reputation: 9158

OpenGL independent translation after doing translation+rotation?

I want to acheive translations/rotations as shown in following video -

http://www.youtube.com/watch?v=92h0xuV4Yrg

I know for independant translation and rotation, typical method is - first rotate the object and then translate.

As shown here:

PushMatrix();
LoadIdentity(); 
Translate(posx,posy,posz);     // Second, move the object to its final destination
Rotate();        // First, apply rotations (which rotate around object-origin
Draw();         
PopMatrix();

But my requirment is to rotate the object around world-coordinate and move it using mouse.

As seen in the video, the rotation is done around the world-origin not around object-origin.That means the logic should be opposite to above (first translate and then do the rotation)

PushMatrix();
LoadIdentity(); 
Rotate();        // Second, apply rotations (which rotate around world-origin)
Translate(posx,posy,posz);     // First, move the object to its final destination
Draw();         
PopMatrix();

But the,final translation in the video is done relative to final position after the rotations.(which is not same as the posx,posy,posz values I manipulate using mouse ).

How to achive this kind of translations

Upvotes: 1

Views: 1211

Answers (1)

user1118321
user1118321

Reputation: 26355

To rotate an object around any point (x,y,z), you do the following:

translate (-x,-y,-z);
rotate (angle);
translate (x, y, z);

That point doesn't have to be a point on the object. It can be the origin, or any random value like (1000, 0.0023, 97.5). You may have to do an additional translation in your case. For example, if your object is defined at the origin, and the user has placed it at (x1,y1,z1) and you want to rotate it around (x,y,z), you'll need to add a:

translate (x1,y1,z1);

after the translate (x,y,z).

Upvotes: 2

Related Questions