1 | #ifndef __MATRIX4x4_H
|
---|
2 | #define __MATRIX4x4_H
|
---|
3 |
|
---|
4 | #include <iostream>
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 | class Vector3;
|
---|
11 | class AxisAlignedBox3;
|
---|
12 |
|
---|
13 |
|
---|
14 | class Matrix4x4
|
---|
15 | {
|
---|
16 | public:
|
---|
17 | /** Default constructor initialising nothig.
|
---|
18 | */
|
---|
19 | Matrix4x4();
|
---|
20 | /** here xXY - 'X' is row, and 'Y' is column - classical mathematical notation
|
---|
21 | */
|
---|
22 | Matrix4x4(float x11, float x12, float x13, float x14,
|
---|
23 | float x21, float x22, float x23, float x24,
|
---|
24 | float x31, float x32, float x33, float x34,
|
---|
25 | float x41, float x42, float x43, float x44);
|
---|
26 | /** Constructor setting the columns of the 3x3 part of the matrix.
|
---|
27 | */
|
---|
28 | Matrix4x4(const Vector3 &a, const Vector3 &b, const Vector3 &c);
|
---|
29 |
|
---|
30 |
|
---|
31 | ////////////////
|
---|
32 | //-- Assignment operators
|
---|
33 |
|
---|
34 | Matrix4x4& operator+= (const Matrix4x4 &A); // add-to
|
---|
35 | Matrix4x4& operator-= (const Matrix4x4 &A); // subtract-from
|
---|
36 | Matrix4x4& operator*= (const Matrix4x4 &A); // multiply by matrix
|
---|
37 | Matrix4x4& operator*= (float A); // scale by scalar
|
---|
38 |
|
---|
39 |
|
---|
40 | ////////////////
|
---|
41 | //-- Fundamental operations
|
---|
42 |
|
---|
43 | /** Invert the matrix .. returns 0 = regular
|
---|
44 | */
|
---|
45 | int Invert();
|
---|
46 | /** Transpose the matrix
|
---|
47 | */
|
---|
48 | void Transpose();
|
---|
49 | /** Sets the columns of the 3x3 part of the matrix.
|
---|
50 | */
|
---|
51 | void SetColumns(const Vector3 &a, const Vector3 &b, const Vector3 &c);
|
---|
52 |
|
---|
53 | float Det3x3() const;
|
---|
54 |
|
---|
55 | /** multiply with homogen vector form, where the 4 coordinat is in h,
|
---|
56 | the resulting homogen coordinate returned in w
|
---|
57 | */
|
---|
58 | Vector3 Transform(float &w, const Vector3 &v, float h) const;
|
---|
59 |
|
---|
60 |
|
---|
61 | ////////////
|
---|
62 | //-- members
|
---|
63 |
|
---|
64 | float x[4][4]; // first index is column [x], the second is row [y]
|
---|
65 |
|
---|
66 |
|
---|
67 | ////////////////////
|
---|
68 |
|
---|
69 | // Invert a given matrix
|
---|
70 | friend Matrix4x4 Invert(const Matrix4x4 &M);
|
---|
71 | // Transpose a given matrix
|
---|
72 | friend Matrix4x4 Transpose(const Matrix4x4 &M);
|
---|
73 |
|
---|
74 |
|
---|
75 | ///////////
|
---|
76 | //-- Create various types of matrix.
|
---|
77 |
|
---|
78 | friend Matrix4x4 IdentityMatrix();
|
---|
79 | friend Matrix4x4 ZeroMatrix();
|
---|
80 | friend Matrix4x4 TranslationMatrix(const Vector3 &Location);
|
---|
81 | friend Matrix4x4 RotationXMatrix(float Angle);
|
---|
82 | friend Matrix4x4 RotationYMatrix(float Angle);
|
---|
83 | friend Matrix4x4 RotationZMatrix(float Angle);
|
---|
84 | friend Matrix4x4 RotationYPRMatrix(float Yaw, float Pitch, float Roll);
|
---|
85 | // about axis 'axis' by angle 'Angle'
|
---|
86 | friend Matrix4x4 RotationAxisMatrix(const Vector3 &axis, float Angle);
|
---|
87 | // create the rotation matrix that rotates 'vecFrom' to 'vecTo'
|
---|
88 | friend Matrix4x4 RotationVectorsMatrix(const Vector3 &vecFrom,
|
---|
89 | const Vector3 &vecTo);
|
---|
90 |
|
---|
91 | friend Matrix4x4 ScaleMatrix(float X, float Y, float Z);
|
---|
92 | friend Matrix4x4 GenRotation(const Vector3 &x, const Vector3 &y,
|
---|
93 | const Vector3 &z);
|
---|
94 | friend Matrix4x4 QuadricMatrix(float a, float b, float c, float d, float e,
|
---|
95 | float f, float g, float h, float j, float k);
|
---|
96 | // returns matrix for transforming normal
|
---|
97 | friend Matrix4x4 NormalTransformMatrix(const Matrix4x4 &M);
|
---|
98 |
|
---|
99 | friend Matrix4x4 MirrorX();
|
---|
100 | friend Matrix4x4 MirrorY();
|
---|
101 | friend Matrix4x4 MirrorZ();
|
---|
102 | friend Matrix4x4 RotationOnly(const Matrix4x4 &x);
|
---|
103 |
|
---|
104 | // Binary operators
|
---|
105 | friend Matrix4x4 operator+ (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
106 | friend Matrix4x4 operator- (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
107 | friend Matrix4x4 operator* (const Matrix4x4 &A, float B);
|
---|
108 | friend Matrix4x4 operator* (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
109 |
|
---|
110 | // friends returning Vector3
|
---|
111 | friend Vector3 operator*(const Matrix4x4 &M, const Vector3 &v);
|
---|
112 | friend Vector3 RotateOnly(const Matrix4x4 &M, const Vector3 &v);
|
---|
113 | friend Vector3 TransformNormal(const Matrix4x4 &M, const Vector3 &v);
|
---|
114 | friend Vector3 GetTranslation(const Matrix4x4 &M);
|
---|
115 |
|
---|
116 | friend Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box);
|
---|
117 |
|
---|
118 | //output is initialized with the same result as glFrustum
|
---|
119 | friend Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far);
|
---|
120 |
|
---|
121 | // look from position into given direction with given up vector
|
---|
122 | friend Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
|
---|
123 |
|
---|
124 | // Overloaded output operator.
|
---|
125 | friend std::ostream& operator<< (std::ostream &s, const Matrix4x4 &M);
|
---|
126 | };
|
---|
127 |
|
---|
128 |
|
---|
129 | /////////
|
---|
130 | //-- forward declaration
|
---|
131 |
|
---|
132 |
|
---|
133 | Matrix4x4 IdentityMatrix();
|
---|
134 | Matrix4x4 Invert(const Matrix4x4 &M);
|
---|
135 | Matrix4x4 Transpose(const Matrix4x4 &M);
|
---|
136 | Matrix4x4 IdentityMatrix();
|
---|
137 | Matrix4x4 ZeroMatrix();
|
---|
138 | Matrix4x4 TranslationMatrix(const Vector3 &Location);
|
---|
139 | Matrix4x4 RotationXMatrix(float Angle);
|
---|
140 | Matrix4x4 RotationYMatrix(float Angle);
|
---|
141 | Matrix4x4 RotationZMatrix(float Angle);
|
---|
142 | Matrix4x4 RotationYPRMatrix(float Yaw, float Pitch, float Roll);
|
---|
143 | Matrix4x4 RotationAxisMatrix(const Vector3 &axis, float Angle);
|
---|
144 | Matrix4x4 RotationVectorsMatrix(const Vector3 &vecFrom,
|
---|
145 | const Vector3 &vecTo);
|
---|
146 | Matrix4x4 ScaleMatrix(float X, float Y, float Z);
|
---|
147 | Matrix4x4 ScaleMatrix(float x);
|
---|
148 | Matrix4x4 GenRotation(const Vector3 &x, const Vector3 &y,
|
---|
149 | const Vector3 &z);
|
---|
150 | Matrix4x4 QuadricMatrix(float a, float b, float c, float d, float e,
|
---|
151 | float f, float g, float h, float j, float k);
|
---|
152 | Matrix4x4 NormalTransformMatrix(const Matrix4x4 &M);
|
---|
153 | Matrix4x4 MirrorX();
|
---|
154 | Matrix4x4 MirrorY();
|
---|
155 | Matrix4x4 MirrorZ();
|
---|
156 | Matrix4x4 RotationOnly(const Matrix4x4 &x);
|
---|
157 | Matrix4x4 operator+ (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
158 | Matrix4x4 operator- (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
159 | Matrix4x4 operator* (const Matrix4x4 &A, float B);
|
---|
160 | Matrix4x4 operator* (const Matrix4x4 &A, const Matrix4x4 &B);
|
---|
161 | Vector3 operator*(const Matrix4x4 &M, const Vector3 &v);
|
---|
162 | Vector3 RotateOnly(const Matrix4x4 &M, const Vector3 &v);
|
---|
163 | Vector3 TransformNormal(const Matrix4x4 &M, const Vector3 &v);
|
---|
164 | Vector3 GetTranslation(const Matrix4x4 &M);
|
---|
165 | Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box);
|
---|
166 | Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far);
|
---|
167 | Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
|
---|
168 | Matrix4x4 GetOrtho(float left, float right, float bottom, float top, float near, float far);
|
---|
169 | Matrix4x4 GetPerspective(float fov, float aspect, float near, float far);
|
---|
170 |
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | #endif
|
---|