101 lines
3.4 KiB
Plaintext
101 lines
3.4 KiB
Plaintext
#! Contains `mat3`, a matrix of three by three: a rotation, a scale, or what
|
|
#! turns and stretches a direction without moving it, as an inertia tensor
|
|
#! does. Its columns are `vec3`s, each 16 bytes, so it's laid out as std140
|
|
#! lays out a GLSL `mat3`, and its columns are added and scaled as lanes.
|
|
|
|
module linear;
|
|
|
|
#! A matrix of three by three, column after column: where it takes x, y, and z.
|
|
pub struct mat3 {
|
|
pub var x_axis: vec3;
|
|
pub var y_axis: vec3;
|
|
pub var z_axis: vec3;
|
|
}
|
|
|
|
#! The matrix that changes nothing.
|
|
pub val mat3::identity := mat3{ vec3::right, vec3::up, vec3::back };
|
|
|
|
#! Turning by a rotation.
|
|
pub def mat3::rotation(by: quat): mat3 {
|
|
val q := by;
|
|
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 mat3{
|
|
vec3{ 1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy) },
|
|
vec3{ 2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx) },
|
|
vec3{ 2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy) },
|
|
};
|
|
}
|
|
|
|
#! Scaling along each axis.
|
|
pub def mat3::scaling(by: vec3): mat3 {
|
|
return mat3{ vec3{ by.x, 0, 0 }, vec3{ 0, by.y, 0 }, vec3{ 0, 0, by.z } };
|
|
}
|
|
|
|
#! The matrix doing another, then this: each of the other's columns, as this
|
|
#! turns and scales it. A number at a time, which an executable outruns lanes
|
|
#! with for three columns.
|
|
pub def mat3::mul(&this, other: &mat3): mat3 {
|
|
return mat3{ this * other.x_axis, this * other.y_axis, this * other.z_axis };
|
|
}
|
|
|
|
#! A direction the matrix turns and scales: each column, scaled by one of its
|
|
#! numbers, added. A number at a time, which an executable outruns lanes with
|
|
#! for one vector.
|
|
pub def mat3::mul(&this, v: &vec3): vec3 {
|
|
return vec3{
|
|
this.x_axis.x * v.x + this.y_axis.x * v.y + this.z_axis.x * v.z,
|
|
this.x_axis.y * v.x + this.y_axis.y * v.y + this.z_axis.y * v.z,
|
|
this.x_axis.z * v.x + this.y_axis.z * v.y + this.z_axis.z * v.z,
|
|
};
|
|
}
|
|
|
|
pub def mat3::mul(&this, by: &f32): mat3 {
|
|
return mat3{ this.x_axis * by, this.y_axis * by, this.z_axis * by };
|
|
}
|
|
|
|
pub def mat3::add(&this, other: &mat3): mat3 {
|
|
return mat3{ this.x_axis + other.x_axis, this.y_axis + other.y_axis, this.z_axis + other.z_axis };
|
|
}
|
|
|
|
pub def mat3::sub(&this, other: &mat3): mat3 {
|
|
return mat3{ this.x_axis - other.x_axis, this.y_axis - other.y_axis, this.z_axis - other.z_axis };
|
|
}
|
|
|
|
pub def mat3::equals(&this, other: &mat3): bool {
|
|
return this.x_axis == other.x_axis && this.y_axis == other.y_axis && this.z_axis == other.z_axis;
|
|
}
|
|
|
|
#! Its rows as its columns, which for a rotation is the rotation back.
|
|
pub def mat3::transposed(&this): mat3 {
|
|
return mat3{
|
|
vec3{ this.x_axis.x, this.y_axis.x, this.z_axis.x },
|
|
vec3{ this.x_axis.y, this.y_axis.y, this.z_axis.y },
|
|
vec3{ this.x_axis.z, this.y_axis.z, this.z_axis.z },
|
|
};
|
|
}
|
|
|
|
#! How much it scales a volume, negative where it mirrors one.
|
|
pub val mat3::determinant: f32 {
|
|
get => this.x_axis.dot(this.y_axis.cross(this.z_axis));
|
|
}
|
|
|
|
#! The matrix undoing this, or none where nothing can, as a scale of zero: each
|
|
#! row the cross product of the other two columns, over the determinant.
|
|
pub def mat3::inverse(&this): mat3 {
|
|
val a := this.y_axis.cross(this.z_axis);
|
|
val b := this.z_axis.cross(this.x_axis);
|
|
val c := this.x_axis.cross(this.y_axis);
|
|
val det := this.x_axis.dot(a);
|
|
return mat3{} if det == 0;
|
|
val k := 1 / det;
|
|
return mat3{ a * k, b * k, c * k }.transposed();
|
|
}
|