OctopusPrime314

03-18-2012, 09:12 AM

Alright so I have been stuck on this problem for a week now and I have decided to enter the scary world of quaternions. So I am dealing with a right handed axis system in opengl so I believe I have to make sure my matrix conversion from euler angles to quaternions has to follow a right handed conversion.

First part of my code is converting my up/down and left/right rotation controls to euler angles. The way I have my conversion set up is I first rotate around the y axis (left/right) and then I convert my x and z (up/down) rotation angles based on my y axis rotation. The 0.01745 is angle to radians conversion. theta is y angle while phi and psi are x and z angles respectively. Here is the method.

rotVector.theta = mp_ViewState->LRangleDeg();

rotVector.phi = mp_ViewState->UDangleDeg()*cos(rotVector.theta*0.0174532925);

rotVector.psi = mp_ViewState->UDangleDeg()*sin(rotVector.theta*0.0174532925);

Now onto the quaternion conversion which I think may be flawed but I followed the template on wikipedia that seemed legit as I have seen many posters use it as a source for study. Here is my first quaternion matrix that represents the x rotation matrix that I will create.

void CTEM_3D_Canvas::updateXmat(){

float a = cos(rotVector.phi/2);

float b = sin(rotVector.phi/2);

float c = sin(rotVector.phi/2);

float d = sin(rotVector.phi/2);

rotVector.xrot[0] = a*a + b*b - c*c - d*d;

rotVector.xrot[1] = 2*b*c - 2*a*d;

rotVector.xrot[2] = 2*b*d + 2*a*c;

rotVector.xrot[3] = 0;

rotVector.xrot[4] = 2*b*c + 2*a*d;

rotVector.xrot[5] = a*a - b*b + c*c - d*d;

rotVector.xrot[6] = 2*c*d - 2*a*b;

rotVector.xrot[7] = 0;

rotVector.xrot[8] = 2*b*d - 2*a*c;

rotVector.xrot[9] = 2*c*d + 2*a*b;

rotVector.xrot[10] = a*a - b*b - c*c + d*d;

rotVector.xrot[11] = 0;

rotVector.xrot[12] = 0;

rotVector.xrot[13] = 0;

rotVector.xrot[14] = 0;

rotVector.xrot[15] = 1;

}

Now the y rotation quaternion...

void CTEM_3D_Canvas::updateYmat(){

float a = cos(rotVector.theta/2);

float b = sin(rotVector.theta/2);

float c = sin(rotVector.theta/2);

float d = sin(rotVector.theta/2);

rotVector.yrot[0] = a*a + b*b - c*c - d*d;

rotVector.yrot[1] = 2*b*c - 2*a*d;

rotVector.yrot[2] = 2*b*d + 2*a*c;

rotVector.yrot[3] = 0;

rotVector.yrot[4] = 2*b*c + 2*a*d;

rotVector.yrot[5] = a*a - b*b + c*c - d*d;

rotVector.yrot[6] = 2*c*d - 2*a*b;

rotVector.yrot[7] = 0;

rotVector.yrot[8] = 2*b*d - 2*a*c;

rotVector.yrot[9] = 2*c*d + 2*a*b;

rotVector.yrot[10] = a*a - b*b - c*c + d*d;

rotVector.yrot[11] = 0;

rotVector.yrot[12] = 0;

rotVector.yrot[13] = 0;

rotVector.yrot[14] = 0;

rotVector.yrot[15] = 1;

}

And finally the z quaternion matrix...

void CTEM_3D_Canvas::updateZmat(){

float a = cos(rotVector.psi/2);

float b = sin(rotVector.psi/2);

float c = sin(rotVector.psi/2);

float d = sin(rotVector.psi/2);

rotVector.zrot[0] = a*a + b*b - c*c - d*d;

rotVector.zrot[1] = 2*b*c - 2*a*d;

rotVector.zrot[2] = 2*b*d + 2*a*c;

rotVector.zrot[3] = 0;

rotVector.zrot[4] = 2*b*c + 2*a*d;

rotVector.zrot[5] = a*a - b*b + c*c - d*d;

rotVector.zrot[6] = 2*c*d - 2*a*b;

rotVector.zrot[7] = 0;

rotVector.zrot[8] = 2*b*d - 2*a*c;

rotVector.zrot[9] = 2*c*d + 2*a*b;

rotVector.zrot[10] = a*a - b*b - c*c + d*d;

rotVector.zrot[11] = 0;

rotVector.zrot[12] = 0;

rotVector.zrot[13] = 0;

rotVector.zrot[14] = 0;

rotVector.zrot[15] = 1;

}

Now I have all my methods made and now I make these method calls in the my code. First I will explain. I first convert from up/down and right/left controls to euler angles. I then convert the x, y and z rotations to three different quaternion rotation matrices. I then use glMultMatrix() three times and insert the x, y and z quaternions multiplied with my projection matrix.

angleConversion();

updateXmat();

updateYmat();

updateZmat();

//Original centered object

glPushMatrix();

glMultMatrixf(rotVector.xrot);

glMultMatrixf(rotVector.yrot);

glMultMatrixf(rotVector.zrot);

drawSquare();

glPopMatrix();

Hopefully someone can help because I have spent a lot of time on this and I am not giving up until I solve this special problem.

First part of my code is converting my up/down and left/right rotation controls to euler angles. The way I have my conversion set up is I first rotate around the y axis (left/right) and then I convert my x and z (up/down) rotation angles based on my y axis rotation. The 0.01745 is angle to radians conversion. theta is y angle while phi and psi are x and z angles respectively. Here is the method.

rotVector.theta = mp_ViewState->LRangleDeg();

rotVector.phi = mp_ViewState->UDangleDeg()*cos(rotVector.theta*0.0174532925);

rotVector.psi = mp_ViewState->UDangleDeg()*sin(rotVector.theta*0.0174532925);

Now onto the quaternion conversion which I think may be flawed but I followed the template on wikipedia that seemed legit as I have seen many posters use it as a source for study. Here is my first quaternion matrix that represents the x rotation matrix that I will create.

void CTEM_3D_Canvas::updateXmat(){

float a = cos(rotVector.phi/2);

float b = sin(rotVector.phi/2);

float c = sin(rotVector.phi/2);

float d = sin(rotVector.phi/2);

rotVector.xrot[0] = a*a + b*b - c*c - d*d;

rotVector.xrot[1] = 2*b*c - 2*a*d;

rotVector.xrot[2] = 2*b*d + 2*a*c;

rotVector.xrot[3] = 0;

rotVector.xrot[4] = 2*b*c + 2*a*d;

rotVector.xrot[5] = a*a - b*b + c*c - d*d;

rotVector.xrot[6] = 2*c*d - 2*a*b;

rotVector.xrot[7] = 0;

rotVector.xrot[8] = 2*b*d - 2*a*c;

rotVector.xrot[9] = 2*c*d + 2*a*b;

rotVector.xrot[10] = a*a - b*b - c*c + d*d;

rotVector.xrot[11] = 0;

rotVector.xrot[12] = 0;

rotVector.xrot[13] = 0;

rotVector.xrot[14] = 0;

rotVector.xrot[15] = 1;

}

Now the y rotation quaternion...

void CTEM_3D_Canvas::updateYmat(){

float a = cos(rotVector.theta/2);

float b = sin(rotVector.theta/2);

float c = sin(rotVector.theta/2);

float d = sin(rotVector.theta/2);

rotVector.yrot[0] = a*a + b*b - c*c - d*d;

rotVector.yrot[1] = 2*b*c - 2*a*d;

rotVector.yrot[2] = 2*b*d + 2*a*c;

rotVector.yrot[3] = 0;

rotVector.yrot[4] = 2*b*c + 2*a*d;

rotVector.yrot[5] = a*a - b*b + c*c - d*d;

rotVector.yrot[6] = 2*c*d - 2*a*b;

rotVector.yrot[7] = 0;

rotVector.yrot[8] = 2*b*d - 2*a*c;

rotVector.yrot[9] = 2*c*d + 2*a*b;

rotVector.yrot[10] = a*a - b*b - c*c + d*d;

rotVector.yrot[11] = 0;

rotVector.yrot[12] = 0;

rotVector.yrot[13] = 0;

rotVector.yrot[14] = 0;

rotVector.yrot[15] = 1;

}

And finally the z quaternion matrix...

void CTEM_3D_Canvas::updateZmat(){

float a = cos(rotVector.psi/2);

float b = sin(rotVector.psi/2);

float c = sin(rotVector.psi/2);

float d = sin(rotVector.psi/2);

rotVector.zrot[0] = a*a + b*b - c*c - d*d;

rotVector.zrot[1] = 2*b*c - 2*a*d;

rotVector.zrot[2] = 2*b*d + 2*a*c;

rotVector.zrot[3] = 0;

rotVector.zrot[4] = 2*b*c + 2*a*d;

rotVector.zrot[5] = a*a - b*b + c*c - d*d;

rotVector.zrot[6] = 2*c*d - 2*a*b;

rotVector.zrot[7] = 0;

rotVector.zrot[8] = 2*b*d - 2*a*c;

rotVector.zrot[9] = 2*c*d + 2*a*b;

rotVector.zrot[10] = a*a - b*b - c*c + d*d;

rotVector.zrot[11] = 0;

rotVector.zrot[12] = 0;

rotVector.zrot[13] = 0;

rotVector.zrot[14] = 0;

rotVector.zrot[15] = 1;

}

Now I have all my methods made and now I make these method calls in the my code. First I will explain. I first convert from up/down and right/left controls to euler angles. I then convert the x, y and z rotations to three different quaternion rotation matrices. I then use glMultMatrix() three times and insert the x, y and z quaternions multiplied with my projection matrix.

angleConversion();

updateXmat();

updateYmat();

updateZmat();

//Original centered object

glPushMatrix();

glMultMatrixf(rotVector.xrot);

glMultMatrixf(rotVector.yrot);

glMultMatrixf(rotVector.zrot);

drawSquare();

glPopMatrix();

Hopefully someone can help because I have spent a lot of time on this and I am not giving up until I solve this special problem.