Skip to content

Matrix API

Markdown

These types and their corresponding functions are for working with matrices stored within memory, as opposed to being preloaded within the XMTRX back-bank of FP registers. Typically this is desirable for one-off operations where there is no batching of matrix operations, especially when clobbering XMTRX is undesirable. Matrices are column-major, as with GL, and shz_mat4x4_t must be aligned on 8-byte boundaries unless you use an _unaligned routine. Beware that some of these routines clobber the matrix currently loaded as the active 4x4 matrix, XMTRX.

  • C header: #include <sh4zam/shz_matrix.h>
  • C++ header: #include <sh4zam/shz_matrix.hpp>
  • C++ types: shz::mat4x4
  • Symbols: 120

For most transform types this API offers several versions of the same operation. Using translation as the example:

VerbMeaning
init_*Initializes the whole matrix to the given transform, setting the other components to identity.
set_*Sets only the values corresponding to the given transform, leaving the others alone.
apply_*Applies the transform, updating only the affected values based on their current values (additively for translation, multiplicatively for scaling and rotation).
translate / scale / rotate_*GL-style: multiplies and accumulates the matrix by a matrix initialized to the given transform. The most general form.
load_* / store_*Moves a matrix between memory and XMTRX. load_apply_* and load_apply_store_* are fused combinations.
transform_*Transforms vectors or points. For vec3 variants, transform_point* implies w = 1 and transform_vec* implies w = 0.

Suffixes used in this module. The full list is in Naming and Suffixes.

  • _unaligned: Works on matrix data that is not 8-byte aligned, passed as a plain float[16]. The default routines use 64-bit paired FMOV loads that need 8-byte alignment.
  • _transpose: Operates with the transposed matrix (row-major input, or inverse-rotation use).
  • _wxyz: Register/column permutation so that FTRV yields W first (cycle 4 instead of 7), which helps perspective division.
  • _safe: Guards degenerate input (zero-length vectors, zero denominators) at a small cost.

Routines for fully initializing a matrix.

SymbolDescription
shz_mat4x4_init_identityInitializes the given matrix to the 4x4 identity matrix.
shz_mat4x4_init_identity_safeSafely initializes the given matrix to the 4x4 identity matrix.
shz_mat4x4_init_zeroInitializes the given matrix with all 0s for its element values.
shz_mat4x4_init_oneInitializes the given matrix with all 1s for its element values.
shz_mat4x4_init_fillInitializes the given matrix with all elements assigned to the given value.
shz_mat4x4_init_translationInitializes the given matrix to a 3D translation matrix with the given coordinates.
shz_mat4x4_init_scaleInitializes the given matrix to a 3D scaling matrix with the given dimensions.
shz_mat4x4_init_rotation_xInitializes the given matrix to a 3D rotation matrix by xAngle radians over the X-axis.
shz_mat4x4_init_rotation_yInitializes the given matrix to a 3D rotation matrix by yAngle radians over the Y-axis.
shz_mat4x4_init_rotation_zInitializes the given matrix to a 3D rotation matrix by zAngle radians over the Z-axis.
shz_mat4x4_init_rotation_xyzInitializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan X-Y-Z angles.
shz_mat4x4_init_rotation_zyxInitializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-Y-X angles.
shz_mat4x4_init_rotation_zxyInitializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Z-X-Y angles.
shz_mat4x4_init_rotation_yxzInitializes the given matrix to a 3D rotation matrix from the intrinsic rotation created by the given Tait-Bryan Y-X-Z angles.
shz_mat4x4_init_rotationInitializes the given 4x4 matrix to a 3D rotation matrix of angle radians about the vector with the given components.
shz_mat4x4_init_rotation_dirInitializes the given 4x4 matrix to contain a 3D rotation matrix of angle radians about the given direction.
shz_mat4x4_init_rotation_quatInitializes the given matrix to a 3D rotation matrix with its orientation given by a quaternion.
shz_mat4x4_init_diagonalInitializes the given matrix to a diagonal matrix with the given 4 values.
shz_mat4x4_init_upper_triangularInitializes the given matrix to an upper triangular matrix whose nonzero entries have the given value.
shz_mat4x4_init_lower_triangularInitializes the given matrix to a lower triangular matrix whose nonzero entries have the given value.
shz_mat4x4_init_symmetric_skewInitializes the given matrix to be the symmetric skew of the given 3D vector components.
shz_mat4x4_init_outer_productInitializes the given matrix to be the result from taking the outer product of the two given 4D vectors.
shz_mat4x4_init_permutation_wxyzInitializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
shz_mat4x4_init_permutation_yzwxInitializes the matrix to to a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
shz_mat4x4_init_screenInitializes the given matrix to the viewport matrix with the given dimenions.
shz_mat4x4_init_lookatInitializes the given matrix to a "lookAt" view matrix.
shz_mat4x4_init_orthoInitializes the given matrix to an orthographic projection matrix.
shz_mat4x4_init_frustumInitializes the given matrix to a frustum projection matrix.
shz_mat4x4_init_perspectiveInitializes the given matrix to a perspective projection matrix.

Routines for decomposing a matrix into its constituent transforms.

SymbolDescription
shz_mat4x4_decomposeDecomposes a 4x4 transform matrix into translation, rotation, and scale.

Routines for getting specific values within a matrix.

SymbolDescription
shz_mat4x4_rowExtracts the row index as a 4D row vector from the given matrix.
shz_mat4x4_colExtracts the col index as a 4D column vector from the given matrix.
shz_mat4x4_get_translationReturns the translational components from the 4th column as a 3D vector.
shz_mat4x4_get_scaleExracts the 3D scale factors from the given 4x4 matrix.
shz_mat4x4_determinantReturns the determinant of the given 4x4 matrix.
shz_mat4x4_traceReturns the trace of the given 4x4 matrix.
shz_mat4x4_3x3Extracts the top-left 3x3 of the given 4D matrix.
shz_mat4x4_3x3_inverse_unscaledExtracts and inverts the top-level, unscaled 3x3 of the given 4x4 matrix.
shz_mat4x4_3x3_inverseExtracts and inverts the top-level 3x3 (which may be scaled) of the given 4x4 matrix.
shz_mat4x4_3x3_determinantReturns the determinant of the given 4x4 matrix's internal top-level 3x3 matrix.

Routines for setting specific values within a matrix

SymbolDescription
shz_mat4x4_set_rowSets the values of mat at the given row to those of the 4D vector, vec.
shz_mat4x4_set_colSets the values of mat at the given col to those of the 4D vector, vec.
shz_mat4x4_swap_rowsSwaps the 4D row vectors located at row1 and row2 within mat.
shz_mat4x4_swap_colsSwaps the 4D column vectors located at col1 and col2 within mat.
shz_mat4x4_set_translationAssigns only the 3D translation-related elements of the given matrix to the given values.
shz_mat4x4_set_scaleAssigns only the 3D scale-related elements of the given matrix to the given values.
shz_mat4x4_set_rotation_quatSets just the rotational component of the matrix to the orientation given by a quaternion, keeping the other elements in-tact.
shz_mat4x4_set_diagonalAssigns only the 4 elements along the diagonal of the given matrix to the given values.

Routines for multiplying and accumulating onto the given matrix.

SymbolDescription
shz_mat4x4_applyMultiplies and accumulates the src 4x4 matrix onto the dst 4x4 matrix.
shz_mat4x4_apply_unalignedMultiplies and accumulates the unaligned src 4x4 matrix onto the dst 4x4 matrix.
shz_mat4x4_apply_transposeMultiplies and accumulates the transposed src 4x4 matrix onto the dst 4x4 matrix.
shz_mat4x4_apply_transpose_unalignedMultiplies and accumulates the transposed unaligned src 4x4 matrix onto the dst 4x4 matrix.
shz_mat4x4_apply_translationAdds the given 3D vector components to the translational values of the given matrix.
shz_mat4x4_apply_scaleMultiplies and accumulates the scale-related elements of the given matrix by the given 3D components.
shz_mat4x4_apply_rotation_xMultiplies and accumulates a rotation matrix by xAngle radians about the X-axis onto the given matrix.
shz_mat4x4_apply_rotation_yMultiplies and accumulates a rotation matrix by yAngle radians about the Y-axis onto the given matrix.
shz_mat4x4_apply_rotation_zMultiplies and accumulates a rotation matrix by zAngle radians about the Z-axis onto the given matrix.
shz_mat4x4_apply_rotation_xyzRotates the given transform matrix about the X axis, then the Y axis, then the Z axis by the given angles in radians.
shz_mat4x4_apply_rotation_zyxRotates the given transform matrix about the Z axis, then the Y axis, then the X axis by the given angles in radians.
shz_mat4x4_apply_rotation_zxyRotates the given transform matrix about the Z axis, then the X axis, then the Y axis by the given angles in radians.
shz_mat4x4_apply_rotation_yxzRotates the given transform matrix about the Y axis, then the X axis, then the Z axis by the given angles in radians.
shz_mat4x4_apply_rotationRotates the given transform matrix about the arbitrary axis given by a 3D direction vector and angle of rotation in radians.
shz_mat4x4_apply_rotation_quatMultiplies and accumulates the given matrix with a rotation matrix whose orientation is given by a quaternion.
shz_mat4x4_apply_lookatApplies the 3D "lookAt" matrix constructed with the given vector components onto the given matrix.
shz_mat4x4_apply_orthoMultiplies and accumulates the ortho matrix constructed from the given values onto the given matrix.
shz_mat4x4_apply_frustumMultiplies and accumulates the frustum matrix constructed from the given values onto the given matrix.
shz_mat4x4_apply_perspectiveMultiplies and accumulates the perspective matrix constructed from the given values onto the given matrix.
shz_mat4x4_apply_screenMultiplies and accumulates the viewport matrix created with the given components ont othe given matrix.
shz_mat4x4_apply_symmetric_skewMultiplies and accumulates the given matrix with a symmetric skew matrix formed from the given 3D vector components.
shz_mat4x4_apply_permutation_wxyzMultiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in WXYZ order.
shz_mat4x4_apply_permutation_yzwxMultiplies and accumulates a permutation matrix, which reorders the components of transformed vectors to be in YZWX order.
shz_mat4x4_apply_selfMultiplies and accumulates the given matrix onto itself.

OpenGL-style 4x4 matrix transforms.

SymbolDescription
shz_mat4x4_translateMultiplies and accumulates mat by a 3D translation matrix with the given components.
shz_mat4x4_scaleMultiplies and accumulates mat by a 3D scaling matrix with the given components.
shz_mat4x4_rotate_xMultiplies and accumulates mat by a 3D rotation matrix about the X axis.
shz_mat4x4_rotate_yMultiplies and accumulates mat by a 3D rotation matrix about the Y axis.
shz_mat4x4_rotate_zMultiplies and accumulates mat by a 3D rotation matrix about the Z axis.
shz_mat4x4_rotate_xyzMultiplies and accumulates mat by 3D rotation matrices about the X then Y then Z axes.
shz_mat4x4_rotate_zyxMultiplies and accumulates mat by 3D rotation matrices about the Z then Y then X axes.
shz_mat4x4_rotate_zxyMultiplies and accumulates mat by 3D rotation matrices about the Z then X then Y axes.
shz_mat4x4_rotate_yxzMultiplies and accumulates mat by 3D rotation matrices about the Y then X then Z axes.
shz_mat4x4_rotateMultiplies and accumulates mat by the 3D rotation matrix formed by the given axis and angle.

Pre-multiplication variants of OpenGL-style 4x4 matrix transforms.

SymbolDescription
shz_mat4x4_translate_reversePre-multiplies and accumulates the given matrix onto the 3D translation matrix with the given components.
shz_mat4x4_scale_reversePre-multiplies and accumulates the given matrix onto the 3D scaling matrix with the given components.

Routines for transforming vectors and points by a matrix.

SymbolDescription
shz_mat4x4_multMultiplies two 4x4 matrices together, storing the result into a third.
shz_mat4x4_mult_unalignedMultiplies two 4x4 matrices together, with the right handed matrix being unaligned, storing the result into a third.
shz_mat4x4_mult_transposeMultiplies the regular matrix, lhs, by the transpose of the matrix, rhs, storing the result into mat.
shz_mat4x4_mult_transpose_unalignedMultiplies the regular matrix, lhs, by the transpose of the unaligned matrix, rhs, storing the result into mat.
shz_mat4x4_transform_vec2Transforms a 2D vector by a 4x4 matrix.
shz_mat4x4_transform_vec3Transforms a 3D vector by a 4x4 matrix.
shz_mat4x4_transform_vec4Transforms a 4D vector by a 4x4 matrix.
shz_mat4x4_transform_vec2_transposeTransforms a 2D vector the the transpose of a 4x4 matrix.
shz_mat4x4_transform_vec3_transposeTransforms a 3D vector the the transpose of a 4x4 matrix.
shz_mat4x4_transform_vec4_transposeTransforms a 4D vector the the transpose of a 4x4 matrix.
shz_mat4x4_transform_point2Transforms a 2D point by a 4x4 matrix.
shz_mat4x4_transform_point3Transforms a 3D point by a 4x4 matrix.
shz_mat4x4_transform_point2_transposeTransforms a 2D point by the transpose of a 4x4 matrix.
shz_mat4x4_transform_point3_transposeTransforms a 3D point by the transpose of a 4x4 matrix.

Other matrix-related operations and routines

SymbolDescription
shz_mat4x4_to_quatConverts the given 4x4 orientation matrix into a quaternion.
shz_mat4x4_transposeStores the transpose of mat within out.
shz_mat4x4_inverseComputes the inverse of a 4x4 matrix.
shz_mat4x4_inverse_block_triangularComputes the inverse of a 4x4 matrix in block-triangular form.
shz_mat4x4_equalReturns true if the two matrices are equal, based on either absolute or relative tolerance.
shz_mat4x4_is_block_triangularReturns true if the given matrix is in block-triangular form: having a bottom row in the form of <0.0f, 0.0f, 0.0f, w>.
shz_mat4x4_copyCopies the given src 4x4 matrix into the given dst 4x4 matrix.
shz_mat4x4_copy_unalignedCopies the given unaligned src 4x4 matrix into the given dst 4x4 matrix.
shz_mat4x4_swapSwaps the contents of the two given matrices, matA and matB.
shz_mat4x4_blendAdds and accumulates a scaled 4x4 matrix into the given matrix.
SymbolDescription
shz_mat4x4_tStructure representing a 4x4 column-major matrix.
shz_mat4x4Alternate shz_mat4x4_t C typedef for those who hate POSIX style.
shz_mat2x2_tStructure representing a 2x2 column-major matrix.
shz_mat3x3_tStructure representing a 3x3 column-major matrix.
shz_mat3x4_tStructure representing a 3x4 column-major matrix.
shz_mat4x3_tStructure representing a 4x3 column-major matrix.
SymbolDescription
shz_mat3x3_transposeStores the transpose of 3x3 matrix mat within out.
shz_mat3x3_inverse_unscaledComputes the inverse of an orthonormal 3x3 matrix.
shz_mat3x3_inverseComputes the inverse of a 3x3 matrix.
shz_mat3x3_scaleUniformly scales each element within a 3x3 matrix by the given value.
shz_mat3x3_transform_vec3Transforms an input vector by a 3x3 matrix, returning the resulting vector.
shz_mat3x3_transform_vec3_transposeTransforms an input vector by the transpose of a 3x3 matrix, returning the resulting vector.