A Problem about Rotation,Translation

I have a model centered in origin, and i want to use mouse to control its transformation, i.e.,left click to control its rotation, right click to control its translation and middle click to control its zooming.

At the beginning of every frame, i added these code:


    //Applying transformation

    DrawScene();  //Draw the model

glPopMatrix();    //Clear the modelview matrix to identity matrix

where Position_x,Position_y,Position_z are modified in the event of RightClick according to the offsets of mouse position. The processing of Rotate_x, Rotate_y, Rotate_z, Scale_x, Scale_y, Scale_z is similar to above.

In most conditions,these code work fine . But i observe that in certain condition, the rotation around y axis is swaped to the rotation around z axis. I don’t know how to avoid this problem. Any one can give me some


That’s exactly what I’ve ever experienced.
In fact, you need NOT do x, y and z rotation together to change your view angle. This always makes you confused. In fact, it’s enough to do only x and y rotation and then you can see every espects of your model. Pay attention to the order of the rotations, which may lead to totally different result. Usually you may do y rotation first.

Thanks for you reply!
In my program, i need that the user can select around which axis(x,y,or z) the model is rotated. So i must change Rot_x, Rot_y, Rot_z simultaneously.
In my opinion, once the modelview matrix is cleard to identity matrix in the beginning of each frame, the rotation order is not a problem. Maybe i am wrong, but i can’t understand why this phenomena will occur.

What your experiencing is called gimble lock… Try searching for Quaternions on this board or google to get more information on them and how can they be used to avoid gimble lock.


Just do the delta_x and delta_y for the rotations, it will work fine, but some may say that it is not realisitic as a particular point will not follow the mouse (exactly), then have a look at quaternians, these will not fix the problem but there should be some stuff for mapping mouse movements onto a sphere using quaternians, this is how you do the trackball type of rotations.