11 #include <type_traits>
17 using other_float_t =
typename std::conditional<std::is_same<T, float>::value, double,
float>::type;
23 cell[0] = cell[1] = cell[2] = cell[3] = cell[4] = cell[5] = cell[6] =
24 cell[7] = cell[8] = cell[9] = cell[10] = cell[11] = cell[12] = cell[13] =
25 cell[14] = cell[15] =
val;
29 memcpy(cell, vals,
sizeof(T) * 16);
42 for (
int i = 0; i < 16; i++)
55 for (
int i = 0; i < 12; i++)
108 m.cell[0] = m.cell[5] = m.cell[10] = m.cell[15] = 1.0f;
124 m[1] = m[2] = m[3] = 0;
126 m[4] = m[6] = m[7] = 0;
128 m[8] = m[9] = m[11] = 0;
129 m[12] = m[13] = m[14] = 0;
137 m[1] = m[2] = m[3] = 0;
139 m[4] = m[6] = m[7] = 0;
141 m[8] = m[9] = m[11] = 0;
142 m[12] = m[13] = m[14] = 0;
209 assert((znear > T(0)) && (zfar > T(0)));
211 const T sx = (T(2) * znear) / (right - left);
212 const T sy = (T(2) * znear) / (top - bottom);
213 const T A = (right + left) / (right - left);
214 const T B = (top + bottom) / (top - bottom);
215 const T
C = (zfar) / (zfar - znear) - 1;
216 const T D = (zfar * znear) / (zfar - znear);
222 T perspective[16] = {
243 assert((znear > T(0)) && (zfar > znear));
245 const T e = 1 / tan(fovR / T(2));
246 const T x = fovX ? e : e / aspect;
247 const T y = fovX ? e / aspect : e;
248 const T z = (znear) / (zfar - znear);
249 const T w = (zfar * znear) / (zfar - znear);
255 T perspective[16] = {
275 assert(znear > T(0));
277 const T e = 1 / tan(fovR / T(2));
278 const T x = fovX ? e : e / aspect;
279 const T y = fovX ? e / aspect : e;
285 T perspective[16] = {
311 assert((znear >= T(-1)) && (zfar > T(0)));
312 T a = T(2) / (right - left);
313 T b = T(2) / (top - bottom);
314 T c = T(1) / (zfar - znear);
316 T tx = (right + left) / (left - right);
317 T ty = (top + bottom) / (bottom - top);
318 T tz = (zfar) / (zfar - znear);
333 assert((znear >= T(-1)) && (zfar > T(0)));
336 T c = T(1) / (zfar - znear);
338 T tz = (zfar) / (zfar - znear);
361 m[0] = x * x * (1 - c) + c;
362 m[1] = y * x * (1 - c) + z * s;
363 m[2] = x * z * (1 - c) - y * s;
365 m[4] = x * y * (1 - c) - z * s;
366 m[5] = y * y * (1 - c) + c;
367 m[6] = y * z * (1 - c) + x * s;
369 m[8] = x * z * (1 - c) + y * s;
370 m[9] = y * z * (1 - c) - x * s;
371 m[10] = z * z * (1 - c) + c;
385 T cos_r = cosf(
float(radians));
386 T sin_r = sinf(
float(radians));
411 T cos_r = cosf(
float(radians));
412 T sin_r = sinf(
float(radians));
437 T cos_r = cosf(
float(radians));
438 T sin_r = sinf(
float(radians));
485 const T &
operator[](
const size_t i)
const {
return cell[i]; }
486 const T *
Data()
const {
return cell; }
491 for (
int i = 0; i < 16; i++)
492 m.cell[i] = a.cell[i] + b.cell[i];
498 for (
int i = 0; i < 16; i++)
499 m.cell[i] = a.cell[i] - b.cell[i];
505 for (
int i = 0; i < 16; ++i) {
506 m.cell[i] = -a.cell[i];
513 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];
514 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];
515 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];
516 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];
518 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];
519 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];
520 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];
521 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];
523 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];
524 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];
525 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];
526 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];
528 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];
529 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];
530 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];
531 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];
537 out.
x = a.cell[0] * v.
x + a.cell[4] * v.
y + a.cell[8] * v.
z + a.cell[12];
538 out.
y = a.cell[1] * v.
x + a.cell[5] * v.
y + a.cell[9] * v.
z + a.cell[13];
539 out.
z = a.cell[2] * v.
x + a.cell[6] * v.
y + a.cell[10] * v.
z + a.cell[14];
546 out.
x = a.cell[0] * v.
x + a.cell[1] * v.
y + a.cell[2] * v.
z;
547 out.
y = a.cell[4] * v.
x + a.cell[5] * v.
y + a.cell[6] * v.
z;
548 out.
z = a.cell[8] * v.
x + a.cell[9] * v.
y + a.cell[10] * v.
z;
576 out.
x = cell[0] * v.
x + cell[1] * v.
y + cell[2] * v.
z;
577 out.
y = cell[4] * v.
x + cell[5] * v.
y + cell[6] * v.
z;
578 out.
z = cell[8] * v.
x + cell[9] * v.
y + cell[10] * v.
z;
585 for (
int i = 0; i < 16; i++)
586 m[i] = a.cell[i] * v;
596 out.
x = cell[0] * v.
x + cell[4] * v.
y + cell[8] * v.
z;
597 out.
y = cell[1] * v.
x + cell[5] * v.
y + cell[9] * v.
z;
598 out.
z = cell[2] * v.
x + cell[6] * v.
y + cell[10] * v.
z;
639 m[12] = -(cell[0] * cell[12] + cell[1] * cell[13] + cell[2] * cell[14]);
640 m[13] = -(cell[4] * cell[12] + cell[5] * cell[13] + cell[6] * cell[14]);
641 m[14] = -(cell[8] * cell[12] + cell[9] * cell[13] + cell[10] * cell[14]);
642 m[3] = m[7] = m[11] = 0;
670 for (
int i = 0; i < 4; i++) {
671 printf(
"%.12f %.12f %.12f %.12f\n", cell[i], cell[i + 4], cell[i + 8], cell[i + 12]);
690 return vector3<T>(cell[2], cell[6], cell[10]);
double val
Definition: PrecalcPath.cpp:40
Definition: matrix3x3.h:13
const T * Data() const
Definition: matrix3x3.h:38
Definition: matrix4x4.h:14
void Translate(const vector3< T > &t)
Definition: matrix4x4.h:602
static matrix4x4 OrthoFrustum(T left, T right, T bottom, T top, T znear, T zfar)
Definition: matrix4x4.h:309
static matrix4x4 Translation(const vector3< T > &v)
Definition: matrix4x4.h:614
static matrix4x4 FrustumMatrix(T left, T right, T bottom, T top, T znear, T zfar)
Definition: matrix4x4.h:207
matrix4x4 Inverse() const
Definition: matrix4x4.h:626
matrix4x4()
Definition: matrix4x4.h:20
static matrix4x4 PerspectiveMatrix(T fovR, T aspect, T znear, T zfar, bool fovX=false)
Definition: matrix4x4.h:241
static matrix4x4 ScaleMatrix(T scale)
Definition: matrix4x4.h:133
const T & operator[](const size_t i) const
Definition: matrix4x4.h:485
T * Data()
Definition: matrix4x4.h:487
friend vector3< T > operator*(const matrix4x4 &a, const vector3< T > &v)
Definition: matrix4x4.h:534
vector3< T > GetTranslate() const
Definition: matrix4x4.h:52
static matrix4x4 RotateXMatrix(T radians)
Definition: matrix4x4.h:382
void SaveTo3x3Matrix(T *r) const
Definition: matrix4x4.h:93
matrix4x4 Transpose() const
Definition: matrix4x4.h:647
vector3< T > Right() const
Definition: matrix4x4.h:678
friend matrix4x4 operator-(const matrix4x4 &a, const matrix4x4 &b)
Definition: matrix4x4.h:495
void Rotate(T ang, T x, T y, T z)
Definition: matrix4x4.h:351
T & operator[](const size_t i)
Definition: matrix4x4.h:484
static matrix4x4 Translation(T x, T y, T z)
Definition: matrix4x4.h:618
vector3< T > InvTransform(const vector3< T > &inVec)
Definition: matrix4x4.h:555
void RotateZ(T radians)
Definition: matrix4x4.h:379
const T * Data() const
Definition: matrix4x4.h:486
void RotateX(T radians)
Definition: matrix4x4.h:381
vector3< T > Up() const
Definition: matrix4x4.h:683
vector3< T > ApplyRotationOnly(const vector3< T > &v) const
Definition: matrix4x4.h:593
matrix4x4(const matrix3x3< T > &m, const vector3< T > &v)
Definition: matrix4x4.h:35
matrix3x3< T > GetOrient() const
Definition: matrix4x4.h:58
void ClearToRotOnly()
Definition: matrix4x4.h:478
void Renormalize()
Definition: matrix4x4.h:460
void Scale(T s)
Definition: matrix4x4.h:116
void RotateY(T radians)
Definition: matrix4x4.h:380
static matrix4x4 InfinitePerspectiveMatrix(T fovR, T aspect, T znear, bool fovX=false)
Definition: matrix4x4.h:273
static matrix4x4 RotateZMatrix(T radians)
Definition: matrix4x4.h:434
friend matrix4x4 operator*(const matrix4x4 &a, T v)
Definition: matrix4x4.h:582
friend matrix4x4 operator+(const matrix4x4 &a, const matrix4x4 &b)
Definition: matrix4x4.h:488
void LoadFrom3x3Matrix(const T *r)
Definition: matrix4x4.h:73
void Translate(T x, T y, T z)
Definition: matrix4x4.h:606
friend vector3< T > operator*(const vector3< T > &v, const matrix4x4 &a)
Definition: matrix4x4.h:543
void SetRotationOnly(const matrix4x4 &m)
Definition: matrix4x4.h:53
static matrix4x4 MakeRotMatrix(const vector3< T > &rx, const vector3< T > &ry, const vector3< T > &rz)
Definition: matrix4x4.h:146
static matrix4x4 RotateMatrix(T ang, T x, T y, T z)
Definition: matrix4x4.h:356
static matrix4x4 ScaleMatrix(T x, T y, T z)
Definition: matrix4x4.h:120
matrix4x4(T val)
Definition: matrix4x4.h:21
void SetTranslate(const vector3< T > &v)
Definition: matrix4x4.h:46
static matrix4x4 Identity()
Definition: matrix4x4.h:105
friend matrix4x4 operator-(const matrix4x4 &a)
Definition: matrix4x4.h:502
static matrix4x4 OrthoMatrix(T width, T height, T znear, T zfar)
Definition: matrix4x4.h:331
static matrix4x4 RotateYMatrix(T radians)
Definition: matrix4x4.h:408
void Print() const
Definition: matrix4x4.h:668
void Scale(T x, T y, T z)
Definition: matrix4x4.h:112
friend matrix4x4 operator*(T v, const matrix4x4 &a)
Definition: matrix4x4.h:589
matrix4x4(const matrix3x3< T > &m)
Definition: matrix4x4.h:31
friend matrix4x4 operator*(const matrix4x4 &a, const matrix4x4 &b)
Definition: matrix4x4.h:510
matrix4x4(const matrix4x4< other_float_t > &m)
Definition: matrix4x4.h:40
matrix4x4(const T *vals)
Definition: matrix4x4.h:27
static matrix4x4 MakeInvRotMatrix(const vector3< T > &rx, const vector3< T > &ry, const vector3< T > &rz)
Definition: matrix4x4.h:167
vector3< T > Back() const
Definition: matrix4x4.h:688
T y
Definition: vector3.h:18
T x
Definition: vector3.h:18
T z
Definition: vector3.h:18
vector3 Normalized() const
Definition: vector3.h:121
vector3 Cross(const vector3 &b) const
Definition: vector3.h:113
matrix4x4< double > matrix4x4d
Definition: matrix4x4.h:695
matrix4x4< float > matrix4x4f
Definition: matrix4x4.h:694
Definition: msvc_bug.cpp:33