1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use cgmath;
use {Quat, Mat4, Vec3};
/// Translation + Rotation + Non-uniform Scale transform in 3D space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform {
/// Translation vector.
pub translation: Vec3,
/// Rotation quaternion.
pub rotation: Quat,
/// *Non-uniform* scaling.
pub scale: Vec3,
}
impl Transform {
/// Returns the identity transform.
pub fn identity() -> Self {
Transform {
translation: vec3!(),
rotation: Quat::identity(),
scale: vec3!(1.0),
}
}
/// Returns the equivalent matrix representation for this transform.
pub fn matrix(&self) -> Mat4 {
let t = cgmath::Matrix4::from_translation(
cgmath::Vector3::new(
self.translation.x,
self.translation.y,
self.translation.z,
),
);
let r = cgmath::Matrix4::from(
cgmath::Quaternion::new(
self.rotation.scalar,
self.rotation.vector.x,
self.rotation.vector.y,
self.rotation.vector.z,
),
);
let s = cgmath::Matrix4::from_nonuniform_scale(
self.scale.x,
self.scale.y,
self.scale.z,
);
let m: [[f32; 4]; 4] = (t * r * s).into();
Mat4::from(m)
}
}