Class Matrix4x4

Matrix with 4x4 elements.

Matrix4x4 constructors

Functions which can be used to create new instances of Matrix4x4.

Synopsis

Methods
Matrix4x4()

Default constructor, initializes the matrix to identity matrix

Matrix4x4()

Constructs the matrix from elements in column-major order

Matrix4x4()

Constructs the matrix from four column vectors

Matrix4x4()

Constructs the matrix from four column vectors

Matrix4x4()

Default constructor, initializes the matrix to identity matrix.

Example
local matrix4x4 = Matrix4x4()
Matrix4x4(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15)

Constructs the matrix from elements in column-major order.

Example
local matrix4x4 = Matrix4x4(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15)
Matrix4x4(x, y, z, translation)

Constructs the matrix from four column vectors.

Example
local matrix4x4 = Matrix4x4(x, y, z, translation)
Matrix4x4(x, y, z, translation)

Constructs the matrix from four column vectors.

Example
local matrix4x4 = Matrix4x4(x, y, z, translation)

Matrix4x4 methods

Methods of Matrix4x4 class.

Synopsis

Methods
Matrix4x4:getRow()

Gets the row of a matrix

Matrix4x4:getColumn()

Gets the column of a matrix

Matrix4x4:setRow()

Sets the row of a matrix

Matrix4x4:setColumn()

Sets the column of a matrix

Matrix4x4:getXAxis()

Get X axis

Matrix4x4:getYAxis()

Get Y axis

Matrix4x4:getZAxis()

Get Z axis

Matrix4x4:getScale()

Get scale as a vector

Matrix4x4:getRotationAngle()

Calculates Euler rotation angles of the matrix

Matrix4x4:scale()

Scales this matrix

Matrix4x4:scaleUniform()

Scales this matrix

Matrix4x4:translate()

Translates this matrix

Matrix4x4:setXAxis()

Set X axis

Matrix4x4:setYAxis()

Set Y axis

Matrix4x4:setZAxis()

Set Z axis

Matrix4x4:getTranslation()

Get translation

Matrix4x4:setTranslation()

Set translation

Matrix4x4:getTranslationX()

Get translation on X axis

Matrix4x4:setTranslationX()

Set translation on X axis

Matrix4x4:getTranslationY()

Get translation on Y axis

Matrix4x4:setTranslationY()

Set translation on Y axis

Matrix4x4:getTranslationZ()

Get translation on Z axis

Matrix4x4:setTranslationZ()

Set translation on Z axis

Matrix4x4.createTranslation()

Creates translation matrix

Matrix4x4.createTranslation()

Creates translation matrix

Matrix4x4.createIdentity()

Creates identity matrix

Matrix4x4.createFromHomogenousMatrix()

Creates a matrix from an existing homogeneous matrix and Z translation

Matrix4x4.createFromHomogenousMatrix()

Creates a matrix from an existing homogeneous matrix

Matrix4x4.createRotation()

Creates a rotation matrix over an arbitrary axis

Matrix4x4.createRotationInDegrees()

Creates a rotation matrix over an arbitrary axis

Matrix4x4.createXRotationInRadians()

Creates a X rotation matrix

Matrix4x4.createXRotationInDegrees()

Creates a X rotation matrix

Matrix4x4.createYRotationInRadians()

Creates a Y rotation matrix

Matrix4x4.createYRotationInDegrees()

Creates a Y rotation matrix

Matrix4x4.createZRotationInRadians()

Creates a Z rotation matrix

Matrix4x4.createZRotationInDegrees()

Creates a Z rotation matrix

Matrix4x4.createScale()

Creates a scaling matrix

Matrix4x4.createOrthogonalProjection()

Creates projection matrix for orthogonal projection

Matrix4x4.createPerspectiveProjection()

Creates a perspective projection matrix for a symmetrical viewing volume

Matrix4x4.createPerspectiveProjection()

Creates a perspective projection matrix for a viewing volume

Matrix4x4.createSubProjection()

Calculate sub-quad projection of an existing projection matrix

Matrix4x4.createLookAtMatrix()

Creates a lookup transformation matrix, so that it is in a given position looking at the given point

Matrix4x4.createSRT()

Create an SRT matrix

Matrix4x4.createFilled()

Creates a matrix with all elements set to \p value

Matrix4x4:transposed()

Applies a transpose to a matrix

Matrix4x4:getRow(index)

Gets the row of a matrix.

Parameters
index (size_t)

Index of the accessed row.

Return Values
(Vector4)

The row.

Matrix4x4:getColumn(index)

Gets the column of a matrix.

Parameters
index (size_t)

Index of the accessed column.

Return Values
(Vector4)

The column.

Matrix4x4:setRow(index, row)

Sets the row of a matrix.

Parameters
index (size_t)

Index of the accessed row.

row (Vector4)

The value to set to the row.

Matrix4x4:setColumn(index, column)

Sets the column of a matrix.

Parameters
index (size_t)

Index of the accessed column.

column (Vector4)

The value to set to the column.

Matrix4x4:getXAxis()

Get X axis.

Matrix4x4:getYAxis()

Get Y axis.

Matrix4x4:getZAxis()

Get Z axis.

Matrix4x4:getScale()

Get scale as a vector.

Matrix4x4:getRotationAngle()

Calculates Euler rotation angles of the matrix. Kanzi Engine represents Euler angles as Tait-Bryan angles where the rotations are applied in the order x, y and z.

Return Values
(Vector3 or nil)

Euler angles in radians, or nil if the matrix is not decomposable.

Matrix4x4:scale(scale)

Scales this matrix.

Parameters
scale (Vector3)

Scaling vector.

Matrix4x4:scaleUniform(scale)

Scales this matrix.

Parameters
scale (float)

Uniform scaling value.

Matrix4x4:translate(translate)

Translates this matrix.

Parameters
translate (Vector3)

Translation vector.

Matrix4x4:setXAxis(v)

Set X axis.

Parameters
v (Vector3)

X axis vector.

Matrix4x4:setYAxis(v)

Set Y axis.

Parameters
v (Vector3)

Y axis vector.

Matrix4x4:setZAxis(v)

Set Z axis.

Parameters
v (Vector3)

Z axis vector.

Matrix4x4:getTranslation()

Get translation.

Return Values
(Vector3)

Translation vector.

Matrix4x4:setTranslation(translation)

Set translation.

Parameters
translation (Vector3)

Translation vector.

Matrix4x4:getTranslationX()

Get translation on X axis.

Return Values
(float)

Translation X component.

Matrix4x4:setTranslationX(translationX)

Set translation on X axis.

Parameters
translationX (float)

Translation X component.

Matrix4x4:getTranslationY()

Get translation on Y axis.

Return Values
(float)

Translation Y component.

Matrix4x4:setTranslationY(translationY)

Set translation on Y axis.

Parameters
translationY (float)

Translation Y component.

Matrix4x4:getTranslationZ()

Get translation on Z axis.

Return Values
(float)

Translation Z component.

Matrix4x4:setTranslationZ(translationZ)

Set translation on Z axis.

Parameters
translationZ (float)

Translation Y component.

Matrix4x4.createTranslation(translation)

Creates translation matrix.

Parameters
translation (Vector3)

Translation vector.

Return Values
(Matrix4x4)

Translation matrix.

Matrix4x4.createTranslation(x, y, z)

Creates translation matrix.

Parameters
x (float)

X component of the Translation vector.

y (float)

Y component of the Translation vector.

z (float)

Z component of the Translation vector.

Return Values
(Matrix4x4)

Translation matrix.

Matrix4x4.createIdentity()

Creates identity matrix.

Return Values
(Matrix4x4)

Identity matrix.

Matrix4x4.createFromHomogenousMatrix(matrix, zTranslation)

Creates a matrix from an existing homogeneous matrix and Z translation.

Parameters
matrix (Matrix3x3)

Homogeneous matrix.

zTranslation (float)

X translation.

Return Values
(Matrix4x4)

Newly constructed matrix.

Matrix4x4.createFromHomogenousMatrix(matrix)

Creates a matrix from an existing homogeneous matrix. 0.0f is used for the Z translation.

Parameters
matrix (Matrix3x3)

Homogeneous matrix.

Return Values
(Matrix4x4)

Newly constructed matrix.

Matrix4x4.createRotation(angleInRadians, rotationAxis)

Creates a rotation matrix over an arbitrary axis. Similar to the matrix used by glRotatef. The rotation axis needs not be normalized.

Parameters
angleInRadians (float)

Rotation angle in radians.

rotationAxis (Vector3)

Arbitrary rotation axis.

Return Values
(Matrix4x4)

Rotation matrix.

Matrix4x4.createRotationInDegrees(angleInDegrees, rotationAxis)

Creates a rotation matrix over an arbitrary axis. Similar to the matrix used by glRotatef. The rotation axis needs not be normalized.

Parameters
angleInDegrees (float)

Rotation angle in degrees.

rotationAxis (Vector3)

Arbitrary rotation axis.

Return Values
(Matrix4x4)

Rotation matrix.

Matrix4x4.createXRotationInRadians(angleInRadians)

Creates a X rotation matrix.

Parameters
angleInRadians (float)

Rotation angle in radians.

Return Values
(Matrix4x4)

X Rotation matrix.

Matrix4x4.createXRotationInDegrees(angleInDegrees)

Creates a X rotation matrix.

Parameters
angleInDegrees (float)

Rotation angle in degrees.

Return Values
(Matrix4x4)

X Rotation matrix.

Matrix4x4.createYRotationInRadians(angleInRadians)

Creates a Y rotation matrix.

Parameters
angleInRadians (float)

Rotation angle in radians.

Return Values
(Matrix4x4)

Y Rotation matrix.

Matrix4x4.createYRotationInDegrees(angleInDegrees)

Creates a Y rotation matrix.

Parameters
angleInDegrees (float)

Rotation angle in degrees.

Return Values
(Matrix4x4)

Y Rotation matrix.

Matrix4x4.createZRotationInRadians(angleInRadians)

Creates a Z rotation matrix.

Parameters
angleInRadians (float)

Rotation angle in radians.

Return Values
(Matrix4x4)

Z Rotation matrix.

Matrix4x4.createZRotationInDegrees(angleInDegrees)

Creates a Z rotation matrix.

Parameters
angleInDegrees (float)

Rotation angle in degrees.

Return Values
(Matrix4x4)

Z Rotation matrix.

Matrix4x4.createScale(scale)

Creates a scaling matrix.

Parameters
scale (Vector3)

Scaling vector.

Return Values
(Matrix4x4)

Scaled matrix.

Matrix4x4.createOrthogonalProjection(left, right, bottom, top, near, far)

Creates projection matrix for orthogonal projection.

Parameters
left (float)

Viewing box left.

right (float)

Viewing box right.

bottom (float)

Viewing box bottom.

top (float)

Viewing box top.

near (float)

Camera near value.

far (float)

Camera far value.

Return Values
(Matrix4x4)

A newly constructed projection matrix.

Matrix4x4.createPerspectiveProjection(near, far, fovType, fovInDegrees, aspectRatio)

Creates a perspective projection matrix for a symmetrical viewing volume.

Parameters
near (float)

Camera near value.

far (float)

Camera far value.

fovType (FieldOfViewType)

Tells whether the fovInDegrees parameter is xfov or yfov.

fovInDegrees (float)

Field of view in degrees.

aspectRatio (float)

The aspect ratio of the viewport.

Return Values
(Matrix4x4)

Perspective projection matrix.

Matrix4x4.createPerspectiveProjection(near, far, fovInDegrees)

Creates a perspective projection matrix for a viewing volume.

Parameters
near (float)

Camera near value.

far (float)

Camera far value.

fovInDegrees (Vector4)

Field of view in degrees, specified in order left, right, up, down. Angles to the right of center and upwards of center are positive.

Return Values
(Matrix4x4)

Perspective projection matrix.

Matrix4x4.createSubProjection(projection, relativeOffsetX, relativeOffsetY, relativeSizeX, relativeSizeY)

Calculate sub-quad projection of an existing projection matrix.

This is a specific solution for an axis-aligned case.

Old projection matrix = [P00 0 P02 P03] [ 0 P11 P12 P13] [ 0 0 P22 P23] [ 0 0 -1 0]

Traslated and scaled: (x, y) |--> (Ox, Oy) + (Sx * x, Sy * y) [Sx P00, 0, Ox P22 + Sx P02, Ox P23] [ 0, Sy P11, Oy P22 + Sy P12, Oy P23] [ 0, 0, P22, P23] [ 0, 0, -1, 0]

Parameters
projection (Matrix4x4)

Projection matrix.

relativeOffsetX (float)

X offset of sub-quad as portion of the size of the projection area.

relativeOffsetY (float)

Y offset of sub-quad as portion of the size of the projection area.

relativeSizeX (float)

Projection area X size in regard to sub-quad area size.

relativeSizeY (float)

Projection area Y size in regard to sub-quad area size.

Return Values
(Matrix4x4)

Modified projection matrix.

Matrix4x4.createLookAtMatrix(position, lookAtPoint, upVector)

Creates a lookup transformation matrix, so that it is in a given position looking at the given point.

Parameters
position (Vector3)

The new translation of the node.

lookAtPoint (Vector3)

A point where the node is looking at.

upVector (Vector3)

The new up vector of the node.

Return Values
(Matrix4x4 or nil)

Lookup matrix if position is not the same as lookAtPoint and upVector is not null, nil otherwise.

Matrix4x4.createSRT(scale, rotationInRadians, translation)

Create an SRT matrix.

Parameters
scale (Vector3)

Scales by axises.

rotationInRadians (Vector3)

Rotation component in radians.

translation (Vector3)

Translation component.

Return Values
(Matrix4x4)

Newly constructed matrix.

Matrix4x4.createFilled(value)

Creates a matrix with all elements set to \p value.

Parameters
value (float)

Value to set to all elements.

Return Values
(Matrix4x4)

Newly constructed matrix.

Matrix4x4:transposed()

Applies a transpose to a matrix.

Return Values
(Matrix4x4)

Newly constructed transpose matrix.

Matrix4x4 functions

Matrix4x4 specific standalone functions, which are not a part of the Matrix4x4 class.

Synopsis

Functions
multiplyAffine()

Multiplies two affine matrices and returns the product

orthonormalizeBase()

Orthonormalizes base vectors of the given matrix

matrixMultiplyVectorBy3x3()

Multiplies a 3D vector with the top 3x3 part of a given matrix

inverse()

Applies an inversion to a matrix

inverseAffine()

Applies an inversion to an affine matrix

inverseOrthogonal()

Applies an inversion to an orthogonal affine matrix

isTranslationOnly()

Returns whether a matrix is translation-only

extractSRT()

Extracts from a matrix the scale, rotation, and translation information

extract3x3()

Extracts the 3x3 rotation and scaling part of a matrix

normalizeBase()

Normalizes base vectors of a matrix

getDeterminant()

Calculates the determinant of a matrix

getDeterminant3x3()

Calculates the determinant of the 3x3 part of a matrix

getTrace()

Calculates the trace of a matrix

createRelativeOrthogonalProjection()

Creates an orthogonal projection matrix for a symmetrical viewing volume

calculateCameraViewPosition()

Calculates a homogeneous camera view position that you can use to calculate the view direction in a shader

componentWiseMultiply()

Multiplies matrix components component-wise and returns the resulting matrix

componentWiseDivide()

Divides matrix components component-wise and returns the resulting matrix

componentWiseMax()

Calculates the component-wise maximum for all matrix elements and returns the resulting matrix

componentWiseMin()

Calculates the component-wise minimum for all matrix elements and returns the resulting matrix

multiplyAffine(left, right)

Multiplies two affine matrices and returns the product.

Parameters
left (Matrix4x4)

Left hand side matrix.

right (Matrix4x4)

Right hand side matrix.

Return Values
(Matrix4x4)

The result matrix.

orthonormalizeBase(matrix)

Orthonormalizes base vectors of the given matrix. The result is in out_matrix. The orthonormalization is done using the Gram-Schmidt process. Z axis is kept still, then Y is handled, then X. You can use this function to prevent value drift.

Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
(Matrix4x4)

The orthonormalized matrix.

matrixMultiplyVectorBy3x3(matrix, vector)

Multiplies a 3D vector with the top 3x3 part of a given matrix.

Parameters
matrix (Matrix4x4)

The matrix that you want to multiply with a 3D vector.

vector (Vector3)

The 3D vector that you want to multiply with a matrix.

Return Values
(Vector3)

The result of the multiplication.

inverse(matrix)

Applies an inversion to a matrix.

Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
(Matrix4x4 or nil)

Inverted matrix if the input matrix is invertible, nil otherwise.

inverseAffine(matrix)

Applies an inversion to an affine matrix.

Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
(Matrix4x4 or nil)

Inverted matrix if the input matrix is invertible, nil otherwise.

inverseOrthogonal(matrix)

Applies an inversion to an orthogonal affine matrix.

Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
(Matrix4x4 or nil)

Inverted matrix if the input matrix is invertible, nil otherwise.

isTranslationOnly(matrix)

Returns whether a matrix is translation-only.

Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
(bool)

If the input matrix is translation-only, true, otherwise false.

extractSRT(matrix)

Extracts from a matrix the scale, rotation, and translation information.

Example
local result, scale, rotationInRadians, translation = extractSRT(matrix)
Parameters
matrix (Matrix4x4)

Input matrix.

Return Values
1. (boolean)

True if the matrix could be decomposed and the extraction of SRT components succeeded, false otherwise.

2. (Vector3)

Scale vector.

3. (Vector3)

Rotation vector, rotation angle in radians.

4. (Vector3)

Translation vector.

extract3x3(matrix)

Extracts the 3x3 rotation and scaling part of a matrix.

normalizeBase(matrix)

Normalizes base vectors of a matrix. The matrix must be an affine transformation.

Return Values
(Matrix4x4)

Matrix4x4 with base vectors normalized.

getDeterminant(matrix)

Calculates the determinant of a matrix.

Parameters
matrix (Matrix4x4)

The matrix for which you want to return the determinant.

Return Values
(float)

Determinant of the matrix.

getDeterminant3x3(matrix)

Calculates the determinant of the 3x3 part of a matrix.

Parameters
matrix (Matrix4x4)

The matrix for which you want to return the determinant.

Return Values
(float)

Determinant of the matrix.

getTrace(matrix)

Calculates the trace of a matrix.

Return Values
(float)

Trace of the matrix.

createRelativeOrthogonalProjection(near, far, fovType, orthoSize, aspectRatio)

Creates an orthogonal projection matrix for a symmetrical viewing volume.

Parameters
near (float)

Camera near value.

far (float)

Camera far value.

fovType (Matrix4x4::FieldOfViewType)

Tells whether the orthoSize parameter is the width or height of the viewing box.

orthoSize (float)

Specifies the size of the viewing box (either width of height).

aspectRatio (float)

Specifies the aspect ratio of the viewing box.

Return Values
(Matrix4x4)

Orthogonal projection matrix.

calculateCameraViewPosition(projectionCameraMatrix)

Calculates a homogeneous camera view position that you can use to calculate the view direction in a shader. For orthogonal projections the w-component of the viewpoint is 0 to indicate that the viewpoint is at infinity.

Parameters
projectionCameraMatrix (Matrix4x4)

The matrix to use for calculating the viewpoint.

Return Values
(Vector4 or nil)

Homogeneous viewpoint.

componentWiseMultiply(m1, m2)

Multiplies matrix components component-wise and returns the resulting matrix.

Parameters
m1 (Matrix4x4)

The first matrix.

m2 (Matrix4x4)

The second matrix.

Return Values
(Matrix4x4)

The result matrix.

componentWiseDivide(m1, m2)

Divides matrix components component-wise and returns the resulting matrix.

Parameters
m1 (Matrix4x4)

The first matrix.

m2 (Matrix4x4)

The second matrix.

Return Values
(Matrix4x4)

The result matrix.

componentWiseMax(v1, v2)

Calculates the component-wise maximum for all matrix elements and returns the resulting matrix.

Parameters
v1 (Matrix4x4)

The first matrix.

v2 (Matrix4x4)

The second matrix.

Return Values
(Matrix4x4)

The result matrix.

componentWiseMin(v1, v2)

Calculates the component-wise minimum for all matrix elements and returns the resulting matrix.

Parameters
v1 (Matrix4x4)

The first matrix.

v2 (Matrix4x4)

The second matrix.

Return Values
(Matrix4x4)

The result matrix.