PDA

View Full Version : Translate after rotate



Darius
10-30-2012, 11:29 AM
I am trying to translate an object in world space after rotating it at the origin. The problem is, after I rotate an object while it's at the origin, its local coordinate system rotates as well, so its axes are not parallel with the world coordinate axes anymore. So, for example, if I needed to translate an object to the point (3, 0, 5) after rotating the object around the y-axis, I would not be able to do this since the object's local coordinate system isn't the same as the world coordinate system. For example:



glRotatef(45, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 5.0);
glutSolidCube(1); // This won't be at (3,0,5) like how I need it


How can I overcome this?

Claude
10-30-2012, 01:17 PM
Try:
glTranslatef(3.0, 0.0, 5.0);
glRotatef(45, 0.0, 1.0, 0.0);

Instructions are executed in reverse order.

Aleksandar
10-30-2012, 02:09 PM
Or even better, read Ch3 of the Red Book (http://www.glprogramming.com/red/chapter03.html).
Instructions are not executed in reverse order! They are executed in the order they are issued.
If you "think in local coordinate system", commands actually move local coordinate system, and all transformations are done in local coordinate system.
If you "think in global coordinate system", object are moved in that system as the transformations are done in global system, but in the reverse order.
Of course, OpenGL Programming Guide would explain everything much better. :)

Aliii
10-31-2012, 03:58 AM
Is this what you are trying to do?:

pushMatrix()
loadIdentity()

for(every object){

pushMatrix()
translate()
rotate()

drawObject()

popMatrix()
}

popMatrix()

Darius
11-01-2012, 01:21 AM
Thanks guys, that cleared things up for me :)
And thanks for the link, I didn't even know the red book was online!

Dan Bartlett
11-01-2012, 05:58 AM
It's a very old version that's online (version 1.1). Recent editions of the red book (is it now orange?) include more of the up to date stuff, with the most recent version covering OpenGL 4.3 AFAIK.

Aleksandar
11-01-2012, 10:50 AM
Nope! The latest is OpenGL Programming Guide: The Official Guide to Learning OpenGL, Versions 3.0 and 3.1 (7th Edition).
The greatest part of the book is about legacy OpenGL. Shaders are just mentioned and without a context (not GL context, but how to use).

Dan Bartlett
11-01-2012, 07:23 PM
Ah yeah, it says "This title has not yet been released"/"Publication Date: 7 Feb 2013" on Amazon for the 8th edition.