259 lines
9.1 KiB
Plaintext
259 lines
9.1 KiB
Plaintext
#! 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;
|
|
import std::serial;
|
|
module malunal::linear;
|
|
|
|
#! A matrix of four by four, column after column. `std::serial` writes it as a
|
|
#! sequence of its 16 numbers, in the same order.
|
|
pub struct mat4 {
|
|
pub var m: f32[16];
|
|
}
|
|
|
|
pub def mat4::encode(&this, out: &mut serial::encoder): void {
|
|
out.begin_array(16);
|
|
for i in 0..16u {
|
|
serial::encode(&this.m[i], out);
|
|
}
|
|
out.end_array();
|
|
}
|
|
|
|
pub def mat4::decode(from: &serial::decoder, at: u32, given: serial::options): result<mat4, serial::error> {
|
|
if from.kind_of(at) != serial::kind::array || from.count(at) != 16 {
|
|
return serial::error::at(serial::error_kind::wrong_kind, from.place(at));
|
|
}
|
|
var made := mat4{};
|
|
var item: u32 = 0;
|
|
for i in 0..16u {
|
|
item = i == 0 ? from.element(at, 0) : from.next_element(at, item);
|
|
val read := serial::decode::<f32>(from, item, given) else (e) { return e; };
|
|
made.m[i] = read;
|
|
}
|
|
return made;
|
|
}
|
|
|
|
#! 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 a vector, as a projection makes a place of clip
|
|
#! space: each column, scaled by one of its numbers, added.
|
|
pub def mat4::mul(&this, v: &vec4): vec4 {
|
|
if lanes_held() {
|
|
return of_lanes::<vec4>(lanes_of(&this.m[0]) * f32x4::splat(v.x) + lanes_of(&this.m[4]) * f32x4::splat(v.y)
|
|
+ lanes_of(&this.m[8]) * f32x4::splat(v.z) + lanes_of(&this.m[12]) * f32x4::splat(v.w));
|
|
}
|
|
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 {
|
|
if lanes_held() {
|
|
return of_lanes::<vec3>(lanes_of(&this.m[0]) * f32x4::splat(p.x) + lanes_of(&this.m[4]) * f32x4::splat(p.y)
|
|
+ lanes_of(&this.m[8]) * f32x4::splat(p.z) + lanes_of(&this.m[12]));
|
|
}
|
|
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 {
|
|
if lanes_held() {
|
|
return of_lanes::<vec3>(lanes_of(&this.m[0]) * f32x4::splat(v.x) + lanes_of(&this.m[4]) * f32x4::splat(v.y)
|
|
+ lanes_of(&this.m[8]) * f32x4::splat(v.z));
|
|
}
|
|
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
|
|
#! the determinants of two by two its first two columns make, and its last
|
|
#! two, each cofactor of three of them, which is half the work of the
|
|
#! cofactors written out, a number at a time.
|
|
pub def mat4::inverse(&this): mat4 {
|
|
val a := &this.m;
|
|
val s0 := a[0] * a[5] - a[4] * a[1];
|
|
val s1 := a[0] * a[6] - a[4] * a[2];
|
|
val s2 := a[0] * a[7] - a[4] * a[3];
|
|
val s3 := a[1] * a[6] - a[5] * a[2];
|
|
val s4 := a[1] * a[7] - a[5] * a[3];
|
|
val s5 := a[2] * a[7] - a[6] * a[3];
|
|
val c5 := a[10] * a[15] - a[14] * a[11];
|
|
val c4 := a[9] * a[15] - a[13] * a[11];
|
|
val c3 := a[9] * a[14] - a[13] * a[10];
|
|
val c2 := a[8] * a[15] - a[12] * a[11];
|
|
val c1 := a[8] * a[14] - a[12] * a[10];
|
|
val c0 := a[8] * a[13] - a[12] * a[9];
|
|
val det := s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0;
|
|
# Scaled by 0 where nothing undoes it, so it returns once.
|
|
val k := det != 0 ? 1 / det : 0;
|
|
return mat4{ m = [
|
|
(a[5] * c5 - a[6] * c4 + a[7] * c3) * k,
|
|
(a[2] * c4 - a[1] * c5 - a[3] * c3) * k,
|
|
(a[13] * s5 - a[14] * s4 + a[15] * s3) * k,
|
|
(a[10] * s4 - a[9] * s5 - a[11] * s3) * k,
|
|
(a[6] * c2 - a[4] * c5 - a[7] * c1) * k,
|
|
(a[0] * c5 - a[2] * c2 + a[3] * c1) * k,
|
|
(a[14] * s2 - a[12] * s5 - a[15] * s1) * k,
|
|
(a[8] * s5 - a[10] * s2 + a[11] * s1) * k,
|
|
(a[4] * c4 - a[5] * c2 + a[7] * c0) * k,
|
|
(a[1] * c2 - a[0] * c4 - a[3] * c0) * k,
|
|
(a[12] * s4 - a[13] * s2 + a[15] * s0) * k,
|
|
(a[9] * s2 - a[8] * s4 - a[11] * s0) * k,
|
|
(a[5] * c1 - a[4] * c3 - a[6] * c0) * k,
|
|
(a[0] * c3 - a[1] * c1 + a[2] * c0) * k,
|
|
(a[13] * s1 - a[12] * s3 - a[14] * s0) * k,
|
|
(a[8] * s3 - a[9] * s1 + a[10] * s0) * k,
|
|
] };
|
|
}
|