PDA

View Full Version : rotation problem with the axes

01-15-2001, 06:30 AM
hi!

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?

thanks
patric

glPushMatrix();
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);

glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,4);
glEnd();

glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(4,0,0);
glEnd();

glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,4,0);
glEnd();

01-15-2001, 08:47 AM

01-15-2001, 03:18 PM
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?

Deiussum
01-16-2001, 04:52 AM
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. http://www.opengl.org/discussion_boards/ubb/smile.gif

spak
01-23-2001, 07:18 PM
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:

initialization
... assuming that you're in MODELVIEW MODE

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

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 );

glPopMatrix();

---------------------------------

for display...

glPushMatrix()
... do translation
... do scale

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

glBegin()... polygon definition.... glEnd()
glPopMatrix()
--------------