FCT
载入中...
搜索中...
未找到
Matrix.h
浏览该文件的文档.
1
2#include "./Vec.h"
3#pragma once
4
5namespace FCT
6{
7
8 class Mat4
9 {
10 public:
11 float m[16];
12
14 {
15 identity();
16 }
17 Mat4(float m11, float m12, float m13, float m14,
18 float m21, float m22, float m23, float m24,
19 float m31, float m32, float m33, float m34,
20 float m41, float m42, float m43, float m44)
21 {
22 m[0] = m11;
23 m[1] = m12;
24 m[2] = m13;
25 m[3] = m14;
26 m[4] = m21;
27 m[5] = m22;
28 m[6] = m23;
29 m[7] = m24;
30 m[8] = m31;
31 m[9] = m32;
32 m[10] = m33;
33 m[11] = m34;
34 m[12] = m41;
35 m[13] = m42;
36 m[14] = m43;
37 m[15] = m44;
38 }
39
40 void identity()
41 {
42 *this = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1};
47 }
48 void translate(float x, float y)
49 {
50 m[3] += x;
51 m[7] += y;
52 }
53 void translate(float x, float y, float z)
54 {
55 Mat4 trans = {
56 1, 0, 0, x,
57 0, 1, 0, y,
58 0, 0, 1, z,
59 0, 0, 0, 1
60 };
61 *this = *this * trans;
62 }
63 void rotateX(float degrees)
64 {
65 float radians = degrees * 3.14159265f / 180.0f;
66 float c = cosf(radians);
67 float s = sinf(radians);
68
69 Mat4 rot = {
70 1, 0, 0, 0,
71 0, c, -s, 0,
72 0, s, c, 0,
73 0, 0, 0, 1};
74 *this = *this * rot;
75 }
76 static Mat4 CreateTranslation(float x, float y)
77 {
78 Mat4 ret;
79 ret.m[3] += x;
80 ret.m[7] += y;
81 return ret;
82 }
83 static Mat4 CreateScale(float scaleX, float scaleY)
84 {
85 Mat4 ret = {
86 scaleX, 0, 0, 0,
87 0, scaleY, 0, 0,
88 0, 0, 1, 0,
89 0, 0, 0, 1};
90 return ret;
91 }
92 static Mat4 Translate(float x, float y)
93 {
94 Mat4 ret;
95 ret.m[8] += x;
96 ret.m[9] += y;
97 return ret;
98 }
99 static Mat4 Translate(float x, float y, float z)
100 {
101 Mat4 ret = {
102 1, 0, 0, x,
103 0, 1, 0, y,
104 0, 0, 1, z,
105 0, 0, 0, 1
106 };
107 return ret;
108 }
109
110
111 static Mat4 Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
112 {
113 return Mat4(
114 2.0f / (right - left), 0, 0, -(right + left) / (right - left),
115 0, 2.0f / (top - bottom), 0, -(top + bottom) / (top - bottom),
116 0, 0, 1.0f / (zFar - zNear), -zNear / (zFar - zNear),
117 0, 0, 0, 1
118 );
119 }
120 static Mat4 LookAt(const Vec3& eye, const Vec3& center, const Vec3& up)
121 {
122 Vec3 f = (center - eye).normalize(); //z
123 Vec3 s = f.cross(up).normalize(); //x
124 Vec3 u = s.cross(f);
125
126 Mat4 result = {
127 s.x, s.y, s.z, -s.dot(eye),
128 u.x, u.y, u.z, -u.dot(eye),
129 f.x, f.y, f.z, -f.dot(eye),
130 0.0f, 0.0f, 0.0f, 1.0f
131 };
132
133 return result;
134 }
135 static Mat4 Scale(float scaleX, float scaleY)
136 {
137 Mat4 ret = {
138 scaleX, 0, 0, 0,
139 0, scaleY, 0, 0,
140 0, 0, 1, 0,
141 0, 0, 0, 1};
142 return ret;
143 }
144 static Mat4 Scale(float scaleX, float scaleY,float scaleZ)
145 {
146 Mat4 ret = {
147 scaleX, 0, 0, 0,
148 0, scaleY, 0, 0,
149 0, 0, scaleZ, 0,
150 0, 0, 0, 1};
151 return ret;
152 }
153 static Mat4 Perspective(float fovx, float aspect, float zNear, float zFar)
154 {
155 float angle = fovx * 3.14159265f / 180.0f;
156 float h = tanf(angle / 2.0f);
157 return Mat4(
158 1 / h, 0, 0, 0,
159 0, aspect/ h, 0, 0,
160 0, 0, zFar / (zFar - zNear), -zFar * zNear / (zFar - zNear),
161 0, 0, 1 , 0
162 );
163 }
164 static Mat4 RotateX(float degrees)
165 {
166 float radians = degrees * 3.14159265f / 180.0f;
167 float c = cosf(radians);
168 float s = sinf(radians);
169
170 Mat4 ret = {
171 1, 0, 0, 0,
172 0, c, -s, 0,
173 0, s, c, 0,
174 0, 0, 0, 1};
175 return ret;
176 }
177 static Mat4 RotateY(float degrees)
178 {
179 float radians = degrees * 3.14159265f / 180.0f;
180 float c = cosf(radians);
181 float s = sinf(radians);
182
183 Mat4 ret = {
184 c, 0, s, 0,
185 0, 1, 0, 0,
186 -s, 0, c, 0,
187 0, 0, 0, 1};
188 return ret;
189 }
190 static Mat4 RotateZ(float degrees)
191 {
192 float radians = degrees * 3.14159265f / 180.0f;
193 float c = cosf(radians);
194 float s = sinf(radians);
195
196 Mat4 ret = {
197 c, -s, 0, 0,
198 s, c, 0, 0,
199 0, 0, 1, 0,
200 0, 0, 0, 1};
201 return ret;
202 }
203 void rotateY(float degrees)
204 {
205 float radians = degrees * 3.14159265f / 180.0f;
206 float c = cosf(radians);
207 float s = sinf(radians);
208
209 Mat4 rot = {
210 c, 0, s, 0,
211 0, 1, 0, 0,
212 -s, 0, c, 0,
213 0, 0, 0, 1};
214 *this = *this * rot;
215 }
216 void rotateZ(float degrees)
217 {
218 float radians = degrees * 3.14159265f / 180.0f;
219 float c = cosf(radians);
220 float s = sinf(radians);
221
222 Mat4 rot = {
223 c, -s, 0, 0,
224 s, c, 0, 0,
225 0, 0, 1, 0,
226 0, 0, 0, 1};
227 *this = *this * rot;
228 }
229 Vec4 operator*(const Vec4& vec) const
230 {
231 return {
232 m[0] * vec.x + m[1] * vec.y + m[2] * vec.z + m[3] * vec.w, // 第1行
233 m[4] * vec.x + m[5] * vec.y + m[6] * vec.z + m[7] * vec.w, // 第2行
234 m[8] * vec.x + m[9] * vec.y + m[10] * vec.z + m[11] * vec.w, // 第3行
235 m[12] * vec.x + m[13] * vec.y + m[14] * vec.z + m[15] * vec.w // 第4行
236 };
237 }
238 void scale(float x, float y, float z)
239 {
240 Mat4 scl = {
241 x, 0, 0, 0,
242 0, y, 0, 0,
243 0, 0, z, 0,
244 0, 0, 0, 1};
245 *this = *this * scl;
246 }
247
248 Mat4 operator*(const Mat4 &rhs) const
249 {
250 Mat4 result;
251 for (int i = 0; i < 4; ++i)
252 {
253 for (int j = 0; j < 4; ++j)
254 {
255 result.m[i * 4 + j] = 0.0f;
256 for (int k = 0; k < 4; ++k)
257 {
258 result.m[i * 4 + j] += m[i * 4 + k] * rhs.m[k * 4 + j];
259 }
260 }
261 }
262 return result;
263 }
264 Mat4& operator*=(const Mat4 &rhs)
265 {
266 *this = *this * rhs;
267 return *this;
268 }
269 friend std::ostream& operator<<(std::ostream& os, const Mat4& mat);
270 };
271 inline std::ostream& operator<<(std::ostream& os, const Mat4& mat)
272 {
273 os << std::fixed << std::setprecision(4);
274 os << "Matrix4x4:\n";
275 for (int i = 0; i < 4; ++i)
276 {
277 os << "[ ";
278 for (int j = 0; j < 4; ++j)
279 {
280 os << std::setw(10) << mat.m[i * 4 + j];
281 if (j < 3) os << ", ";
282 }
283 os << " ]\n";
284 }
285 return os;
286 }
287
288 class Mat3 : public Mat4
289 {
290
291 };
292} // namespace FCT
void rotateZ(float degrees)
friend std::ostream & operator<<(std::ostream &os, const Mat4 &mat)
static Mat4 CreateTranslation(float x, float y)
static Mat4 Perspective(float fovx, float aspect, float zNear, float zFar)
static Mat4 RotateX(float degrees)
void translate(float x, float y, float z)
Mat4(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
void rotateX(float degrees)
static Mat4 RotateZ(float degrees)
static Mat4 LookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up)
static Mat4 Scale(float scaleX, float scaleY)
static Mat4 Scale(float scaleX, float scaleY, float scaleZ)
void scale(float x, float y, float z)
void translate(float x, float y)
float m[16]
void identity()
static Mat4 Translate(float x, float y, float z)
Mat4 & operator*=(const Mat4 &rhs)
static Mat4 Ortho(float left, float right, float bottom, float top, float zNear, float zFar)
Mat4 operator*(const Mat4 &rhs) const
Vec4 operator*(const Vec4 &vec) const
static Mat4 RotateY(float degrees)
static Mat4 CreateScale(float scaleX, float scaleY)
void rotateY(float degrees)
static Mat4 Translate(float x, float y)
Vec3 normalize(const Vec3 &v)
定义 Vec.h:192
std::ostream & operator<<(std::ostream &os, const Mat4 &mat)
float x
定义 Vec.h:89
float dot(const Vec3 &rhs) const
定义 Vec.h:180
Vec3 cross(const Vec3 &other) const
定义 Vec.h:154
Vec3 normalize() const
定义 Vec.h:149
float z
定义 Vec.h:89
float y
定义 Vec.h:89
float z
定义 Vec.h:212
float y
定义 Vec.h:212
float w
定义 Vec.h:212
float x
定义 Vec.h:212