CSEngine
Loading...
Searching...
No Matches
Matrix.h
1#pragma once
2
3#include "Vector.h"
4#include "MatrixDef.h"
5
6namespace CSE {
7
8 template <typename T>
9 class Matrix2 {
10 public:
11 // T m_mat[4] = {};
12 vec2 x;
13 vec2 y;
14 public:
15 Matrix2() {
16 MAT2_XX = 1;
17 MAT2_XY = 0;
18 MAT2_YX = 0;
19 MAT2_YY = 1;
20 }
21
22 explicit Matrix2(const T* m) {
23 MAT2_XX = m[0];
24 MAT2_XY = m[1];
25 MAT2_YX = m[2];
26 MAT2_YY = m[3];
27 }
28
29 ~Matrix2() = default;
30 const T* Pointer() const {
31 return &MAT2_XX;
32 }
33 };
34
35 template <typename T>
36 class Matrix3 {
37 public:
38 // T m_mat[9] = {};
39 vec3 x;
40 vec3 y;
41 vec3 z;
42
43 Matrix3() {
44 MAT3_XX = 1;
45 MAT3_XY = 0;
46 MAT3_XZ = 0;
47 MAT3_YX = 0;
48 MAT3_YY = 1;
49 MAT3_YZ = 0;
50 MAT3_ZX = 0;
51 MAT3_ZY = 0;
52 MAT3_ZZ = 1;
53 }
54
55 explicit Matrix3(const T* m) {
56 MAT3_XX = m[0];
57 MAT3_XY = m[1];
58 MAT3_XZ = m[2];
59 MAT3_YX = m[3];
60 MAT3_YY = m[4];
61 MAT3_YZ = m[5];
62 MAT3_ZX = m[6];
63 MAT3_ZY = m[7];
64 MAT3_ZZ = m[8];
65 }
66
67 ~Matrix3() = default;
68
69 Matrix3 Transposed() const {
70 Matrix3 m;
71 m.MAT3_XX = MAT3_XX;
72 m.MAT3_XY = MAT3_YX;
73 m.MAT3_XZ = MAT3_ZX;
74 m.MAT3_YX = MAT3_XY;
75 m.MAT3_YY = MAT3_YY;
76 m.MAT3_YZ = MAT3_ZY;
77 m.MAT3_ZX = MAT3_XZ;
78 m.MAT3_ZY = MAT3_YZ;
79 m.MAT3_ZZ = MAT3_ZZ;
80 return m;
81 }
82
83 Vector3<T> operator*(const Vector3<T>& b) const {
84 Vector3<T> v;
85 v.x = MAT3_XX * b.x + MAT3_XY * b.y + MAT3_XZ * b.z;
86 v.y = MAT3_YX * b.x + MAT3_YY * b.y + MAT3_YZ * b.z;
87 v.z = MAT3_ZX * b.x + MAT3_ZY * b.y + MAT3_ZZ * b.z;
88 return v;
89 }
90
91 const T* Pointer() const {
92 return &MAT3_XX;
93 }
94 };
95
96 template <typename T>
97 class Matrix4 {
98 public:
99 // T m_mat[16] = {};
100 vec4 x;
101 vec4 y;
102 vec4 z;
103 vec4 w;
104
105 Matrix4() {
106 MAT4_XX = 1;
107 MAT4_XY = 0;
108 MAT4_XZ = 0;
109 MAT4_XW = 0;
110 MAT4_YX = 0;
111 MAT4_YY = 1;
112 MAT4_YZ = 0;
113 MAT4_YW = 0;
114 MAT4_ZX = 0;
115 MAT4_ZY = 0;
116 MAT4_ZZ = 1;
117 MAT4_ZW = 0;
118 MAT4_WX = 0;
119 MAT4_WY = 0;
120 MAT4_WZ = 0;
121 MAT4_WW = 1;
122 }
123
124 explicit Matrix4(const Matrix3<T>& m) {
125 MAT4_XX = m.MAT4_XX;
126 MAT4_XY = m.MAT4_XY;
127 MAT4_XZ = m.MAT4_XZ;
128 MAT4_XW = 0;
129 MAT4_YX = m.MAT4_YX;
130 MAT4_YY = m.MAT4_YY;
131 MAT4_YZ = m.MAT4_YZ;
132 MAT4_YW = 0;
133 MAT4_ZX = m.MAT4_ZX;
134 MAT4_ZY = m.MAT4_ZY;
135 MAT4_ZZ = m.MAT4_ZZ;
136 MAT4_ZW = 0;
137 MAT4_WX = 0;
138 MAT4_WY = 0;
139 MAT4_WZ = 0;
140 MAT4_WW = 1;
141 }
142
143 explicit Matrix4(const T* m) {
144 MAT4_XX = m[0];
145 MAT4_XY = m[1];
146 MAT4_XZ = m[2];
147 MAT4_XW = m[3];
148 MAT4_YX = m[4];
149 MAT4_YY = m[5];
150 MAT4_YZ = m[6];
151 MAT4_YW = m[7];
152 MAT4_ZX = m[8];
153 MAT4_ZY = m[9];
154 MAT4_ZZ = m[10];
155 MAT4_ZW = m[11];
156 MAT4_WX = m[12];
157 MAT4_WY = m[13];
158 MAT4_WZ = m[14];
159 MAT4_WW = m[15];
160 }
161
162 ~Matrix4() = default;
163
164 Matrix4 operator*(const Matrix4& b) const {
165 Matrix4 m;
166 m.MAT4_XX = MAT4_XX * b.MAT4_XX + MAT4_XY * b.MAT4_YX + MAT4_XZ * b.MAT4_ZX + MAT4_XW * b.MAT4_WX;
167 m.MAT4_XY = MAT4_XX * b.MAT4_XY + MAT4_XY * b.MAT4_YY + MAT4_XZ * b.MAT4_ZY + MAT4_XW * b.MAT4_WY;
168 m.MAT4_XZ = MAT4_XX * b.MAT4_XZ + MAT4_XY * b.MAT4_YZ + MAT4_XZ * b.MAT4_ZZ + MAT4_XW * b.MAT4_WZ;
169 m.MAT4_XW = MAT4_XX * b.MAT4_XW + MAT4_XY * b.MAT4_YW + MAT4_XZ * b.MAT4_ZW + MAT4_XW * b.MAT4_WW;
170 m.MAT4_YX = MAT4_YX * b.MAT4_XX + MAT4_YY * b.MAT4_YX + MAT4_YZ * b.MAT4_ZX + MAT4_YW * b.MAT4_WX;
171 m.MAT4_YY = MAT4_YX * b.MAT4_XY + MAT4_YY * b.MAT4_YY + MAT4_YZ * b.MAT4_ZY + MAT4_YW * b.MAT4_WY;
172 m.MAT4_YZ = MAT4_YX * b.MAT4_XZ + MAT4_YY * b.MAT4_YZ + MAT4_YZ * b.MAT4_ZZ + MAT4_YW * b.MAT4_WZ;
173 m.MAT4_YW = MAT4_YX * b.MAT4_XW + MAT4_YY * b.MAT4_YW + MAT4_YZ * b.MAT4_ZW + MAT4_YW * b.MAT4_WW;
174 m.MAT4_ZX = MAT4_ZX * b.MAT4_XX + MAT4_ZY * b.MAT4_YX + MAT4_ZZ * b.MAT4_ZX + MAT4_ZW * b.MAT4_WX;
175 m.MAT4_ZY = MAT4_ZX * b.MAT4_XY + MAT4_ZY * b.MAT4_YY + MAT4_ZZ * b.MAT4_ZY + MAT4_ZW * b.MAT4_WY;
176 m.MAT4_ZZ = MAT4_ZX * b.MAT4_XZ + MAT4_ZY * b.MAT4_YZ + MAT4_ZZ * b.MAT4_ZZ + MAT4_ZW * b.MAT4_WZ;
177 m.MAT4_ZW = MAT4_ZX * b.MAT4_XW + MAT4_ZY * b.MAT4_YW + MAT4_ZZ * b.MAT4_ZW + MAT4_ZW * b.MAT4_WW;
178 m.MAT4_WX = MAT4_WX * b.MAT4_XX + MAT4_WY * b.MAT4_YX + MAT4_WZ * b.MAT4_ZX + MAT4_WW * b.MAT4_WX;
179 m.MAT4_WY = MAT4_WX * b.MAT4_XY + MAT4_WY * b.MAT4_YY + MAT4_WZ * b.MAT4_ZY + MAT4_WW * b.MAT4_WY;
180 m.MAT4_WZ = MAT4_WX * b.MAT4_XZ + MAT4_WY * b.MAT4_YZ + MAT4_WZ * b.MAT4_ZZ + MAT4_WW * b.MAT4_WZ;
181 m.MAT4_WW = MAT4_WX * b.MAT4_XW + MAT4_WY * b.MAT4_YW + MAT4_WZ * b.MAT4_ZW + MAT4_WW * b.MAT4_WW;
182 return m;
183 }
184
185 Vector4<T> operator*(const Vector4<T>& b) const {
186 Vector4<T> v;
187 v.x = MAT4_XX * b.x + MAT4_XY * b.y + MAT4_XZ * b.z + MAT4_XW * b.w;
188 v.y = MAT4_YX * b.x + MAT4_YY * b.y + MAT4_YZ * b.z + MAT4_YW * b.w;
189 v.z = MAT4_ZX * b.x + MAT4_ZY * b.y + MAT4_ZZ * b.z + MAT4_ZW * b.w;
190 v.w = MAT4_WX * b.x + MAT4_WY * b.y + MAT4_WZ * b.z + MAT4_WW * b.w;
191 return v;
192 }
193
194 Matrix4& operator*=(const Matrix4& b) {
195 Matrix4 m = *this * b;
196 return (*this = m);
197 }
198
199 Matrix4 Transposed() const {
200 Matrix4 m;
201 m.MAT4_XX = MAT4_XX;
202 m.MAT4_XY = MAT4_YX;
203 m.MAT4_XZ = MAT4_ZX;
204 m.MAT4_XW = MAT4_WX;
205 m.MAT4_YX = MAT4_XY;
206 m.MAT4_YY = MAT4_YY;
207 m.MAT4_YZ = MAT4_ZY;
208 m.MAT4_YW = MAT4_WY;
209 m.MAT4_ZX = MAT4_XZ;
210 m.MAT4_ZY = MAT4_YZ;
211 m.MAT4_ZZ = MAT4_ZZ;
212 m.MAT4_ZW = MAT4_WZ;
213 m.MAT4_WX = MAT4_XW;
214 m.MAT4_WY = MAT4_YW;
215 m.MAT4_WZ = MAT4_ZW;
216 m.MAT4_WW = MAT4_WW;
217 return m;
218 }
219
220 Matrix3<T> ToMat3() const {
221 Matrix3<T> m;
222 m.MAT3_XX = MAT4_XX;
223 m.MAT3_YX = MAT4_YX;
224 m.MAT3_ZX = MAT4_ZX;
225 m.MAT3_XY = MAT4_XY;
226 m.MAT3_YY = MAT4_YY;
227 m.MAT3_ZY = MAT4_ZY;
228 m.MAT3_XZ = MAT4_XZ;
229 m.MAT3_YZ = MAT4_YZ;
230 m.MAT3_ZZ = MAT4_ZZ;
231 return m;
232 }
233
234 const T* Pointer() const {
235 return &MAT4_XX;
236 }
237
238 static Matrix4<T> Identity() {
239 return Matrix4();
240 }
241
242 static Matrix4<T> Translate(T x, T y, T z) {
243 Matrix4 m;
244 m.MAT4_XX = 1;
245 m.MAT4_XY = 0;
246 m.MAT4_XZ = 0;
247 m.MAT4_XW = 0;
248 m.MAT4_YX = 0;
249 m.MAT4_YY = 1;
250 m.MAT4_YZ = 0;
251 m.MAT4_YW = 0;
252 m.MAT4_ZX = 0;
253 m.MAT4_ZY = 0;
254 m.MAT4_ZZ = 1;
255 m.MAT4_ZW = 0;
256 m.MAT4_WX = x;
257 m.MAT4_WY = y;
258 m.MAT4_WZ = z;
259 m.MAT4_WW = 1;
260 return m;
261 }
262
263 static Matrix4<T> Scale(T s) {
264 Matrix4 m;
265 m.MAT4_XX = s;
266 m.MAT4_XY = 0;
267 m.MAT4_XZ = 0;
268 m.MAT4_XW = 0;
269 m.MAT4_YX = 0;
270 m.MAT4_YY = s;
271 m.MAT4_YZ = 0;
272 m.MAT4_YW = 0;
273 m.MAT4_ZX = 0;
274 m.MAT4_ZY = 0;
275 m.MAT4_ZZ = s;
276 m.MAT4_ZW = 0;
277 m.MAT4_WX = 0;
278 m.MAT4_WY = 0;
279 m.MAT4_WZ = 0;
280 m.MAT4_WW = 1;
281 return m;
282 }
283
284 static Matrix4<T> Scale(T x, T y, T z) {
285 Matrix4 m;
286 m.MAT4_XX = x;
287 m.MAT4_XY = 0;
288 m.MAT4_XZ = 0;
289 m.MAT4_XW = 0;
290 m.MAT4_YX = 0;
291 m.MAT4_YY = y;
292 m.MAT4_YZ = 0;
293 m.MAT4_YW = 0;
294 m.MAT4_ZX = 0;
295 m.MAT4_ZY = 0;
296 m.MAT4_ZZ = z;
297 m.MAT4_ZW = 0;
298 m.MAT4_WX = 0;
299 m.MAT4_WY = 0;
300 m.MAT4_WZ = 0;
301 m.MAT4_WW = 1;
302 return m;
303 }
304
305 static Matrix4<T> RotateZ(T degrees) {
306 T radians = degrees * 3.14159f / 180.0f;
307 T s = std::sin(radians);
308 T c = std::cos(radians);
309
310 Matrix4 m;
311 m.MAT4_XX = c;
312 m.MAT4_XY = -s;
313 m.MAT4_XZ = 0;
314 m.MAT4_XW = 0;
315 m.MAT4_YX = s;
316 m.MAT4_YY = c;
317 m.MAT4_YZ = 0;
318 m.MAT4_YW = 0;
319 m.MAT4_ZX = 0;
320 m.MAT4_ZY = 0;
321 m.MAT4_ZZ = 1;
322 m.MAT4_ZW = 0;
323 m.MAT4_WX = 0;
324 m.MAT4_WY = 0;
325 m.MAT4_WZ = 0;
326 m.MAT4_WW = 1;
327 return m;
328 }
329
330
331 static Matrix4<T> RotateY(T degrees) {
332 T radians = degrees * 3.14159f / 180.0f;
333 T s = std::sin(radians);
334 T c = std::cos(radians);
335
336 Matrix4 m;
337 m.MAT4_XX = c;
338 m.MAT4_XY = 0;
339 m.MAT4_XZ = s;
340 m.MAT4_XW = 0;
341 m.MAT4_YX = 0;
342 m.MAT4_YY = 1;
343 m.MAT4_YZ = 0;
344 m.MAT4_YW = 0;
345 m.MAT4_ZX = -s;
346 m.MAT4_ZY = 0;
347 m.MAT4_ZZ = c;
348 m.MAT4_ZW = 0;
349 m.MAT4_WX = 0;
350 m.MAT4_WY = 0;
351 m.MAT4_WZ = 0;
352 m.MAT4_WW = 1;
353 return m;
354 }
355
356 static Matrix4<T> RotateX(T degrees) {
357 T radians = degrees * 3.14159f / 180.0f;
358 T s = std::sin(radians);
359 T c = std::cos(radians);
360
361 Matrix4 m;
362 m.MAT4_XX = 1;
363 m.MAT4_XY = 0;
364 m.MAT4_XZ = 0;
365 m.MAT4_XW = 0;
366 m.MAT4_YX = 0;
367 m.MAT4_YY = c;
368 m.MAT4_YZ = -s;
369 m.MAT4_YW = 0;
370 m.MAT4_ZX = 0;
371 m.MAT4_ZY = s;
372 m.MAT4_ZZ = c;
373 m.MAT4_ZW = 0;
374 m.MAT4_WX = 0;
375 m.MAT4_WY = 0;
376 m.MAT4_WZ = 0;
377 m.MAT4_WW = 1;
378 return m;
379 }
380
381 static Matrix4<T> Invert(Matrix4<T> m) {
382 Matrix4<T> inv;
383 T det;
384
385 inv.MAT4_XX = m.MAT4_YY * m.MAT4_ZZ * m.MAT4_WW -
386 m.MAT4_YY * m.MAT4_ZW * m.MAT4_WZ -
387 m.MAT4_ZY * m.MAT4_YZ * m.MAT4_WW +
388 m.MAT4_ZY * m.MAT4_YW * m.MAT4_WZ +
389 m.MAT4_WY * m.MAT4_YZ * m.MAT4_ZW -
390 m.MAT4_WY * m.MAT4_YW * m.MAT4_ZZ;
391
392 inv.MAT4_YX = -m.MAT4_YX * m.MAT4_ZZ * m.MAT4_WW +
393 m.MAT4_YX * m.MAT4_ZW * m.MAT4_WZ +
394 m.MAT4_ZX * m.MAT4_YZ * m.MAT4_WW -
395 m.MAT4_ZX * m.MAT4_YW * m.MAT4_WZ -
396 m.MAT4_WX * m.MAT4_YZ * m.MAT4_ZW +
397 m.MAT4_WX * m.MAT4_YW * m.MAT4_ZZ;
398
399 inv.MAT4_ZX = m.MAT4_YX * m.MAT4_ZY * m.MAT4_WW -
400 m.MAT4_YX * m.MAT4_ZW * m.MAT4_WY -
401 m.MAT4_ZX * m.MAT4_YY * m.MAT4_WW +
402 m.MAT4_ZX * m.MAT4_YW * m.MAT4_WY +
403 m.MAT4_WX * m.MAT4_YY * m.MAT4_ZW -
404 m.MAT4_WX * m.MAT4_YW * m.MAT4_ZY;
405
406 inv.MAT4_WX = -m.MAT4_YX * m.MAT4_ZY * m.MAT4_WZ +
407 m.MAT4_YX * m.MAT4_ZZ * m.MAT4_WY +
408 m.MAT4_ZX * m.MAT4_YY * m.MAT4_WZ -
409 m.MAT4_ZX * m.MAT4_YZ * m.MAT4_WY -
410 m.MAT4_WX * m.MAT4_YY * m.MAT4_ZZ +
411 m.MAT4_WX * m.MAT4_YZ * m.MAT4_ZY;
412
413 inv.MAT4_XY = -m.MAT4_XY * m.MAT4_ZZ * m.MAT4_WW +
414 m.MAT4_XY * m.MAT4_ZW * m.MAT4_WZ +
415 m.MAT4_ZY * m.MAT4_XZ * m.MAT4_WW -
416 m.MAT4_ZY * m.MAT4_XW * m.MAT4_WZ -
417 m.MAT4_WY * m.MAT4_XZ * m.MAT4_ZW +
418 m.MAT4_WY * m.MAT4_XW * m.MAT4_ZZ;
419
420 inv.MAT4_YY = m.MAT4_XX * m.MAT4_ZZ * m.MAT4_WW -
421 m.MAT4_XX * m.MAT4_ZW * m.MAT4_WZ -
422 m.MAT4_ZX * m.MAT4_XZ * m.MAT4_WW +
423 m.MAT4_ZX * m.MAT4_XW * m.MAT4_WZ +
424 m.MAT4_WX * m.MAT4_XZ * m.MAT4_ZW -
425 m.MAT4_WX * m.MAT4_XW * m.MAT4_ZZ;
426
427 inv.MAT4_ZY = -m.MAT4_XX * m.MAT4_ZY * m.MAT4_WW +
428 m.MAT4_XX * m.MAT4_ZW * m.MAT4_WY +
429 m.MAT4_ZX * m.MAT4_XY * m.MAT4_WW -
430 m.MAT4_ZX * m.MAT4_XW * m.MAT4_WY -
431 m.MAT4_WX * m.MAT4_XY * m.MAT4_ZW +
432 m.MAT4_WX * m.MAT4_XW * m.MAT4_ZY;
433
434 inv.MAT4_WY = m.MAT4_XX * m.MAT4_ZY * m.MAT4_WZ -
435 m.MAT4_XX * m.MAT4_ZZ * m.MAT4_WY -
436 m.MAT4_ZX * m.MAT4_XY * m.MAT4_WZ +
437 m.MAT4_ZX * m.MAT4_XZ * m.MAT4_WY +
438 m.MAT4_WX * m.MAT4_XY * m.MAT4_ZZ -
439 m.MAT4_WX * m.MAT4_XZ * m.MAT4_ZY;
440
441 inv.MAT4_XZ = m.MAT4_XY * m.MAT4_YZ * m.MAT4_WW -
442 m.MAT4_XY * m.MAT4_YW * m.MAT4_WZ -
443 m.MAT4_YY * m.MAT4_XZ * m.MAT4_WW +
444 m.MAT4_YY * m.MAT4_XW * m.MAT4_WZ +
445 m.MAT4_WY * m.MAT4_XZ * m.MAT4_YW -
446 m.MAT4_WY * m.MAT4_XW * m.MAT4_YZ;
447
448 inv.MAT4_YZ = -m.MAT4_XX * m.MAT4_YZ * m.MAT4_WW +
449 m.MAT4_XX * m.MAT4_YW * m.MAT4_WZ +
450 m.MAT4_YX * m.MAT4_XZ * m.MAT4_WW -
451 m.MAT4_YX * m.MAT4_XW * m.MAT4_WZ -
452 m.MAT4_WX * m.MAT4_XZ * m.MAT4_YW +
453 m.MAT4_WX * m.MAT4_XW * m.MAT4_YZ;
454
455 inv.MAT4_ZZ = m.MAT4_XX * m.MAT4_YY * m.MAT4_WW -
456 m.MAT4_XX * m.MAT4_YW * m.MAT4_WY -
457 m.MAT4_YX * m.MAT4_XY * m.MAT4_WW +
458 m.MAT4_YX * m.MAT4_XW * m.MAT4_WY +
459 m.MAT4_WX * m.MAT4_XY * m.MAT4_YW -
460 m.MAT4_WX * m.MAT4_XW * m.MAT4_YY;
461
462 inv.MAT4_WZ = -m.MAT4_XX * m.MAT4_YY * m.MAT4_WZ +
463 m.MAT4_XX * m.MAT4_YZ * m.MAT4_WY +
464 m.MAT4_YX * m.MAT4_XY * m.MAT4_WZ -
465 m.MAT4_YX * m.MAT4_XZ * m.MAT4_WY -
466 m.MAT4_WX * m.MAT4_XY * m.MAT4_YZ +
467 m.MAT4_WX * m.MAT4_XZ * m.MAT4_YY;
468
469 inv.MAT4_XW = -m.MAT4_XY * m.MAT4_YZ * m.MAT4_ZW +
470 m.MAT4_XY * m.MAT4_YW * m.MAT4_ZZ +
471 m.MAT4_YY * m.MAT4_XZ * m.MAT4_ZW -
472 m.MAT4_YY * m.MAT4_XW * m.MAT4_ZZ -
473 m.MAT4_ZY * m.MAT4_XZ * m.MAT4_YW +
474 m.MAT4_ZY * m.MAT4_XW * m.MAT4_YZ;
475
476 inv.MAT4_YW = m.MAT4_XX * m.MAT4_YZ * m.MAT4_ZW -
477 m.MAT4_XX * m.MAT4_YW * m.MAT4_ZZ -
478 m.MAT4_YX * m.MAT4_XZ * m.MAT4_ZW +
479 m.MAT4_YX * m.MAT4_XW * m.MAT4_ZZ +
480 m.MAT4_ZX * m.MAT4_XZ * m.MAT4_YW -
481 m.MAT4_ZX * m.MAT4_XW * m.MAT4_YZ;
482
483 inv.MAT4_ZW = -m.MAT4_XX * m.MAT4_YY * m.MAT4_ZW +
484 m.MAT4_XX * m.MAT4_YW * m.MAT4_ZY +
485 m.MAT4_YX * m.MAT4_XY * m.MAT4_ZW -
486 m.MAT4_YX * m.MAT4_XW * m.MAT4_ZY -
487 m.MAT4_ZX * m.MAT4_XY * m.MAT4_YW +
488 m.MAT4_ZX * m.MAT4_XW * m.MAT4_YY;
489
490 inv.MAT4_WW = m.MAT4_XX * m.MAT4_YY * m.MAT4_ZZ -
491 m.MAT4_XX * m.MAT4_YZ * m.MAT4_ZY -
492 m.MAT4_YX * m.MAT4_XY * m.MAT4_ZZ +
493 m.MAT4_YX * m.MAT4_XZ * m.MAT4_ZY +
494 m.MAT4_ZX * m.MAT4_XY * m.MAT4_YZ -
495 m.MAT4_ZX * m.MAT4_XZ * m.MAT4_YY;
496
497 det = m.MAT4_XX * inv.MAT4_XX + m.MAT4_XY * inv.MAT4_YX + m.MAT4_XZ * inv.MAT4_ZX + m.MAT4_XW * inv.MAT4_WX;
498
499 if (det == 0)
500 return Identity();
501
502 det = 1.0 / det;
503
504 Matrix4<T> invOut;
505
506 invOut.MAT4_XX = inv.MAT4_XX * det;
507 invOut.MAT4_XY = inv.MAT4_XY * det;
508 invOut.MAT4_XZ = inv.MAT4_XZ * det;
509 invOut.MAT4_XW = inv.MAT4_XW * det;
510
511 invOut.MAT4_YX = inv.MAT4_YX * det;
512 invOut.MAT4_YY = inv.MAT4_YY * det;
513 invOut.MAT4_YZ = inv.MAT4_YZ * det;
514 invOut.MAT4_YW = inv.MAT4_YW * det;
515
516 invOut.MAT4_ZX = inv.MAT4_ZX * det;
517 invOut.MAT4_ZY = inv.MAT4_ZY * det;
518 invOut.MAT4_ZZ = inv.MAT4_ZZ * det;
519 invOut.MAT4_ZW = inv.MAT4_ZW * det;
520
521 invOut.MAT4_WX = inv.MAT4_WX * det;
522 invOut.MAT4_WY = inv.MAT4_WY * det;
523 invOut.MAT4_WZ = inv.MAT4_WZ * det;
524 invOut.MAT4_WW = inv.MAT4_WW * det;
525
526 return invOut;
527 }
528
529 static Matrix4<T> Frustum(T tLeft, T tRight, T tBottom, T tTop, T tNear, T tFar) {
530 T a = 2 * tNear / (tRight - tLeft);
531 T b = 2 * tNear / (tTop - tBottom);
532 T c = (tRight + tLeft) / (tRight - tLeft);
533 T d = (tTop + tBottom) / (tTop - tBottom);
534 T e = -(tFar + tNear) / (tFar - tNear);
535 T f = -2 * tFar * tNear / (tFar - tNear);
536 Matrix4 m;
537 m.MAT4_XX = a;
538 m.MAT4_XY = 0;
539 m.MAT4_XZ = 0;
540 m.MAT4_XW = 0;
541 m.MAT4_YX = 0;
542 m.MAT4_YY = b;
543 m.MAT4_YZ = 0;
544 m.MAT4_YW = 0;
545 m.MAT4_ZX = c;
546 m.MAT4_ZY = d;
547 m.MAT4_ZZ = e;
548 m.MAT4_ZW = -1;
549 m.MAT4_WX = 0;
550 m.MAT4_WY = 0;
551 m.MAT4_WZ = f;
552 m.MAT4_WW = 1;
553 return m;
554 }
555
556 static Matrix4<T> Perspective(T fovy, T aspect, T tNear, T tFar) {
557 Matrix4 m;
558 // radians = (fovy / 2 * Pi / 180)
559 T radians = (T) (fovy * 0.008726644f);
560 T deltaZ = (T) (tFar - tNear);
561 // ref : https://twitter.com/SebAaltonen/status/1571792776802570240?s=20&t=XOYhRlj4BYe_1j8YP3LJJQ
562 T h = 1.0f / tan(radians);
563 T w = h / aspect;
564 T a = -tNear / deltaZ;
565 T b = (tNear * tFar) / deltaZ;
566
567 m.MAT4_XX = w;
568 m.MAT4_YY = h;
569 m.MAT4_ZZ = -a;
570 m.MAT4_ZW = -1.0f;
571 m.MAT4_WZ = -b;
572 m.MAT4_WW = 0.0f;
573
574 return m;
575 }
576
577 static Matrix4<T> Ortho(T xMin, T xMax, T yMin, T yMax, T zMin, T zMax) {
578 Matrix4 m;
579
580 T xDiff = xMax - xMin;
581 T yDiff = yMax - yMin;
582 T zDiff = zMax - zMin;
583
584 m.MAT4_XX = 2.0f / xDiff;
585 m.MAT4_YY = 2.0f / yDiff;
586 m.MAT4_ZZ = -2.0f / zDiff;
587
588 m.MAT4_WX = -(xMax + xMin) / xDiff;
589 m.MAT4_WY = -(yMax + yMin) / yDiff;
590 m.MAT4_WZ = -(zMax + zMin) / zDiff;
591 m.MAT4_WW = 1.0f;
592
593 return m;
594 }
595
596 static Matrix4<T> LookAt(const Vector3<T>& eye,
597 const Vector3<T>& target,
598 const Vector3<T>& up) {
599 Vector3<T> z = (eye - target).Normalized();
600 Vector3<T> x = up.Cross(z).Normalized();
601 Vector3<T> y = z.Cross(x).Normalized();
602
603 Matrix4<T> m;
604
605 m.MAT4_XX = x.x;
606 m.MAT4_XY = x.y;
607 m.MAT4_XZ = x.z;
608 m.MAT4_XW = 0;
609 m.MAT4_YX = y.x;
610 m.MAT4_YY = y.y;
611 m.MAT4_YZ = y.z;
612 m.MAT4_YW = 0;
613 m.MAT4_ZX = z.x;
614 m.MAT4_ZY = z.y;
615 m.MAT4_ZZ = z.z;
616 m.MAT4_ZW = 0;
617 m.MAT4_WX = 0;
618 m.MAT4_WY = 0;
619 m.MAT4_WZ = 0;
620 m.MAT4_WW = 1;
621
622 Vector4<T> eyePrime = m * Vector4<T>(-eye, 1);
623 m = m.Transposed();
624 m.w = eyePrime;
625 return m;
626 }
627 };
628
629 typedef Matrix2<float> mat2;
630 typedef Matrix3<float> mat3;
631 typedef Matrix4<float> mat4;
632}