Advertisement

Translating and Rotating?

Started by October 17, 2005 10:41 PM
6 comments, last by haegarr 18 years, 11 months ago
I have a program that loads object files from a file. I then need to be able to select an object, move it around, and be able to rotate its around its center. I'm keeping track of the translation/rotation data in a struct. Here is how i'm trying to do the translations and rotation: glLoadIdentity() glTranslate(translation data) glRotate() drawObject glTranslation(-translation data) This works fine for the translation, but it seems to be rotating about the global axis. I also tried: glLoadIdentity() glRotate() glTranslate(translation data) drawObject glTranslation(-translation data) This caused the same exact thing to happen. Any ideas on what I am doing wrong?
Calling glTranslate after drawObject is going to have no effect whatsoever. The only thing that matters is what the matrix stack is when the draw call is made.
Advertisement
ok, so any idea how to make the object rotate around it's center instead of the global axis?
Yes. The way you rotate about an arbitrary axis: translate to the given axis, rotate, translate back. What you're doing wrong, is not translating back (at least, not in any way that matters).
you should also be conserving your matrices by using calls to PushMatrix() and PopMatrix() around the code you are currently using. It may not have much of an effect here but as you expand your code it will become important (not to mention it's just good practice :) )
so i should do:

translate
rotate
translate
draw


?

Also i need to be able to move the object around the screen. Will i have to do additional translate operations to accomplish this?
Advertisement
Yes and yes.
Be aware of the different meanings of the done translations. Rotation (and also scaling) need a "center" location that is mapped onto itself. To reach this goal, you have to translate so that the center falls onto (0,0,0) which is invariant against rotation/scaling. However, since that location isn't your actual origin, you have to translate back after doing rotation/scaling. At least you have to do the usual translation to re-locate the object in the world. In a formula it may look like this (C^-1 being first applied)

M := T * C * R * C^(-1)

where T is the translation in world, C ist the translation to the center, and R ist the rotation. Of course, in this formula T * C could be summarized. Notice furthus that this isn't the one-and-only solution, but it is right if restricted to rotation and translation, and requesting the translation being invariant to the rotation (what is used widely).

You could extend this e.g. if you want to integrate scaling in particular directions. In such a case you have to rotate to the scaling direction and then to scale, yielding in something like

M := T * C * R * O * S * O^-1 * C^-1

where O is the scaling orientation and S is the scaling (this formula is used by VRML). Hope I've could made clear the principles of why to do such translations Sneftel has mentioned.

This topic is closed to new replies.

Advertisement