rotation of a rotation in OGL

maha_x

New member
Im trying to do two sequencial rotations in OGL. It doesnt work just by adding two sequential glRotatef() cmds. What I remember from my math class is that I should multiply these two different rotation matrixs together before using the matrix to rotate my geometry. But I cant find a way to extract the first rotation matrix to be stored and then multiplied by the second rotation matrix. Im sure it's possible, but how?
 
No I didnt get it. I try to do 3rd person view, so first I rotate the object to face backwards to the camera and then rotate with normal of the polygon the object is resting on to tilt the object into natural position. The last rotation works fine alone, but I cant get the first rotation to work correctly. Lets say I have a vector for the camera direction, components angle_x, angle_y, angle_z. Each is a value between 0.0 and 1.0 (vector is normalized). I'd command glRotatef(90, angle_x, angle_y, angle_z); The object seems to rotate around wrong axis. I tried changing the x, y, z order in the glRotatef but no help. Curiously if I run the same command with 180 degree It works except the object rotates 180 degrees when the camera turns 90 degrees. What's up?
 
Try this Matrix:
Code:
  Matrix4f rotation = new Matrix4f();
  Matrix4f rotateY  = new Matrix4f();
  Matrix4f rotateX  = new Matrix4f();
  Matrix4f rotateZ  = new Matrix4f();
  Matrix4f trans    = new Matrix4f();
  Matrix4f back     = new Matrix4f();

  public Matrix4f initMatrix(float xTheta, float yTheta, float zTheta, Vector3f loca)
  {
    rotateX.m00 = 1f; rotateX.m01 =                      0f; rotateX.m02 =                      0f; rotateX.m03 = 0f;
    rotateX.m10 = 0f; rotateX.m11 = (float)Math.cos(xTheta); rotateX.m12 = (float)Math.sin(xTheta); rotateX.m13 = 0f;
    rotateX.m20 = 0f; rotateX.m21 =-(float)Math.sin(xTheta); rotateX.m22 = (float)Math.cos(xTheta); rotateX.m23 = 0f;
    rotateX.m30 = 0f; rotateX.m31 =                      0f; rotateX.m32 =                      0f; rotateX.m33 = 1f;

    rotateY.m00 = (float)Math.cos(yTheta); rotateY.m01 = 0f; rotateY.m02 =-(float)Math.sin(yTheta); rotateY.m03 = 0f;
    rotateY.m10 =                      0f; rotateY.m11 = 1f; rotateY.m12 =                      0f; rotateY.m13 = 0f;
    rotateY.m20 = (float)Math.sin(yTheta); rotateY.m21 = 0f; rotateY.m22 = (float)Math.cos(yTheta); rotateY.m23 = 0f;
    rotateY.m30 =                      0f; rotateY.m31 = 0f; rotateY.m32 =                      0f; rotateY.m33 = 1f;

    rotateZ.m00 = (float)Math.cos(zTheta); rotateZ.m01 = (float)Math.sin(zTheta); rotateZ.m02 = 0f; rotateZ.m03 = 0f;
    rotateZ.m10 =-(float)Math.sin(zTheta); rotateZ.m11 = (float)Math.cos(zTheta); rotateZ.m12 = 0f; rotateZ.m13 = 0f;
    rotateZ.m20 =                      0f; rotateZ.m21 =                      0f; rotateZ.m22 = 1f; rotateZ.m23 = 0f;
    rotateZ.m30 =                      0f; rotateZ.m31 =                      0f; rotateZ.m32 = 0f; rotateZ.m33 = 1f;

    trans.m00 =      1f; trans.m01 =      0f; trans.m02 =      0f; trans.m03 = 0f;
    trans.m10 =      0f; trans.m11 =      1f; trans.m12 =      0f; trans.m13 = 0f;
    trans.m20 =      0f; trans.m21 =      0f; trans.m22 =      1f; trans.m23 = 0f;
    trans.m30 = -loca.x; trans.m31 = -loca.y; trans.m32 = -loca.z; trans.m33 = 1f;

    back.m00 =       1f; back.m01 =       0f; back.m02 =       0f;  back.m03 = 0f;
    back.m10 =       0f; back.m11 =       1f; back.m12 =       0f;  back.m13 = 0f;
    back.m20 =       0f; back.m21 =       0f; back.m22 =       1f;  back.m23 = 0f;
    back.m30 =   loca.x; back.m31 =   loca.y; back.m32 =   loca.z;  back.m33 = 1f;

    Matrix4f temp = new Matrix4f();

    temp.mul(rotateZ, rotateY);
    rotation.mul(temp,rotateX);

    temp = new Matrix4f();

    temp.mul(trans, rotation);
    rotation.mul(temp,back);

    return rotation;
  }
Baasically what it does is: First it translates your object back to the origin. Then it applies whatever rotation you want based on the theta angles you passed. Finally it sends the freshly rotated object back to where it was before the transformation.
PS: This was written using Java3D, but I guess you can make the few steps to port it to c++. After all Java is a derivative of C/C++
 
Re: rotation of a rotation in OGL

maha_x said:
Im trying to do two sequencial rotations in OGL. It doesnt work just by adding two sequential glRotatef() cmds. What I remember from my math class is that I should multiply these two different rotation matrixs together before using the matrix to rotate my geometry. But I cant find a way to extract the first rotation matrix to be stored and then multiplied by the second rotation matrix. Im sure it's possible, but how?

The glRotate() function does not only create a rotation matrix. It also multiplies it with the current matrix. So just calling glRotate() two times will include both rotations. Like this:

glLoadIdentity(); // I
glRotatef(rot1, ... ); // RotM1 * I
glRotatef(rot2, ... ); // RotM2 * RotM1 * I
...

Note that it performs left-multiplications, so the last rotate you pass in is the first rotation applied to the vertex.
 
Back
Top