Files
physics/linear/mat4.sakura
T

212 lines
8.6 KiB
Plaintext
Raw Normal View History

2026-10-27 22:08:25 -05:00
#! Contains `mat4`, a matrix of four by four: what moves, turns, and scales a
#! place, or projects it, as a camera does. Its numbers are column after
#! column, so a column is 16 bytes, read as one `f32x4`, and its products are
#! each column scaled and added at once.
import std::math;
module linear;
#! A matrix of four by four, column after column.
pub struct mat4 {
pub var m: f32[16];
}
#! The matrix that changes nothing.
pub val mat4::identity := mat4{ m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] };
#! Moving by a vector.
pub def mat4::translation(by: vec3): mat4 {
var made := mat4::identity;
made.m[12] = by.x;
made.m[13] = by.y;
made.m[14] = by.z;
return made;
}
#! Scaling along each axis.
pub def mat4::scaling(by: vec3): mat4 {
var made := mat4::identity;
made.m[0] = by.x;
made.m[5] = by.y;
made.m[10] = by.z;
return made;
}
#! Turning by a rotation.
pub def mat4::rotation(by: quat): mat4 {
return mat4::compose(vec3::zero, by, vec3::one);
}
#! Scaling, then turning, then moving: what a transform does.
pub def mat4::compose(translation: vec3, rotation: quat, scale: vec3): mat4 {
val q := rotation;
val xx := q.x * q.x;
val yy := q.y * q.y;
val zz := q.z * q.z;
val xy := q.x * q.y;
val xz := q.x * q.z;
val yz := q.y * q.z;
val wx := q.w * q.x;
val wy := q.w * q.y;
val wz := q.w * q.z;
return mat4{ m = [
(1 - 2 * (yy + zz)) * scale.x, 2 * (xy + wz) * scale.x, 2 * (xz - wy) * scale.x, 0,
2 * (xy - wz) * scale.y, (1 - 2 * (xx + zz)) * scale.y, 2 * (yz + wx) * scale.y, 0,
2 * (xz + wy) * scale.z, 2 * (yz - wx) * scale.z, (1 - 2 * (xx + yy)) * scale.z, 0,
translation.x, translation.y, translation.z, 1,
] };
}
#! A perspective projection, as a camera's: its field of view up and down, the
#! width over the height it's drawn at, and the nearest and furthest it draws.
pub def mat4::perspective(fov_y: f32, aspect: f32, near: f32, far: f32): mat4 {
val f := 1 / math::tan(fov_y / 2);
var made := mat4{};
made.m[0] = f / aspect;
made.m[5] = f;
made.m[10] = far / (near - far);
made.m[11] = -1;
made.m[14] = near * far / (near - far);
return made;
}
#! An orthographic projection of a box: its edges, and how near and far it
#! reaches down -z.
pub def mat4::orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32): mat4 {
var made := mat4::identity;
made.m[0] = 2 / (right - left);
made.m[5] = 2 / (top - bottom);
made.m[10] = 1 / (near - far);
made.m[12] = -(right + left) / (right - left);
made.m[13] = -(top + bottom) / (top - bottom);
made.m[14] = near / (near - far);
return made;
}
#! The view of a camera at a place looking at another, y as near to `up` as it
#! can be: what moves the world into the camera's space.
pub def mat4::look_at(eye: vec3, target: vec3, up: vec3 = vec3::up): mat4 {
return mat4::compose(eye, quat::looking(target - eye, up), vec3::one).inverse();
}
#! The matrix doing another, then this: each of the other's columns, each of
#! this one's columns scaled by one of its numbers, added.
pub def mat4::mul(&this, other: &mat4): mat4 {
var made := mat4{};
if lanes_held() {
val c0 := lanes_of(&this.m[0]);
val c1 := lanes_of(&this.m[4]);
val c2 := lanes_of(&this.m[8]);
val c3 := lanes_of(&this.m[12]);
val out := (&mut made) as *mut f32x4;
for c in 0..4u {
out[c] = c0 * f32x4::splat(other.m[c * 4]) + c1 * f32x4::splat(other.m[c * 4 + 1])
+ c2 * f32x4::splat(other.m[c * 4 + 2]) + c3 * f32x4::splat(other.m[c * 4 + 3]);
}
return made;
}
for c in 0..4u {
for r in 0..4u {
made.m[c * 4 + r] = this.m[r] * other.m[c * 4] + this.m[4 + r] * other.m[c * 4 + 1]
+ this.m[8 + r] * other.m[c * 4 + 2] + this.m[12 + r] * other.m[c * 4 + 3];
}
}
return made;
}
# What the matrix makes of one vector is worked a number at a time, which an
# executable outruns lanes with for one.
pub def mat4::mul(&this, v: &vec4): vec4 {
return vec4{
this.m[0] * v.x + this.m[4] * v.y + this.m[8] * v.z + this.m[12] * v.w,
this.m[1] * v.x + this.m[5] * v.y + this.m[9] * v.z + this.m[13] * v.w,
this.m[2] * v.x + this.m[6] * v.y + this.m[10] * v.z + this.m[14] * v.w,
this.m[3] * v.x + this.m[7] * v.y + this.m[11] * v.z + this.m[15] * v.w,
};
}
pub def mat4::equals(&this, other: &mat4): bool {
for i in 0..16u {
return false if this.m[i] != other.m[i];
}
return true;
}
#! A place moved by the matrix, which moves, turns, and scales but doesn't
#! project.
pub def mat4::transform_point(&this, p: vec3): vec3 {
return vec3{
this.m[0] * p.x + this.m[4] * p.y + this.m[8] * p.z + this.m[12],
this.m[1] * p.x + this.m[5] * p.y + this.m[9] * p.z + this.m[13],
this.m[2] * p.x + this.m[6] * p.y + this.m[10] * p.z + this.m[14],
};
}
#! A direction turned and scaled by the matrix, which a move doesn't change.
pub def mat4::transform_vector(&this, v: vec3): vec3 {
return vec3{
this.m[0] * v.x + this.m[4] * v.y + this.m[8] * v.z,
this.m[1] * v.x + this.m[5] * v.y + this.m[9] * v.z,
this.m[2] * v.x + this.m[6] * v.y + this.m[10] * v.z,
};
}
#! Where the matrix moves the origin to.
pub val mat4::translation_of: vec3 {
get => vec3{ this.m[12], this.m[13], this.m[14] };
}
#! A column, from 0.
pub def mat4::column(&this, which: u64): vec4 {
return vec4{ this.m[which * 4], this.m[which * 4 + 1], this.m[which * 4 + 2], this.m[which * 4 + 3] };
}
#! What it does to directions: its first three columns' first three numbers.
pub def mat4::linear_part(&this): mat3 {
return mat3{
vec3{ this.m[0], this.m[1], this.m[2] },
vec3{ this.m[4], this.m[5], this.m[6] },
vec3{ this.m[8], this.m[9], this.m[10] },
};
}
pub def mat4::transposed(&this): mat4 {
var made := mat4{};
for c in 0..4u {
for r in 0..4u {
made.m[c * 4 + r] = this.m[r * 4 + c];
}
}
return made;
}
#! The matrix undoing this, or none where nothing can, as a scale of zero: of
#! cofactors, a number at a time, which outruns gathering them into lanes.
pub def mat4::inverse(&this): mat4 {
val a := &this.m;
var inv: f32[16];
inv[0] = a[5] * a[10] * a[15] - a[5] * a[11] * a[14] - a[9] * a[6] * a[15] + a[9] * a[7] * a[14] + a[13] * a[6] * a[11] - a[13] * a[7] * a[10];
inv[4] = -a[4] * a[10] * a[15] + a[4] * a[11] * a[14] + a[8] * a[6] * a[15] - a[8] * a[7] * a[14] - a[12] * a[6] * a[11] + a[12] * a[7] * a[10];
inv[8] = a[4] * a[9] * a[15] - a[4] * a[11] * a[13] - a[8] * a[5] * a[15] + a[8] * a[7] * a[13] + a[12] * a[5] * a[11] - a[12] * a[7] * a[9];
inv[12] = -a[4] * a[9] * a[14] + a[4] * a[10] * a[13] + a[8] * a[5] * a[14] - a[8] * a[6] * a[13] - a[12] * a[5] * a[10] + a[12] * a[6] * a[9];
inv[1] = -a[1] * a[10] * a[15] + a[1] * a[11] * a[14] + a[9] * a[2] * a[15] - a[9] * a[3] * a[14] - a[13] * a[2] * a[11] + a[13] * a[3] * a[10];
inv[5] = a[0] * a[10] * a[15] - a[0] * a[11] * a[14] - a[8] * a[2] * a[15] + a[8] * a[3] * a[14] + a[12] * a[2] * a[11] - a[12] * a[3] * a[10];
inv[9] = -a[0] * a[9] * a[15] + a[0] * a[11] * a[13] + a[8] * a[1] * a[15] - a[8] * a[3] * a[13] - a[12] * a[1] * a[11] + a[12] * a[3] * a[9];
inv[13] = a[0] * a[9] * a[14] - a[0] * a[10] * a[13] - a[8] * a[1] * a[14] + a[8] * a[2] * a[13] + a[12] * a[1] * a[10] - a[12] * a[2] * a[9];
inv[2] = a[1] * a[6] * a[15] - a[1] * a[7] * a[14] - a[5] * a[2] * a[15] + a[5] * a[3] * a[14] + a[13] * a[2] * a[7] - a[13] * a[3] * a[6];
inv[6] = -a[0] * a[6] * a[15] + a[0] * a[7] * a[14] + a[4] * a[2] * a[15] - a[4] * a[3] * a[14] - a[12] * a[2] * a[7] + a[12] * a[3] * a[6];
inv[10] = a[0] * a[5] * a[15] - a[0] * a[7] * a[13] - a[4] * a[1] * a[15] + a[4] * a[3] * a[13] + a[12] * a[1] * a[7] - a[12] * a[3] * a[5];
inv[14] = -a[0] * a[5] * a[14] + a[0] * a[6] * a[13] + a[4] * a[1] * a[14] - a[4] * a[2] * a[13] - a[12] * a[1] * a[6] + a[12] * a[2] * a[5];
inv[3] = -a[1] * a[6] * a[11] + a[1] * a[7] * a[10] + a[5] * a[2] * a[11] - a[5] * a[3] * a[10] - a[9] * a[2] * a[7] + a[9] * a[3] * a[6];
inv[7] = a[0] * a[6] * a[11] - a[0] * a[7] * a[10] - a[4] * a[2] * a[11] + a[4] * a[3] * a[10] + a[8] * a[2] * a[7] - a[8] * a[3] * a[6];
inv[11] = -a[0] * a[5] * a[11] + a[0] * a[7] * a[9] + a[4] * a[1] * a[11] - a[4] * a[3] * a[9] - a[8] * a[1] * a[7] + a[8] * a[3] * a[5];
inv[15] = a[0] * a[5] * a[10] - a[0] * a[6] * a[9] - a[4] * a[1] * a[10] + a[4] * a[2] * a[9] + a[8] * a[1] * a[6] - a[8] * a[2] * a[5];
val det := a[0] * inv[0] + a[1] * inv[4] + a[2] * inv[8] + a[3] * inv[12];
# Scaled by 0 where nothing undoes it, so it returns once.
val k := det != 0 ? 1 / det : 0;
for i in 0..16u {
inv[i] *= k;
}
return mat4{ m = inv };
}