I am currently writing a game in C++ using OpenGL. I have implemented a Quaternion class to handle 6DOF movement and it worked great until the day I realised that I need a pivot point systmem in wich each of my models would rotate around a pivot point. If I were using glRotate I would write the translation for each separate mesh like this:
// add pivot offset
glTranslatef( pivotVec.x, pivotVec.y, pivotVec.z );
// rotate
glRotate( orientation.x, 1, 0, 0 );
glRotate( orientation.y, 0, 1, 0 );
glRotate( orientation.z, 0, 0, 1 );
// remove pivot offset
glTranslatef( -pivotVec.x, -pivotVec.y, -pivotVec.z );
// translate to position
glTranslatef( position.x, position.y, position.z );
It seems so simple, the matrix derived from the quaternion would be sent to OpenGL with a glMultMatrix call. The thing that confuses me is that to translate and rotate correctly, I had to place the glTranslate command before the glMultMatrix command. I have re-read chapter 3 in red book a few times and I am still convinced that something is wrong as it clearly states that if you what to translate an object globaly with the rotation applied at the translated local origin, you should call glRotate first. I have also tried to make the best out of thing to try and achieve my pivot point effect with my current system, but no matter what I do, just could not come up with a way to achieve that effect. I am not dumb so I realised that if any one would be kind enough to help me, he/she would probably want some source code of my quaternion class and my transformation code, so here it is:
Quaternion to Matrix:
void Quaternion::CreateMatrix(float *Matrix)
{
if(!Matrix)
return;
Matrix[0] = 1.0f - 2.0f * (y * y + z * z);
Matrix[1] = 2.0f * (x * y + z * w);
Matrix[2] = 2.0f * (x * z - y * w);
Matrix[3] = 0.0f;
Matrix[4] = 2.0f * (x * y - z * w);
Matrix[5] = 1.0f - 2.0f * ( x * x + z * z );
Matrix[6] = 2.0f * (z * y + x * w);
Matrix[7] = 0.0f;
Matrix[8] = 2.0f * (x * z + y * w);
Matrix[9] = 2.0f * (y * z - x * w);
Matrix[10] = 1.0f - 2.0f * (x * x + y * y );
Matrix[11] = 0.0f;
Matrix[12] = 0;
Matrix[13] = 0;
Matrix[14] = 0;
Matrix[15] = 1.0f;
}
Transformation code:
//create our local object matrix
glPushMatrix();
//translate
glTranslatef(pos.x, pos.y , pos.z);
//convert the orientation quaternion into a rotation matrix for OpenGL use
float * rotMatrix;
rotMatrix = new float[16];
ori.CreateMatrix(rotMatrix);
glMultMatrixf(rotMatrix);
delete [] rotMatrix;
//draw stuff
// Drawing code goes here...
glPopMatrix();
All replies will be greatly appreciated!