# OpenGL Object Rotation on local axis in OpneGL

This topic is 5006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am programming a littel simulator for R/C-Planes in OpenGL. I Have an Object (my plane) i want to rotate aroung the x- (pitch) an z-axis (roll). This makes 2 rotations. Illustration: www.roebbeling.de/3dstuff/perspec.jpg My Problem, when i do the first rotation (x-axis) the axis of the object are not rotated. so when i make the second rotation i cant roll tth plane. i rotate around the normal z-axis and not the rotated z-axis. You can download a testfile in C under: www.roebbeling.de/3dstuff/zagi.c I have read about rotation around an arbitrary axis by quaternions, but i cant figure out to do this with an object. That means i have to rotate every point in the object. Is there another way of doing this.

##### Share on other sites

The typical transformation order is Scales, then Rotations, then Translations. This is because rotations are relative to the given axis and scales are relative to the origrin. You may have seen it written as TRSx = x', but that isn't as clear as (T(R(Sx))) = x'

(man, that's the last time I try even simple ascii art. Apologies to the mods if they see all of those edits I made)

##### Share on other sites
I have tried it without any translation or scaling. The Problem stays the same.

##### Share on other sites
Quote:
 My Problem, when i do the first rotation (x-axis) the axis of the object are not rotated. so when i make the second rotation i cant roll tth plane. i rotate around the normal z-axis and not the rotated z-axis.

Change the order of rotations. You want R=Rx*Rz.
There's 2 ways to think of rotations (or better coord systems). If you think left to right, ie Rx -> Rz (in OpenGL, Direct3D it's the reverse) then think in terms of local coord system of the plane - that is you're rotating the plane's coord system around its local x-axis and then rotating it about its local z-axis which has changed. If you think right to left, ie, Rz -> Rx (its still R=Rx*Rz but you're *thinking* in a different direction) then you're rotating the plane around the global coord system which is you're rotating about global z-axis first and then rotating about global x-axis. You'll see both ways of thinking end up with the same result. The first way of thinking is very helpful when you do articulated objects where you have a hierarchy of bones and the transformation of a bone is influenced by its parent bone.

For small rotation angles your Euler angle method works fine.

##### Share on other sites
Here's EXACTLY what you need to do. Create a matrix which is the identity matrix. Then each time you perform a rotation, get the modelview matrix with glGetMatrixf(GL_MODELVIEW, mat), where you declared float mat[16];

Then simply take your created matrix, L and multiply it with mat
L * mat;

Then store the result back in L. Then clear the modelview matrix with glLoadIdentity and simply call glLoadMatrix(L);

What this will basically do is concatenate all your rotations and it will enable you to do local rotations no matter what the orientation of the model is.

You do the same thing after translating or scaling your model.

So here's a short example

glClear(...);
// perform camera rotations, translations, ect... here

glPushMatrix();

glRotate(angle, 0, 1, 0); // rotate model around y-axis
glGetMatrixf(GL_MODELVIEW, mat);
multiplyMatrices(local_matrix, mat);

glPopMatrix();
glMultMatrixf(local_matrix);

##### Share on other sites
Quote:
 Original post by oconnellseanmSo here's a short exampleglClear(...);glLoadIdentity();// perform camera rotations, translations, ect... hereglPushMatrix();glLoadIdentity();glRotate(angle, 0, 1, 0); // rotate model around y-axisglGetMatrixf(GL_MODELVIEW, mat);multiplyMatrices(local_matrix, mat);glPopMatrix();glMultMatrixf(local_matrix);

That's no different to:

glClear(...);glLoadIdentity();// perform camera rotations, translations, ect...    hereglRotate(angle, 0, 1, 0);             // rotate model around y-axis

Except that your version is horribly inefficient. When you call glRotate, the rotation matrix is automatically post-multiplied with the current modelview matrix.

##### Share on other sites
Actually its completely different from what you said.

You do not just call glRotate, you also STORE that rotation and multiply it against your local_matrix, which is storing every single transformation you perform on your model. Its very fast and it works perfectly. This is exactly how I do it in my world editor software, http://www.freeworld3d.org. You can rotate against the local or world axes.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633346
• Total Posts
3011442
• ### Who's Online (See full list)

There are no registered users currently online

×