Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Math::t_Matrix4< T > Struct Template Reference

Template structure representing a 4x4 matrix. More...

#include <Matrix4.hpp>

Public Member Functions

constexpr t_Matrix4 ()
 Default constructor.
 
constexpr t_Matrix4 (T v)
 Constructs a diagonal matrix with the same value on the diagonal.
 
constexpr t_Matrix4 (T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15)
 Constructs a matrix from sixteen individual elements.
 
constexpr t_Matrix4 (T v[16])
 Constructs a matrix from an array of sixteen elements.
 
constexpr t_Matrix4 (const t_Vector4< T > &c0, const t_Vector4< T > &c1, const t_Vector4< T > &c2, const t_Vector4< T > &c3)
 Constructs a matrix from four column vectors.
 
template<typename R >
 t_Matrix4 (const t_Matrix4< R > &rhs)
 Type-converting constructor.
 
constexpr size_t size () const
 Gets the total number of elements in the matrix.
 
constexpr size_t columns () const
 Gets the number of columns in the matrix.
 
constexpr size_t rows () const
 Gets the number of rows in the matrix.
 
t_Vector4< T > & operator[] (size_t c)
 Column access operator (mutable).
 
const t_Vector4< T > & operator[] (size_t c) const
 Column access operator (const).
 
T & operator() (size_t c, size_t r)
 Element access operator (mutable).
 
operator() (size_t c, size_t r) const
 Element access operator (const).
 
template<typename R >
t_Matrix4operator= (const t_Matrix4< R > &rhs)
 Type-converting assignment operator.
 
void set (T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10, T v11, T v12, T v13, T v14, T v15)
 Sets all sixteen matrix elements.
 
template<typename R >
void set (const t_Matrix4< R > &rhs)
 Sets this matrix equal to another matrix.
 
T * data ()
 Gets a pointer to the underlying data.
 
const T * data () const
 Gets a const pointer to the underlying data.
 
t_Matrix4 operator* (T scalar) const
 Scalar multiplication operator.
 
t_Matrix4operator*= (T scalar)
 Scalar multiplication assignment operator.
 
t_Matrix4 operator* (const t_Matrix4 &rhs) const
 Matrix multiplication operator.
 
t_Matrix4operator*= (const t_Matrix4 &rhs)
 Matrix multiplication assignment operator.
 
t_Vector4< T > operator* (const t_Vector4< T > &v) const
 Matrix-vector multiplication operator.
 
t_Matrix4 operator+ (const t_Matrix4 &rhs) const
 Matrix addition operator.
 
t_Matrix4operator+= (const t_Matrix4 &rhs)
 Matrix addition assignment operator.
 
t_Matrix4 operator- (const t_Matrix4 &rhs) const
 Matrix subtraction operator.
 
t_Matrix4operator-= (const t_Matrix4 &rhs)
 Matrix subtraction assignment operator.
 
t_Matrix4 operator/ (T scalar) const
 Scalar division operator.
 
t_Matrix4operator/= (T scalar)
 Scalar division assignment operator.
 
bool operator== (const t_Matrix4 &rhs) const
 Equality comparison operator.
 
bool operator!= (const t_Matrix4 &rhs) const
 Inequality comparison operator.
 
bool operator< (const t_Matrix4 &rhs) const
 Less-than comparison operator for ordering.
 
t_Matrix4 transpose () const
 Computes the transpose of this matrix.
 
t_Matrix4 operator- () const
 Unary negation operator.
 
 operator bool () const noexcept
 Boolean conversion operator.
 

Public Attributes

t_Vector4< T > value [4]
 Column vectors comprising the matrix.
 

Detailed Description

template<typename T>
struct Infinity::Types::Math::t_Matrix4< T >

Template structure representing a 4x4 matrix.

t_Matrix4 provides a column-major 4x4 matrix implementation for 3D transformations in homogeneous coordinates. This is the standard matrix for representing complete 3D transformations including translation, rotation, scale, and perspective projection. The matrix is stored as four column vectors, following the convention used by modern graphics APIs like OpenGL and Vulkan.

Common applications in procedural generation and 3D graphics:

  • Complete 3D transformations (translation, rotation, scale combined)
  • Model-view-projection matrices for rendering
  • Camera transformations (view and projection matrices)
  • Skeletal animation transformations (bone matrices)
  • Coordinate space conversions (object to world, world to view, etc.)
  • Perspective and orthographic projections
  • Instance transformations for procedurally placed objects

Matrix layout (column-major):

[ m00 m01 m02 m03 ] [ value[0].x value[1].x value[2].x value[3].x ]
[ m10 m11 m12 m13 ] = [ value[0].y value[1].y value[2].y value[3].y ]
[ m20 m21 m22 m23 ] [ value[0].z value[1].z value[2].z value[3].z ]
[ m30 m31 m32 m33 ] [ value[0].w value[1].w value[2].w value[3].w ]
t_Vector4< T > value[4]
Column vectors comprising the matrix.
Definition Matrix4.hpp:102

For typical 3D transformations, the upper-left 3x3 submatrix represents rotation and scale, the fourth column (value[3].xyz) represents translation, and the bottom row is typically [0, 0, 0, 1] for affine transformations.

The template parameter allows instantiation with different numeric types (float, double) to match precision requirements.

Example usage:

// Identity matrix (default constructor)
Matrix4 identity;
// Translation matrix
Matrix4 translation(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
5, 10, 3, 1 // Translate by (5, 10, 3)
);
// Scale matrix
Matrix4 scale(
2, 0, 0, 0,
0, 2, 0, 0,
0, 0, 2, 0,
0, 0, 0, 1 // Uniform scale by 2
);
// Combine transformations (scale then translate)
Matrix4 transform = translation * scale;
// Transform a point in homogeneous coordinates
Vector4 point(1, 2, 3, 1); // w=1 for position
Vector4 transformed = transform * point;
// Transpose for converting between APIs
Matrix4 transposed = transform.transpose();
Template structure representing a 4x4 matrix.
Definition Matrix4.hpp:93
t_Matrix4 transpose() const
Computes the transpose of this matrix.
Definition Matrix4.hpp:705
Template structure representing a 4-component vector.
Definition Vector4.hpp:85
Template Parameters
TThe numeric type for matrix elements (float, double).
Note
Matrices are stored in column-major order for compatibility with OpenGL, Vulkan, and other modern graphics APIs.
Matrix-vector multiplication treats vectors as column vectors.
For affine transformations (translation, rotation, scale), the bottom row is typically [0, 0, 0, 1].
See also
Matrix4, Matrix4d
t_Vector4, t_Matrix3

Constructor & Destructor Documentation

◆ t_Matrix4() [1/6]

template<typename T >
constexpr Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( )
inlineconstexpr

Default constructor.

Constructs an identity matrix:

[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]

◆ t_Matrix4() [2/6]

template<typename T >
constexpr Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( v)
inlineexplicitconstexpr

Constructs a diagonal matrix with the same value on the diagonal.

Creates a matrix with v on the diagonal and zeros elsewhere:

[ v 0 0 0 ]
[ 0 v 0 0 ]
[ 0 0 v 0 ]
[ 0 0 0 v ]
Parameters
vThe value for all diagonal elements.
Matrix4 scaleMatrix(2.0f); // Uniform scale by 2 (including w)

◆ t_Matrix4() [3/6]

template<typename T >
constexpr Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( v0,
v1,
v2,
v3,
v4,
v5,
v6,
v7,
v8,
v9,
v10,
v11,
v12,
v13,
v14,
v15 
)
inlineconstexpr

Constructs a matrix from sixteen individual elements.

Elements are specified in row-major order for readability, but stored in column-major order internally.

Parameters
v0through v15 Matrix elements in row-major order.
// Translation matrix: translate by (5, 10, 3)
Matrix4 translation(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
5, 10, 3, 1
);

◆ t_Matrix4() [4/6]

template<typename T >
constexpr Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( v[16])
inlineexplicitconstexpr

Constructs a matrix from an array of sixteen elements.

Elements are read in row-major order from the array.

Parameters
vArray of 16 elements in row-major order.
float data[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
T * data()
Gets a pointer to the underlying data.
Definition Matrix4.hpp:368

◆ t_Matrix4() [5/6]

template<typename T >
constexpr Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( const t_Vector4< T > &  c0,
const t_Vector4< T > &  c1,
const t_Vector4< T > &  c2,
const t_Vector4< T > &  c3 
)
inlineconstexpr

Constructs a matrix from four column vectors.

Directly specifies the four columns of the matrix.

Parameters
c0First column vector.
c1Second column vector.
c2Third column vector.
c3Fourth column vector (typically translation for affine transforms).
Vector4 xAxis(1, 0, 0, 0);
Vector4 yAxis(0, 1, 0, 0);
Vector4 zAxis(0, 0, 1, 0);
Vector4 translation(5, 10, 3, 1);
Matrix4 transform(xAxis, yAxis, zAxis, translation);

◆ t_Matrix4() [6/6]

template<typename T >
template<typename R >
Infinity::Types::Math::t_Matrix4< T >::t_Matrix4 ( const t_Matrix4< R > &  rhs)
inlineexplicit

Type-converting constructor.

Constructs a matrix from another matrix with a different element type.

Template Parameters
RThe source element type.
Parameters
rhsMatrix to convert from.

Member Function Documentation

◆ columns()

template<typename T >
constexpr size_t Infinity::Types::Math::t_Matrix4< T >::columns ( ) const
inlineconstexpr

Gets the number of columns in the matrix.

Returns
4 (always 4 columns).

◆ data() [1/2]

template<typename T >
T * Infinity::Types::Math::t_Matrix4< T >::data ( )
inline

Gets a pointer to the underlying data.

Returns a pointer to the first element, allowing direct memory access. Elements are stored in column-major order, suitable for passing directly to graphics APIs like OpenGL.

Returns
Pointer to the first matrix element.

◆ data() [2/2]

template<typename T >
const T * Infinity::Types::Math::t_Matrix4< T >::data ( ) const
inline

Gets a const pointer to the underlying data.

Returns
Const pointer to the first matrix element.

◆ operator bool()

template<typename T >
Infinity::Types::Math::t_Matrix4< T >::operator bool ( ) const
inlineexplicitnoexcept

Boolean conversion operator.

Returns true if the matrix is non-zero (at least one column is non-zero).

Returns
true if matrix is non-zero, false if zero matrix.

◆ operator!=()

template<typename T >
bool Infinity::Types::Math::t_Matrix4< T >::operator!= ( const t_Matrix4< T > &  rhs) const
inline

Inequality comparison operator.

Parameters
rhsMatrix to compare against.
Returns
true if any element differs.

◆ operator()() [1/2]

template<typename T >
T & Infinity::Types::Math::t_Matrix4< T >::operator() ( size_t  c,
size_t  r 
)
inline

Element access operator (mutable).

Returns a reference to the element at the specified column and row.

Parameters
cColumn index (0, 1, 2, or 3).
rRow index (0, 1, 2, or 3).
Returns
Reference to the matrix element.
m(3, 0) = 5.0f; // Set translation X (column 3, row 0)

◆ operator()() [2/2]

template<typename T >
T Infinity::Types::Math::t_Matrix4< T >::operator() ( size_t  c,
size_t  r 
) const
inline

Element access operator (const).

Returns the value of the element at the specified column and row.

Parameters
cColumn index (0, 1, 2, or 3).
rRow index (0, 1, 2, or 3).
Returns
Value of the matrix element.

◆ operator*() [1/3]

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator* ( const t_Matrix4< T > &  rhs) const
inline

Matrix multiplication operator.

Performs standard matrix multiplication (this * rhs). This composes transformations: the result applies rhs first, then this.

Parameters
rhsRight-hand side matrix.
Returns
Result of matrix multiplication.
Matrix4 scale = ...;
Matrix4 rotation = ...;
Matrix4 translation = ...;
// Apply scale, then rotation, then translation
Matrix4 transform = translation * rotation * scale;

◆ operator*() [2/3]

template<typename T >
t_Vector4< T > Infinity::Types::Math::t_Matrix4< T >::operator* ( const t_Vector4< T > &  v) const
inline

Matrix-vector multiplication operator.

Transforms a vector by this matrix, treating the vector as a column vector. This is used to transform points and directions in homogeneous coordinates.

Parameters
vVector to transform.
Returns
Transformed vector.
Matrix4 transform = ...;
Vector4 point(1, 2, 3, 1); // Position (w=1)
Vector4 transformed = transform * point;
// For perspective projection, don't forget to divide by w
if (transformed.w != 0) {
transformed.x /= transformed.w;
transformed.y /= transformed.w;
transformed.z /= transformed.w;
}
T y
Definition Vector4.hpp:100
T x
Definition Vector4.hpp:100
T w
Homogeneous coordinate or quaternion interpretation.
Definition Vector4.hpp:100
T z
Definition Vector4.hpp:100

◆ operator*() [3/3]

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator* ( scalar) const
inline

Scalar multiplication operator.

Multiplies all matrix elements by a scalar value.

Parameters
scalarScalar value to multiply by.
Returns
Scaled matrix.
Matrix4 scaled = m * 2.0f; // Double all elements

◆ operator*=() [1/2]

template<typename T >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator*= ( const t_Matrix4< T > &  rhs)
inline

Matrix multiplication assignment operator.

Multiplies this matrix by another matrix and stores the result.

Parameters
rhsRight-hand side matrix.
Returns
Reference to this matrix.

◆ operator*=() [2/2]

template<typename T >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator*= ( scalar)
inline

Scalar multiplication assignment operator.

Multiplies all elements of this matrix by a scalar.

Parameters
scalarScalar value to multiply by.
Returns
Reference to this matrix.

◆ operator+()

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator+ ( const t_Matrix4< T > &  rhs) const
inline

Matrix addition operator.

Adds two matrices element-wise.

Parameters
rhsMatrix to add.
Returns
Sum of the matrices.

◆ operator+=()

template<typename T >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator+= ( const t_Matrix4< T > &  rhs)
inline

Matrix addition assignment operator.

Adds another matrix to this matrix element-wise.

Parameters
rhsMatrix to add.
Returns
Reference to this matrix.

◆ operator-() [1/2]

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator- ( ) const
inline

Unary negation operator.

Returns a matrix with all elements negated.

Returns
Negated matrix.

◆ operator-() [2/2]

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator- ( const t_Matrix4< T > &  rhs) const
inline

Matrix subtraction operator.

Subtracts one matrix from another element-wise.

Parameters
rhsMatrix to subtract.
Returns
Difference of the matrices.

◆ operator-=()

template<typename T >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator-= ( const t_Matrix4< T > &  rhs)
inline

Matrix subtraction assignment operator.

Subtracts another matrix from this matrix element-wise.

Parameters
rhsMatrix to subtract.
Returns
Reference to this matrix.

◆ operator/()

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::operator/ ( scalar) const
inline

Scalar division operator.

Divides all matrix elements by a scalar value. For floating-point types, uses reciprocal multiplication for better performance.

Parameters
scalarScalar value to divide by.
Returns
Scaled matrix.
Warning
Division by zero is undefined behavior.

◆ operator/=()

template<typename T >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator/= ( scalar)
inline

Scalar division assignment operator.

Divides all elements of this matrix by a scalar.

Parameters
scalarScalar value to divide by.
Returns
Reference to this matrix.
Warning
Division by zero is undefined behavior.

◆ operator<()

template<typename T >
bool Infinity::Types::Math::t_Matrix4< T >::operator< ( const t_Matrix4< T > &  rhs) const
inline

Less-than comparison operator for ordering.

Performs lexicographic comparison of columns. Useful for using matrices as keys in ordered containers like std::map.

Parameters
rhsMatrix to compare against.
Returns
true if this matrix is lexicographically less than rhs.

◆ operator=()

template<typename T >
template<typename R >
t_Matrix4 & Infinity::Types::Math::t_Matrix4< T >::operator= ( const t_Matrix4< R > &  rhs)
inline

Type-converting assignment operator.

Assigns from a matrix with a different element type.

Template Parameters
RThe source element type.
Parameters
rhsMatrix to assign from.
Returns
Reference to this matrix.

◆ operator==()

template<typename T >
bool Infinity::Types::Math::t_Matrix4< T >::operator== ( const t_Matrix4< T > &  rhs) const
inline

Equality comparison operator.

Two matrices are equal if all their elements are equal.

Parameters
rhsMatrix to compare against.
Returns
true if all elements are equal.

◆ operator[]() [1/2]

template<typename T >
t_Vector4< T > & Infinity::Types::Math::t_Matrix4< T >::operator[] ( size_t  c)
inline

Column access operator (mutable).

Returns a reference to the specified column vector.

Parameters
cColumn index (0, 1, 2, or 3).
Returns
Reference to the column vector.
m[3] = Vector4(5, 10, 3, 1); // Set translation column
t_Vector4< float > Vector4
Alias for t_Vector4<float>, the default 4D vector type.
Definition Vector4.hpp:555

◆ operator[]() [2/2]

template<typename T >
const t_Vector4< T > & Infinity::Types::Math::t_Matrix4< T >::operator[] ( size_t  c) const
inline

Column access operator (const).

Returns a const reference to the specified column vector.

Parameters
cColumn index (0, 1, 2, or 3).
Returns
Const reference to the column vector.

◆ rows()

template<typename T >
constexpr size_t Infinity::Types::Math::t_Matrix4< T >::rows ( ) const
inlineconstexpr

Gets the number of rows in the matrix.

Returns
4 (always 4 rows).

◆ set() [1/2]

template<typename T >
template<typename R >
void Infinity::Types::Math::t_Matrix4< T >::set ( const t_Matrix4< R > &  rhs)
inline

Sets this matrix equal to another matrix.

Template Parameters
RThe source element type.
Parameters
rhsMatrix to copy from.

◆ set() [2/2]

template<typename T >
void Infinity::Types::Math::t_Matrix4< T >::set ( v0,
v1,
v2,
v3,
v4,
v5,
v6,
v7,
v8,
v9,
v10,
v11,
v12,
v13,
v14,
v15 
)
inline

Sets all sixteen matrix elements.

Elements are specified in row-major order for readability.

Parameters
v0through v15 Matrix elements in row-major order.

◆ size()

template<typename T >
constexpr size_t Infinity::Types::Math::t_Matrix4< T >::size ( ) const
inlineconstexpr

Gets the total number of elements in the matrix.

Returns
16 (always 4x4 = 16 elements).

◆ transpose()

template<typename T >
t_Matrix4 Infinity::Types::Math::t_Matrix4< T >::transpose ( ) const
inline

Computes the transpose of this matrix.

Returns a new matrix with rows and columns swapped. For orthogonal matrices (like pure rotation matrices), the transpose equals the inverse.

Returns
Transposed matrix.
Matrix4 m = ...;
Matrix4 mT = m.transpose();
// mT[i][j] == m[j][i]
// For rotation matrices: M^T = M^-1
Matrix4 rotation = ...;
Matrix4 inverse = rotation.transpose();

Member Data Documentation

◆ value

template<typename T >
t_Vector4<T> Infinity::Types::Math::t_Matrix4< T >::value[4]

Column vectors comprising the matrix.

Stored in column-major order: value[0] is the first column, value[1] is the second column, value[2] is the third column, value[3] is the fourth column (typically translation for affine transforms).