1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
2 | // MATRIX4X4.h
|
---|
3 | // Class declaration for a 4x4 matrix
|
---|
4 | // You may use this code however you wish, but if you do, please credit me and
|
---|
5 | // provide a link to my website in a readme file or similar
|
---|
6 | // Downloaded from: www.paulsprojects.net
|
---|
7 | // Created: 20th July 2002
|
---|
8 | // Updated: 19th August 2002 - Corrected 2nd SetPerspective for n!=1.0f
|
---|
9 | // 26th September 2002 - Added nudge to prevent artifacts with infinite far plane
|
---|
10 | // - Improved speed
|
---|
11 | // 7th November 2002 - Added Affine Inverse functions
|
---|
12 | // - Changed constructors
|
---|
13 | // - Added special cases for row3 = (0, 0, 0, 1)
|
---|
14 | // 17th December 2002 - Converted from radians to degrees for consistency
|
---|
15 | // with OpenGL. Should have been done a long time ago...
|
---|
16 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
17 |
|
---|
18 | #ifndef MATRIX4X4_H
|
---|
19 | #define MATRIX4X4_H
|
---|
20 |
|
---|
21 | class MATRIX4X4
|
---|
22 | {
|
---|
23 | public:
|
---|
24 | MATRIX4X4()
|
---|
25 | { LoadIdentity(); }
|
---|
26 | MATRIX4X4( float e0, float e1, float e2, float e3,
|
---|
27 | float e4, float e5, float e6, float e7,
|
---|
28 | float e8, float e9, float e10, float e11,
|
---|
29 | float e12, float e13, float e14, float e15);
|
---|
30 | MATRIX4X4(const float * rhs);
|
---|
31 | MATRIX4X4(const MATRIX4X4 & rhs);
|
---|
32 | ~MATRIX4X4() {} //empty
|
---|
33 |
|
---|
34 | void SetEntry(int position, float value);
|
---|
35 | float GetEntry(int position) const;
|
---|
36 | VECTOR4D GetRow(int position) const;
|
---|
37 | VECTOR4D GetColumn(int position) const;
|
---|
38 |
|
---|
39 | void LoadIdentity(void);
|
---|
40 | void LoadZero(void);
|
---|
41 |
|
---|
42 | //binary operators
|
---|
43 | MATRIX4X4 operator+(const MATRIX4X4 & rhs) const;
|
---|
44 | MATRIX4X4 operator-(const MATRIX4X4 & rhs) const;
|
---|
45 | MATRIX4X4 operator*(const MATRIX4X4 & rhs) const;
|
---|
46 | MATRIX4X4 operator*(const float rhs) const;
|
---|
47 | MATRIX4X4 operator/(const float rhs) const;
|
---|
48 | friend MATRIX4X4 operator*(float scaleFactor, const MATRIX4X4 & rhs);
|
---|
49 |
|
---|
50 | bool operator==(const MATRIX4X4 & rhs) const;
|
---|
51 | bool operator!=(const MATRIX4X4 & rhs) const;
|
---|
52 |
|
---|
53 | //self-add etc
|
---|
54 | void operator+=(const MATRIX4X4 & rhs);
|
---|
55 | void operator-=(const MATRIX4X4 & rhs);
|
---|
56 | void operator*=(const MATRIX4X4 & rhs);
|
---|
57 | void operator*=(const float rhs);
|
---|
58 | void operator/=(const float rhs);
|
---|
59 |
|
---|
60 | //unary operators
|
---|
61 | MATRIX4X4 operator-(void) const;
|
---|
62 | MATRIX4X4 operator+(void) const {return (*this);}
|
---|
63 |
|
---|
64 | //multiply a vector by this matrix
|
---|
65 | VECTOR4D operator*(const VECTOR4D rhs) const;
|
---|
66 |
|
---|
67 | //rotate a 3d vector by rotation part
|
---|
68 | void RotateVector3D(VECTOR3D & rhs) const
|
---|
69 | {rhs=GetRotatedVector3D(rhs);}
|
---|
70 |
|
---|
71 | void InverseRotateVector3D(VECTOR3D & rhs) const
|
---|
72 | {rhs=GetInverseRotatedVector3D(rhs);}
|
---|
73 |
|
---|
74 | VECTOR3D GetRotatedVector3D(const VECTOR3D & rhs) const;
|
---|
75 | VECTOR3D GetInverseRotatedVector3D(const VECTOR3D & rhs) const;
|
---|
76 |
|
---|
77 | //translate a 3d vector by translation part
|
---|
78 | void TranslateVector3D(VECTOR3D & rhs) const
|
---|
79 | {rhs=GetTranslatedVector3D(rhs);}
|
---|
80 |
|
---|
81 | void InverseTranslateVector3D(VECTOR3D & rhs) const
|
---|
82 | {rhs=GetInverseTranslatedVector3D(rhs);}
|
---|
83 |
|
---|
84 | VECTOR3D GetTranslatedVector3D(const VECTOR3D & rhs) const;
|
---|
85 | VECTOR3D GetInverseTranslatedVector3D(const VECTOR3D & rhs) const;
|
---|
86 |
|
---|
87 | //Other methods
|
---|
88 | void Invert(void);
|
---|
89 | MATRIX4X4 GetInverse(void) const;
|
---|
90 | void Transpose(void);
|
---|
91 | MATRIX4X4 GetTranspose(void) const;
|
---|
92 | void InvertTranspose(void);
|
---|
93 | MATRIX4X4 GetInverseTranspose(void) const;
|
---|
94 |
|
---|
95 | //Inverse of a rotation/translation only matrix
|
---|
96 | void AffineInvert(void);
|
---|
97 | MATRIX4X4 GetAffineInverse(void) const;
|
---|
98 | void AffineInvertTranspose(void);
|
---|
99 | MATRIX4X4 GetAffineInverseTranspose(void) const;
|
---|
100 |
|
---|
101 | //set to perform an operation on space - removes other entries
|
---|
102 | void SetTranslation(const VECTOR3D & translation);
|
---|
103 | void SetScale(const VECTOR3D & scaleFactor);
|
---|
104 | void SetUniformScale(const float scaleFactor);
|
---|
105 | void SetRotationAxis(const double angle, const VECTOR3D & axis);
|
---|
106 | void SetRotationX(const double angle);
|
---|
107 | void SetRotationY(const double angle);
|
---|
108 | void SetRotationZ(const double angle);
|
---|
109 | void SetRotationEuler(const double angleX, const double angleY, const double angleZ);
|
---|
110 | void SetPerspective(float left, float right, float bottom, float top, float n, float f);
|
---|
111 | void SetPerspective(float fovy, float aspect, float n, float f);
|
---|
112 | void SetOrtho(float left, float right, float bottom, float top, float n, float f);
|
---|
113 |
|
---|
114 | //set parts of the matrix
|
---|
115 | void SetTranslationPart(const VECTOR3D & translation);
|
---|
116 | void SetRotationPartEuler(const double angleX, const double angleY, const double angleZ);
|
---|
117 | void SetRotationPartEuler(const VECTOR3D & rotations)
|
---|
118 | {
|
---|
119 | SetRotationPartEuler((double)rotations.x, (double)rotations.y, (double)rotations.z);
|
---|
120 | }
|
---|
121 |
|
---|
122 | //cast to pointer to a (float *) for glGetFloatv etc
|
---|
123 | operator float* () const {return (float*) this;}
|
---|
124 | operator const float* () const {return (const float*) this;}
|
---|
125 |
|
---|
126 | //member variables
|
---|
127 | float entries[16];
|
---|
128 | };
|
---|
129 |
|
---|
130 | #endif //MATRIX4X4_H |
---|