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; }
163 memset(
M, 0, 16 *
sizeof(T));
172 memset(
M, 0, 16 *
sizeof(T));
173 M[0] =
M[5] =
M[10] =
M[15] = ((T)1);
186 void setOrtho(T left, T right, T bottom, T top, T zNear, T zFar)
188 memset(
M, 0, 16 *
sizeof(T));
189 M[0] = ((T)2) / (right - left);
190 M[5] = ((T)2) / (top - bottom);
191 M[10] = -((T)2) / (zFar - zNear);
192 M[12] = (right + left) / (left - right);
193 M[13] = (top + bottom) / (bottom - top);
194 M[14] = (zFar + zNear) / (zNear - zFar);
208 void setFrustum(T left, T right, T bottom, T top, T zNear, T zFar)
210 memset(
M, 0, 16 *
sizeof(T));
211 M[0] = (((T)2) * zNear) / (right - left);
212 M[5] = (((T)2) * zNear) / (top - bottom);
213 M[8] = (right + left) / (right - left);
214 M[9] = (top + bottom) / (top - bottom);
215 M[10] = (zFar + zNear) / (zNear - zFar);
217 M[14] = (((T)2) * zFar * zNear) / (zNear - zFar);
233 static const T deg2rad = (T)(
M_PI / 180);
234 const T aux = tan((fovy / ((T)2)) * deg2rad);
235 const T top = zNear * aux;
236 const T bottom = -top;
237 const T right = zNear * aspect * aux;
238 const T left = -right;
239 setFrustum(left, right, bottom, top, zNear, zFar);
253 static const T deg2rad = (T)(
M_PI / 180);
256 const T nx = x / norm;
257 const T ny = y / norm;
258 const T nz = z / norm;
259 const T c = cos(deg2rad * angle);
260 const T oneminusc = ((T)1) - c;
261 const T s = sin(deg2rad * angle);
263 memset(
M, 0, 16 *
sizeof(T));
264 M[0] = nx * nx * oneminusc + c;
265 M[1] = ny * nx * oneminusc + nz * s;
266 M[2] = nx * nz * oneminusc - ny * s;
267 M[4] = nx * ny * oneminusc - nz * s;
268 M[5] = ny * ny * oneminusc + c;
269 M[6] = ny * nz * oneminusc + nx * s;
270 M[8] = nx * nz * oneminusc + ny * s;
271 M[9] = ny * nz * oneminusc - nx * s;
272 M[10] = nz * nz * oneminusc + c;
303 *
this = (mat * (*this));
331 memset(
M, 0, 16 *
sizeof(T));
366 *
this = (mat * (*this));
392 memset(
M, 0, 16 *
sizeof(T));
424 *
this = (mat * (*this));
467 void setLookAt(T eyeX, T eyeY, T eyeZ, T centerX, T centerY, T centerZ, T upX, T upY, T upZ)
474 M[0] = s.
x;
M[4] = s.
y;
M[8] = s.
z;
M[12] = -s.
x * eyeX - s.
y * eyeY - s.
z * eyeZ;
475 M[1] = u.
x;
M[5] = u.
y;
M[9] = u.
z;
M[13] = -u.
x * eyeX - u.
y * eyeY - u.
z * eyeZ;
476 M[2] = -f.
x;
M[6] = -f.
y;
M[10] = -f.
z;
M[14] = f.
x * eyeX + f.
y * eyeY + f.
z * eyeZ;
477 M[3] = 0;
M[7] = 0;
M[11] = 0;
M[15] = (T)1;
507 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13] * V.
w,
508 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14] * V.
w,
509 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15] * V.
w };
519 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13] * w,
520 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14] * w,
521 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15] * w };
531 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z,
532 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z,
533 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z };
543 M[1] * V.
x +
M[5] * V.
y +
M[9] * V.
z +
M[13],
544 M[2] * V.
x +
M[6] * V.
y +
M[10] * V.
z +
M[14],
545 M[3] * V.
x +
M[7] * V.
y +
M[11] * V.
z +
M[15]};
565 for (
int i = 0; i < 16; i++) {
M[i] *= a; }
603 inline void print(Stream & outputStream = Serial)
const
605 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[0],
M[4],
M[8],
M[12]);
606 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[1],
M[5],
M[9],
M[13]);
607 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n",
M[2],
M[6],
M[10],
M[14]);
608 outputStream.printf(
"%.3f \t %.3f \t %.3f \t %.3f\n\n",
M[3],
M[7],
M[11],
M[15]);
622 return Vec4<T>{ M.
M[0] * V.
x + M.
M[4] * V.
y + M.
M[8] * V.
z + M.
M[12] * V.
w,
623 M.
M[1] * V.
x + M.
M[5] * V.
y + M.
M[9] * V.
z + M.
M[13] * V.
w,
624 M.
M[2] * V.
x + M.
M[6] * V.
y + M.
M[10] * V.
z + M.
M[14] * V.
w,
625 M.
M[3] * V.
x + M.
M[7] * V.
y + M.
M[11] * V.
z + M.
M[15] * V.
w };
636 for (
int i = 0; i < 4; i++)
638 for (
int j = 0; j < 4; j++)
641 for (
int k = 0; k < 4; k++) { R.
M[i + j * 4] += A.
M[i + k * 4] * B.
M[k + j * 4]; }
654 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:633
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:176
TGX_INLINE float precise_sqrt(float x)
Compute the square root of a float (exact computation).
Definition: Misc.h:254
#define M_PI
define Pi in case math.h is not yet included.
Definition: Misc.h:110
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:540
void setPerspective(T fovy, T aspect, T zNear, T zFar)
Set as a perspective projection matrix.
Definition: Mat4.h:231
TGX_INLINE Vec4< T > mult0(const Vec3< T > &V) const
Matrix-vector multiplication (last component of vector set to w = 0).
Definition: Mat4.h:528
void multScale(const Vec3< T > v)
Pre-multiply this matrix by a dilatation matrix.
Definition: Mat4.h:435
void transpose()
Transpose the matrix (in place)
Definition: Mat4.h:573
void setTranslate(const Vec3< T > v)
Set as a translation matrix.
Definition: Mat4.h:349
void setScale(T x, T y, T z)
Set as a dilatation matrix.
Definition: Mat4.h:390
void multRotate(T angle, T x, T y, T z)
Pre-multiply this matrix by a rotation matrix.
Definition: Mat4.h:299
void setScale(const Vec3< T > v)
Set as a dilatation matrix.
Definition: Mat4.h:407
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:467
void multRotate(T angle, const Vec3< T > v)
Pre-multiply this matrix by a rotation matrix.
Definition: Mat4.h:316
TGX_INLINE Vec4< T > mult(const Vec4< T > V) const
Matrix-vector multiplication.
Definition: Mat4.h:504
void setZero()
Set as the null matrix (all coefficients equal 0).
Definition: Mat4.h:161
TGX_INLINE Vec4< T > mult(const Vec3< T > &V, float w) const
Matrix-vector multiplication.
Definition: Mat4.h:516
void setTranslate(T x, T y, T z)
Set as a translation matrix.
Definition: Mat4.h:329
void multScale(T x, T y, T z)
Pre-multiply this matrix by a dilatation matrix.
Definition: Mat4.h:420
void setIdentity()
Set as the identity matrix.
Definition: Mat4.h:170
void setRotate(T angle, T x, T y, T z)
Set as a rotation matrix.
Definition: Mat4.h:251
void print(Stream &outputStream=Serial) const
Output a representation of the matrix using a given Stream.
Definition: Mat4.h:603
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:208
void multTranslate(T x, T y, T z)
Pre-multiply this matrix by a translation matrix.
Definition: Mat4.h:362
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:186
void operator*=(T a)
Scalar multiplication.
Definition: Mat4.h:563
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:495
void multTranslate(const Vec3< T > v)
Pre-multiply this matrix by a translation matrix.
Definition: Mat4.h:377
Mat4 & operator=(const Mat4 &mat)=default
Assignement operator.
void setRotate(T angle, const Vec3< T > v)
Set as a rotation matrix.
Definition: Mat4.h:285
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:444
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