opengl matrix rotation quaternions

yeahdixon picture yeahdixon · Jul 28, 2010 · Viewed 8.2k times · Source

Im trying to do a simple rotation of a cube about the x and y axis:

I want to always rotate the cube over the x axis by an amount x and rotate the cube over the yaxis by an amount y independent of the x axis rotation

first i naively did :

glRotatef(x,1,0,0);
glRotatef(y,0,1,0);

then

but that first rotates over x then rotates over y i want to rotate over the y independently of the x access.

I started looking into quaternions, so i tried :

Quaternion Rotation1;
Rotation1.createFromAxisAngle(0,1, 0, globalRotateY);
Rotation1.normalize();

Quaternion Rotation2;
Rotation2.createFromAxisAngle(1,0, 0, globalRotateX);
Rotation2.normalize();

GLfloat Matrix[16];

Quaternion q=Rotation2 * Rotation1;

q.createMatrix(Matrix);
glMultMatrixf(Matrix);

that just does almost exactly what was accomplished doing 2 consecutive glRotates ...so i think im missing a step or 2.

is quaternions the way to go or should i be using something different? AND if quaternions are the way to go what steps can i add to make the cube rotate independently of each axis. i think someone else has the same issue: Rotating OpenGL scene in 2 axes

Answer

yeahdixon picture yeahdixon · Jul 29, 2010

I got this to work correctly using quaternions: Im sure there are other ways, but afeter some reseatch , this worked perfectly for me. I posted a similar version on another forum. http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=280859&#Post280859

first create the quaternion representation of the angles of change x/y then each frame multiply the changing angles quaternions to an accumulating quaternion , then finally convert that quaternion to matrix form to multiply the current matrix. Here is the main code of the loop:

Quaternion3D Rotation1=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(-1.0f,0,0), DEGREES_TO_RADIANS(globalRotateX));
Quaternion3DNormalize(&Rotation1);

Quaternion3D Rotation2=Quaternion3DMakeWithAxisAndAngle(Vector3DMake(0.0f,-1.0f,0), DEGREES_TO_RADIANS(globalRotateY));
Quaternion3DNormalize(&Rotation2);


Matrix3D Mat;
Matrix3DSetIdentity(Mat);
Quaternion3DMultiply(&QAccum, &Rotation1);

Quaternion3DMultiply(&QAccum, &Rotation2);

Matrix3DSetUsingQuaternion3D(Mat, QAccum);
globalRotateX=0;
globalRotateY=0;

glMultMatrixf(Mat);

then draw cube