00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef CAJUN_CQUATERNION_H
00013 #define CAJUN_CQUATERNION_H
00014
00015 #define M_GET_RADIANS(degree) (float)((degree * M_PI) / 180.0f)
00016
00018 class CQuaternion
00019 {
00020 public:
00022 CQuaternion ()
00023 {
00024
00025 x = y = z = 0.0f;
00026 w = 1.0f;
00027 }
00035 CQuaternion (float xAxis, float yAxis, float zAxis, float wAxis)
00036 {
00037
00038 x = xAxis; y = yAxis; z = zAxis;
00039 w = wAxis;
00040 }
00041
00046 void operator= (const CQuaternion &q)
00047 {
00048
00049 w = q.w; x = q.x; y = q.y; z = q.z;
00050 }
00051
00057 CQuaternion operator* (const CQuaternion &q)
00058 {
00059
00060
00061 CQuaternion result;
00062
00063 result.x = w * q.x + x * q.w + y * q.z - z * q.y;
00064 result.y = w * q.y - x * q.z + y * q.w + z * q.x;
00065 result.z = w * q.z + x * q.y - y * q.x + z * q.w;
00066 result.w = w * q.w - x * q.x - y * q.y - z * q.z;
00067
00068 return result;
00069 }
00070
00075 CQuaternion Conjugate ()
00076 {
00077
00078 return CQuaternion (-x, -y, -z, w);
00079 }
00080
00088 void Rotatef (float amount, float xAxis, float yAxis, float zAxis)
00089 {
00090
00091
00092
00093
00094
00095 if ((xAxis != 0 && xAxis != 1) ||
00096 (yAxis != 0 && yAxis != 1) ||
00097 (zAxis != 0 && zAxis != 1))
00098 {
00099 float length = (float)sqrt(xAxis * xAxis + yAxis * yAxis + zAxis * zAxis);
00100 xAxis /= length; yAxis /= length; zAxis /= length;
00101 }
00102
00103
00104 float angle = M_GET_RADIANS(amount);
00105
00106
00107
00108 float sine = (float)sin(angle / 2.0f);
00109
00110
00111 x = xAxis * sine;
00112 y = yAxis * sine;
00113 z = zAxis * sine;
00114 w = (float)cos (angle / 2.0f);
00115
00116
00117 float length = 1 / (float)sqrt (x * x + y * y + z * z + w * w);
00118 x *= length;
00119 y *= length;
00120 z *= length;
00121 }
00122
00127 void CreateMatrix (float *pMatrix)
00128 {
00129
00130
00131
00132
00133
00134 if (! pMatrix)
00135 return;
00136
00137
00138 pMatrix[0] = 1.0f - 2.0f * (y * y + z * z);
00139 pMatrix[1] = 2.0f * (x * y + z * w);
00140 pMatrix[2] = 2.0f * (x * z - y * w);
00141 pMatrix[3] = 0.0f;
00142
00143
00144 pMatrix[4] = 2.0f * (x * y - z * w);
00145 pMatrix[5] = 1.0f - 2.0f * (x * x + z * z);
00146 pMatrix[6] = 2.0f * (z * y + x * w);
00147 pMatrix[7] = 0.0f;
00148
00149
00150 pMatrix[8] = 2.0f * (x * z + y * w);
00151 pMatrix[9] = 2.0f * (y * z - x * w);
00152 pMatrix[10] = 1.0f - 2.0f * (x * x + y * y);
00153 pMatrix[11] = 0.0f;
00154
00155
00156 pMatrix[12] = 0;
00157 pMatrix[13] = 0;
00158 pMatrix[14] = 0;
00159 pMatrix[15] = 1.0f;
00160 }
00162 float x;
00164 float y;
00166 float z;
00168 float w;
00169 };
00170 #endif
00171
00172
00173
00174
00175
00176