how to rotate a object around different axes for many times?

I wrote codes below to rotate a cube aroud x axes first,and then rotate around Y axes:

glPushMatrix();
glRotatef(90.0,0,1,0);
glRotatef(90.0,1,0,0);
glTranslatef(0,0,-5);
DrawCube();
glPopMatrix();

but I fail,after execuete the “glRotatef(90.0,1,0,0);” statement,the Y axes also rotate around x,and then execuete the “glRotatef(90.0,0,1,0);” statement,the cube looks like turn round z axes,so I fail.
anyone can help me?how to rotate a object around different axes for many times?

You are experiencing Gimbal lock. Do a quick search on the net and you should be able to find a couple different ways to handle the rotations differently to deal with this.

-Evan

Out of curiosity…would replacing the two glRotate*() procedures with glRotatef(90.0f,1.0f,1.0f,0.0f) fix the problem?

  • Halcyon

Edit: Sorry, forgot to mention that I was reading the Red Book, and it says the glRotate*(…) procedure does a rotation around an arbitrary axis. So, instead of doing two consecutive rotation around two axes, would just having one rotation around one axis (the combination of the other two axes) prevent the gimbal lock? Because if you can specify any axis, it is a lot like what a quaternion does (allow rotation about an arbitraty axis).

[This message has been edited by Halcyon (edited 01-06-2003).]

Hi,

It seems to me like you don’t understand the opengl local coordinate system. Indeed, if you use glRotate, you will rotate the axes as well. Even the glTranslate will translate the cube in the direction of the new z-axis. So it works differently than the world coordinate transformations that are used in pov-ray, for example. A quick fix here would be to reverse the order of your transformations. When you get the hang of it, you will find out that there are several advantages in the way opengl does it’s transformations.

-Ilkka

Halycon,

you idea works perfectly… in theory.
The problem is that the combined axis and the combined angle are very, very difficult to compute.

The best way, in my opinion, to handle combined rotations (and hence the gimbal lock) is as matrices, because it’s very simple to use with opengl (you work directly on the modelview matrix, you do only standard opengl calls like glRotated) but some other people prefer quaternions because they allow for more effective computations.

Originally posted by Morglum:
[b]Halycon,

you idea works perfectly… in theory.
The problem is that the combined axis and the combined angle are very, very difficult to compute.
[/b]

Forgive my ignorance, but don’t quaternions allow you to do this in a reasonably straightforward way?

Maybe (I didn’t know, thanks), but I was speaking of rotations expressed as axis+angle.