103 lines
4.3 KiB
Plaintext
103 lines
4.3 KiB
Plaintext
#! Contains where models are: a model's matrix within its parent, by FBX's
|
|||
|
|
#! chain of translation, rotation offset and pivot, pre-rotation, rotation,
|
||
|
|
#! post-rotation, and scaling offset and pivot, as its SDK puts them together;
|
||
|
|
#! its mesh's within it; and its matrix in the world, through its parents. And
|
||
|
|
#! turning a scene's axes and units into y up, z to the front, and meters.
|
||
|
|
|
||
|
|
import std::math;
|
||
|
|
import malunal::linear;
|
||
|
|
module malunal::fbx;
|
||
|
|
|
||
|
|
# A rotation of angles in degrees about x, y, and z, turning in an order.
|
||
|
|
def euler_matrix(degrees: vec3, order: rotation_order): mat4 {
|
||
|
|
val k := (math::pi / 180) as f32;
|
||
|
|
val x := mat4::rotation(quat::axis_angle(vec3::right, degrees.x * k));
|
||
|
|
val y := mat4::rotation(quat::axis_angle(vec3::up, degrees.y * k));
|
||
|
|
val z := mat4::rotation(quat::axis_angle(vec3::back, degrees.z * k));
|
||
|
|
# The axis turning first is the rightmost, nearest what's turned.
|
||
|
|
if order == rotation_order::xzy {
|
||
|
|
return y.mul(&z).mul(&x);
|
||
|
|
}
|
||
|
|
if order == rotation_order::yzx {
|
||
|
|
return x.mul(&z).mul(&y);
|
||
|
|
}
|
||
|
|
if order == rotation_order::yxz {
|
||
|
|
return z.mul(&x).mul(&y);
|
||
|
|
}
|
||
|
|
if order == rotation_order::zxy {
|
||
|
|
return y.mul(&x).mul(&z);
|
||
|
|
}
|
||
|
|
if order == rotation_order::zyx {
|
||
|
|
return x.mul(&y).mul(&z);
|
||
|
|
}
|
||
|
|
return z.mul(&y).mul(&x);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Its matrix within its parent: its translation, rotation offset, rotation
|
||
|
|
#! pivot, pre-rotation, rotation, post-rotation turned back, the pivot taken
|
||
|
|
#! away, then its scaling offset and pivot, scale, and the pivot taken away,
|
||
|
|
#! as `T * Roff * Rp * Rpre * R * Rpost⁻¹ * Rp⁻¹ * Soff * Sp * S * Sp⁻¹`.
|
||
|
|
#! Where its rotation isn't active, its pre- and post-rotations and order are
|
||
|
|
#! taken as none and `xyz`.
|
||
|
|
pub def model::local_matrix(&this): mat4 {
|
||
|
|
val active := this.rotation_active;
|
||
|
|
val order := active ? this.order : rotation_order::xyz;
|
||
|
|
val pre := active ? euler_matrix(this.pre_rotation, rotation_order::xyz) : mat4::identity;
|
||
|
|
val post := active ? euler_matrix(this.post_rotation, rotation_order::xyz).transposed() : mat4::identity;
|
||
|
|
val turned := euler_matrix(this.rotation, order);
|
||
|
|
var made := mat4::translation(this.translation);
|
||
|
|
made = made.mul(&mat4::translation(this.rotation_offset));
|
||
|
|
made = made.mul(&mat4::translation(this.rotation_pivot));
|
||
|
|
made = made.mul(&pre).mul(&turned).mul(&post);
|
||
|
|
made = made.mul(&mat4::translation(this.rotation_pivot.negate()));
|
||
|
|
made = made.mul(&mat4::translation(this.scaling_offset));
|
||
|
|
made = made.mul(&mat4::translation(this.scaling_pivot));
|
||
|
|
made = made.mul(&mat4::scaling(this.scale));
|
||
|
|
made = made.mul(&mat4::translation(this.scaling_pivot.negate()));
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Its mesh's matrix within it, which its children don't follow:
|
||
|
|
#! `GeometricTranslation`, `GeometricRotation`, and `GeometricScaling`.
|
||
|
|
pub def model::geometry_matrix(&this): mat4 {
|
||
|
|
val turned := euler_matrix(this.geometric_rotation, rotation_order::xyz);
|
||
|
|
return mat4::translation(this.geometric_translation).mul(&turned).mul(&mat4::scaling(this.geometric_scale));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A model's matrix in the world: its parents', each's within its own, then
|
||
|
|
#! its own, multiplied as matrices are, as FBX's default way of inheriting
|
||
|
|
#! does.
|
||
|
|
pub def scene::world_matrix(&this, of: u32): mat4 {
|
||
|
|
var made := this.models[of as u64].local_matrix();
|
||
|
|
var above := this.models[of as u64].parent;
|
||
|
|
var steps: u64 = 0;
|
||
|
|
while above != absent && steps < this.models.length {
|
||
|
|
made = this.models[above as u64].local_matrix().mul(&made);
|
||
|
|
above = this.models[above as u64].parent;
|
||
|
|
steps++;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The matrix turning the scene's axes into x across, y up, and z to the
|
||
|
|
#! front, as glTF's are, mirroring where they're left-handed.
|
||
|
|
pub def settings::axes(&this): mat4 {
|
||
|
|
var made := mat4{};
|
||
|
|
made.m[15] = 1;
|
||
|
|
made.m[(this.coord_axis % 3) as u64 * 4] = this.coord_sign < 0 ? -1 : 1;
|
||
|
|
made.m[(this.up_axis % 3) as u64 * 4 + 1] = this.up_sign < 0 ? -1 : 1;
|
||
|
|
made.m[(this.front_axis % 3) as u64 * 4 + 2] = this.front_sign < 0 ? -1 : 1;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many meters a unit is.
|
||
|
|
pub def settings::meters_per_unit(&this): f32 {
|
||
|
|
return (this.unit_scale / 100) as f32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The matrix turning the scene into meters, with x across, y up, and z to the
|
||
|
|
#! front: its axes, scaled.
|
||
|
|
pub def settings::conversion(&this): mat4 {
|
||
|
|
val k := this.meters_per_unit();
|
||
|
|
return mat4::scaling(vec3{ k, k, k }).mul(&this.axes());
|
||
|
|
}
|