00001 //============================================================================
00002 // mat44.hpp : 4x4 OpenGL-style matrix template.
00003 //============================================================================
00004
00005 #ifndef _MAT44_
00006 #define _MAT44_
00007
00008 #include "vec3f.hpp"
00009 #include "vec4f.hpp"
00010
00011 static const float Mat44TORADS = 0.0174532f;
00012 static const float Mat44VIEWPORT_TOL = 0.001f;
00013
00014 //----------------------------------------------------------------------------
00015 // M[16] = [ 0 4 8 12 ] | 16 floats were used instead of the normal [4][4]
00016 // [ 1 5 9 13 ] | to be compliant with OpenGL. OpenGL uses
00017 // [ 2 6 10 14 ] | premultiplication with column vectors. These
00018 // [ 3 7 11 15 ] | matrices can be fed directly into the OpenGL
00019 // | matrix stack with glLoadMatrix or glMultMatrix.
00020 //
00021 // [ x' y' z' w' ] = [ 0 4 8 12 ] [ x ]
00022 // [ 1 5 9 13 ] * [ y ]
00023 // [ 2 6 10 14 ] [ z ]
00024 // [ 3 7 11 15 ] [ w ]
00025 //
00026 // Loading a [4][4] format matrix directly into the matrix stack (assuming
00027 // premult/col vecs) results in a transpose matrix. M[0]=M[0][0], but
00028 // M[1]!=M[1][0] since M[0][1] would cast to M[1].
00029 //
00030 // However, if we assumed postmult/row vectors we could use [4][4] format,
00031 // but all transformations in this module would be transposed.
00032 //----------------------------------------------------------------------------
00033 template<class Type>
00034 class Mat44
00035 {
00036 public:
00037 Type M[16]; // 0,1,2,3 = 1st col; 4,5,6,7 = 2nd col; etc.
00038
00039 Mat44();
00040 Mat44(const Type *N);
00041 Mat44(Type M0, Type M4, Type M8, Type M12,
00042 Type M1, Type M5, Type M9, Type M13,
00043 Type M2, Type M6, Type M10, Type M14,
00044 Type M3, Type M7, Type M11, Type M15);
00045 Mat44<Type>& operator = (const Mat44& A); // ASSIGNMENT (=)
00046 Mat44<Type>& operator = (const Type* a); // ASSIGNMENT (=) FROM AN ARRAY OF TypeS
00047 Mat44<Type> operator * (const Mat44& A) const; // MULTIPLICATION (*)
00048 Vec3<Type> operator * (const Vec3<Type>& V) const; // MAT-VECTOR MULTIPLICATION (*) W/ PERSP DIV
00049 Vec3<Type> multNormal(const Vec3<Type>& V) const; // MAT-VECTOR MULTIPLICATION _WITHOUT_ PERSP DIV
00050 Vec3<Type> multPoint(const Vec3<Type>& V) const; // MAT-POINT MULTIPLICATION _WITHOUT_ PERSP DIV
00051 Vec4<Type> operator * (const Vec4<Type>& V) const; // MAT-VECTOR MULTIPLICATION (*)
00052 Mat44<Type> operator * (Type a) const; // SCALAR POST-MULTIPLICATION
00053 Mat44<Type>& operator *= (Type a); // ACCUMULATE MULTIPLY (*=)
00055 // friend Vec3<Type> operator * (const Vec3<Type>& V, const Mat44& M); // MAT-VECTOR PRE-MULTIPLICATON (*) W/ PERP DIV
00056 // friend Vec4<Type> operator * (const Vec4<Type>& V, const Mat44& M); // MAT-VECTOR PRE-MULTIPLICATON (*)
00057 // friend Mat44<Type> operator * (Type a, const Mat44& M) const; // SCALAR PRE-MULTIPLICATION
00058 // Mat44<Type> operator / (Type a) const; // SCALAR DIVISION
00059 // Mat44<Type> operator + (Mat44& M) const; // ADDITION (+)
00060 // Mat44<Type>& operator += (Mat44& M); // ACCUMULATE ADD (+=)
00061 // Mat44<Type>& operator /= (Type a); // ACCUMULATE DIVIDE (/=)
00062 // bool Invserse(); // MATRIX INVERSE
00064
00065 operator const Type*() const; // CAST TO A Type ARRAY
00066 operator Type*(); // CAST TO A Type ARRAY
00067 Type& operator()(int col, int row); // 2D ARRAY ACCESSOR
00068 const Type& operator()(int col, int row) const; // 2D ARRAY ACCESSOR
00069 void Set(const Type* a); // SAME AS ASSIGNMENT (=) FROM AN ARRAY OF TypeS
00070 void Set(Type M0, Type M4, Type M8, Type M12,
00071 Type M1, Type M5, Type M9, Type M13,
00072 Type M2, Type M6, Type M10, Type M14,
00073 Type M3, Type M7, Type M11, Type M15);
00074
00075
00076 void Identity();
00077 void Transpose();
00078 void Translate(Type Tx, Type Ty, Type Tz);
00079 void Translate(const Vec3<Type>& T);
00080 void invTranslate(Type Tx, Type Ty, Type Tz);
00081 void invTranslate(const Vec3<Type>& T);
00082 void Scale(Type Sx, Type Sy, Type Sz);
00083 void Scale(const Vec3<Type>& S);
00084 void invScale(Type Sx, Type Sy, Type Sz);
00085 void invScale(const Vec3<Type>& S);
00086 void Rotate(Type DegAng, const Vec3<Type>& Axis);
00087 void invRotate(Type DegAng, const Vec3<Type>& Axis);
00088 Type Trace(void) const;
00089 void Frustum(Type l, Type r, Type b, Type t, Type n, Type f);
00090 void invFrustum(Type l, Type r, Type b, Type t, Type n, Type f);
00091 void Perspective(Type Yfov, Type Aspect, Type Ndist, Type Fdist);
00092 void invPerspective(Type Yfov, Type Aspect, Type Ndist, Type Fdist);
00093 void Viewport(int WW, int WH);
00094 void invViewport(int WW, int WH);
00095 void LookAt(const Vec3<Type>& Eye,
00096 const Vec3<Type>& LookAtPt,
00097 const Vec3<Type>& ViewUp);
00098 void invLookAt(const Vec3<Type>& Eye,
00099 const Vec3<Type>& LookAtPt,
00100 const Vec3<Type>& ViewUp);
00101 void Viewport2(int WW, int WH);
00102 void invViewport2(int WW, int WH);
00103 void Print() const;
00104 void CopyInto(Type *Mat) const;
00105
00106 static void SWAP(Type& a, Type& b) {Type t; t=a;a=b;b=t;}
00107 };
00108
00109 #include "mat44impl.hpp"
00110
00111 typedef Mat44<float> Mat44f;
00112 typedef Mat44<double> Mat44d;
00113
00114 #endif
00115
00116
00117
00118
1.2.10 written by Dimitri van Heesch,
© 1997-2001