CGPdocs

mat4

4x4 matrix of floats (affine and projective transforms).

alias of matrix_stack<float,4,4>module 06_matsource: mat4.hpp
A mat4 is stored row by row: M(i,j) is row i and column j, at position 4*i + j in memory; M[i] is a row, col_x() to col_w() are the columns, and an affine matrix holds the linear block and the translation t.
A mat4 is stored row by row: M(i,j) is row i and column j, at position 4*i + j in memory; M[i] is a row, col_x() to col_w() are the columns, and an affine matrix holds the linear block and the translation t. Tap to enlarge.

Fields and methods

Members of the struct matrix_stack<float,4,4>. Most of what you do with a CGP type goes through the free functions listed below.

Fields

numarray_stack<numarray_stack<float,4>,4> dataCoefficients stored row by row as 4 rows of 4 floats, contiguous in memory.

Methods

matrix_stack()Builds a matrix with all elements set to zero.11 overloads src
matrix_stack(numarray_stack<numarray_stack<float,4>,4> const& elements)Builds a mat4 from an array of four vec4 rows. src
matrix_stack(vec4 const& row_1, vec4 const& row_2, vec4 const& row_3, vec4 const& row_4)Builds a mat4 from its four rows (not columns). src
matrix_stack(numarray_stack<float,16> const& elements)Builds a mat4 from 16 values in row-major order. src
matrix_stack(float xx, float xy, float xz, float xw, float yx, float yy, float yz, float yw, float zx, float zy, float zz, float zw, float wx, float wy, float wz, float ww)Builds a mat4 from its 16 elements given row by row (xx, xy, xz, xw, yx, ..., ww), the translation being xw, yw, zw. src
matrix_stack(mat3 const& M)Builds the mat4 block matrix ((M, 0), (0, 1)) from a 3x3 linear part (explicit). src
matrix_stack(matrix_stack<float,N1_arg,N2_arg> const& M)Builds a mat2, mat3 or mat4 from a float matrix of another size (explicit), copying the common top-left block and setting the other elements to zero (a mat4 built from a mat3 uses the dedicated constructor instead, which puts 1 at (3,3)). src
matrix_stack(float value)Builds the mat2, mat3 or mat4 with the given value on the whole diagonal and zeros elsewhere (explicit, as in glm). src
matrix_stack(float xx, float yy, float zz, float ww = 1.0f)Builds a mat2 from its 4 elements given row by row (xx, xy, yx, yy). src
matrix_stack(std::initializer_list<float> const& arg)Builds a mat2, mat3 or mat4 from a brace list of floats; N*N or more values fill it in row-major order (extra ones ignored), 1 value or N values build a diagonal matrix (mat4 also accepts 3 values, with 1 as last diagonal element), and any other count raises an error. src
matrix_stack(std::initializer_list<numarray_stack<float,4>> const& arg)Builds a mat4 from a brace list of at least four vec4 rows (asserted); extra rows are ignored. src
Returns the identity matrix (ones on the main diagonal, zeros elsewhere; for a non-square generic matrix the diagonal has min(N1,N2) ones). src
Returns a mat2, mat3 or mat4 with all elements equal to zero. src
Returns a mat2, mat3 or mat4 with all elements equal to the given value. src
static mat4 build_diagonal(float value)Returns the mat2, mat3 or mat4 with the given value on the whole diagonal (including element (3,3) of a mat4) and zeros elsewhere.3 overloads src
static mat4 build_diagonal(vec4 const& arg)Returns the diagonal mat4 whose diagonal is the given vec4. src
static mat4 build_diagonal(float x, float y, float z, float w)Returns the diagonal mat4 diag(x, y, z, w). src
static mat4 build_scaling(float value)Returns the uniform scaling matrix of the given factor; same as build_diagonal for mat2 and mat3, while a mat4 keeps 1 as element (3,3).3 overloads src
static mat4 build_scaling(vec3 const& arg)Returns the scaling matrix of factors (arg.x, arg.y, arg.z); for mat3 the diagonal matrix, for mat4 diag(arg.x, arg.y, arg.z, 1). src
static mat4 build_scaling(float x, float y, float z)Returns the scaling matrix of factors (x, y, z); for mat3 the diagonal matrix, for mat4 diag(x, y, z, 1). src
Returns the mat4 with linear part linear (top-left 3x3 block), translation tr (last column) and last row (0,0,0,1). src
static mat4 build_translation(vec3 const& tr)Returns the mat4 of the translation tr (identity with tr in the last column).2 overloads src
static mat4 build_translation(float x, float y, float z)Returns the mat4 of the translation (x, y, z). src
Returns the mat4 ((M, 0), (0, 1)), same as the mat4(mat3) constructor. src
Returns the matrix (mat3, or mat4 with zero translation) of the rotation of angle in radians around axis, normalized internally. src
Returns the matrix (mat3, or mat4 with zero translation) of the rotation of a unit quaternion, whose norm is asserted to be within 0.05 of 1. src
Sets the mat4 to the identity and returns a reference to it. src
Sets all 16 elements of the mat4 to zero and returns a reference to it. src
mat4& set_diagonal(float value)Replaces the mat4 by the diagonal matrix with the given value on its four diagonal elements and returns a reference to it.2 overloads src
mat4& set_diagonal(float xx, float yy, float zz, float ww)Replaces the mat4 by diag(xx, yy, zz, ww) and returns a reference to it. src
Overwrites the top-left 3x3 linear block with L, leaving the translation and the last row unchanged, and returns a reference to the mat4. src
mat4& set_block_linear_as_scaling(vec3 const& s)Overwrites the top-left 3x3 block with diag(s.x, s.y, s.z), leaving the rest unchanged, and returns a reference to the mat4.3 overloads src
mat4& set_block_linear_as_scaling(float sx, float sy, float sz)Overwrites the top-left 3x3 block with diag(sx, sy, sz), leaving the rest unchanged, and returns a reference to the mat4. src
mat4& set_block_linear_as_scaling(float s)Overwrites the top-left 3x3 block with the uniform scaling diag(s, s, s), leaving the rest unchanged, and returns a reference to the mat4. src
Overwrites the top-left 3x3 block with the rotation of angle in radians around axis (normalized internally), leaving the rest unchanged, and returns a reference to the mat4. src
mat4& set_block_translation(vec3 const& tr)Overwrites the translation part, elements (0,3), (1,3) and (2,3), with tr and returns a reference to the mat4.2 overloads src
mat4& set_block_translation(float x, float y, float z)Overwrites the translation part, elements (0,3), (1,3) and (2,3), with (x, y, z) and returns a reference to the mat4. src
Returns the translation part (m(0,3), m(1,3), m(2,3)) of the mat4 as a vec3. src
Returns a copy of the top-left 3x3 block of the mat4 as a mat3. src
Multiplies the top-left 3x3 block of the mat4 by s in place and returns a reference to it. src
Multiplies the translation part (first three elements of the last column) by s in place and returns a reference to the mat4. src
Multiplies the first three rows of the mat4 (linear block and translation) by s in place, leaving the last row unchanged, and returns a reference to it. src
Adds xyz to the translation part of the mat4 in place and returns a reference to it. src
Replaces the linear block L of the mat4 by T*L in place, leaving the translation unchanged, and returns a reference to it. src
Returns the total number of elements N1*N2 (4, 9 and 16 for mat2, mat3 and mat4). src
Returns the dimensions {N1, N2} (number of rows, number of columns) as an int2. src
Sets every element to the given value and returns a reference to the matrix. src
Returns a copy of the first column of the mat4 as a vec4. src
Returns a copy of the second column of the mat4 as a vec4. src
Returns a copy of the third column of the mat4 as a vec4. src
Returns a copy of the fourth column of the mat4 as a vec4 (translation and 1 for an affine matrix). src
Returns a const reference to the first row of the mat4 (a vec4). src
Returns a const reference to the second row of the mat4 (a vec4). src
Returns a const reference to the third row of the mat4 (a vec4). src
Returns a const reference to the fourth row of the mat4 (a vec4). src
Returns the first three elements of the first row of the mat4 as a vec3. src
Returns the first three elements of the second row of the mat4 as a vec3. src
Returns the first three elements of the third row of the mat4 as a vec3. src
Returns the first three elements of the fourth row of the mat4 as a vec3. src
Returns the first three elements of the first column of the mat4 as a vec3. src
Returns the first three elements of the second column of the mat4 as a vec3. src
Returns the first three elements of the third column of the mat4 as a vec3. src
Returns the first three elements of the fourth column of the mat4, i.e. its translation part, as a vec3. src
Returns a const reference to row k (a vec2, vec3 or vec4 for mat2, mat3 or mat4), with bound checking unless CGP_NO_DEBUG is defined. src
vec4 const& operator()(int k2) const
vec4& operator()(int k2)
Returns a const reference to row k1 (a vector of N2 elements), with bound checking unless CGP_NO_DEBUG is defined.2 overloads src
float const& operator()(int k1, int k2) const
float& operator()(int k1, int k2)
Returns a const reference to the element at row k1 and column k2, with bound checking unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at the row-major offset k1*N2+k2; bound-checked for the generic matrix and mat4 (unless CGP_NO_DEBUG is defined) but not for mat2 and mat3. src
Applies the mat4 to the point (p, 1) and returns the xyz part divided by w (perspective division, w is not checked against zero). src
Applies the top-left 3x3 block of the mat4 to the vector v, ignoring the translation and the last row. src
Returns the (N1-1)x(N2-1) matrix obtained by removing row k1 and column k2 (asserts k1<N1 and k2<N2), e.g. a mat2 for a mat3. src
Copies a smaller matrix into this one with its top-left corner at (offset_1, offset_2), default (0,0), and returns a reference to this matrix; the block must fit (static_assert on its size, assert on the offsets). src
Returns a const pointer to the first element (0,0) of the contiguous row-major storage. src
Returns a const pointer one past the last element (N1-1,N2-1) of the contiguous row-major storage. src
Returns a const pointer to the first element (0,0) of the contiguous row-major storage. src
Returns a const pointer one past the last element (N1-1,N2-1) of the contiguous row-major storage. src
Returns a const reference to the element at row k1 and column k2; bound-checked for mat2, mat3 and mat4 (unless CGP_NO_DEBUG is defined) but not for the generic matrix_stack. src
float const& coeff(int k1, int k2) const
float& coeff(int k1, int k2)
Returns a const reference to the element at row k1 and column k2, without bound checking.2 overloads src
vec4 const& coeff(int k1) const
vec4& coeff(int k1)
Returns a const reference to row k1, without bound checking. src
Returns a const reference to the element at the given row-major offset, without bound checking. src
Returns the inverse ((R^T, -R^T t), (0, 1)) of the mat4, assuming it is a rotation R followed by a translation t; the result is wrong if the linear block is not orthonormal, and the last row is ignored. src

Functions taking a mat4

Transform 1

mat4 inverse(mat4 const& m)

Matrix inverse.

Compute 2

vec3 camera_ray_direction(
    mat4 const& camera_frame,
    mat4 const& perspective_inverse,
    vec2 const& screen_position
)

Unit world-space direction of the ray through a screen position given in normalized device coordinates [-1,1]^2, from the camera frame and the inverse projection matrix.

float det(mat4 const& m)

Determinant of a 4x4 matrix, by cofactor expansion along the last row.

Convert 1

std::string type_str(mat4 const&)

Type name as a string: "mat4".

Other 1

Documented on another page

  • opengl_uniformSend a mat4 uniform (row-major, transposed on upload) to the shader; warns if the uniform is not found and expected is true.on opengl_shader_structure

Functions returning a mat4

Construct 5

mat4 projection_orthographic(
    float left,
    float right,
    float bottom,
    float top,
    float z_near,
    float z_far
)

OpenGL orthographic projection matrix mapping the box [left,right] x [bottom,top] x [-z_near,-z_far] to the unit cube.

mat4 projection_orthographic_inverse(
    float left,
    float right,
    float bottom,
    float top,
    float z_near,
    float z_far
)

Inverse of the orthographic projection matrix of projection_orthographic, computed in closed form.

mat4 projection_perspective(float fov, float aspect, float z_near, float z_far)

OpenGL perspective projection matrix from a vertical field of view in radians, an aspect ratio (width/height) and the near and far distances.

mat4 projection_perspective_inverse(float fov, float aspect, float z_near, float z_far)

Inverse of the perspective projection matrix of projection_perspective, computed in closed form.

mat4 tensor_product(vec4 const& a, vec4 const& b)

Outer product a * transpose(b), as a 4x4 matrix.

Operators

Show the 18 operators (+, -, *, ==, ...)

operator*

5 overloads
mat4 operator*(mat4 const& a, mat4 const& b)
source

Matrix product of two 4x4 matrices.

mat4 operator*(float s, mat4 const& M)
source

4x4 matrix scaled by a scalar.

mat4 operator*(mat4 const& T1, affine const& T2)
source

Product of a mat4 with the 4x4 matrix of the affine transform.

mat4 operator*(mat4 const& T1, affine_rt const& T2)
source

Product of a mat4 with the 4x4 matrix of the rigid transform.

mat4 operator*(mat4 const& T1, affine_rts const& T2)
source

Product of a mat4 with the 4x4 matrix of the transform.

operator*=

2 overloads
mat4& operator*=(mat4& a, mat4 const& b)
source

Replaces a by the matrix product a*b.

mat4& operator*=(mat4& M, float s)
source

Multiplies each coefficient of a 4x4 matrix by a scalar in place.

mat4& operator+=(mat4& a, mat4 const& b)

Adds a 4x4 matrix coefficient-wise in place.

Documented on another page

CGP - Computer Graphics Programming.