#[repr(C)]pub struct Mat4 {
pub cols: [Vec4; 4],
}
Expand description
A 4x4 column-major matrix, used for 3D affine transformations.
This is the primary type for representing transformations (translation, rotation, scale) in 3D space. It is also used for camera view and projection matrices. The memory layout is column-major, which is compatible with modern graphics APIs like Vulkan, Metal, and DirectX.
Fields§
§cols: [Vec4; 4]
The columns of the matrix. cols[0]
is the first column, and so on.
Implementations§
Source§impl Mat4
impl Mat4
Sourcepub fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Self
pub fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Self
Creates a new matrix from four column vectors.
Sourcepub fn from_translation(v: Vec3) -> Self
pub fn from_translation(v: Vec3) -> Self
Sourcepub fn from_scale(scale: Vec3) -> Self
pub fn from_scale(scale: Vec3) -> Self
Creates a non-uniform scaling matrix.
Sourcepub fn from_rotation_x(angle: f32) -> Self
pub fn from_rotation_x(angle: f32) -> Self
Creates a matrix for a rotation around the X-axis.
§Arguments
angle
: The angle of rotation in radians.
Sourcepub fn from_rotation_y(angle: f32) -> Self
pub fn from_rotation_y(angle: f32) -> Self
Creates a matrix for a right-handed rotation around the Y-axis.
§Arguments
angle
: The angle of rotation in radians.
Sourcepub fn from_rotation_z(angle: f32) -> Self
pub fn from_rotation_z(angle: f32) -> Self
Creates a matrix for a rotation around the Z-axis.
§Arguments
angle
: The angle of rotation in radians.
Sourcepub fn from_axis_angle(axis: Vec3, angle: f32) -> Self
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self
Creates a rotation matrix from a normalized axis and an angle.
§Arguments
axis
: The axis of rotation. Must be a unit vector.angle
: The angle of rotation in radians.
Sourcepub fn from_quat(q: Quaternion) -> Self
pub fn from_quat(q: Quaternion) -> Self
Creates a rotation matrix from a quaternion.
Sourcepub fn perspective_rh_zo(
fov_y_radians: f32,
aspect_ratio: f32,
z_near: f32,
z_far: f32,
) -> Self
pub fn perspective_rh_zo( fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32, ) -> Self
Creates a right-handed perspective projection matrix with a [0, 1] depth range (ZO).
§Arguments
fov_y_radians
: Vertical field of view in radians.aspect_ratio
: Width divided by height of the viewport.z_near
: Distance to the near clipping plane (must be positive).z_far
: Distance to the far clipping plane (must be positive and >z_near
).
Sourcepub fn orthographic_rh_zo(
left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Self
pub fn orthographic_rh_zo( left: f32, right: f32, bottom: f32, top: f32, z_near: f32, z_far: f32, ) -> Self
Creates a right-handed orthographic projection matrix with a [0, 1] depth range (ZO).
Sourcepub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Option<Self>
pub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Option<Self>
Creates a right-handed view matrix for a camera looking from eye
towards target
.
§Arguments
eye
: The position of the camera in world space.target
: The point in world space that the camera is looking at.up
: A vector indicating the “up” direction of the world (commonlyVec3::Y
).
§Returns
Returns Some(Mat4)
if a valid view matrix can be constructed, or None
if
eye
and target
are too close, or if up
is parallel to the view direction.
Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Returns the transpose of the matrix, where rows and columns are swapped.
Sourcepub fn determinant(&self) -> f32
pub fn determinant(&self) -> f32
Computes the determinant of the matrix.
Sourcepub fn inverse(&self) -> Option<Self>
pub fn inverse(&self) -> Option<Self>
Computes the inverse of the matrix.
Returns None
if the matrix is not invertible.
Sourcepub fn affine_inverse(&self) -> Option<Self>
pub fn affine_inverse(&self) -> Option<Self>
Computes the inverse of an affine transformation matrix more efficiently
and with better numerical stability than the general inverse
method.
An affine matrix is one composed of only translation, rotation, and scale.
§Returns
None
if the matrix is not affine or is not invertible.