Skip to content

Matrix4

Represents a 4×4 floating-point matrix.

This class stores a 4×4 matrix in column-major order, consistent with the rest of the math module. It is used for affine transforms in 3D graphics, including world, view, and projection matrices. Matrix4 supports construction from individual components or column vectors, as well as element and column access and equality comparison.

Construction

Constructors


Matrix4() constexpr

Constructs a matrix from four column vectors.

cpp
Matrix4::Matrix4(const Vector4& vec0, const Vector4& vec1, const Vector4& vec2, const Vector4& vec3);
ParameterDescription
vec0First column.
vec1Second column.
vec2Third column.
vec3Fourth column.

Matrix4() constexpr

Constructs a matrix from individual components (row-major).

cpp
Matrix4::Matrix4(float n00, float n01, float n02, float n03, float n10, float n11, float n12, float n13, float n20, float n21, float n22, float n23, float n30, float n31, float n32, float n33);
ParameterDescription
n00First row, first element.
n01First row, second element.
n02First row, third element.
n03First row, fourth element.
n10Second row, first element.
n11Second row, second element.
n12Second row, third element.
n13Second row, fourth element.
n20Third row, first element.
n21Third row, second element.
n22Third row, third element.
n23Third row, fourth element.
n30Fourth row, first element.
n31Fourth row, second element.
n32Fourth row, third element.
n33Fourth row, fourth element.

Matrix4() constexpr

Constructs a diagonal matrix.

cpp
Matrix4::Matrix4(float value);
ParameterDescription
valueValue to place on the diagonal.

Functions

operator()() float &

Accesses an element by (row, column).

cpp
float& Matrix4::operator()(int row, int col);
ParameterDescription
rowRow index.
colColumn index.

operator()() const float

Accesses an element by (row, column).

cpp
const float Matrix4::operator()(int row, int col) const;
ParameterDescription
rowRow index.
colColumn index.

operator[]() Vector4&

Accesses a column vector.

cpp
Vector4& Matrix4::operator[](int col);
ParameterDescription
colColumn index.

operator[]() constVector4&

Accesses a column vector.

cpp
const Vector4& Matrix4::operator[](int col) const;
ParameterDescription
colColumn index.

Matrix4::Identity() Matrix4

Returns the identity matrix.

cpp
static constexpr Matrix4 Matrix4::Identity();

Determinant() float

Computes the determinant of a 4×4 matrix.

cpp
float Determinant(const Matrix4& mat);
ParameterDescription
matInput matrix.

Inverse() Matrix4

Computes the inverse of a 4×4 matrix.

cpp
Matrix4 Inverse(const Matrix4& mat);
ParameterDescription
matInput matrix.

operator*() Matrix4

Multiplies two 4×4 matrices.

cpp
Matrix4 operator* (const Matrix4& a, const Matrix4& b);
ParameterDescription
aLeft matrix.
bRight matrix.

operator*() Vector3

Transforms a 3D point by a 4×4 matrix.

The input vector is treated as a position with w = 1.0.

cpp
Vector3 operator* (const Matrix4& mat, const Vector3& vec);
ParameterDescription
matInput matrix.
vecInput vector.

operator*() Vector4

Multiplies a matrix by a 4D vector.

cpp
Vector4 operator* (const Matrix4& mat, const Vector4& vec);
ParameterDescription
matInput matrix.
vecInput vector.

Transpose() Matrix4

Returns the transpose of a 4×4 matrix.

cpp
Matrix4 Transpose(const Matrix4& mat);
ParameterDescription
matInput matrix.

Released under the MIT License.