00001
00002
00003
00004
00005
00006
00007
00008
00009 template<class Type>
00010 Mat44<Type>::Mat44()
00011 {
00012 Identity();
00013 }
00014
00015 template<class Type>
00016 Mat44<Type>::Mat44(const Type *N)
00017 {
00018 M[0]=N[0]; M[4]=N[4]; M[8]=N[8]; M[12]=N[12];
00019 M[1]=N[1]; M[5]=N[5]; M[9]=N[9]; M[13]=N[13];
00020 M[2]=N[2]; M[6]=N[6]; M[10]=N[10]; M[14]=N[14];
00021 M[3]=N[3]; M[7]=N[7]; M[11]=N[11]; M[15]=N[15];
00022 }
00023
00024 template<class Type>
00025 Mat44<Type>::Mat44(Type M0, Type M4, Type M8, Type M12,
00026 Type M1, Type M5, Type M9, Type M13,
00027 Type M2, Type M6, Type M10, Type M14,
00028 Type M3, Type M7, Type M11, Type M15)
00029 {
00030 M[0]=M0; M[4]=M4; M[8]=M8; M[12]=M12;
00031 M[1]=M1; M[5]=M5; M[9]=M9; M[13]=M13;
00032 M[2]=M2; M[6]=M6; M[10]=M10; M[14]=M14;
00033 M[3]=M3; M[7]=M7; M[11]=M11; M[15]=M15;
00034 }
00035
00036
00037
00038
00039 template<class Type>
00040 Mat44<Type>& Mat44<Type>::operator = (const Mat44& A)
00041 {
00042 M[0]=A.M[0]; M[4]=A.M[4]; M[8]=A.M[8]; M[12]=A.M[12];
00043 M[1]=A.M[1]; M[5]=A.M[5]; M[9]=A.M[9]; M[13]=A.M[13];
00044 M[2]=A.M[2]; M[6]=A.M[6]; M[10]=A.M[10]; M[14]=A.M[14];
00045 M[3]=A.M[3]; M[7]=A.M[7]; M[11]=A.M[11]; M[15]=A.M[15];
00046 return(*this);
00047 }
00048
00049 template<class Type>
00050 Mat44<Type>& Mat44<Type>::operator = (const Type* a) {
00051 for (int i=0;i<16;i++) {
00052 M[i] = a[i];
00053 }
00054 return *this;
00055 }
00056
00057 template<class Type>
00058 Mat44<Type> Mat44<Type>::operator * (const Mat44& A) const
00059 {
00060 Mat44<Type> NewM( M[0]*A.M[0] + M[4]*A.M[1] + M[8]*A.M[2] + M[12]*A.M[3],
00061 M[0]*A.M[4] + M[4]*A.M[5] + M[8]*A.M[6] + M[12]*A.M[7],
00062 M[0]*A.M[8] + M[4]*A.M[9] + M[8]*A.M[10] + M[12]*A.M[11],
00063 M[0]*A.M[12] + M[4]*A.M[13] + M[8]*A.M[14] + M[12]*A.M[15],
00064
00065 M[1]*A.M[0] + M[5]*A.M[1] + M[9]*A.M[2] + M[13]*A.M[3],
00066 M[1]*A.M[4] + M[5]*A.M[5] + M[9]*A.M[6] + M[13]*A.M[7],
00067 M[1]*A.M[8] + M[5]*A.M[9] + M[9]*A.M[10] + M[13]*A.M[11],
00068 M[1]*A.M[12] + M[5]*A.M[13] + M[9]*A.M[14] + M[13]*A.M[15],
00069
00070 M[2]*A.M[0] + M[6]*A.M[1] + M[10]*A.M[2] + M[14]*A.M[3],
00071 M[2]*A.M[4] + M[6]*A.M[5] + M[10]*A.M[6] + M[14]*A.M[7],
00072 M[2]*A.M[8] + M[6]*A.M[9] + M[10]*A.M[10] + M[14]*A.M[11],
00073 M[2]*A.M[12] + M[6]*A.M[13] + M[10]*A.M[14] + M[14]*A.M[15],
00074
00075 M[3]*A.M[0] + M[7]*A.M[1] + M[11]*A.M[2] + M[15]*A.M[3],
00076 M[3]*A.M[4] + M[7]*A.M[5] + M[11]*A.M[6] + M[15]*A.M[7],
00077 M[3]*A.M[8] + M[7]*A.M[9] + M[11]*A.M[10] + M[15]*A.M[11],
00078 M[3]*A.M[12] + M[7]*A.M[13] + M[11]*A.M[14] + M[15]*A.M[15] );
00079 return(NewM);
00080 }
00081
00082 template<class Type>
00083 Vec3<Type> Mat44<Type>::operator * (const Vec3<Type>& V) const
00084 {
00085 Type W = M[3]*V.x + M[7]*V.y + M[11]*V.z + M[15];
00086 Vec3<Type> NewV( (M[0]*V.x + M[4]*V.y + M[8]*V.z + M[12]) / W,
00087 (M[1]*V.x + M[5]*V.y + M[9]*V.z + M[13]) / W,
00088 (M[2]*V.x + M[6]*V.y + M[10]*V.z + M[14]) / W );
00089 return(NewV);
00090 }
00091
00092
00093
00094
00095
00096 template<class Type>
00097 Vec3<Type> Mat44<Type>::multNormal(const Vec3<Type>& N) const
00098 {
00099 Vec3<Type> NewN( (M[0]*N.x + M[4]*N.y + M[8]*N.z ),
00100 (M[1]*N.x + M[5]*N.y + M[9]*N.z ),
00101 (M[2]*N.x + M[6]*N.y + M[10]*N.z) );
00102 return (NewN);
00103 }
00104
00105
00106
00107
00108 template<class Type>
00109 Vec3<Type> Mat44<Type>::multPoint(const Vec3<Type>& P) const
00110 {
00111 Vec3<Type> NewP( (M[0]*P.x + M[4]*P.y + M[8]*P.z + M[12]),
00112 (M[1]*P.x + M[5]*P.y + M[9]*P.z + M[13]),
00113 (M[2]*P.x + M[6]*P.y + M[10]*P.z + M[14]) );
00114 return (NewP);
00115 }
00116
00117
00118 template<class Type>
00119 Vec4<Type> Mat44<Type>::operator * (const Vec4<Type>& V) const
00120 {
00121 Vec4<Type> NewV;
00122 NewV.x = M[0]*V.x + M[4]*V.y + M[8]*V.z + M[12]*V.w;
00123 NewV.y = M[1]*V.x + M[5]*V.y + M[9]*V.z + M[13]*V.w;
00124 NewV.z = M[2]*V.x + M[6]*V.y + M[10]*V.z + M[14]*V.w;
00125 NewV.w = M[3]*V.x + M[7]*V.y + M[11]*V.z + M[15]*V.w;
00126 return(NewV);
00127 }
00128
00129
00130 template<class Type>
00131 Mat44<Type> Mat44<Type>::operator * (Type a) const
00132 {
00133 Mat44<Type> NewM( M[0] * a, M[1] * a, M[2] * a, M[3] * a,
00134 M[4] * a, M[5] * a, M[6] * a, M[7] * a,
00135 M[8] * a, M[9] * a, M[10] * a, M[11] * a,
00136 M[12] * a, M[13] * a, M[14] * a, M[15] * a);
00137 return NewM;
00138 }
00139
00140 template<class Type>
00141 Mat44<Type>& Mat44<Type>::operator *= (Type a)
00142 {
00143 for (int i = 0; i < 16; i++)
00144 {
00145 M[i] *= a;
00146 }
00147
00148 return *this;
00149 }
00150
00151 template<class Type>
00152 Mat44<Type>::operator const Type*() const
00153 {
00154 return M;
00155 }
00156
00157 template<class Type>
00158 Mat44<Type>::operator Type*()
00159 {
00160 return M;
00161 }
00162
00163 template<class Type>
00164 Type& Mat44<Type>::operator()(int col, int row)
00165 {
00166 return M[4*col+row];
00167 }
00168
00169 template<class Type>
00170 const Type& Mat44<Type>::operator()(int col, int row) const
00171 {
00172 return M[4*col+row];
00173 }
00174
00175 template<class Type>
00176 void Mat44<Type>::Set(const Type* a)
00177 {
00178 for (int i=0;i<16;i++) {
00179 M[i] = a[i];
00180 }
00181
00182 }
00183
00184 template<class Type>
00185 void Mat44<Type>::Set(Type M0, Type M4, Type M8, Type M12,
00186 Type M1, Type M5, Type M9, Type M13,
00187 Type M2, Type M6, Type M10, Type M14,
00188 Type M3, Type M7, Type M11, Type M15)
00189 {
00190 M[0]=M0; M[4]=M4; M[8]=M8; M[12]=M12;
00191 M[1]=M1; M[5]=M5; M[9]=M9; M[13]=M13;
00192 M[2]=M2; M[6]=M6; M[10]=M10; M[14]=M14;
00193 M[3]=M3; M[7]=M7; M[11]=M11; M[15]=M15;
00194 }
00195
00196
00197
00198
00199
00200 template<class Type>
00201 void Mat44<Type>::Identity()
00202 {
00203 M[0]=M[5]=M[10]=M[15]=1;
00204 M[1]=M[2]=M[3]=M[4]=M[6]=M[7]=M[8]=M[9]=M[11]=M[12]=M[13]=M[14]=0;
00205 }
00206
00207 template<class Type>
00208 void Mat44<Type>::Transpose()
00209 {
00210 SWAP(M[1],M[4]);
00211 SWAP(M[2],M[8]);
00212 SWAP(M[6],M[9]);
00213 SWAP(M[3],M[12]);
00214 SWAP(M[7],M[13]);
00215 SWAP(M[11],M[14]);
00216 }
00217
00218
00219
00220
00221 template <class Type>
00222 void Mat44<Type>::Translate(Type Tx, Type Ty, Type Tz)
00223 {
00224 M[0]=1; M[4]=0; M[8]=0; M[12]=Tx;
00225 M[1]=0; M[5]=1; M[9]=0; M[13]=Ty;
00226 M[2]=0; M[6]=0; M[10]=1; M[14]=Tz;
00227 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00228 }
00229
00230 template<class Type>
00231 void Mat44<Type>::Translate(const Vec3<Type>& T)
00232 {
00233 M[0]=1; M[4]=0; M[8]=0; M[12]=T.x;
00234 M[1]=0; M[5]=1; M[9]=0; M[13]=T.y;
00235 M[2]=0; M[6]=0; M[10]=1; M[14]=T.z;
00236 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00237 }
00238
00239 template<class Type>
00240 void Mat44<Type>::invTranslate(Type Tx, Type Ty, Type Tz)
00241 {
00242 M[0]=1; M[4]=0; M[8]=0; M[12]=-Tx;
00243 M[1]=0; M[5]=1; M[9]=0; M[13]=-Ty;
00244 M[2]=0; M[6]=0; M[10]=1; M[14]=-Tz;
00245 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00246 }
00247
00248 template<class Type>
00249 void Mat44<Type>::invTranslate(const Vec3<Type>& T)
00250 {
00251 M[0]=1; M[4]=0; M[8]=0; M[12]=-T.x;
00252 M[1]=0; M[5]=1; M[9]=0; M[13]=-T.y;
00253 M[2]=0; M[6]=0; M[10]=1; M[14]=-T.z;
00254 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00255 }
00256
00257 template<class Type>
00258 void Mat44<Type>::Scale(Type Sx, Type Sy, Type Sz)
00259 {
00260 M[0]=Sx; M[4]=0; M[8]=0; M[12]=0;
00261 M[1]=0; M[5]=Sy; M[9]=0; M[13]=0;
00262 M[2]=0; M[6]=0; M[10]=Sz; M[14]=0;
00263 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00264 }
00265
00266 template<class Type>
00267 void Mat44<Type>::Scale(const Vec3<Type>& S)
00268 {
00269 M[0]=S.x; M[4]=0; M[8]=0; M[12]=0;
00270 M[1]=0; M[5]=S.y; M[9]=0; M[13]=0;
00271 M[2]=0; M[6]=0; M[10]=S.z; M[14]=0;
00272 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00273 }
00274
00275 template<class Type>
00276 void Mat44<Type>::invScale(Type Sx, Type Sy, Type Sz)
00277 {
00278 M[0]=1/Sx; M[4]=0; M[8]=0; M[12]=0;
00279 M[1]=0; M[5]=1/Sy; M[9]=0; M[13]=0;
00280 M[2]=0; M[6]=0; M[10]=1/Sz; M[14]=0;
00281 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00282 }
00283
00284 template<class Type>
00285 void Mat44<Type>::invScale(const Vec3<Type>& S)
00286 {
00287 M[0]=1/S.x; M[4]=0; M[8]=0; M[12]=0;
00288 M[1]=0; M[5]=1/S.y; M[9]=0; M[13]=0;
00289 M[2]=0; M[6]=0; M[10]=1/S.z; M[14]=0;
00290 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00291 }
00292
00293 template<class Type>
00294 void Mat44<Type>::Rotate(Type DegAng, const Vec3<Type>& Axis)
00295 {
00296 Type RadAng = DegAng*Mat44TORADS;
00297 Type ca=(Type)cos(RadAng),
00298 sa=(Type)sin(RadAng);
00299 if (Axis.x==1 && Axis.y==0 && Axis.z==0)
00300 {
00301 M[0]=1; M[4]=0; M[8]=0; M[12]=0;
00302 M[1]=0; M[5]=ca; M[9]=-sa; M[13]=0;
00303 M[2]=0; M[6]=sa; M[10]=ca; M[14]=0;
00304 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00305 }
00306 else if (Axis.x==0 && Axis.y==1 && Axis.z==0)
00307 {
00308 M[0]=ca; M[4]=0; M[8]=sa; M[12]=0;
00309 M[1]=0; M[5]=1; M[9]=0; M[13]=0;
00310 M[2]=-sa; M[6]=0; M[10]=ca; M[14]=0;
00311 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00312 }
00313 else if (Axis.x==0 && Axis.y==0 && Axis.z==1)
00314 {
00315 M[0]=ca; M[4]=-sa; M[8]=0; M[12]=0;
00316 M[1]=sa; M[5]=ca; M[9]=0; M[13]=0;
00317 M[2]=0; M[6]=0; M[10]=1; M[14]=0;
00318 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00319 }
00320 else
00321 {
00322 Type l = Axis.LengthSqr();
00323 Type x, y, z;
00324 x=Axis.x, y=Axis.y, z=Axis.z;
00325 if (l > Type(1.0001) || l < Type(0.9999) && l!=0)
00326 {
00327
00328 l=Type(1.0)/sqrt(l);
00329 x*=l; y*=l; z*=l;
00330 }
00331 Type x2=x*x, y2=y*y, z2=z*z;
00332 M[0]=x2+ca*(1-x2); M[4]=(x*y)+ca*(-x*y)+sa*(-z); M[8]=(x*z)+ca*(-x*z)+sa*y;
00333 M[1]=(x*y)+ca*(-x*y)+sa*z; M[5]=y2+ca*(1-y2); M[9]=(y*z)+ca*(-y*z)+sa*(-x);
00334 M[2]=(x*z)+ca*(-x*z)+sa*(-y); M[6]=(y*z)+ca*(-y*z)+sa*x; M[10]=z2+ca*(1-z2);
00335 M[12]=M[13]=M[14]=M[3]=M[7]=M[11]=0;
00336 M[15]=1;
00337 }
00338 }
00339
00340 template<class Type>
00341 void Mat44<Type>::invRotate(Type DegAng, const Vec3<Type>& Axis)
00342 {
00343 Rotate(DegAng,Axis);
00344 Transpose();
00345 }
00346
00347 template<class Type>
00348 inline Type Mat44<Type>::Trace() const
00349 {
00350 return M[0] + M[5] + M[10] + M[15];
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 template<class Type>
00369 void Mat44<Type>::Frustum(Type l, Type r, Type b, Type t, Type n, Type f)
00370 {
00371 M[0]=(2*n)/(r-l); M[4]=0; M[8]=(r+l)/(r-l); M[12]=0;
00372 M[1]=0; M[5]=(2*n)/(t-b); M[9]=(t+b)/(t-b); M[13]=0;
00373 M[2]=0; M[6]=0; M[10]=-(f+n)/(f-n); M[14]=(-2*f*n)/(f-n);
00374 M[3]=0; M[7]=0; M[11]=-1; M[15]=0;
00375 }
00376
00377 template<class Type>
00378 void Mat44<Type>::invFrustum(Type l, Type r, Type b, Type t, Type n, Type f)
00379 {
00380 M[0]=(r-l)/(2*n); M[4]=0; M[8]=0; M[12]=(r+l)/(2*n);
00381 M[1]=0; M[5]=(t-b)/(2*n); M[9]=0; M[13]=(t+b)/(2*n);
00382 M[2]=0; M[6]=0; M[10]=0; M[14]=-1;
00383 M[3]=0; M[7]=0; M[11]=-(f-n)/(2*f*n); M[15]=(f+n)/(2*f*n);
00384 }
00385
00386
00387
00388
00389 template<class Type>
00390 void Mat44<Type>::Perspective(Type Yfov, Type Aspect, Type Ndist, Type Fdist)
00391 {
00392 Yfov *= 0.0174532f;
00393 Type wT=tanf(Yfov*0.5f)*Ndist, wB=-wT;
00394 Type wR=wT*Aspect, wL=-wR;
00395 Frustum(wL,wR,wB,wT,Ndist,Fdist);
00396 }
00397
00398 template<class Type>
00399 void Mat44<Type>::invPerspective(Type Yfov, Type Aspect, Type Ndist, Type Fdist)
00400 {
00401 Yfov *= 0.0174532f;
00402 Type wT=tanf(Yfov*0.5f)*Ndist, wB=-wT;
00403 Type wR=wT*Aspect, wL=-wR;
00404 invFrustum(wL,wR,wB,wT,Ndist,Fdist);
00405 }
00406
00407
00408
00409
00410
00411
00412 template<class Type>
00413 void Mat44<Type>::Viewport(int WW, int WH)
00414 {
00415 Type WW2=(Type)WW*0.5f, WH2=(Type)WH*0.5f;
00416 M[0]=WW2; M[4]=0; M[8]=0; M[12]=WW2;
00417 M[1]=0; M[5]=WH2; M[9]=0; M[13]=WH2;
00418 M[2]=0; M[6]=0; M[10]=0.5; M[14]=0.5;
00419 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00420 }
00421
00422 template<class Type>
00423 void Mat44<Type>::invViewport(int WW, int WH)
00424 {
00425 Type WW2=2.0f/(Type)WW, WH2=2.0f/(Type)WH;
00426 M[0]=WW2; M[4]=0; M[8]=0; M[12]=-1.0;
00427 M[1]=0; M[5]=WH2; M[9]=0; M[13]=-1.0;
00428 M[2]=0; M[6]=0; M[10]=2.0; M[14]=-1.0;
00429 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00430 }
00431
00432
00433
00434
00435 template<class Type>
00436 void Mat44<Type>::LookAt(const Vec3<Type>& Eye,
00437 const Vec3<Type>& LookAtPt,
00438 const Vec3<Type>& ViewUp)
00439 {
00440 Vec3<Type> Z = Eye-LookAtPt; Z.Normalize();
00441 Vec3<Type> X = ViewUp/Z; X.Normalize();
00442 Vec3<Type> Y = Z/X; Y.Normalize();
00443 Vec3<Type> Tr = -Eye;
00444 M[0]=X.x; M[4]=X.y; M[8]=X.z; M[12]=X*Tr;
00445 M[1]=Y.x; M[5]=Y.y; M[9]=Y.z; M[13]=Y*Tr;
00446 M[2]=Z.x; M[6]=Z.y; M[10]=Z.z; M[14]=Z*Tr;
00447 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00448 }
00449
00450 template<class Type>
00451 void Mat44<Type>::invLookAt(const Vec3<Type>& Eye,
00452 const Vec3<Type>& LookAtPt,
00453 const Vec3<Type>& ViewUp)
00454 {
00455 Vec3<Type> Z = Eye-LookAtPt; Z.Normalize();
00456 Vec3<Type> X = ViewUp/Z; X.Normalize();
00457 Vec3<Type> Y = Z/X; Y.Normalize();
00458 M[0]=X.x; M[4]=Y.x; M[8]=Z.x; M[12]=Eye.x;
00459 M[1]=X.y; M[5]=Y.y; M[9]=Z.y; M[13]=Eye.y;
00460 M[2]=X.z; M[6]=Y.z; M[10]=Z.z; M[14]=Eye.z;
00461 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 template<class Type>
00477 void Mat44<Type>::Viewport2(int WW, int WH)
00478 {
00479 Type WW2=(WW-Mat44VIEWPORT_TOL)*0.5f, WH2=(WH-Mat44VIEWPORT_TOL)*0.5f;
00480 M[0]=WW2; M[4]=0; M[8]=0; M[12]=WW2;
00481 M[1]=0; M[5]=WH2; M[9]=0; M[13]=WH2;
00482 M[2]=0; M[6]=0; M[10]=0.5; M[14]=0.5;
00483 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00484 }
00485
00486 template<class Type>
00487 void Mat44<Type>::invViewport2(int WW, int WH)
00488 {
00489 Type WW2=2.0f/(WW-Mat44VIEWPORT_TOL), WH2=2.0f/(WH-Mat44VIEWPORT_TOL);
00490 M[0]=WW2; M[4]=0; M[8]=0; M[12]=-1.0;
00491 M[1]=0; M[5]=WH2; M[9]=0; M[13]=-1.0;
00492 M[2]=0; M[6]=0; M[10]=2.0; M[14]=-1.0;
00493 M[3]=0; M[7]=0; M[11]=0; M[15]=1;
00494 }
00495
00496
00497
00498
00499 template<class Type>
00500 void Mat44<Type>::Print() const
00501 {
00502 printf("\n%f %f %f %f\n",M[0],M[4],M[8],M[12]);
00503 printf("%f %f %f %f\n",M[1],M[5],M[9],M[13]);
00504 printf("%f %f %f %f\n",M[2],M[6],M[10],M[14]);
00505 printf("%f %f %f %f\n\n",M[3],M[7],M[11],M[15]);
00506 }
00507
00508
00509
00510
00511 template<class Type>
00512 void Mat44<Type>::CopyInto(Type *Mat) const
00513 {
00514 Mat[0]=M[0]; Mat[4]=M[4]; Mat[8]=M[8]; Mat[12]=M[12];
00515 Mat[1]=M[1]; Mat[5]=M[5]; Mat[9]=M[9]; Mat[13]=M[13];
00516 Mat[2]=M[2]; Mat[6]=M[6]; Mat[10]=M[10]; Mat[14]=M[14];
00517 Mat[3]=M[3]; Mat[7]=M[7]; Mat[11]=M[11]; Mat[15]=M[15];
00518 }
00519