43 template<
typename T>
struct Vec2;
45 template<
typename T>
struct Vec3;
47 template<
typename T>
struct Vec4;
49 template<
typename T>
struct Mat4;
87 template<
typename T>
struct Mat4
90 static_assert(std::is_floating_point<T>::value,
"The template parameter T of class Mat4<T> must be a floating point number.");
94 #if (MTOOLS_TGX_EXTENSIONS)
95 #include <mtools/extensions/tgx/tgx_ext_Mat4.inl>
126 constexpr Mat4(T x1, T y1, T z1, T t1,
127 T x2, T y2, T z2, T t2,
128 T x3, T y3, T z3, T t3,
129 T x4, T y4, T z4, T t4) :
M{ x1,x2,x3,x4, y1,y2,y3,y4, z1,z2,z3,z4, t1,t2,t3,t4 }
137 Mat4(
const T* mat) { memcpy(
M, mat, 16 *
sizeof(T)); }
155 operator T*() {
return M; }
160 operator const T*()
const {
return M; }
168 memset(
M, 0, 16 *
sizeof(T));
177 memset(
M, 0, 16 *
sizeof(T));
178 M[0] =
M[5] =
M[10] =
M[15] = ((T)1);
191 void setOrtho(T left, T right, T bottom, T top, T zNear, T zFar)
193 memset(
M, 0, 16 *
sizeof(T));
194 M[0] = ((T)2) / (right - left);
195 M[5] = ((T)2) / (top - bottom);
196 M[10] = -((T)2) / (zFar - zNear);
197 M[12] = (right + left) / (left - right);
198 M[13] = (top + bottom) / (bottom - top);
199 M[14] = (zFar + zNear) / (zNear - zFar);
213 void setFrustum(T left, T right, T bottom, T top, T zNear, T zFar)
215 memset(
M, 0, 16 *
sizeof(T));
216 M[0] = (((T)2) * zNear) / (right - left);
217 M[5] = (((T)2) * zNear) / (top - bottom);
218 M[8] = (right + left) / (right - left);
219 M[9] = (top + bottom) / (top - bottom);
220 M[10] = (zFar + zNear) / (zNear - zFar);
222 M[14] = (((T)2) * zFar * zNear) / (zNear - zFar);
238 static const T deg2rad = (T)(
M_PI / 180);
239 const T angle = (fovy / ((T)2)) * deg2rad;
241 if constexpr (std::is_same<T, float>::value)
242 aux = (T)tanf((
float)angle);
244 aux = (T)tan((
double)angle);
245 const T top = zNear * aux;
246 const T bottom = -top;
247 const T right = zNear * aspect * aux;
248 const T left = -right;
249 setFrustum(left, right, bottom, top, zNear, zFar);
263 static const T deg2rad = (T)(
M_PI / 180);
266 const T nx = x / norm;
267 const T ny = y / norm;
268 const T nz = z / norm;
269 const T angle_rad = deg2rad * angle;
271 if constexpr (std::is_same<T, float>::value)
273 c = (T)cosf((
float)angle_rad);
274 s = (T)sinf((
float)angle_rad);
278 c = (T)cos((
double)angle_rad);
279 s = (T)sin((
double)angle_rad);
281 const T oneminusc = ((T)1) - c;
283 memset(
M, 0, 16 *
sizeof(T));
284 M[0] = nx * nx * oneminusc + c;
285 M[1] = ny * nx * oneminusc + nz * s;
286 M[2] = nx * nz * oneminusc - ny * s;
287 M[4] = nx * ny * oneminusc - nz * s;
288 M[5] = ny * ny * oneminusc + c;
289 M[6] = ny * nz * oneminusc + nx * s;
290 M[8] = nx * nz * oneminusc + ny * s;
291 M[9] = ny * nz * oneminusc - nx * s;
292 M[10] = nz * nz * oneminusc + c;
323 *
this = (mat * (*this));
351 memset(
M, 0, 16 *
sizeof(T));
386 *
this = (mat * (*this));
412 memset(
M, 0, 16 *
sizeof(T));
444 *
this = (mat * (*this));
487 void setLookAt(T eyeX, T eyeY, T eyeZ, T centerX, T centerY, T centerZ, T upX, T upY, T upZ)
494 M[0] = s.
x;
M[4] = s.
y;
M[8] = s.
z;
M[12] = -s.
x * eyeX - s.
y * eyeY - s.
z * eyeZ;
495 M[1] = u.
x;
M[5] = u.
y;
M[9] = u.
z;
M[13] = -u.
x * eyeX - u.
y * eyeY - u.
z * eyeZ;
496 M[2] = -f.
x;
M[6] = -f.
y;
M[10] = -f.
z;
M[14] = f.
x * eyeX + f.
y * eyeY + f.
z * eyeZ;
497 M[3] = 0;
M[7] = 0;
M[11] = 0;
M[15] = (T)1;
527 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13] * V.
w,
528 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14] * V.
w,
529 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15] * V.
w };
539 if constexpr (std::is_same<T, float>::value)
541 return Vec4<T>{ fmaf(
M[0], V.
x, fmaf(
M[4], V.
y, fmaf(
M[8], V.
z,
M[12] * w))),
542 fmaf(
M[1], V.
x, fmaf(
M[5], V.
y, fmaf(
M[9], V.
z,
M[13] * w))),
543 fmaf(
M[2], V.
x, fmaf(
M[6], V.
y, fmaf(
M[10], V.
z,
M[14] * w))),
544 fmaf(
M[3], V.
x, fmaf(
M[7], V.
y, fmaf(
M[11], V.
z,
M[15] * w))) };
548 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13] * w,
549 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14] * w,
550 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15] * w };
560 if constexpr (std::is_same<T, float>::value)
562 return Vec4<T>{ fmaf(
M[0], V.
x, fmaf(
M[4], V.
y,
M[8] * V.
z)),
563 fmaf(
M[1], V.
x, fmaf(
M[5], V.
y,
M[9] * V.
z)),
564 fmaf(
M[2], V.
x, fmaf(
M[6], V.
y,
M[10] * V.
z)),
565 fmaf(
M[3], V.
x, fmaf(
M[7], V.
y,
M[11] * V.
z)) };
569 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z,
570 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z,
571 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z };
581 if constexpr (std::is_same<T, float>::value)
583 return Vec4<T>{ fmaf(
M[0], V.
x, fmaf(
M[4], V.
y, fmaf(
M[8], V.
z,
M[12]))),
584 fmaf(
M[1], V.
x, fmaf(
M[5], V.
y, fmaf(
M[9], V.
z,
M[13]))),
585 fmaf(
M[2], V.
x, fmaf(
M[6], V.
y, fmaf(
M[10], V.
z,
M[14]))),
586 fmaf(
M[3], V.
x, fmaf(
M[7], V.
y, fmaf(
M[11], V.
z,
M[15]))) };
590 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13],
591 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14],
592 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15]};
612 for (
int i = 0; i < 16; i++) {
M[i] *= a; }
650 inline void print(Stream & outputStream = Serial)
const
652 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[0],
M[4],
M[8],
M[12]);
653 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[1],
M[5],
M[9],
M[13]);
654 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[2],
M[6],
M[10],
M[14]);
655 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n\n",
M[3],
M[7],
M[11],
M[15]);
670 if constexpr (std::is_same<T, float>::value)
672 return Vec4<T>{ fmaf(M.
M[0], V.
x, fmaf(M.
M[4], V.
y, fmaf(M.
M[8], V.
z, M.
M[12] * V.
w))),
673 fmaf(M.
M[1], V.
x, fmaf(M.
M[5], V.
y, fmaf(M.
M[9], V.
z, M.
M[13] * V.
w))),
674 fmaf(M.
M[2], V.
x, fmaf(M.
M[6], V.
y, fmaf(M.
M[10], V.
z, M.
M[14] * V.
w))),
675 fmaf(M.
M[3], V.
x, fmaf(M.
M[7], V.
y, fmaf(M.
M[11], V.
z, M.
M[15] * V.
w))) };
678 return Vec4<T>{ M.
M[0] * V.
x + M.
M[4] * V.
y + M.
M[8] * V.
z + M.
M[12] * V.
w,
679 M.
M[1] * V.
x + M.
M[5] * V.
y + M.
M[9] * V.
z + M.
M[13] * V.
w,
680 M.
M[2] * V.
x + M.
M[6] * V.
y + M.
M[10] * V.
z + M.
M[14] * V.
w,
681 M.
M[3] * V.
x + M.
M[7] * V.
y + M.
M[11] * V.
z + M.
M[15] * V.
w };
692 for (
int i = 0; i < 4; i++)
694 for (
int j = 0; j < 4; j++)
697 for (
int k = 0; k < 4; k++) { R.
M[i + j * 4] += A.
M[i + k * 4] * B.
M[k + j * 4]; }
710 for (
int i = 0; i < 16; i++) { R.
M[i] *= a; }
Mat4< T > operator*(const Mat4< T > &A, const Mat4< T > &B)
Matrix-matrix multiplication.
Definition: Mat4.h:689
Utility/miscellaneous functions used throughout the library.
TGX_INLINE void swap(T &a, T &b)
Baby let me swap you one more time...
Definition: Misc.h:145
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:248
#define M_PI
define Pi in case math.h is not yet included.
Definition: Misc.h:79
T crossProduct(const Vec2< T > &U, const Vec2< T > &V)
Return the cross product UxV (i.e.
Definition: Vec2.h:569
T dotProduct(const Vec2< T > U, const Vec2< T > V)
Return the dot product U.V between two vectors.
Definition: Vec2.h:560
Vec2< T > normalize(Vec2< T > V)
Return the vector normalized to have unit norm (do nothing if the vector is 0).
Definition: Vec2.h:459
Generic 4x4 matrix [specializations fMat4, dMat4].
Definition: Mat4.h:88
TGX_INLINE Vec4< T > mult1(const Vec3< T > &V) const
Matrix-vector multiplication (last component of vector set to w = 1)
Definition: Mat4.h:578
void setPerspective(T fovy, T aspect, T zNear, T zFar)
Set as a perspective projection matrix.
Definition: Mat4.h:236
TGX_INLINE Vec4< T > mult0(const Vec3< T > &V) const
Matrix-vector multiplication (last component of vector set to w = 0).
Definition: Mat4.h:557
void multScale(const Vec3< T > v)
Pre-multiply this matrix by a dilatation matrix.
Definition: Mat4.h:455
void transpose()
Transpose the matrix (in place)
Definition: Mat4.h:620
void setTranslate(const Vec3< T > v)
Set as a translation matrix.
Definition: Mat4.h:369
void setScale(T x, T y, T z)
Set as a dilatation matrix.
Definition: Mat4.h:410
void multRotate(T angle, T x, T y, T z)
Pre-multiply this matrix by a rotation matrix.
Definition: Mat4.h:319
TGX_INLINE Vec4< T > mult(const Vec3< T > &V, T w) const
Matrix-vector multiplication.
Definition: Mat4.h:536
void setScale(const Vec3< T > v)
Set as a dilatation matrix.
Definition: Mat4.h:427
void setLookAt(T eyeX, T eyeY, T eyeZ, T centerX, T centerY, T centerZ, T upX, T upY, T upZ)
Set the matrix for a camera looking at a given direction.
Definition: Mat4.h:487
void multRotate(T angle, const Vec3< T > v)
Pre-multiply this matrix by a rotation matrix.
Definition: Mat4.h:336
TGX_INLINE Vec4< T > mult(const Vec4< T > V) const
Matrix-vector multiplication.
Definition: Mat4.h:524
void setZero()
Set as the null matrix (all coefficients equal 0).
Definition: Mat4.h:166
void setTranslate(T x, T y, T z)
Set as a translation matrix.
Definition: Mat4.h:349
void multScale(T x, T y, T z)
Pre-multiply this matrix by a dilatation matrix.
Definition: Mat4.h:440
void setIdentity()
Set as the identity matrix.
Definition: Mat4.h:175
void setRotate(T angle, T x, T y, T z)
Set as a rotation matrix.
Definition: Mat4.h:261
Mat4(const T *mat)
Constructor from an array (with column major ordering same as M).
Definition: Mat4.h:137
void setFrustum(T left, T right, T bottom, T top, T zNear, T zFar)
Set as a perspective projection matrix.
Definition: Mat4.h:213
void multTranslate(T x, T y, T z)
Pre-multiply this matrix by a translation matrix.
Definition: Mat4.h:382
Mat4()
Default constructor.
Definition: Mat4.h:120
void setOrtho(T left, T right, T bottom, T top, T zNear, T zFar)
Set as an orthographic projection matrix.
Definition: Mat4.h:191
void operator*=(T a)
Scalar multiplication.
Definition: Mat4.h:610
void setLookAt(const Vec3< T > eye, const Vec3< T > center, const Vec3< T > up)
Set the matrix for a camera looking at a given direction.
Definition: Mat4.h:515
void multTranslate(const Vec3< T > v)
Pre-multiply this matrix by a translation matrix.
Definition: Mat4.h:397
Mat4 & operator=(const Mat4 &mat)=default
Assignment operator.
void setRotate(T angle, const Vec3< T > v)
Set as a rotation matrix.
Definition: Mat4.h:305
constexpr Mat4(T x1, T y1, T z1, T t1, T x2, T y2, T z2, T t2, T x3, T y3, T z3, T t3, T x4, T y4, T z4, T t4)
Constructor from explicit values.
Definition: Mat4.h:126
void invertYaxis()
invert the y axis, same as multScale({1,-1,1})
Definition: Mat4.h:464
T M[16]
The matrix array in column major ordering:
Definition: Mat4.h:114
Mat4(const Mat4 &mat)=default
Copy constructor.
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
Generic 3D vector [specializations iVec3, fVec3, dVec3].
Definition: Vec3.h:70
T z
'z' coordinate (third dimension)
Definition: Vec3.h:83
Geenric 4D vector [specializations iVec4, fVec4, dVec4].
Definition: Vec4.h:71
T w
'w' coordinate (fourth dimension)
Definition: Vec4.h:85