Skip to content

struct

Mat4

pub struct Mat4 {
    pub m: [f32; 16],
}

A 4×4 matrix stored column-major (m[col * 4 + row]), as WebGL expects.

Fields

m

pub m: [f32; 16]

The 16 values, column by column.

Methods

IDENTITY

pub const IDENTITY: Mat4;

The identity matrix: no transform.

mul

pub fn mul(&self, o: &Mat4) -> Mat4

self * o — applies o first, then self.

translation

pub fn translation(t: Vec3) -> Mat4

A translation by t.

scaling

pub fn scaling(s: Vec3) -> Mat4

A scaling by s along each axis.

rotation_x

pub fn rotation_x(a: f32) -> Mat4

A rotation by a radians about the x axis.

rotation_y

pub fn rotation_y(a: f32) -> Mat4

A rotation by a radians about the y axis.

rotation_z

pub fn rotation_z(a: f32) -> Mat4

A rotation by a radians about the z axis.

perspective

pub fn perspective(fovy: f32, aspect: f32, near: f32, far: f32) -> Mat4

OpenGL-style perspective projection (fovy in radians).

orthographic

pub fn orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4

OpenGL-style orthographic projection of a view-space box.

look_at

pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Mat4

View matrix for a camera at eye looking at target.

transform_point

pub fn transform_point(&self, p: Vec3) -> Vec3

Transform a point, including the perspective divide.

transform_w

pub fn transform_w(&self, p: Vec3) -> f32

The clip-space w of a transformed point (negative ⇒ behind camera).

to_array

pub fn to_array(&self) -> [f32; 16]

The 16 values, column-major, as WebGL takes them.

transform_direction

pub fn transform_direction(&self, d: Vec3) -> Vec3

Transform a direction (no translation, no divide).

from_quat

pub fn from_quat(q: [f32; 4]) -> Mat4

Rotation from a unit quaternion (x, y, z, w) (glTF's order).

from_trs

pub fn from_trs(t: Vec3, r: [f32; 4], s: Vec3) -> Mat4

Translation × rotation (quaternion) × scale.

inverse

pub fn inverse(&self) -> Mat4

The inverse (identity if the matrix is singular).

axis_scale

pub fn axis_scale(&self) -> Vec3

How much it scales along each of its own axes (the lengths of its first three columns).

transpose

pub fn transpose(&self) -> Mat4

The transpose.

normal_matrix

pub fn normal_matrix(&self) -> [f32; 9]

The 3×3 normal matrix (inverse transpose of the upper left), as nine column-major values.

Trait implementations