Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
matrix4_ptr.c File Reference
#include <string.h>
#include <vecmat.h>
#include "features/cpu.h"

Go to the source code of this file.

Functions

void mat4_identity_ptr (matrix4 *res)
 Sets the matrix to the identity matrix.
VECMAT_SCALAR_API void mat4_mul_ptr_scalar (matrix4 *res, const matrix4 *a, const matrix4 *b)
 Multiplies two 4x4 matrices.
VECMAT_SCALAR_API void mat4_transpose_ptr_scalar (matrix4 *res, const matrix4 *m)
 Transposes the given 4x4 matrix.
void mat4_mul_ptr (matrix4 *res, const matrix4 *a, const matrix4 *b)
void mat4_transpose_ptr (matrix4 *res, const matrix4 *m)
void mat4_inverse_ptr (matrix4 *res, const matrix4 *m)
 Computes the inverse of a 4x4 matrix.
void mat4_translate_ptr (matrix4 *res, const vector3 *v)
 Sets the matrix to a translation matrix.
void mat4_scale_ptr (matrix4 *res, const vector3 *v)
 Sets the matrix to a scaling matrix using the provided scale vector.
void mat4_rotation_ptr (matrix4 *res, const vector3 *axis, const vm_float_t angle)
 Sets the matrix to a rotation matrix around the specified axis by the given angle.
void mat4_rotation_x_ptr (matrix4 *res, const vm_float_t degrees)
 Builds a 4x4 rotation matrix around the X axis (degrees).
void mat4_rotation_y_ptr (matrix4 *res, const vm_float_t degrees)
 Builds a 4x4 rotation matrix around the Y axis (degrees).
void mat4_rotation_z_ptr (matrix4 *res, const vm_float_t degrees)
 Builds a 4x4 rotation matrix around the Z axis (degrees).
void mat4_from_mat3_ptr (matrix4 *res, const matrix3 *m)
 Embeds a matrix3 into the upper-left of a matrix4.
void mat4_trs_ptr (matrix4 *res, const vector3 *translation, const quaternion *rotation, const vector3 *scale)
 Builds a 4x4 TRS matrix from translation, rotation, and scale.
void mat4_extract_translation_ptr (vector3 *res, const matrix4 *m)
 Extracts the translation vector from a matrix4.
void mat4_extract_scale_ptr (vector3 *res, const matrix4 *m)
 Extracts the scale vector from a matrix4.
void mat4_extract_rotation_ptr (quaternion *res, const matrix4 *m)
 Extracts the rotation quaternion from a matrix4.
void mat4_perspective_ptr (matrix4 *res, const vm_float_t fov, const vm_float_t aspect, const vm_float_t near, const vm_float_t far)
 Creates a perspective projection matrix.
void mat4_ortho_ptr (matrix4 *res, const vm_float_t left, const vm_float_t right, const vm_float_t bottom, const vm_float_t top, const vm_float_t near, const vm_float_t far)
 Sets the matrix to an orthographic projection matrix.
void mat4_look_at_ptr (matrix4 *res, const vector3 *position, const vector3 *target, const vector3 *up)
 Constructs a view matrix for a camera positioned at the given location, looking towards a target, with a specified up direction.
void mat4_perspective_fov_ptr (matrix4 *res, const vm_float_t fov, const vm_float_t w, const vm_float_t h, const vm_float_t n, const vm_float_t f)
 Sets the matrix to a perspective projection matrix.
void mat4_perspective_infinite_ptr (matrix4 *res, vm_float_t const fov_y, vm_float_t const aspect, vm_float_t const n)
 Sets the matrix to an infinite perspective projection matrix.
VECMAT_SCALAR_API void mat4_mul_vec4_ptr_scalar (vector4 *res, const matrix4 *m, const vector4 *v)
 Multiplies a 4x4 matrix by a vector4.
void mat4_mul_vec4_ptr (vector4 *res, const matrix4 *m, const vector4 *v)
VECMAT_SCALAR_API void mat4_mul_vec3_ptr_scalar (vector3 *res, const matrix4 *m, const vector3 *v, const vm_float_t w)
 Transforms a vector3 by a 4x4 matrix using homogeneous w.
void mat4_mul_vec3_ptr (vector3 *res, const matrix4 *m, const vector3 *v, const vm_float_t w)

Function Documentation

◆ mat4_extract_rotation_ptr()

void mat4_extract_rotation_ptr ( quaternion * res,
const matrix4 * m )

◆ mat4_extract_scale_ptr()

void mat4_extract_scale_ptr ( vector3 * res,
const matrix4 * m )

Extracts the scale vector from a matrix4.

Parameters
resOutput value.
mInput matrix.

Definition at line 323 of file matrix4_ptr.c.

References matrix4::m11, matrix4::m12, matrix4::m13, matrix4::m21, matrix4::m22, matrix4::m23, matrix4::m31, matrix4::m32, matrix4::m33, VECMAT_SQRT, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_extract_rotation_ptr(), and mat4_extract_scale().

◆ mat4_extract_translation_ptr()

void mat4_extract_translation_ptr ( vector3 * res,
const matrix4 * m )

Extracts the translation vector from a matrix4.

Parameters
resOutput value.
mInput matrix.

Definition at line 310 of file matrix4_ptr.c.

References matrix4::m14, matrix4::m24, matrix4::m34, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_extract_translation().

◆ mat4_from_mat3_ptr()

void mat4_from_mat3_ptr ( matrix4 * res,
const matrix3 * m )

◆ mat4_identity_ptr()

void mat4_identity_ptr ( matrix4 * res)

Sets the matrix to the identity matrix.

The identity matrix is a 4x4 matrix with 1s on the main diagonal and 0s elsewhere.

Parameters
resPointer to the matrix4 to set to identity.

Definition at line 16 of file matrix4_ptr.c.

References matrix4::v.

Referenced by mat4_from_mat3_ptr(), mat4_identity(), mat4_rotation_x_ptr(), mat4_rotation_y_ptr(), mat4_rotation_z_ptr(), and quat_to_mat4_ptr().

◆ mat4_inverse_ptr()

void mat4_inverse_ptr ( matrix4 * res,
const matrix4 * m )

Computes the inverse of a 4x4 matrix.

This function calculates the inverse of the given 4x4 matrix using the adjugate matrix and determinant. If the matrix is singular (determinant is zero), the result is set to the identity matrix.

Parameters
resPointer to the matrix4 where the inverse will be stored.
mPointer to the matrix4 to invert.

Definition at line 98 of file matrix4_ptr.c.

References matrix4::v.

Referenced by mat4_inverse().

◆ mat4_look_at_ptr()

void mat4_look_at_ptr ( matrix4 * res,
const vector3 * position,
const vector3 * target,
const vector3 * up )

Constructs a view matrix for a camera positioned at the given location, looking towards a target, with a specified up direction.

This function computes a 4x4 view matrix that orients the camera at the position vector, directing it towards the target vector, while aligning the up direction. The matrix is stored in column-major order.

Parameters
resPointer to the matrix4 where the result is stored.
positionPointer to the vector3 representing the camera's position.
targetPointer to the vector3 representing the point the camera is looking at.
upPointer to the vector3 representing the up direction for the camera.

Definition at line 418 of file matrix4_ptr.c.

References mat4_identity(), matrix4::v, vec3_cross(), vec3_dot(), vec3_normalize(), vec3_sub(), vector3::x, vector3::y, and vector3::z.

Referenced by mat4_look_at().

◆ mat4_mul_ptr()

void mat4_mul_ptr ( matrix4 * res,
const matrix4 * a,
const matrix4 * b )

Definition at line 69 of file matrix4_ptr.c.

References mat4_mul_ptr_scalar(), and vm_cpu_init().

Referenced by mat4_mul().

◆ mat4_mul_ptr_scalar()

VECMAT_SCALAR_API void mat4_mul_ptr_scalar ( matrix4 * res,
const matrix4 * a,
const matrix4 * b )

Multiplies two 4x4 matrices.

This function computes the product of two input matrices and stores the result in the provided matrix. The multiplication follows standard matrix multiplication rules.

Parameters
resPointer to the matrix4 where the result is stored.
aPointer to the first matrix4 operand.
bPointer to the second matrix4 operand.

Definition at line 35 of file matrix4_ptr.c.

References matrix4::v, and VECMAT_SCALAR_API.

Referenced by mat4_mul_ptr().

◆ mat4_mul_vec3_ptr()

void mat4_mul_vec3_ptr ( vector3 * res,
const matrix4 * m,
const vector3 * v,
const vm_float_t w )

Definition at line 538 of file matrix4_ptr.c.

References mat4_mul_vec3_ptr_scalar(), and vm_cpu_init().

Referenced by mat4_mul_vec3().

◆ mat4_mul_vec3_ptr_scalar()

VECMAT_SCALAR_API void mat4_mul_vec3_ptr_scalar ( vector3 * res,
const matrix4 * m,
const vector3 * v,
const vm_float_t w )

Transforms a vector3 by a 4x4 matrix using homogeneous w.

Parameters
resOutput value.
mInput matrix.
vInput vector.
wHomogeneous w component.

Definition at line 528 of file matrix4_ptr.c.

References matrix4::m11, matrix4::m12, matrix4::m13, matrix4::m14, matrix4::m21, matrix4::m22, matrix4::m23, matrix4::m24, matrix4::m31, matrix4::m32, matrix4::m33, matrix4::m34, VECMAT_SCALAR_API, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_mul_vec3_ptr().

◆ mat4_mul_vec4_ptr()

void mat4_mul_vec4_ptr ( vector4 * res,
const matrix4 * m,
const vector4 * v )

Definition at line 510 of file matrix4_ptr.c.

References mat4_mul_vec4_ptr_scalar(), and vm_cpu_init().

Referenced by mat4_mul_vec4().

◆ mat4_mul_vec4_ptr_scalar()

VECMAT_SCALAR_API void mat4_mul_vec4_ptr_scalar ( vector4 * res,
const matrix4 * m,
const vector4 * v )

◆ mat4_ortho_ptr()

void mat4_ortho_ptr ( matrix4 * res,
const vm_float_t left,
const vm_float_t right,
const vm_float_t bottom,
const vm_float_t top,
const vm_float_t near,
const vm_float_t far )

Sets the matrix to an orthographic projection matrix.

This function configures the matrix for an orthographic projection, mapping a rectangular frustum defined by the clipping planes to the canonical view volume.

Parameters
resPointer to the matrix4 to set to the orthographic projection matrix.
leftThe left clipping plane coordinate.
rightThe right clipping plane coordinate.
bottomThe bottom clipping plane coordinate.
topThe top clipping plane coordinate.
nearThe near clipping plane coordinate.
farThe far clipping plane coordinate.

Definition at line 393 of file matrix4_ptr.c.

References matrix4::v.

Referenced by mat4_ortho().

◆ mat4_perspective_fov_ptr()

void mat4_perspective_fov_ptr ( matrix4 * res,
const vm_float_t fov,
const vm_float_t w,
const vm_float_t h,
const vm_float_t n,
const vm_float_t f )

Sets the matrix to a perspective projection matrix.

This function constructs a right-handed perspective projection matrix using the specified field of view, viewport width and height, near clipping plane, and far clipping plane.

Parameters
resPointer to the matrix4 to set to the perspective projection matrix.
fovField of view angle in degrees.
wViewport width.
hViewport height.
nNear clipping plane distance.
fFar clipping plane distance.

Definition at line 452 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::v, and VECMAT_TAN.

Referenced by mat4_perspective_fov().

◆ mat4_perspective_infinite_ptr()

void mat4_perspective_infinite_ptr ( matrix4 * res,
vm_float_t const fov_y,
vm_float_t const aspect,
vm_float_t const n )

Sets the matrix to an infinite perspective projection matrix.

This function constructs a perspective projection matrix with an infinite far plane, which is useful for rendering scenes where depth precision is less critical beyond the near plane. The matrix is set such that the field of view and aspect ratio are applied, with the near plane at distance n.

Parameters
resPointer to the matrix4 to set to the infinite perspective projection.
fov_yVertical field of view in degrees.
aspectAspect ratio of the viewport (width / height).
nDistance to the near clipping plane.

Definition at line 479 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::v, and VECMAT_TAN.

Referenced by mat4_perspective_infinite().

◆ mat4_perspective_ptr()

void mat4_perspective_ptr ( matrix4 * res,
const vm_float_t fov,
const vm_float_t aspect,
const vm_float_t near,
const vm_float_t far )

Creates a perspective projection matrix.

This function computes a right-handed perspective projection matrix based on the given field of view, aspect ratio, and near and far clipping planes. The matrix is stored in the provided result pointer.

Parameters
resPointer to the matrix4 to store the perspective projection matrix.
fovField of view in degrees.
aspectAspect ratio of the viewport (width divided by height).
nearDistance to the near clipping plane.
farDistance to the far clipping plane.

Definition at line 365 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::v, and VECMAT_TAN.

Referenced by mat4_perspective().

◆ mat4_rotation_ptr()

void mat4_rotation_ptr ( matrix4 * res,
const vector3 * axis,
const vm_float_t angle )

Sets the matrix to a rotation matrix around the specified axis by the given angle.

The rotation is performed around the normalized axis vector by the specified angle in degrees. The resulting matrix is a 4x4 rotation matrix stored in column-major order.

Parameters
resPointer to the matrix4 to store the resulting rotation matrix.
axisPointer to the vector3 representing the axis of rotation.
angleThe rotation angle in degrees.

Definition at line 195 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::v, vec3_normalize(), VECMAT_COS, VECMAT_SIN, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_rotation().

◆ mat4_rotation_x_ptr()

void mat4_rotation_x_ptr ( matrix4 * res,
const vm_float_t degrees )

Builds a 4x4 rotation matrix around the X axis (degrees).

Parameters
resOutput value.
degreesRotation angle in degrees.

Definition at line 223 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::m22, matrix4::m23, matrix4::m32, matrix4::m33, mat4_identity_ptr(), VECMAT_COS, and VECMAT_SIN.

Referenced by mat4_rotation_x().

◆ mat4_rotation_y_ptr()

void mat4_rotation_y_ptr ( matrix4 * res,
const vm_float_t degrees )

Builds a 4x4 rotation matrix around the Y axis (degrees).

Parameters
resOutput value.
degreesRotation angle in degrees.

Definition at line 241 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::m11, matrix4::m13, matrix4::m31, matrix4::m33, mat4_identity_ptr(), VECMAT_COS, and VECMAT_SIN.

Referenced by mat4_rotation_y().

◆ mat4_rotation_z_ptr()

void mat4_rotation_z_ptr ( matrix4 * res,
const vm_float_t degrees )

Builds a 4x4 rotation matrix around the Z axis (degrees).

Parameters
resOutput value.
degreesRotation angle in degrees.

Definition at line 259 of file matrix4_ptr.c.

References deg_to_rad(), matrix4::m11, matrix4::m12, matrix4::m21, matrix4::m22, mat4_identity_ptr(), VECMAT_COS, and VECMAT_SIN.

Referenced by mat4_rotation_z().

◆ mat4_scale_ptr()

void mat4_scale_ptr ( matrix4 * res,
const vector3 * v )

Sets the matrix to a scaling matrix using the provided scale vector.

This function constructs a 4x4 scaling matrix where the diagonal elements correspond to the x, y, and z scale factors from the input vector. The bottom-right element is set to 1.0f for homogeneous coordinates. All other elements are implicitly zero (not set, assuming the matrix is initialized).

Parameters
resPointer to the matrix4 to set as a scaling matrix.
vPointer to the vector3 containing the scale factors (x, y, z).

Definition at line 176 of file matrix4_ptr.c.

References matrix4::v, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_scale().

◆ mat4_translate_ptr()

void mat4_translate_ptr ( matrix4 * res,
const vector3 * v )

Sets the matrix to a translation matrix.

This function initializes a 4x4 matrix to represent a translation transformation based on the provided vector, with the translation components placed in the last column and the rest forming an identity matrix.

Parameters
resPointer to the matrix4 to set.
vPointer to the vector3 containing the translation values.

Definition at line 145 of file matrix4_ptr.c.

References matrix4::v, vector3::x, vector3::y, and vector3::z.

Referenced by mat4_translate().

◆ mat4_transpose_ptr()

void mat4_transpose_ptr ( matrix4 * res,
const matrix4 * m )

Definition at line 79 of file matrix4_ptr.c.

References mat4_transpose_ptr_scalar(), and vm_cpu_init().

Referenced by mat4_transpose().

◆ mat4_transpose_ptr_scalar()

VECMAT_SCALAR_API void mat4_transpose_ptr_scalar ( matrix4 * res,
const matrix4 * m )

Transposes the given 4x4 matrix.

This function computes the transpose of the input matrix, where the element at row i and column j becomes the element at row j and column i in the result matrix.

Parameters
resPointer to the matrix4 where the transposed result will be stored.
mPointer to the constant matrix4 to be transposed.

Definition at line 60 of file matrix4_ptr.c.

References matrix4::v, and VECMAT_SCALAR_API.

Referenced by mat4_transpose_ptr().

◆ mat4_trs_ptr()

void mat4_trs_ptr ( matrix4 * res,
const vector3 * translation,
const quaternion * rotation,
const vector3 * scale )

Builds a 4x4 TRS matrix from translation, rotation, and scale.

Parameters
resOutput value.
translationTranslation vector.
rotationRotation quaternion.
scaleScale vector.

Definition at line 293 of file matrix4_ptr.c.

References matrix4::m11, matrix4::m12, matrix4::m13, matrix4::m14, matrix4::m21, matrix4::m22, matrix4::m23, matrix4::m24, matrix4::m31, matrix4::m32, matrix4::m33, matrix4::m34, quat_to_mat4_ptr(), vector3::x, vector3::y, and vector3::z.

Referenced by mat4_trs().