rotation problem with the axes


i have the 3 X,Y,Z axes rendered using glut and rotation of the scene using the mouse.

i noticed while playing with it, rotation around the Z axis rotates the object(the 3 axes) around the local Z axis while attempting to rotate around the X/Y axes causes the scene to rotate around the global X/Y axes. The following code is taken from my “display()” function.

I notice that if i swap the order of the 3 rotations, the last one will rotate around the local axis.
How can i cause all the rotations to work either locally or globally and not a mix as it is at present?


glLoadIdentity ();
glTranslatef(changeTX, -changeTY, 0.0);

glRotatef (changeRY, 0.0, 1.0, 0.0);
glRotatef (changeRX, 1.0, 0.0, 0.0);
glRotatef (changeRZ, 1.0, 0.0, 0.0);




your rotation about he z goes about the x?

thanks grady for pointing that out, but no that is not the error causing me problems
( actually i am only rotating X,Y but i added in Z in this post for completeness…n i simply copy n paste the glRotate statement for Z)
for any suggestions for rotating the object by its local axes or global axes?

There are a couple of ways to think about how transformations work, but I tend to think of them as all being global. The reason your last rotation appears to be local is simply because it just so happens that the local and global coordinates are the same at the time. When thinking in terms of global transformations, you should be aware that due to the properties of matrix mathematics, the last thing you do is actually the first transformation applied.

Basically, that means that your code first rotates around the z axis, then the x, then the y, then translates.

If you’re only rotating around the X and Y, I would think that rotating Y, then X would give a decent result… in other words…

glRotate(currentXR, 1.0, 0.0, 0.0);
glRotate(currentYR, 0.0, 1.0, 0.0);

If what you want is to have the mouse rotate the object around the axes as you CURRENTLY SEE it, it’s going to take a bit more work.

I had a similar trouble with the rotation about the axis as you see it on the window. My solution is not elegant, but here it is:

… assuming that you’re in MODELVIEW MODE

GLfloat pRotateMatrix[16];
glGetFloatv( GL_MODELVIEW_MATRIX , pRotateMatrix );

rotating about your “viewing” axis

glRotatef( xangle , 1 , 0 , 0 );
glRotatef( yangle , 0 , 1 , 0 );
glRotatef( zangle , 0 , 0 , 1 );

// above takes care of local rotation

// below applies the transform to your current rotatation transformation.

glMultMatrixf( pRotateMatrix );

// save the matrix for later
glGetFloatv( GL_MODELVIEW_MATRIX , pRotateMatrix );


for display…

… do translation
… do scale

glMultMatrixf( pRotateMatrix); // this takes care of rotation

glBegin()… polygon definition… glEnd()