Pioneer
Loading...
Searching...
No Matches
matrix4x4.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _MATRIX4X4_H
5#define _MATRIX4X4_H
6
7#include "matrix3x3.h"
8#include "vector3.h"
9
10#include <cassert>
11#include <math.h>
12#include <stdio.h>
13#include <cassert>
14#include <type_traits>
15
16template <typename T>
17class matrix4x4 {
18private:
19 T cell[16];
20 using other_float_t = typename std::conditional<std::is_same<T, float>::value, double, float>::type;
21
22public:
24 explicit matrix4x4(T val)
25 {
26 cell[0] = cell[1] = cell[2] = cell[3] = cell[4] = cell[5] = cell[6] =
27 cell[7] = cell[8] = cell[9] = cell[10] = cell[11] = cell[12] = cell[13] =
28 cell[14] = cell[15] = val;
29 }
30 explicit matrix4x4(const T *vals)
31 {
32 memcpy(cell, vals, sizeof(T) * 16);
33 }
35 {
37 }
38 matrix4x4(const matrix3x3<T> &m, const vector3<T> &v)
39 {
41 SetTranslate(v);
42 }
44 {
45 for (int i = 0; i < 16; i++)
46 cell[i] = T(m[i]);
47 }
48
49 void SetTranslate(const vector3<T> &v)
50 {
51 cell[12] = v.x;
52 cell[13] = v.y;
53 cell[14] = v.z;
54 }
55 vector3<T> GetTranslate() const { return vector3<T>(cell[12], cell[13], cell[14]); }
57 {
58 for (int i = 0; i < 12; i++)
59 cell[i] = m.cell[i];
60 }
62 {
64 m[0] = cell[0];
65 m[1] = cell[4];
66 m[2] = cell[8];
67 m[3] = cell[1];
68 m[4] = cell[5];
69 m[5] = cell[9];
70 m[6] = cell[2];
71 m[7] = cell[6];
72 m[8] = cell[10];
73 return m;
74 }
75 // row-major 3x3 matrix
76 void LoadFrom3x3Matrix(const T *r)
77 {
78 cell[0] = r[0];
79 cell[4] = r[1];
80 cell[8] = r[2];
81 cell[12] = 0;
82 cell[1] = r[3];
83 cell[5] = r[4];
84 cell[9] = r[5];
85 cell[13] = 0;
86 cell[2] = r[6];
87 cell[6] = r[7];
88 cell[10] = r[8];
89 cell[14] = 0;
90 cell[3] = 0;
91 cell[7] = 0;
92 cell[11] = 0;
93 cell[15] = 1;
94 }
95 // row-major
96 void SaveTo3x3Matrix(T *r) const
97 {
98 r[0] = cell[0];
99 r[1] = cell[4];
100 r[2] = cell[8];
101 r[3] = cell[1];
102 r[4] = cell[5];
103 r[5] = cell[9];
104 r[6] = cell[2];
105 r[7] = cell[6];
106 r[8] = cell[10];
107 }
108 // matrix4x4 is column-major
109 // but it seems more natural to write the matrix by row
110 template <std::size_t N>
111 static matrix4x4 FromRowMajor(const T (&a)[N])
112 {
113 static_assert(N == 16, "Incorrect number of elements specified");
114 matrix4x4 m;
115 auto &c = m.cell;
116 c[0] = a[0]; c[4] = a[1]; c[8] = a[2]; c[12] = a[3];
117 c[1] = a[4]; c[5] = a[5]; c[9] = a[6]; c[13] = a[7];
118 c[2] = a[8]; c[6] = a[9]; c[10] = a[10]; c[14] = a[11];
119 c[3] = a[12]; c[7] = a[13]; c[11] = a[14]; c[15] = a[15];
120 return m;
121 }
123 {
124 matrix4x4 m = matrix4x4(0.0);
125 m.cell[0] = m.cell[5] = m.cell[10] = m.cell[15] = 1.0f;
126 return m;
127 }
128 //glscale equivalent
129 void Scale(T x, T y, T z)
130 {
131 *this = (*this) * ScaleMatrix(x, y, z);
132 }
133 void Scale(T s)
134 {
135 *this = (*this) * ScaleMatrix(s, s, s);
136 }
137 static matrix4x4 ScaleMatrix(T x, T y, T z)
138 {
139 matrix4x4 m;
140 m[0] = x;
141 m[1] = m[2] = m[3] = 0;
142 m[5] = y;
143 m[4] = m[6] = m[7] = 0;
144 m[10] = z;
145 m[8] = m[9] = m[11] = 0;
146 m[12] = m[13] = m[14] = 0;
147 m[15] = 1;
148 return m;
149 }
150 static matrix4x4 ScaleMatrix(T scale)
151 {
152 matrix4x4 m;
153 m[0] = scale;
154 m[1] = m[2] = m[3] = 0;
155 m[5] = scale;
156 m[4] = m[6] = m[7] = 0;
157 m[10] = scale;
158 m[8] = m[9] = m[11] = 0;
159 m[12] = m[13] = m[14] = 0;
160 m[15] = 1;
161 return m;
162 }
163 static matrix4x4 MakeRotMatrix(const vector3<T> &rx, const vector3<T> &ry, const vector3<T> &rz)
164 {
165 matrix4x4 m;
166 m[0] = rx.x;
167 m[4] = rx.y;
168 m[8] = rx.z;
169 m[12] = 0;
170 m[1] = ry.x;
171 m[5] = ry.y;
172 m[9] = ry.z;
173 m[13] = 0;
174 m[2] = rz.x;
175 m[6] = rz.y;
176 m[10] = rz.z;
177 m[14] = 0;
178 m[3] = 0;
179 m[7] = 0;
180 m[11] = 0;
181 m[15] = 1;
182 return m;
183 }
184 static matrix4x4 MakeInvRotMatrix(const vector3<T> &rx, const vector3<T> &ry, const vector3<T> &rz)
185 {
186 matrix4x4 m;
187 m[0] = rx.x;
188 m[4] = ry.x;
189 m[8] = rz.x;
190 m[12] = 0;
191 m[1] = rx.y;
192 m[5] = ry.y;
193 m[9] = rz.y;
194 m[13] = 0;
195 m[2] = rx.z;
196 m[6] = ry.z;
197 m[10] = rz.z;
198 m[14] = 0;
199 m[3] = 0;
200 m[7] = 0;
201 m[11] = 0;
202 m[15] = 1;
203 return m;
204 }
205
207 // Matrix Construction Functions
208 // NOTE: all matrix functions here are optimized for reverse-Z depth buffers.
209 // Compared to "standard" DirectX or OpenGL matricies they invert the Z value
210 // so it ranges from 1.0 at the near plane to 0.0 at the far plane.
212
213 // Construct a perspective projection matrix based on arbitrary left/right/top/bottom
214 // plane positions.
215 // This method is slower than the others, but supports view frustrums that are not
216 // aligned with the Z-axis. Unless you know what you're doing, you shouldn't use this.
217 //
218 // @param left - the minimum x-value of the view volume at the near plane
219 // @param right - the maximum x-value of the view volume at the near plane
220 // @param bottom - the maximum y-value of the view volume at the near plane
221 // @param top - the maximum y-value of the view volume at the near plane
222 // @param znear - the near clipping plane
223 // @param zfar - the far clipping plane
224 static matrix4x4 FrustumMatrix(T left, T right, T bottom, T top, T znear, T zfar)
225 {
226 assert((znear > T(0)) && (zfar > T(0)));
227 // these expressions come from the documentation for glFrustum
228 const T sx = (T(2) * znear) / (right - left);
229 const T sy = (T(2) * znear) / (top - bottom);
230 const T A = (right + left) / (right - left);
231 const T B = (top + bottom) / (top - bottom);
232 const T C = (zfar) / (zfar - znear) - 1;
233 const T D = (zfar * znear) / (zfar - znear);
234 matrix4x4 m;
235
236 // http://glprogramming.com/red/appendixf.html
237 // OpenGL 'Red Book' on Perspective Projection
238 // Presented here in row-major notation (because that's what matrix4x4f uses internally)
239 T perspective[16] = {
240 sx, 0, 0, 0,
241 0, sy, 0, 0,
242 A, B, C, -1,
243 0, 0, D, 0
244 };
245 return matrix4x4(&perspective[0]);
246 }
247
248 // Construct a perspective projection matrix based field of view and aspect ratio.
249 // This method is the optimized case when you know your screen aspect ratio and
250 // field of view and aren't interested in fancy math. Use this function or
251 // InfinitePerspectiveMatrix if at all possible.
252 //
253 // @param fovR - the camera FOV in radians
254 // @param aspect - the aspect ratio (width / height) of the viewport
255 // @param znear - the near clipping plane
256 // @param zfar - the far clipping plane
257 // @param fovX - whether the field of view is horizontal or vertical (default)
258 static matrix4x4 PerspectiveMatrix(T fovR, T aspect, T znear, T zfar, bool fovX = false)
259 {
260 assert((znear > T(0)) && (zfar > znear));
261
262 const T e = 1 / tan(fovR / T(2));
263 const T x = fovX ? e : e / aspect;
264 const T y = fovX ? e * aspect : e;
265 const T z = (znear) / (zfar - znear);
266 const T w = (zfar * znear) / (zfar - znear);
267
268 // Based on: http://www.terathon.com/gdc07_lengyel.pdf
269 // Unlike gluProject / FrustumMatrix, this projection matrix can only be
270 // symmetric about the Z axis.
271 // This is what you want in 99% of cases, and simplifies the math a good deal.
272 T perspective[16] = {
273 x, 0, 0, 0,
274 0, y, 0, 0,
275 0, 0, z, -1,
276 0, 0, w, 0
277 };
278
279 return matrix4x4(&perspective[0]);
280 }
281
282 // Construct an infinite far-plane perspective projection matrix.
283 // Unless you specifically want to clip objects beyond a specific distance,
284 // this projection will work for any object at any distance.
285 //
286 // @param fovR - the camera FOV in radians
287 // @param aspect - the aspect ratio (width / height) of the viewport
288 // @param znear - the near clipping plane
289 // @param fovX - whether the field of view is horizontal or vertical (default)
290 static matrix4x4 InfinitePerspectiveMatrix(T fovR, T aspect, T znear, bool fovX = false)
291 {
292 assert(znear > T(0));
293
294 const T e = 1 / tan(fovR / T(2));
295 const T x = fovX ? e : e / aspect;
296 const T y = fovX ? e / aspect : e;
297 const T w = znear;
298
299 // Based on: http://dev.theomader.com/depth-precision/
300 // An 'infinite far-plane' projection matrix. There is no concept of a zFar value,
301 // and it can handle everything up to and including homogeneous coordinates with w=0.
302 T perspective[16] = {
303 x, 0, 0, 0,
304 0, y, 0, 0,
305 0, 0, 0, -1,
306 0, 0, w, 0
307 };
308
309 return matrix4x4(&perspective[0]);
310 }
311
313 // set a orthographic frustum with 6 params similar to glOrtho()
314 // (left, right, bottom, top, near, far)
315 //
316 // Derived from:
317 // [1] https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dxmatrixorthorh
318 // [2] https://thxforthefish.com/posts/reverse_z/
319 //
320 // Specifically, the `tz` and `c` terms are based on a right-handed DirectX-style
321 // (Z=0..1) matrix [1], multiplied by the given reversing matrix [2].
322 // If looking at the sources, keep in mind that [1] is presented in row-major order
323 // and [2] is presented in column-major order, and thus multiplication occurs in
324 // column-column fashion.
326 static matrix4x4 OrthoFrustum(T left, T right, T bottom, T top, T znear, T zfar)
327 {
328 assert((znear >= T(-1)) && (zfar >= T(0)));
329 T a = T(2) / (right - left);
330 T b = T(2) / (top - bottom);
331 T c = T(1) / (zfar - znear);
332
333 T tx = (right + left) / (left - right);
334 T ty = (top + bottom) / (bottom - top);
335 T tz = (zfar) / (zfar - znear);
336
337 T ortho[16] = {
338 a, 0, 0, 0,
339 0, b, 0, 0,
340 0, 0, c, 0,
341 tx, ty, tz, 1
342 };
343 matrix4x4 m(&ortho[0]);
344 return m;
345 }
346
347 // Optimized form that takes width/height and near/far planes
348 static matrix4x4 OrthoMatrix(T width, T height, T znear, T zfar)
349 {
350 assert((znear >= T(-1)) && (zfar > T(0)));
351 T a = T(2) / width;
352 T b = T(2) / height;
353 T c = T(1) / (zfar - znear);
354
355 T tz = (zfar) / (zfar - znear);
356
357 T ortho[16] = {
358 a, 0, 0, 0,
359 0, b, 0, 0,
360 0, 0, c, 0,
361 0, 0, tz, 1
362 };
363 matrix4x4 m(&ortho[0]);
364 return m;
365 }
366
367 //glRotate equivalent (except radians instead of degrees)
368 void Rotate(T ang, T x, T y, T z)
369 {
370 *this = (*this) * RotateMatrix(ang, x, y, z);
371 }
372 // (x,y,z) must be normalized
373 static matrix4x4 RotateMatrix(T ang, T x, T y, T z)
374 {
375 matrix4x4 m;
376 T c = cos(ang);
377 T s = sin(ang);
378 m[0] = x * x * (1 - c) + c;
379 m[1] = y * x * (1 - c) + z * s;
380 m[2] = x * z * (1 - c) - y * s;
381 m[3] = 0;
382 m[4] = x * y * (1 - c) - z * s;
383 m[5] = y * y * (1 - c) + c;
384 m[6] = y * z * (1 - c) + x * s;
385 m[7] = 0;
386 m[8] = x * z * (1 - c) + y * s;
387 m[9] = y * z * (1 - c) - x * s;
388 m[10] = z * z * (1 - c) + c;
389 m[11] = 0;
390 m[12] = 0;
391 m[13] = 0;
392 m[14] = 0;
393 m[15] = 1;
394 return m;
395 }
396 void RotateZ(T radians) { *this = (*this) * RotateZMatrix(radians); }
397 void RotateY(T radians) { *this = (*this) * RotateYMatrix(radians); }
398 void RotateX(T radians) { *this = (*this) * RotateXMatrix(radians); }
399 static matrix4x4 RotateXMatrix(T radians)
400 {
401 matrix4x4 m;
402 T cos_r = cosf(float(radians));
403 T sin_r = sinf(float(radians));
404 m[0] = 1.0f;
405 m[1] = 0;
406 m[2] = 0;
407 m[3] = 0;
408
409 m[4] = 0;
410 m[5] = cos_r;
411 m[6] = -sin_r;
412 m[7] = 0;
413
414 m[8] = 0;
415 m[9] = sin_r;
416 m[10] = cos_r;
417 m[11] = 0;
418
419 m[12] = 0;
420 m[13] = 0;
421 m[14] = 0;
422 m[15] = 1.0f;
423 return m;
424 }
425 static matrix4x4 RotateYMatrix(T radians)
426 {
427 matrix4x4 m;
428 T cos_r = cosf(float(radians));
429 T sin_r = sinf(float(radians));
430 m[0] = cos_r;
431 m[1] = 0;
432 m[2] = sin_r;
433 m[3] = 0;
434
435 m[4] = 0;
436 m[5] = 1;
437 m[6] = 0;
438 m[7] = 0;
439
440 m[8] = -sin_r;
441 m[9] = 0;
442 m[10] = cos_r;
443 m[11] = 0;
444
445 m[12] = 0;
446 m[13] = 0;
447 m[14] = 0;
448 m[15] = 1.0f;
449 return m;
450 }
451 static matrix4x4 RotateZMatrix(T radians)
452 {
453 matrix4x4 m;
454 T cos_r = cosf(float(radians));
455 T sin_r = sinf(float(radians));
456 m[0] = cos_r;
457 m[1] = -sin_r;
458 m[2] = 0;
459 m[3] = 0;
460
461 m[4] = sin_r;
462 m[5] = cos_r;
463 m[6] = 0;
464 m[7] = 0;
465
466 m[8] = 0;
467 m[9] = 0;
468 m[10] = 1.0f;
469 m[11] = 0;
470
471 m[12] = 0;
472 m[13] = 0;
473 m[14] = 0;
474 m[15] = 1.0f;
475 return m;
476 }
478 {
479 vector3<T> x(cell[0], cell[4], cell[8]);
480 vector3<T> y(cell[1], cell[5], cell[9]);
481 vector3<T> z(cell[2], cell[6], cell[10]);
482 x = x.Normalized();
483 z = x.Cross(y).Normalized();
484 y = z.Cross(x).Normalized();
485 cell[0] = x.x;
486 cell[4] = x.y;
487 cell[8] = x.z;
488 cell[1] = y.x;
489 cell[5] = y.y;
490 cell[9] = y.z;
491 cell[2] = z.x;
492 cell[6] = z.y;
493 cell[10] = z.z;
494 }
496 {
497 cell[12] = 0;
498 cell[13] = 0;
499 cell[14] = 0;
500 }
501 T &operator[](const size_t i) { return cell[i]; }
502 const T &operator[](const size_t i) const { return cell[i]; }
503 const T *Data() const { return cell; }
504 T *Data() { return cell; }
505 friend matrix4x4 operator+(const matrix4x4 &a, const matrix4x4 &b)
506 {
507 matrix4x4 m;
508 for (int i = 0; i < 16; i++)
509 m.cell[i] = a.cell[i] + b.cell[i];
510 return m;
511 }
512 friend matrix4x4 operator-(const matrix4x4 &a, const matrix4x4 &b)
513 {
514 matrix4x4 m;
515 for (int i = 0; i < 16; i++)
516 m.cell[i] = a.cell[i] - b.cell[i];
517 return m;
518 }
520 {
521 matrix4x4 m;
522 for (int i = 0; i < 16; ++i) {
523 m.cell[i] = -a.cell[i];
524 }
525 return m;
526 }
527 friend matrix4x4 operator*(const matrix4x4 &a, const matrix4x4 &b)
528 {
529 matrix4x4 m;
530 m.cell[0] = a.cell[0] * b.cell[0] + a.cell[4] * b.cell[1] + a.cell[8] * b.cell[2] + a.cell[12] * b.cell[3];
531 m.cell[1] = a.cell[1] * b.cell[0] + a.cell[5] * b.cell[1] + a.cell[9] * b.cell[2] + a.cell[13] * b.cell[3];
532 m.cell[2] = a.cell[2] * b.cell[0] + a.cell[6] * b.cell[1] + a.cell[10] * b.cell[2] + a.cell[14] * b.cell[3];
533 m.cell[3] = a.cell[3] * b.cell[0] + a.cell[7] * b.cell[1] + a.cell[11] * b.cell[2] + a.cell[15] * b.cell[3];
534
535 m.cell[4] = a.cell[0] * b.cell[4] + a.cell[4] * b.cell[5] + a.cell[8] * b.cell[6] + a.cell[12] * b.cell[7];
536 m.cell[5] = a.cell[1] * b.cell[4] + a.cell[5] * b.cell[5] + a.cell[9] * b.cell[6] + a.cell[13] * b.cell[7];
537 m.cell[6] = a.cell[2] * b.cell[4] + a.cell[6] * b.cell[5] + a.cell[10] * b.cell[6] + a.cell[14] * b.cell[7];
538 m.cell[7] = a.cell[3] * b.cell[4] + a.cell[7] * b.cell[5] + a.cell[11] * b.cell[6] + a.cell[15] * b.cell[7];
539
540 m.cell[8] = a.cell[0] * b.cell[8] + a.cell[4] * b.cell[9] + a.cell[8] * b.cell[10] + a.cell[12] * b.cell[11];
541 m.cell[9] = a.cell[1] * b.cell[8] + a.cell[5] * b.cell[9] + a.cell[9] * b.cell[10] + a.cell[13] * b.cell[11];
542 m.cell[10] = a.cell[2] * b.cell[8] + a.cell[6] * b.cell[9] + a.cell[10] * b.cell[10] + a.cell[14] * b.cell[11];
543 m.cell[11] = a.cell[3] * b.cell[8] + a.cell[7] * b.cell[9] + a.cell[11] * b.cell[10] + a.cell[15] * b.cell[11];
544
545 m.cell[12] = a.cell[0] * b.cell[12] + a.cell[4] * b.cell[13] + a.cell[8] * b.cell[14] + a.cell[12] * b.cell[15];
546 m.cell[13] = a.cell[1] * b.cell[12] + a.cell[5] * b.cell[13] + a.cell[9] * b.cell[14] + a.cell[13] * b.cell[15];
547 m.cell[14] = a.cell[2] * b.cell[12] + a.cell[6] * b.cell[13] + a.cell[10] * b.cell[14] + a.cell[14] * b.cell[15];
548 m.cell[15] = a.cell[3] * b.cell[12] + a.cell[7] * b.cell[13] + a.cell[11] * b.cell[14] + a.cell[15] * b.cell[15];
549 return m;
550 }
551 friend vector3<T> operator*(const matrix4x4 &a, const vector3<T> &v)
552 {
553 vector3<T> out;
554 out.x = a.cell[0] * v.x + a.cell[4] * v.y + a.cell[8] * v.z + a.cell[12];
555 out.y = a.cell[1] * v.x + a.cell[5] * v.y + a.cell[9] * v.z + a.cell[13];
556 out.z = a.cell[2] * v.x + a.cell[6] * v.y + a.cell[10] * v.z + a.cell[14];
557 return out;
558 }
559 // scam for doing a transpose operation
560 friend vector3<T> operator*(const vector3<T> &v, const matrix4x4 &a)
561 {
562 vector3<T> out;
563 out.x = a.cell[0] * v.x + a.cell[1] * v.y + a.cell[2] * v.z;
564 out.y = a.cell[4] * v.x + a.cell[5] * v.y + a.cell[6] * v.z;
565 out.z = a.cell[8] * v.x + a.cell[9] * v.y + a.cell[10] * v.z;
566 return out;
567 }
568
569 // Transform a vector by the affine inverse of a matrix4x4
570 // internally this does a transpose operation, and thus only works on
571 // Euclidean (translation + rotation) matricies.
573 {
574 // Formula derivation from songho (https://songho.ca/opengl):
575 //
576 // M = [ R | T ]
577 // [ --+-- ] (R denotes 3x3 rotation/reflection matrix)
578 // [ 0 | 1 ] (T denotes 1x3 translation matrix)
579 //
580 // y = M*x -> y = R*x + T -> x = R^-1*(y - T) -> x = R^T*y - R^T*T
581 // (R is orthogonal, R^-1 = R^T)
582
583 // thanks to https://stackoverflow.com/a/2625420
584 // "Depending on your situation, it may be faster to compute the result of
585 // inv(A) * x instead of actually forming inv(A)..."
586 //
587 // inv(A) * [x] = [ inv(M) * (x - b) ]
588 // [1] = [ 1 ]
589 //
590
591 vector3<T> v = inVec - GetTranslate();
592 vector3<T> out;
593 out.x = cell[0] * v.x + cell[1] * v.y + cell[2] * v.z;
594 out.y = cell[4] * v.x + cell[5] * v.y + cell[6] * v.z;
595 out.z = cell[8] * v.x + cell[9] * v.y + cell[10] * v.z;
596 return out;
597 }
598
599 friend matrix4x4 operator*(const matrix4x4 &a, T v)
600 {
601 matrix4x4 m;
602 for (int i = 0; i < 16; i++)
603 m[i] = a.cell[i] * v;
604 return m;
605 }
606 friend matrix4x4 operator*(T v, const matrix4x4 &a)
607 {
608 return (a * v);
609 }
611 {
612 vector3<T> out;
613 out.x = cell[0] * v.x + cell[4] * v.y + cell[8] * v.z;
614 out.y = cell[1] * v.x + cell[5] * v.y + cell[9] * v.z;
615 out.z = cell[2] * v.x + cell[6] * v.y + cell[10] * v.z;
616 return out;
617 }
618 //gltranslate equivalent
619 void Translate(const vector3<T> &t)
620 {
621 Translate(t.x, t.y, t.z);
622 }
623 void Translate(T x, T y, T z)
624 {
625 matrix4x4 m = Identity();
626 m[12] = x;
627 m[13] = y;
628 m[14] = z;
629 *this = (*this) * m;
630 }
632 {
633 return Translation(v.x, v.y, v.z);
634 }
635 static matrix4x4 Translation(T x, T y, T z)
636 {
637 matrix4x4 m = Identity();
638 m[12] = x;
639 m[13] = y;
640 m[14] = z;
641 return m;
642 }
644 {
645 matrix4x4 m;
646 // this only works for matrices containing only rotation and transform
647 m[0] = cell[0];
648 m[1] = cell[4];
649 m[2] = cell[8];
650 m[4] = cell[1];
651 m[5] = cell[5];
652 m[6] = cell[9];
653 m[8] = cell[2];
654 m[9] = cell[6];
655 m[10] = cell[10];
656 m[12] = -(cell[0] * cell[12] + cell[1] * cell[13] + cell[2] * cell[14]);
657 m[13] = -(cell[4] * cell[12] + cell[5] * cell[13] + cell[6] * cell[14]);
658 m[14] = -(cell[8] * cell[12] + cell[9] * cell[13] + cell[10] * cell[14]);
659 m[3] = m[7] = m[11] = 0;
660 m[15] = 1.0f;
661
662 return m;
663 }
665 {
666 matrix4x4 m;
667 m[0] = cell[0];
668 m[1] = cell[4];
669 m[2] = cell[8];
670 m[3] = cell[12];
671 m[4] = cell[1];
672 m[5] = cell[5];
673 m[6] = cell[9];
674 m[7] = cell[13];
675 m[8] = cell[2];
676 m[9] = cell[6];
677 m[10] = cell[10];
678 m[11] = cell[14];
679 m[12] = cell[3];
680 m[13] = cell[7];
681 m[14] = cell[11];
682 m[15] = cell[15];
683 return m;
684 }
685 void Print() const
686 {
687 for (int i = 0; i < 4; i++) {
688 printf("%.12f %.12f %.12f %.12f\n", cell[i], cell[i + 4], cell[i + 8], cell[i + 12]);
689 }
690 printf("\n");
691 }
692
693 //convenience accessors for getting right/up/back vectors
694 //from rotation matrices
696 {
697 return vector3<T>(cell[0], cell[1], cell[2]);
698 }
699
701 {
702 return vector3<T>(cell[4], cell[5], cell[6]);
703 }
704
706 {
707 return vector3<T>(cell[8], cell[9], cell[10]);
708 }
709};
710
713
714static const matrix4x4f matrix4x4fIdentity(matrix4x4f::Identity());
715static const matrix4x4d matrix4x4dIdentity(matrix4x4d::Identity());
716
717#endif /* _MATRIX4X4_H */
double val
Definition PrecalcPath.cpp:40
Definition matrix3x3.h:13
const T * Data() const
Definition matrix3x3.h:38
Definition matrix4x4.h:17
void Translate(const vector3< T > &t)
Definition matrix4x4.h:619
static matrix4x4 FromRowMajor(const T(&a)[N])
Definition matrix4x4.h:111
static matrix4x4 OrthoFrustum(T left, T right, T bottom, T top, T znear, T zfar)
Definition matrix4x4.h:326
static matrix4x4 Translation(const vector3< T > &v)
Definition matrix4x4.h:631
static matrix4x4 FrustumMatrix(T left, T right, T bottom, T top, T znear, T zfar)
Definition matrix4x4.h:224
matrix4x4 Inverse() const
Definition matrix4x4.h:643
const T * Data() const
Definition matrix4x4.h:503
matrix4x4()
Definition matrix4x4.h:23
static matrix4x4 PerspectiveMatrix(T fovR, T aspect, T znear, T zfar, bool fovX=false)
Definition matrix4x4.h:258
static matrix4x4 ScaleMatrix(T scale)
Definition matrix4x4.h:150
friend vector3< T > operator*(const vector3< T > &v, const matrix4x4 &a)
Definition matrix4x4.h:560
vector3< T > Back() const
Definition matrix4x4.h:705
static matrix4x4 RotateXMatrix(T radians)
Definition matrix4x4.h:399
void SaveTo3x3Matrix(T *r) const
Definition matrix4x4.h:96
matrix4x4 Transpose() const
Definition matrix4x4.h:664
matrix3x3< T > GetOrient() const
Definition matrix4x4.h:61
friend matrix4x4 operator-(const matrix4x4 &a, const matrix4x4 &b)
Definition matrix4x4.h:512
void Rotate(T ang, T x, T y, T z)
Definition matrix4x4.h:368
static matrix4x4 Translation(T x, T y, T z)
Definition matrix4x4.h:635
vector3< T > Right() const
Definition matrix4x4.h:695
void RotateZ(T radians)
Definition matrix4x4.h:396
T & operator[](const size_t i)
Definition matrix4x4.h:501
void RotateX(T radians)
Definition matrix4x4.h:398
vector3< T > Up() const
Definition matrix4x4.h:700
matrix4x4(const matrix3x3< T > &m, const vector3< T > &v)
Definition matrix4x4.h:38
vector3< T > GetTranslate() const
Definition matrix4x4.h:55
void ClearToRotOnly()
Definition matrix4x4.h:495
void Renormalize()
Definition matrix4x4.h:477
void Scale(T s)
Definition matrix4x4.h:133
T * Data()
Definition matrix4x4.h:504
void RotateY(T radians)
Definition matrix4x4.h:397
static matrix4x4 InfinitePerspectiveMatrix(T fovR, T aspect, T znear, bool fovX=false)
Definition matrix4x4.h:290
static matrix4x4 RotateZMatrix(T radians)
Definition matrix4x4.h:451
friend matrix4x4 operator*(const matrix4x4 &a, T v)
Definition matrix4x4.h:599
friend matrix4x4 operator+(const matrix4x4 &a, const matrix4x4 &b)
Definition matrix4x4.h:505
void LoadFrom3x3Matrix(const T *r)
Definition matrix4x4.h:76
void Translate(T x, T y, T z)
Definition matrix4x4.h:623
friend vector3< T > operator*(const matrix4x4 &a, const vector3< T > &v)
Definition matrix4x4.h:551
void SetRotationOnly(const matrix4x4 &m)
Definition matrix4x4.h:56
static matrix4x4 MakeRotMatrix(const vector3< T > &rx, const vector3< T > &ry, const vector3< T > &rz)
Definition matrix4x4.h:163
static matrix4x4 RotateMatrix(T ang, T x, T y, T z)
Definition matrix4x4.h:373
static matrix4x4 ScaleMatrix(T x, T y, T z)
Definition matrix4x4.h:137
matrix4x4(T val)
Definition matrix4x4.h:24
void SetTranslate(const vector3< T > &v)
Definition matrix4x4.h:49
static matrix4x4 Identity()
Definition matrix4x4.h:122
friend matrix4x4 operator-(const matrix4x4 &a)
Definition matrix4x4.h:519
static matrix4x4 OrthoMatrix(T width, T height, T znear, T zfar)
Definition matrix4x4.h:348
static matrix4x4 RotateYMatrix(T radians)
Definition matrix4x4.h:425
void Print() const
Definition matrix4x4.h:685
void Scale(T x, T y, T z)
Definition matrix4x4.h:129
friend matrix4x4 operator*(T v, const matrix4x4 &a)
Definition matrix4x4.h:606
const T & operator[](const size_t i) const
Definition matrix4x4.h:502
matrix4x4(const matrix3x3< T > &m)
Definition matrix4x4.h:34
friend matrix4x4 operator*(const matrix4x4 &a, const matrix4x4 &b)
Definition matrix4x4.h:527
vector3< T > InvTransform(const vector3< T > &inVec)
Definition matrix4x4.h:572
matrix4x4(const matrix4x4< other_float_t > &m)
Definition matrix4x4.h:43
vector3< T > ApplyRotationOnly(const vector3< T > &v) const
Definition matrix4x4.h:610
matrix4x4(const T *vals)
Definition matrix4x4.h:30
static matrix4x4 MakeInvRotMatrix(const vector3< T > &rx, const vector3< T > &ry, const vector3< T > &rz)
Definition matrix4x4.h:184
Definition vector3.h:16
T y
Definition vector3.h:20
T x
Definition vector3.h:20
T z
Definition vector3.h:20
vector3 Normalized() const
Definition vector3.h:136
vector3 Cross(const vector3 &b) const
Definition vector3.h:128
matrix4x4< double > matrix4x4d
Definition matrix4x4.h:712
matrix4x4< float > matrix4x4f
Definition matrix4x4.h:711
Definition msvc_bug.cpp:33