P.S.: This method provides an
anticlockwise positive rotation direction due to the
right-handed coordinate system in OpenGL and the
left-handed coordinate system in original quaternion coordinate system!
Here an assumption of a quaternion is in the form like this:
q = x*i + y*j + z*k +w;
here q is an quaternion, x, y, z, w are real and i, j, k are imaginary, which means :
pow(i, 2) == -1; pow(j, 2) == -1; pow(k, 2) == -1;
the Euler angle form rotation matrix generated with rotation angle "angR" and axis "rotAxis" via quaternion can be obtained by the following steps:
1. angR = 0.5*Pi*angR/180; // translate angR into radian.
2. angR = - angR; // transform to right-handed coordinate system.
3. rotAxis = normalize(rotAxis); // normalize the axis.
4. x = rotAxis.x*sin(angR);
y = rotAxis.y*sin(angR);
z = rotAxis.z*sin(angR);
5. rotation matrix with homogeneous coordinates is the following matrix (for OpenGL, column-major):
6. multiple transformation matrix can be calculated by matrix multiplication normally, like
/***************************************************/
/* Simp Quaternion Rotation. */
/***************************************************/
CShaderProgram prog;
CModelMatrix modelMat;
CQuaternion quat;
float angRX = 90.0f; float angRY = 45.0f; float angRZ = 30.0f;
modelMat.Translatef(10.0f, 0.0f, 0.0f) ;
modelMat.LeftMultiply(quat.GetRotationMatrix(angRX, 1, 0, 0));
modelMat.LeftMultiply(quat.GetRotationMatrix(angRY, 0, 1, 0));
modelMat.LeftMultiply(quat.GetRotationMatrix(angRZ, 0, 0, 1));
prog.LoadModelMatrix(modelMat);
Here an assumption of a quaternion is in the form like this:
q = x*i + y*j + z*k +w;
here q is an quaternion, x, y, z, w are real and i, j, k are imaginary, which means :
pow(i, 2) == -1; pow(j, 2) == -1; pow(k, 2) == -1;
the Euler angle form rotation matrix generated with rotation angle "angR" and axis "rotAxis" via quaternion can be obtained by the following steps:
1. angR = 0.5*Pi*angR/180; // translate angR into radian.
2. angR = - angR; // transform to right-handed coordinate system.
3. rotAxis = normalize(rotAxis); // normalize the axis.
4. x = rotAxis.x*sin(angR);
y = rotAxis.y*sin(angR);
z = rotAxis.z*sin(angR);
5. rotation matrix with homogeneous coordinates is the following matrix (for OpenGL, column-major):
6. multiple transformation matrix can be calculated by matrix multiplication normally, like
/***************************************************/
/* Simp Quaternion Rotation. */
/***************************************************/
CShaderProgram prog;
CModelMatrix modelMat;
CQuaternion quat;
float angRX = 90.0f; float angRY = 45.0f; float angRZ = 30.0f;
modelMat.Translatef(10.0f, 0.0f, 0.0f) ;
modelMat.LeftMultiply(quat.GetRotationMatrix(angRX, 1, 0, 0));
modelMat.LeftMultiply(quat.GetRotationMatrix(angRY, 0, 1, 0));
modelMat.LeftMultiply(quat.GetRotationMatrix(angRZ, 0, 0, 1));
prog.LoadModelMatrix(modelMat);