AI_TMatrix44.h

Go to the documentation of this file.
00001 #ifndef AI__MATRIX44_H
00002 #define AI__MATRIX44_H
00003 //------------------------------------------------------------------------------
00012 #include "AI_TVector4.h"
00013 #include "AI_TVector3.h"
00014 #include "AI_Quaternion.h"
00015 #include "euler.h"
00016 
00017 static float ai_matrix44_ident[16] = 
00018 {
00019     1.0f, 0.0f, 0.0f, 0.0f,
00020     0.0f, 1.0f, 0.0f, 0.0f,
00021     0.0f, 0.0f, 1.0f, 0.0f,
00022     0.0f, 0.0f, 0.0f, 1.0f,
00023 };
00024 
00025 class AI_TMatrix44 
00026 {
00027 public:
00029     AI_TMatrix44();
00031     AI_TMatrix44(const AI_TVector4& v0, const AI_TVector4& v1, const AI_TVector4& v2, const AI_TVector4& v3);
00033     AI_TMatrix44(const AI_TMatrix44& m1);
00035     AI_TMatrix44(float _m11, float _m12, float _m13, float _m14,
00036               float _m21, float _m22, float _m23, float _m24,
00037               float _m31, float _m32, float _m33, float _m34,
00038               float _m41, float _m42, float _m43, float _m44);
00040     AI_TMatrix44(const AI_Quaternion& q);
00042     AI_Quaternion get_quaternion() const;
00044     void set(const AI_TVector4& v0, const AI_TVector4& v1, const AI_TVector4& v2, const AI_TVector4& v3);
00046     void set(const AI_TMatrix44& m1);
00048     void set(float _m11, float _m12, float _m13, float _m14,
00049              float _m21, float _m22, float _m23, float _m24,
00050              float _m31, float _m32, float _m33, float _m34,
00051              float _m41, float _m42, float _m43, float _m44);
00053     void set(const AI_Quaternion& q);
00055     void ident();
00057     void transpose();
00059     float det();
00061     void invert(void);
00063     void invert_simple(void);
00065     void mult_simple(const AI_TMatrix44& m1);
00067     AI_TVector3 transform_coord(const AI_TVector3& v) const;
00069     AI_TVector3& x_component() const;
00071     AI_TVector3& y_component() const;
00073     AI_TVector3& z_component() const;
00075     AI_TVector3& pos_component() const;
00077     void rotate_x(const float a);
00079     void rotate_y(const float a);
00081     void rotate_z(const float a);
00083     void rotate(const AI_TVector3& vec, float a);
00085     void translate(const AI_TVector3& t);
00087     void set_translation(const AI_TVector3& t);
00089     void scale(const AI_TVector3& s);
00091     void lookatLh(const AI_TVector3& to, const AI_TVector3& up);
00093     void lookatRh(const AI_TVector3& to, const AI_TVector3& up);
00095     void perspFovLh(float fovY, float aspect, float zn, float zf);
00097     void perspFovRh(float fovY, float aspect, float zn, float zf);
00099     void perspOffCenterLh(float minX, float maxX, float minY, float maxY, float zn, float zf);
00101     void perspOffCenterRh(float minX, float maxX, float minY, float maxY, float zn, float zf);
00103     void orthoLh(float w, float h, float zn, float zf);
00105     void orthoRh(float w, float h, float zn, float zf);
00107     void billboard(const AI_TVector3& to, const AI_TVector3& up);
00109     void operator *= (const AI_TMatrix44& m1);
00111     void mult(const AI_TVector4& src, AI_TVector4& dst) const;
00113     void mult(const AI_TVector3& src, AI_TVector3& dst) const;
00114 
00115     float m[4][4];
00116 };
00117 
00118 //------------------------------------------------------------------------------
00121 inline
00122 AI_TMatrix44::AI_TMatrix44()
00123 {
00124     memcpy(&(m[0][0]), ai_matrix44_ident, sizeof(ai_matrix44_ident));
00125 }
00126 
00127 //------------------------------------------------------------------------------
00130 inline
00131 AI_TMatrix44::AI_TMatrix44(const AI_TVector4& v0, const AI_TVector4& v1, const AI_TVector4& v2, const AI_TVector4& v3)
00132 {
00133     m[0][0] = v0.x; m[0][1] = v0.y; m[0][2] = v0.z; m[0][3] = v0.w;
00134     m[1][0] = v1.x; m[1][1] = v1.y; m[1][2] = v1.z; m[1][3] = v1.w;
00135     m[2][0] = v2.x; m[2][1] = v2.y; m[2][2] = v2.z; m[2][3] = v2.w;
00136     m[3][0] = v3.x; m[3][1] = v3.y; m[3][2] = v3.z; m[3][3] = v3.w;
00137 }
00138 
00139 //------------------------------------------------------------------------------
00142 inline
00143 AI_TMatrix44::AI_TMatrix44(const AI_TMatrix44& m1) 
00144 {
00145     memcpy(m, &(m1.m[0][0]), 16 * sizeof(float));
00146 }
00147 
00148 //------------------------------------------------------------------------------
00151 inline
00152 AI_TMatrix44::AI_TMatrix44(float _m11, float _m12, float _m13, float _m14,
00153                      float _m21, float _m22, float _m23, float _m24,
00154                      float _m31, float _m32, float _m33, float _m34,
00155                      float _m41, float _m42, float _m43, float _m44)
00156 {
00157     m[0][0] = _m11; m[0][1] = _m12; m[0][2] = _m13; m[0][3] = _m14;
00158     m[1][0] = _m21; m[1][1] = _m22; m[1][2] = _m23; m[1][3] = _m24;
00159     m[2][0] = _m31; m[2][1] = _m32; m[2][2] = _m33; m[2][3] = _m34;
00160     m[3][0] = _m41; m[3][1] = _m42; m[3][2] = _m43; m[3][3] = _m44;
00161 }
00162 
00163 //------------------------------------------------------------------------------
00166 inline
00167 AI_TMatrix44::AI_TMatrix44(const AI_Quaternion& q) 
00168 {
00169     float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
00170     x2 = q.x + q.x; y2 = q.y + q.y; z2 = q.z + q.z;
00171     xx = q.x * x2;   xy = q.x * y2;   xz = q.x * z2;
00172     yy = q.y * y2;   yz = q.y * z2;   zz = q.z * z2;
00173     wx = q.w * x2;   wy = q.w * y2;   wz = q.w * z2;
00174 
00175     m[0][0] = 1.0f - (yy + zz);
00176     m[1][0] = xy - wz;
00177     m[2][0] = xz + wy;
00178 
00179     m[0][1] = xy + wz;
00180     m[1][1] = 1.0f - (xx + zz);
00181     m[2][1] = yz - wx;
00182 
00183     m[0][2] = xz - wy;
00184     m[1][2] = yz + wx;
00185     m[2][2] = 1.0f - (xx + yy);
00186 
00187     m[3][0] = m[3][1] = m[3][2] = 0.0f;
00188     m[0][3] = m[1][3] = m[2][3] = 0.0f;
00189     m[3][3] = 1.0f;
00190 }
00191 
00192 //------------------------------------------------------------------------------
00197 inline
00198 AI_Quaternion 
00199 AI_TMatrix44::get_quaternion() const
00200 {
00201     float qa[4];
00202     float tr = m[0][0] + m[1][1] + m[2][2];
00203     if (tr > 0.0f) 
00204     {
00205         float s = ai_sqrt (tr + 1.0f);
00206         qa[3] = s * 0.5f;
00207         s = 0.5f / s;
00208         qa[0] = (m[1][2] - m[2][1]) * s;
00209         qa[1] = (m[2][0] - m[0][2]) * s;
00210         qa[2] = (m[0][1] - m[1][0]) * s;
00211     } 
00212     else 
00213     {
00214         int i, j, k, nxt[3] = {1,2,0};
00215         i = 0;
00216         if (m[1][1] > m[0][0]) i=1;
00217         if (m[2][2] > m[i][i]) i=2;
00218         j = nxt[i];
00219         k = nxt[j];
00220         float s = ai_sqrt((m[i][i] - (m[j][j] + m[k][k])) + 1.0f);
00221         qa[i] = s * 0.5f;
00222         s = 0.5f / s;
00223         qa[3] = (m[j][k] - m[k][j])* s;
00224         qa[j] = (m[i][j] + m[j][i]) * s;
00225         qa[k] = (m[i][k] + m[k][i]) * s;
00226     }
00227     AI_Quaternion q(qa[0],qa[1],qa[2],qa[3]);
00228     return q;
00229 }
00230 
00231 //------------------------------------------------------------------------------
00234 inline
00235 void 
00236 AI_TMatrix44::set(const AI_TVector4& v0, const AI_TVector4& v1, const AI_TVector4& v2, const AI_TVector4& v3) 
00237 {
00238     m[0][0]=v0.x; m[0][1]=v0.y; m[0][2]=v0.z, m[0][3]=v0.w;
00239     m[1][0]=v1.x; m[1][1]=v1.y; m[1][2]=v1.z; m[1][3]=v1.w;
00240     m[2][0]=v2.x; m[2][1]=v2.y; m[2][2]=v2.z; m[2][3]=v2.w;
00241     m[3][0]=v3.x; m[3][1]=v3.y; m[3][2]=v3.z; m[3][3]=v3.w;
00242 }
00243 
00244 //------------------------------------------------------------------------------
00247 inline
00248 void 
00249 AI_TMatrix44::set(const AI_TMatrix44& m1) 
00250 {
00251     memcpy(m, &(m1.m[0][0]), 16*sizeof(float));
00252 }
00253 
00254 //------------------------------------------------------------------------------
00257 inline
00258 void
00259 AI_TMatrix44::set(float _m11, float _m12, float _m13, float _m14,
00260                float _m21, float _m22, float _m23, float _m24,
00261                float _m31, float _m32, float _m33, float _m34,
00262                float _m41, float _m42, float _m43, float _m44)
00263 {
00264     m[0][0]=_m11; m[0][1]=_m12; m[0][2]=_m13; m[0][3]=_m14;
00265     m[1][0]=_m21; m[1][1]=_m22; m[1][2]=_m23; m[1][3]=_m24;
00266     m[2][0]=_m31; m[2][1]=_m32; m[2][2]=_m33; m[2][3]=_m34;
00267     m[3][0]=_m41; m[3][1]=_m42; m[3][2]=_m43; m[3][3]=_m44;
00268 }
00269 
00270 //------------------------------------------------------------------------------
00273 inline
00274 void 
00275 AI_TMatrix44::set(const AI_Quaternion& q) 
00276 {
00277     float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
00278     x2 = q.x + q.x; y2 = q.y + q.y; z2 = q.z + q.z;
00279     xx = q.x * x2;   xy = q.x * y2;   xz = q.x * z2;
00280     yy = q.y * y2;   yz = q.y * z2;   zz = q.z * z2;
00281     wx = q.w * x2;   wy = q.w * y2;   wz = q.w * z2;
00282 
00283     m[0][0] = 1.0f - (yy + zz);
00284     m[1][0] = xy - wz;
00285     m[2][0] = xz + wy;
00286 
00287     m[0][1] = xy + wz;
00288     m[1][1] = 1.0f - (xx + zz);
00289     m[2][1] = yz - wx;
00290 
00291     m[0][2] = xz - wy;
00292     m[1][2] = yz + wx;
00293     m[2][2] = 1.0f - (xx + yy);
00294 
00295     m[3][0] = m[3][1] = m[3][2] = 0.0f;
00296     m[0][3] = m[1][3] = m[2][3] = 0.0f;
00297     m[3][3] = 1.0f;
00298 }
00299 
00300 //------------------------------------------------------------------------------
00303 inline
00304 void 
00305 AI_TMatrix44::ident() 
00306 {
00307     memcpy(&(m[0][0]), ai_matrix44_ident, sizeof(ai_matrix44_ident));
00308 }
00309 
00310 //------------------------------------------------------------------------------
00313 inline
00314 void 
00315 AI_TMatrix44::transpose() 
00316 {
00317     #undef n_swap
00318     #define n_swap(x,y) { float t=x; x=y; y=t; }
00319     n_swap(m[0][1], m[1][0]);
00320     n_swap(m[0][2], m[2][0]);
00321     n_swap(m[0][3], m[3][0]);
00322     n_swap(m[1][2], m[2][1]);
00323     n_swap(m[1][3], m[3][1]);
00324     n_swap(m[2][3], m[3][2]);
00325 }
00326 
00327 //------------------------------------------------------------------------------
00330 inline
00331 float 
00332 AI_TMatrix44::det() 
00333 {
00334     return
00335         (m[0][0] * m[1][1] - m[0][1] * m[1][0]) * (m[2][2] * m[3][3] - m[2][3] * m[3][2])
00336        -(m[0][0] * m[1][2] - m[0][2] * m[1][0]) * (m[2][1] * m[3][3] - m[2][3] * m[3][1])
00337        +(m[0][0] * m[1][3] - m[0][3] * m[1][0]) * (m[2][1] * m[3][2] - m[2][2] * m[3][1])
00338        +(m[0][1] * m[1][2] - m[0][2] * m[1][1]) * (m[2][0] * m[3][3] - m[2][3] * m[3][0])
00339        -(m[0][1] * m[1][3] - m[0][3] * m[1][1]) * (m[2][0] * m[3][2] - m[2][2] * m[3][0])
00340        +(m[0][2] * m[1][3] - m[0][3] * m[1][2]) * (m[2][0] * m[3][1] - m[2][1] * m[3][0]);
00341 }
00342 
00343 //------------------------------------------------------------------------------
00346 inline
00347 void
00348 AI_TMatrix44::invert() 
00349 {
00350     float s = det();
00351     if (s == 0.0) return;
00352     s = 1/s;
00353     this->set(
00354         s*(m[1][1]*(m[2][2]*m[3][3] - m[2][3]*m[3][2]) + m[1][2]*(m[2][3]*m[3][1] - m[2][1]*m[3][3]) + m[1][3]*(m[2][1]*m[3][2] - m[2][2]*m[3][1])),
00355         s*(m[2][1]*(m[0][2]*m[3][3] - m[0][3]*m[3][2]) + m[2][2]*(m[0][3]*m[3][1] - m[0][1]*m[3][3]) + m[2][3]*(m[0][1]*m[3][2] - m[0][2]*m[3][1])),
00356         s*(m[3][1]*(m[0][2]*m[1][3] - m[0][3]*m[1][2]) + m[3][2]*(m[0][3]*m[1][1] - m[0][1]*m[1][3]) + m[3][3]*(m[0][1]*m[1][2] - m[0][2]*m[1][1])),
00357         s*(m[0][1]*(m[1][3]*m[2][2] - m[1][2]*m[2][3]) + m[0][2]*(m[1][1]*m[2][3] - m[1][3]*m[2][1]) + m[0][3]*(m[1][2]*m[2][1] - m[1][1]*m[2][2])),
00358         s*(m[1][2]*(m[2][0]*m[3][3] - m[2][3]*m[3][0]) + m[1][3]*(m[2][2]*m[3][0] - m[2][0]*m[3][2]) + m[1][0]*(m[2][3]*m[3][2] - m[2][2]*m[3][3])),
00359         s*(m[2][2]*(m[0][0]*m[3][3] - m[0][3]*m[3][0]) + m[2][3]*(m[0][2]*m[3][0] - m[0][0]*m[3][2]) + m[2][0]*(m[0][3]*m[3][2] - m[0][2]*m[3][3])),
00360         s*(m[3][2]*(m[0][0]*m[1][3] - m[0][3]*m[1][0]) + m[3][3]*(m[0][2]*m[1][0] - m[0][0]*m[1][2]) + m[3][0]*(m[0][3]*m[1][2] - m[0][2]*m[1][3])),
00361         s*(m[0][2]*(m[1][3]*m[2][0] - m[1][0]*m[2][3]) + m[0][3]*(m[1][0]*m[2][2] - m[1][2]*m[2][0]) + m[0][0]*(m[1][2]*m[2][3] - m[1][3]*m[2][2])),
00362         s*(m[1][3]*(m[2][0]*m[3][1] - m[2][1]*m[3][0]) + m[1][0]*(m[2][1]*m[3][3] - m[2][3]*m[3][1]) + m[1][1]*(m[2][3]*m[3][0] - m[2][0]*m[3][3])),
00363         s*(m[2][3]*(m[0][0]*m[3][1] - m[0][1]*m[3][0]) + m[2][0]*(m[0][1]*m[3][3] - m[0][3]*m[3][1]) + m[2][1]*(m[0][3]*m[3][0] - m[0][0]*m[3][3])),
00364         s*(m[3][3]*(m[0][0]*m[1][1] - m[0][1]*m[1][0]) + m[3][0]*(m[0][1]*m[1][3] - m[0][3]*m[1][1]) + m[3][1]*(m[0][3]*m[1][0] - m[0][0]*m[1][3])),
00365         s*(m[0][3]*(m[1][1]*m[2][0] - m[1][0]*m[2][1]) + m[0][0]*(m[1][3]*m[2][1] - m[1][1]*m[2][3]) + m[0][1]*(m[1][0]*m[2][3] - m[1][3]*m[2][0])),
00366         s*(m[1][0]*(m[2][2]*m[3][1] - m[2][1]*m[3][2]) + m[1][1]*(m[2][0]*m[3][2] - m[2][2]*m[3][0]) + m[1][2]*(m[2][1]*m[3][0] - m[2][0]*m[3][1])),
00367         s*(m[2][0]*(m[0][2]*m[3][1] - m[0][1]*m[3][2]) + m[2][1]*(m[0][0]*m[3][2] - m[0][2]*m[3][0]) + m[2][2]*(m[0][1]*m[3][0] - m[0][0]*m[3][1])),
00368         s*(m[3][0]*(m[0][2]*m[1][1] - m[0][1]*m[1][2]) + m[3][1]*(m[0][0]*m[1][2] - m[0][2]*m[1][0]) + m[3][2]*(m[0][1]*m[1][0] - m[0][0]*m[1][1])),
00369         s*(m[0][0]*(m[1][1]*m[2][2] - m[1][2]*m[2][1]) + m[0][1]*(m[1][2]*m[2][0] - m[1][0]*m[2][2]) + m[0][2]*(m[1][0]*m[2][1] - m[1][1]*m[2][0])));
00370 }
00371 
00372 //------------------------------------------------------------------------------
00378 inline
00379 void 
00380 AI_TMatrix44::invert_simple() 
00381 {
00382     float s = det();
00383     if (s == 0.0f) return;
00384     s = 1.0f/s;
00385     this->set(
00386         s * ((m[1][1] * m[2][2]) - (m[1][2] * m[2][1])),
00387         s * ((m[2][1] * m[0][2]) - (m[2][2] * m[0][1])),
00388         s * ((m[0][1] * m[1][2]) - (m[0][2] * m[1][1])),
00389         0.0f,
00390         s * ((m[1][2] * m[2][0]) - (m[1][0] * m[2][2])),
00391         s * ((m[2][2] * m[0][0]) - (m[2][0] * m[0][2])),
00392         s * ((m[0][2] * m[1][0]) - (m[0][0] * m[1][2])),
00393         0.0f,
00394         s * ((m[1][0] * m[2][1]) - (m[1][1] * m[2][0])),
00395         s * ((m[2][0] * m[0][1]) - (m[2][1] * m[0][0])),
00396         s * ((m[0][0] * m[1][1]) - (m[0][1] * m[1][0])),
00397         0.0f,
00398         s * (m[1][0]*(m[2][2]*m[3][1] - m[2][1]*m[3][2]) + m[1][1]*(m[2][0]*m[3][2] - m[2][2]*m[3][0]) + m[1][2]*(m[2][1]*m[3][0] - m[2][0]*m[3][1])),
00399         s * (m[2][0]*(m[0][2]*m[3][1] - m[0][1]*m[3][2]) + m[2][1]*(m[0][0]*m[3][2] - m[0][2]*m[3][0]) + m[2][2]*(m[0][1]*m[3][0] - m[0][0]*m[3][1])),
00400         s * (m[3][0]*(m[0][2]*m[1][1] - m[0][1]*m[1][2]) + m[3][1]*(m[0][0]*m[1][2] - m[0][2]*m[1][0]) + m[3][2]*(m[0][1]*m[1][0] - m[0][0]*m[1][1])),
00401         1.0f);
00402 }
00403 
00404 //------------------------------------------------------------------------------
00408 inline
00409 void
00410 AI_TMatrix44::mult_simple(const AI_TMatrix44& m1) 
00411 {
00412     int i;
00413     for (i=0; i<4; i++) 
00414     {
00415         float mi0 = m[i][0];
00416         float mi1 = m[i][1];
00417         float mi2 = m[i][2];
00418         m[i][0] = mi0*m1.m[0][0] + mi1*m1.m[1][0] + mi2*m1.m[2][0];
00419         m[i][1] = mi0*m1.m[0][1] + mi1*m1.m[1][1] + mi2*m1.m[2][1];
00420         m[i][2] = mi0*m1.m[0][2] + mi1*m1.m[1][2] + mi2*m1.m[2][2];
00421     }
00422     m[3][0] += m1.m[3][0];
00423     m[3][1] += m1.m[3][1];
00424     m[3][2] += m1.m[3][2];
00425     m[0][3] = 0.0f;
00426     m[1][3] = 0.0f;
00427     m[2][3] = 0.0f;
00428     m[3][3] = 1.0f;
00429 }
00430 
00431 //------------------------------------------------------------------------------
00435 inline
00436 AI_TVector3
00437 AI_TMatrix44::transform_coord(const AI_TVector3& v) const
00438 {
00439     float d = 1.0f / (m[0][3]*v.x + m[1][3]*v.y + m[2][3]*v.z + m[3][3]);
00440     return AI_TVector3(
00441         (m[0][0]*v.x + m[1][0]*v.y + m[2][0]*v.z + m[3][0]) * d,
00442         (m[0][1]*v.x + m[1][1]*v.y + m[2][1]*v.z + m[3][1]) * d,
00443         (m[0][2]*v.x + m[1][2]*v.y + m[2][2]*v.z + m[3][2]) * d);
00444 }
00445 
00446 //------------------------------------------------------------------------------
00449 inline
00450 AI_TVector3&
00451 AI_TMatrix44::x_component() const
00452 {  
00453     return *(AI_TVector3*)&m[0][0];
00454 }
00455 
00456 //------------------------------------------------------------------------------
00459 inline
00460 AI_TVector3&
00461 AI_TMatrix44::y_component() const
00462 {
00463     return *(AI_TVector3*)&m[1][0];
00464 }
00465 
00466 //------------------------------------------------------------------------------
00469 inline
00470 AI_TVector3&
00471 AI_TMatrix44::z_component() const
00472 {
00473     return *(AI_TVector3*)&m[2][0];
00474 }
00475 
00476 //------------------------------------------------------------------------------
00479 inline
00480 AI_TVector3& 
00481 AI_TMatrix44::pos_component() const
00482 {
00483     return *(AI_TVector3*)&m[3][0];
00484 }
00485 
00486 //------------------------------------------------------------------------------
00489 inline
00490 void
00491 AI_TMatrix44::rotate_x(const float a) 
00492 {
00493     float c = ai_cos(a);
00494     float s = ai_sin(a);
00495     int i;
00496     for (i=0; i<4; i++) {
00497         float mi1 = m[i][1];
00498         float mi2 = m[i][2];
00499         m[i][1] = mi1*c + mi2*-s;
00500         m[i][2] = mi1*s + mi2*c;
00501     }
00502 }
00503 
00504 //------------------------------------------------------------------------------
00507 inline
00508 void 
00509 AI_TMatrix44::rotate_y(const float a) 
00510 {
00511     float c = ai_cos(a);
00512     float s = ai_sin(a);
00513     int i;
00514     for (i=0; i<4; i++) {
00515         float mi0 = m[i][0];
00516         float mi2 = m[i][2];
00517         m[i][0] = mi0*c + mi2*s;
00518         m[i][2] = mi0*-s + mi2*c;
00519     }
00520 }
00521 
00522 //------------------------------------------------------------------------------
00525 inline
00526 void 
00527 AI_TMatrix44::rotate_z(const float a) 
00528 {
00529     float c = ai_cos(a);
00530     float s = ai_sin(a);
00531     int i;
00532     for (i=0; i<4; i++) {
00533         float mi0 = m[i][0];
00534         float mi1 = m[i][1];
00535         m[i][0] = mi0*c + mi1*-s;
00536         m[i][1] = mi0*s + mi1*c;
00537     }
00538 }
00539 
00540 //------------------------------------------------------------------------------
00543 inline
00544 void 
00545 AI_TMatrix44::translate(const AI_TVector3& t) 
00546 {
00547     m[3][0] += t.x;
00548     m[3][1] += t.y;
00549     m[3][2] += t.z;
00550 }
00551 
00552 //------------------------------------------------------------------------------
00555 inline
00556 void
00557 AI_TMatrix44::set_translation(const AI_TVector3& t) 
00558 {
00559     m[3][0] = t.x;
00560     m[3][1] = t.y;
00561     m[3][2] = t.z;
00562 };
00563 
00564 //------------------------------------------------------------------------------
00567 inline
00568 void
00569 AI_TMatrix44::scale(const AI_TVector3& s) 
00570 {
00571     int i;
00572     for (i=0; i<4; i++) 
00573     {
00574         m[i][0] *= s.x;
00575         m[i][1] *= s.y;
00576         m[i][2] *= s.z;
00577     }
00578 }
00579 
00580 //------------------------------------------------------------------------------
00583 inline
00584 void 
00585 AI_TMatrix44::lookatRh(const AI_TVector3& at, const AI_TVector3& up) 
00586 {
00587     AI_TVector3 eye(m[3][0], m[3][1], m[3][2]);
00588     AI_TVector3 zaxis = eye - at;
00589     zaxis.norm();
00590     AI_TVector3 xaxis = up * zaxis;
00591     xaxis.norm();
00592     AI_TVector3 yaxis = zaxis * xaxis;
00593     m[0][0] = xaxis.x;  m[0][1] = xaxis.y;  m[0][2] = xaxis.z;  m[0][3] = 0.0f;
00594     m[1][0] = yaxis.x;  m[1][1] = yaxis.y;  m[1][2] = yaxis.z;  m[1][3] = 0.0f;
00595     m[2][0] = zaxis.x;  m[2][1] = zaxis.y;  m[2][2] = zaxis.z;  m[2][3] = 0.0f;
00596 }
00597 
00598 //------------------------------------------------------------------------------
00601 inline
00602 void 
00603 AI_TMatrix44::lookatLh(const AI_TVector3& at, const AI_TVector3& up) 
00604 {
00605     AI_TVector3 eye(m[3][0], m[3][1], m[3][2]);
00606     AI_TVector3 zaxis = at - eye;
00607     zaxis.norm();
00608     AI_TVector3 xaxis = up * zaxis;
00609     xaxis.norm();
00610     AI_TVector3 yaxis = zaxis * xaxis;
00611     m[0][0] = xaxis.x;  m[0][1] = yaxis.x;  m[0][2] = zaxis.x;  m[0][3] = 0.0f;
00612     m[1][0] = xaxis.y;  m[1][1] = yaxis.y;  m[1][2] = zaxis.y;  m[1][3] = 0.0f;
00613     m[2][0] = xaxis.z;  m[2][1] = yaxis.z;  m[2][2] = zaxis.z;  m[2][3] = 0.0f;
00614 }
00615 
00616 //------------------------------------------------------------------------------
00619 inline
00620 void
00621 AI_TMatrix44::perspFovLh(float fovY, float aspect, float zn, float zf)
00622 {
00623     float h = float(1.0 / tan(fovY * 0.5f));
00624     float w = h / aspect;
00625     m[0][0] = w;    m[0][1] = 0.0f; m[0][2] = 0.0f;                   m[0][3] = 0.0f;
00626     m[1][0] = 0.0f; m[1][1] = h;    m[1][2] = 0.0f;                   m[1][3] = 0.0f;
00627     m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = zf / (zf - zn);         m[2][3] = 1.0f;
00628     m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = -zn * (zf / (zf - zn)); m[3][3] = 0.0f;
00629 }
00630 
00631 //------------------------------------------------------------------------------
00634 inline
00635 void
00636 AI_TMatrix44::perspFovRh(float fovY, float aspect, float zn, float zf)
00637 {
00638     float h = float(1.0 / tan(fovY * 0.5f));
00639     float w = h / aspect;
00640     m[0][0] = w;    m[0][1] = 0.0f; m[0][2] = 0.0f;                  m[0][3] = 0.0f;
00641     m[1][0] = 0.0f; m[1][1] = h;    m[1][2] = 0.0f;                  m[1][3] = 0.0f;
00642     m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = zf / (zn - zf);        m[2][3] = -1.0f;
00643     m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = zn * (zf / (zn - zf)); m[3][3] = 0.0f;
00644 }
00645 
00646 //------------------------------------------------------------------------------
00649 inline
00650 void
00651 AI_TMatrix44::perspOffCenterLh(float minX, float maxX, float minY, float maxY, float zn, float zf)
00652 {
00653     m[0][0] = 2.0f * zn / (maxX - minX); m[0][1] = 0.0f, m[0][2] = 0.0f; m[0][3] = 0.0f;
00654     m[1][0] = 0.0f; m[1][1] = 2.0f * zn / (maxY - minY); m[1][2] = 0.0f; m[1][3] = 0.0f;
00655     m[2][0] = (minX + maxX) / (minX - maxX); m[2][1] = (maxY + minY) / (minY - maxY); m[2][2] = zf / (zf - zn); m[2][3] = 1.0f;
00656     m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = zn * zf / (zn - zf); m[3][3] = 0.0f;
00657 }
00658 
00659 //------------------------------------------------------------------------------
00662 inline
00663 void
00664 AI_TMatrix44::perspOffCenterRh(float minX, float maxX, float minY, float maxY, float zn, float zf)
00665 {
00666     m[0][0] = 2.0f * zn / (maxX - minX); m[0][1] = 0.0f, m[0][2] = 0.0f; m[0][3] = 0.0f;
00667     m[1][0] = 0.0f; m[1][1] = 2.0f * zn / (maxY - minY); m[1][2] = 0.0f; m[1][3] = 0.0f;
00668     m[2][0] = (minX + maxX) / (maxX - minX); m[2][1] = (maxY + minY) / (maxY - minY); m[2][2] = zf / (zn - zf); m[2][3] = -1.0f;
00669     m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = zn * zf / (zn - zf); m[3][3] = 0.0f;
00670 }
00671 
00672 //------------------------------------------------------------------------------
00675 inline
00676 void
00677 AI_TMatrix44::orthoLh(float w, float h, float zn, float zf)
00678 {
00679     m[0][0] = 2.0f / w; m[0][1] = 0.0f;     m[0][2] = 0.0f;             m[0][3] = 0.0f;
00680     m[1][0] = 0.0f;     m[1][1] = 2.0f / h; m[1][2] = 0.0f;             m[1][3] = 0.0f;
00681     m[2][0] = 0.0f;     m[2][1] = 0.0f;     m[2][2] = 1.0f / (zf - zn); m[2][3] = 0.0f;
00682     m[3][0] = 0.0f;     m[3][1] = 0.0f;     m[3][2] = zn / (zn - zf);   m[3][3] = 1.0f;
00683 }
00684 
00685 //------------------------------------------------------------------------------
00688 inline
00689 void
00690 AI_TMatrix44::orthoRh(float w, float h, float zn, float zf)
00691 {
00692     m[0][0] = 2.0f / w; m[0][1] = 0.0f;     m[0][2] = 0.0f;             m[0][3] = 0.0f;
00693     m[1][0] = 0.0f;     m[1][1] = 2.0f / h; m[1][2] = 0.0f;             m[1][3] = 0.0f;
00694     m[2][0] = 0.0f;     m[2][1] = 0.0f;     m[2][2] = 1.0f / (zn - zf); m[2][3] = 0.0f;
00695     m[3][0] = 0.0f;     m[3][1] = 0.0f;     m[3][2] = zn / (zn - zf);   m[3][3] = 1.0f;
00696 }
00697 
00698 //------------------------------------------------------------------------------
00701 inline
00702 void 
00703 AI_TMatrix44::billboard(const AI_TVector3& to, const AI_TVector3& up)
00704 {
00705     AI_TVector3 from(m[3][0], m[3][1], m[3][2]);
00706     AI_TVector3 z(from - to);
00707     z.norm();
00708     AI_TVector3 y(up);
00709     y.norm();
00710     AI_TVector3 x(y * z);
00711     z = x * y;       
00712 
00713     m[0][0]=x.x;  m[0][1]=x.y;  m[0][2]=x.z;  m[0][3]=0.0f;
00714     m[1][0]=y.x;  m[1][1]=y.y;  m[1][2]=y.z;  m[1][3]=0.0f;
00715     m[2][0]=z.x;  m[2][1]=z.y;  m[2][2]=z.z;  m[2][3]=0.0f;
00716 }
00717 
00718 //------------------------------------------------------------------------------
00721 inline
00722 void
00723 AI_TMatrix44::operator *= (const AI_TMatrix44& m1) 
00724 {
00725     int i;
00726     for (i=0; i<4; i++) 
00727     {
00728         float mi0 = m[i][0];
00729         float mi1 = m[i][1];
00730         float mi2 = m[i][2];
00731         float mi3 = m[i][3];
00732         m[i][0] = mi0*m1.m[0][0] + mi1*m1.m[1][0] + mi2*m1.m[2][0] + mi3*m1.m[3][0];
00733         m[i][1] = mi0*m1.m[0][1] + mi1*m1.m[1][1] + mi2*m1.m[2][1] + mi3*m1.m[3][1];
00734         m[i][2] = mi0*m1.m[0][2] + mi1*m1.m[1][2] + mi2*m1.m[2][2] + mi3*m1.m[3][2];
00735         m[i][3] = mi0*m1.m[0][3] + mi1*m1.m[1][3] + mi2*m1.m[2][3] + mi3*m1.m[3][3];
00736     }
00737 }
00738 
00739 //------------------------------------------------------------------------------
00742 inline
00743 void 
00744 AI_TMatrix44::rotate(const AI_TVector3& vec, float a)
00745 {
00746     AI_TVector3 v(vec);
00747     v.norm();
00748     float sa = (float) ai_sin(a);
00749     float ca = (float) ai_cos(a);
00750 
00751     AI_TMatrix44 rotM;
00752     rotM.m[0][0] = ca + (1.0f - ca) * v.x * v.x;
00753     rotM.m[0][1] = (1.0f - ca) * v.x * v.y - sa * v.z;
00754     rotM.m[0][2] = (1.0f - ca) * v.z * v.x + sa * v.y;
00755     rotM.m[1][0] = (1.0f - ca) * v.x * v.y + sa * v.z;
00756     rotM.m[1][1] = ca + (1.0f - ca) * v.y * v.y;
00757     rotM.m[1][2] = (1.0f - ca) * v.y * v.z - sa * v.x;
00758     rotM.m[2][0] = (1.0f - ca) * v.z * v.x - sa * v.y;
00759     rotM.m[2][1] = (1.0f - ca) * v.y * v.z + sa * v.x;
00760     rotM.m[2][2] = ca + (1.0f - ca) * v.z * v.z;
00761     
00762     (*this) *= rotM;
00763 }
00764 
00765 //------------------------------------------------------------------------------
00768 inline
00769 void
00770 AI_TMatrix44::mult(const AI_TVector4& src, AI_TVector4& dst) const
00771 {
00772     dst.x = m[0][0]*src.x + m[1][0]*src.y + m[2][0]*src.z + m[3][0]*src.w;
00773     dst.y = m[0][1]*src.x + m[1][1]*src.y + m[2][1]*src.z + m[3][1]*src.w;
00774     dst.z = m[0][2]*src.x + m[1][2]*src.y + m[2][2]*src.z + m[3][2]*src.w;
00775     dst.w = m[0][3]*src.x + m[1][3]*src.y + m[2][3]*src.z + m[3][3]*src.w;
00776 }
00777 
00778 //------------------------------------------------------------------------------
00781 inline
00782 void
00783 AI_TMatrix44::mult(const AI_TVector3& src, AI_TVector3& dst) const
00784 {
00785     dst.x = m[0][0]*src.x + m[1][0]*src.y + m[2][0]*src.z + m[3][0];
00786     dst.y = m[0][1]*src.x + m[1][1]*src.y + m[2][1]*src.z + m[3][1];
00787     dst.z = m[0][2]*src.x + m[1][2]*src.y + m[2][2]*src.z + m[3][2];
00788 }
00789 
00790 //------------------------------------------------------------------------------
00793 static 
00794 inline 
00795 AI_TMatrix44 operator * (const AI_TMatrix44& m0, const AI_TMatrix44& m1) 
00796 {
00797     AI_TMatrix44 m2(
00798         m0.m[0][0]*m1.m[0][0] + m0.m[0][1]*m1.m[1][0] + m0.m[0][2]*m1.m[2][0] + m0.m[0][3]*m1.m[3][0],
00799         m0.m[0][0]*m1.m[0][1] + m0.m[0][1]*m1.m[1][1] + m0.m[0][2]*m1.m[2][1] + m0.m[0][3]*m1.m[3][1],
00800         m0.m[0][0]*m1.m[0][2] + m0.m[0][1]*m1.m[1][2] + m0.m[0][2]*m1.m[2][2] + m0.m[0][3]*m1.m[3][2],
00801         m0.m[0][0]*m1.m[0][3] + m0.m[0][1]*m1.m[1][3] + m0.m[0][2]*m1.m[2][3] + m0.m[0][3]*m1.m[3][3],
00802 
00803         m0.m[1][0]*m1.m[0][0] + m0.m[1][1]*m1.m[1][0] + m0.m[1][2]*m1.m[2][0] + m0.m[1][3]*m1.m[3][0],
00804         m0.m[1][0]*m1.m[0][1] + m0.m[1][1]*m1.m[1][1] + m0.m[1][2]*m1.m[2][1] + m0.m[1][3]*m1.m[3][1],
00805         m0.m[1][0]*m1.m[0][2] + m0.m[1][1]*m1.m[1][2] + m0.m[1][2]*m1.m[2][2] + m0.m[1][3]*m1.m[3][2],
00806         m0.m[1][0]*m1.m[0][3] + m0.m[1][1]*m1.m[1][3] + m0.m[1][2]*m1.m[2][3] + m0.m[1][3]*m1.m[3][3],
00807 
00808         m0.m[2][0]*m1.m[0][0] + m0.m[2][1]*m1.m[1][0] + m0.m[2][2]*m1.m[2][0] + m0.m[2][3]*m1.m[3][0],
00809         m0.m[2][0]*m1.m[0][1] + m0.m[2][1]*m1.m[1][1] + m0.m[2][2]*m1.m[2][1] + m0.m[2][3]*m1.m[3][1],
00810         m0.m[2][0]*m1.m[0][2] + m0.m[2][1]*m1.m[1][2] + m0.m[2][2]*m1.m[2][2] + m0.m[2][3]*m1.m[3][2],
00811         m0.m[2][0]*m1.m[0][3] + m0.m[2][1]*m1.m[1][3] + m0.m[2][2]*m1.m[2][3] + m0.m[2][3]*m1.m[3][3],
00812 
00813         m0.m[3][0]*m1.m[0][0] + m0.m[3][1]*m1.m[1][0] + m0.m[3][2]*m1.m[2][0] + m0.m[3][3]*m1.m[3][0],
00814         m0.m[3][0]*m1.m[0][1] + m0.m[3][1]*m1.m[1][1] + m0.m[3][2]*m1.m[2][1] + m0.m[3][3]*m1.m[3][1],
00815         m0.m[3][0]*m1.m[0][2] + m0.m[3][1]*m1.m[1][2] + m0.m[3][2]*m1.m[2][2] + m0.m[3][3]*m1.m[3][2],
00816         m0.m[3][0]*m1.m[0][3] + m0.m[3][1]*m1.m[1][3] + m0.m[3][2]*m1.m[2][3] + m0.m[3][3]*m1.m[3][3]);
00817     return m2;
00818 }
00819 
00820 //------------------------------------------------------------------------------
00823 static 
00824 inline 
00825 AI_TVector3 operator * (const AI_TMatrix44& m, const AI_TVector3& v)
00826 {
00827     return AI_TVector3(
00828         m.m[0][0]*v.x + m.m[1][0]*v.y + m.m[2][0]*v.z + m.m[3][0],
00829         m.m[0][1]*v.x + m.m[1][1]*v.y + m.m[2][1]*v.z + m.m[3][1],
00830         m.m[0][2]*v.x + m.m[1][2]*v.y + m.m[2][2]*v.z + m.m[3][2]);
00831 }
00832 
00833 //------------------------------------------------------------------------------
00836 static 
00837 inline 
00838 AI_TVector4 operator * (const AI_TMatrix44& m, const AI_TVector4& v)
00839 {
00840     return AI_TVector4(
00841         m.m[0][0]*v.x + m.m[1][0]*v.y + m.m[2][0]*v.z + m.m[3][0]*v.w,
00842         m.m[0][1]*v.x + m.m[1][1]*v.y + m.m[2][1]*v.z + m.m[3][1]*v.w,
00843         m.m[0][2]*v.x + m.m[1][2]*v.y + m.m[2][2]*v.z + m.m[3][2]*v.w,
00844         m.m[0][3]*v.x + m.m[1][3]*v.y + m.m[2][3]*v.z + m.m[3][3]*v.w);
00845 };
00846 
00847 //------------------------------------------------------------------------------
00848 #endif