00001 00007 #include "Quaternion.h" 00008 00009 Quaternion::Quaternion() { 00010 m_x = m_y = m_z = 0.0f; 00011 m_w = 1.0f; 00012 } 00013 00014 Quaternion::~Quaternion() { 00015 } 00016 00017 void Quaternion::createMatrix(float *pMatrix) { 00018 if (!pMatrix) 00019 return; 00020 00021 // First row 00022 pMatrix[ 0] = 1.0f - 2.0f * ( m_y * m_y + m_z * m_z ); 00023 pMatrix[ 1] = 2.0f * (m_x * m_y + m_z * m_w); 00024 pMatrix[ 2] = 2.0f * (m_x * m_z - m_y * m_w); 00025 pMatrix[ 3] = 0.0f; 00026 00027 // Second row 00028 pMatrix[ 4] = 2.0f * ( m_x * m_y - m_z * m_w ); 00029 pMatrix[ 5] = 1.0f - 2.0f * ( m_x * m_x + m_z * m_z ); 00030 pMatrix[ 6] = 2.0f * (m_z * m_y + m_x * m_w ); 00031 pMatrix[ 7] = 0.0f; 00032 00033 // Third row 00034 pMatrix[ 8] = 2.0f * ( m_x * m_z + m_y * m_w ); 00035 pMatrix[ 9] = 2.0f * ( m_y * m_z - m_x * m_w ); 00036 pMatrix[10] = 1.0f - 2.0f * ( m_x * m_x + m_y * m_y ); 00037 pMatrix[11] = 0.0f; 00038 00039 // Fourth row 00040 pMatrix[12] = 0; 00041 pMatrix[13] = 0; 00042 pMatrix[14] = 0; 00043 pMatrix[15] = 1.0f; 00044 } 00045 00046 void Quaternion::createFromAxisAngle(float x, float y, float z, float degrees) { 00047 float angle = float((degrees/180.0f)*PI); 00048 float result = (float)sin(angle/2.0f); 00049 m_w = (float)cos(angle/2.0f); 00050 m_x = float(x*result); 00051 m_y = float(y*result); 00052 m_z = float(z*result); 00053 } 00054 00055 Quaternion Quaternion::operator *(Quaternion q) { 00056 Quaternion r; 00057 00058 r.m_w = m_w*q.m_w - m_x*q.m_x - m_y*q.m_y - m_z*q.m_z; 00059 r.m_x = m_w*q.m_x + m_x*q.m_w + m_y*q.m_z - m_z*q.m_y; 00060 r.m_y = m_w*q.m_y + m_y*q.m_w + m_z*q.m_x - m_x*q.m_z; 00061 r.m_z = m_w*q.m_z + m_z*q.m_w + m_x*q.m_y - m_y*q.m_x; 00062 00063 return(r); 00064 }