20template <
typename T,
size_t Rows,
size_t Cols>
25 if constexpr (Rows == Cols) {
27 for (
size_t i = 0; i < Rows; ++i) {
28 for (
size_t j = 0; j < Cols; ++j) {
30 (i == j) ?
static_cast<T
>(1) :
static_cast<T
>(0);
35 std::memset(data, 0,
sizeof(data));
40 Matrix(std::initializer_list<std::initializer_list<T>> list) {
41 if (list.size() != Rows) {
42 throw std::invalid_argument(
43 "Number of rows does not match matrix dimension");
47 for (
const auto& row : list) {
48 if (row.size() != Cols) {
49 throw std::invalid_argument(
50 "Number of columns does not match matrix dimension");
54 for (
const auto& val : row) {
63 assert(row < Rows && col < Cols &&
"Matrix indices out of range");
64 return data[row][col];
69 assert(row < Rows && col < Cols &&
"Matrix indices out of range");
70 return data[row][col];
76 for (
size_t i = 0; i < Rows; ++i) {
77 for (
size_t j = 0; j < Cols; ++j) {
78 result(i, j) = data[i][j] + other(i, j);
87 for (
size_t i = 0; i < Rows; ++i) {
88 for (
size_t j = 0; j < Cols; ++j) {
89 result(i, j) = data[i][j] - other(i, j);
96 template <
size_t OtherCols>
101 for (
size_t i = 0; i < Rows; ++i) {
102 for (
size_t j = 0; j < OtherCols; ++j) {
103 T sum =
static_cast<T
>(0);
104 for (
size_t k = 0; k < Cols; ++k) {
105 sum += data[i][k] * other(k, j);
117 for (
size_t i = 0; i < Rows; ++i) {
118 for (
size_t j = 0; j < Cols; ++j) {
119 result(j, i) = data[i][j];
127 static_assert(Rows == Cols,
128 "Determinant is only defined for square matrices");
130 if constexpr (Rows == 1)
return data[0][0];
131 if constexpr (Rows == 2) {
132 return data[0][0] * data[1][1] - data[0][1] * data[1][0];
134 if constexpr (Rows == 3) {
136 (data[1][1] * data[2][2] - data[1][2] * data[2][1]) -
138 (data[1][0] * data[2][2] - data[1][2] * data[2][0]) +
140 (data[1][0] * data[2][1] - data[1][1] * data[2][0]);
143 throw std::runtime_error(
144 "Determinant not implemented for matrices larger than 3x3");
149 static_assert(Rows == Cols,
150 "Inverse is only defined for square matrices");
153 if (std::abs(det) < std::numeric_limits<T>::epsilon()) {
154 throw std::runtime_error(
155 "Matrix is singular (determinant is near zero)");
158 if constexpr (Rows == 2) {
160 result(0, 0) = data[1][1] / det;
161 result(0, 1) = -data[0][1] / det;
162 result(1, 0) = -data[1][0] / det;
163 result(1, 1) = data[0][0] / det;
167 throw std::runtime_error(
168 "Inverse not implemented for matrices larger than 2x2");
173 std::ostringstream ss;
175 for (
size_t i = 0; i < Rows; ++i) {
177 for (
size_t j = 0; j < Cols; ++j) {
178 ss << std::setw(10) << std::fixed << std::setprecision(4)
179 << data[i][j] <<
" ";
195 static_assert(Rows == Cols,
196 "Identity matrix is only defined for square matrices");
204 static_assert(Rows == 4 && Cols == 4,
"Perspective matrix must be 4x4");
205 T tanHalfFovY = tan(fovY / 2.0f);
209 result(0, 0) = 1.0f / (aspectRatio * tanHalfFovY);
210 result(1, 1) = 1.0f / tanHalfFovY;
211 result(2, 2) = farPlane / (farPlane - nearPlane);
213 result(3, 2) = (-nearPlane * farPlane) / (farPlane - nearPlane);
223 static_assert(Rows == 4 && Cols == 4,
"Orthographic matrix must be 4x4");
228 result(0, 0) = 2.0f / (right - left);
229 result(1, 1) = 2.0f / (top - bottom);
230 result(2, 2) = -2.0f / (farPlane - nearPlane);
233 result(0, 3) = -(right + left) / (right - left);
234 result(1, 3) = -(top + bottom) / (top - bottom);
235 result(2, 3) = -(farPlane + nearPlane) / (farPlane - nearPlane);
244 static_assert(Rows == 4 && Cols == 4,
"View matrix must be 4x4");
253 result(0, 0) = xAxis[0];
254 result(1, 0) = xAxis[1];
255 result(2, 0) = xAxis[2];
256 result(3, 0) = -xAxis.
Dot(eye);
258 result(0, 1) = yAxis[0];
259 result(1, 1) = yAxis[1];
260 result(2, 1) = yAxis[2];
261 result(3, 1) = -yAxis.
Dot(eye);
263 result(0, 2) = +zAxis[0];
264 result(1, 2) = +zAxis[1];
265 result(2, 2) = +zAxis[2];
266 result(3, 2) = -zAxis.
Dot(eye);
287 result(0, 0) = xAxis[0];
288 result(1, 0) = xAxis[1];
289 result(2, 0) = xAxis[2];
291 result(0, 1) = yAxis[0];
292 result(1, 1) = yAxis[1];
293 result(2, 1) = yAxis[2];
295 result(0, 2) = -zAxis[0];
296 result(1, 2) = -zAxis[1];
297 result(2, 2) = -zAxis[2];
299 result(3, 0) = -xAxis.
Dot(eye);
300 result(3, 1) = -yAxis.
Dot(eye);
301 result(3, 2) = zAxis.
Dot(eye);
314 result(0, 0) = xAxis[0];
315 result(1, 0) = xAxis[1];
316 result(2, 0) = xAxis[2];
318 result(0, 1) = yAxis[0];
319 result(1, 1) = yAxis[1];
320 result(2, 1) = yAxis[2];
322 result(0, 2) = zAxis[0];
323 result(1, 2) = zAxis[1];
324 result(2, 2) = zAxis[2];
326 result(3, 0) = -xAxis.
Dot(eye);
327 result(3, 1) = -yAxis.
Dot(eye);
328 result(3, 2) = -zAxis.
Dot(eye);
337 forward[0] = cos(yaw) * cos(pitch);
338 forward[1] = sin(pitch);
339 forward[2] = sin(yaw) * cos(pitch);
341 return LookToLH(position, forward,
Vector<T, 3>{0, 1, 0});
349 sin(phi) * cos(theta),
351 sin(phi) * sin(theta)
362 result(3, 0) = translation.
GetX();
363 result(3, 1) = translation.
GetY();
364 result(3, 2) = translation.
GetZ();
379 result(0, 0) = scale.
GetX();
380 result(1, 1) = scale.
GetY();
381 result(2, 2) = scale.
GetZ();
390 scaling(0, 0) = scale.
GetX();
391 scaling(1, 1) = scale.
GetY();
392 scaling(2, 2) = scale.
GetZ();
396 return translateBack * scaling * translateToOrigin;
408#include <DirectXMath.h>
413 DirectX::XMMATRIX worldMatrix;
415 DirectX::XMMATRIX normalMatrix = XMMatrixTranspose(XMMatrixInverse(
nullptr, worldMatrix));
418 DirectX::XMFLOAT4X4 floatMat;
419 DirectX::XMStoreFloat4x4(&floatMat, mat);
422 for (
int row = 0; row < 4; ++row) {
423 for (
int col = 0; col < 4; ++col) {
424 matr(row, col) = floatMat.m[row][col];
static Matrix< T, 4, 4 > Scale(const Vector3D &scale, const Vector3D ¢er)
Builds a matrix that scales about an arbitrary center point.
static Matrix< T, Rows, Rows > Identity()
Matrix< T, Rows, Cols > Inverse() const
static Matrix< T, 4, 4 > FreeLook(const Vector< T, 3 > &position, T yaw, T pitch)
View matrix for a yaw/pitch-driven free camera at position.
static Matrix< T, 4, 4 > Perspective(T fovY, T aspectRatio, T nearPlane, T farPlane)
Builds a left-handed perspective projection (reversed-Z: near maps to depth 1).
static Matrix< T, 4, 4 > Orthographic(T left, T right, T bottom, T top, T nearPlane, T farPlane)
Builds an orthographic projection over the given box.
static Matrix< T, 4, 4 > LookTo(const Vector< T, 3 > &eye, const Vector< T, 3 > &direction, const Vector< T, 3 > &up)
Left-handed view matrix looking from eye along a direction (rather than at a point).
T & operator()(size_t row, size_t col)
Element access; indices are bounds-checked by assert in debug builds.
Matrix(std::initializer_list< std::initializer_list< T > > list)
Matrix< T, Cols, Rows > Transpose() const
Returns the transposed matrix; does not modify this one.
const T & operator()(size_t row, size_t col) const
Read-only element access; bounds-checked by assert in debug builds.
static Matrix< T, 4, 4 > Rotate(const Quaternion &rotation)
Builds a rotation matrix from a quaternion.
static Matrix< T, 4, 4 > Scale(const Vector3D &scale)
Builds a matrix that scales about the origin.
friend std::ostream & operator<<(std::ostream &os, const Matrix< T, Rows, Cols > &matrix)
static Matrix< T, 4, 4 > Translate(const Vector3D &translation)
Builds a translation matrix.
static Matrix< T, 4, 4 > OrbitView(const Vector< T, 3 > &target, T distance, T theta, T phi)
View matrix for a camera orbiting target at the given distance and angles.
Matrix< T, Rows, OtherCols > operator*(const Matrix< T, Cols, OtherCols > &other) const
Row-by-column matrix product.
static Matrix< T, 4, 4 > LookAt(const Vector< T, 3 > &eye, const Vector< T, 3 > ¢er, const Vector< T, 3 > &up)
Left-handed view matrix looking from eye toward center.
Matrix< T, Rows, Cols > operator-(const Matrix< T, Rows, Cols > &other) const
Matrix< T, Rows, Cols > operator+(const Matrix< T, Rows, Cols > &other) const
static Matrix< T, 4, 4 > LookAtRH(const Vector< T, 3 > &eye, const Vector< T, 3 > ¢er, const Vector< T, 3 > &up)
Right-handed view matrix looking from eye toward center.
constexpr Matrix() noexcept
std::string ToString() const
Represents a quaternion for 3D rotations.
Matrix< float, 4, 4 > toRotationMatrix() const
T Dot(const Vector< T, N > &other) const
std::enable_if< M==3, Vector< T, N > >::type Cross(const Vector< T, N > &other) const
Vector< T, N > Normalized() const
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Matrix< float, 4, 4 > Matrix4
Root namespace for everything the engine exposes.