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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use cgmath;
use maths::{Euler, Matrix4f, Quaternion, Vector3f};

///Represents a transformation in 3D space: translation (position), scale, rotation (Euler's angles).
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Transform {
    pub position: Vector3f,
    pub scale: Vector3f,
    pub rotation: Vector3f,
}

impl Default for Transform {
    fn default() -> Self {
        Self::new()
    }
}

impl Transform {
    ///Create a transform with default position, scale, and rotation.
    pub fn new() -> Self {
        Self {
            position: Vector3f::new(0.0, 0.0, 0.0),
            scale: Vector3f::new(1.0, 1.0, 1.0),
            rotation: Vector3f::new(0.0, 0.0, 0.0),
        }
    }

    ///Create a transform with set position, with default scale and rotation.
    pub fn from_position(position: Vector3f) -> Self {
        Self {
            position,
            scale: Vector3f::new(1.0, 1.0, 1.0),
            rotation: Vector3f::new(0.0, 0.0, 0.0),
        }
    }

    ///Create a transform with set scale, with default position and rotation.
    pub fn from_scale(scale: Vector3f) -> Self {
        Self {
            position: Vector3f::new(0.0, 0.0, 0.0),
            scale,
            rotation: Vector3f::new(0.0, 0.0, 0.0),
        }
    }

    ///Create a transform with set rotation, with default position and scale.
    pub fn from_rotation(rotation: Vector3f) -> Self {
        Self {
            position: Vector3f::new(0.0, 0.0, 0.0),
            scale: Vector3f::new(1.0, 1.0, 1.0),
            rotation,
        }
    }

    ///Creates a matrix that applies the transform to a vector, or another matrix.
    pub fn matrix(&self) -> Matrix4f {
        let quaternion = Quaternion::from(Euler::new(
            cgmath::Deg(self.rotation.x),
            cgmath::Deg(self.rotation.y),
            cgmath::Deg(self.rotation.z),
        ));

        let rot = Matrix4f::from(quaternion);
        let scale = Matrix4f::new(
            self.scale.x,
            0.,
            0.,
            0.,
            0.,
            self.scale.y,
            0.,
            0.,
            0.,
            0.,
            self.scale.z,
            0.,
            0.,
            0.,
            0.,
            1.,
        );
        let translate = Matrix4f::new(
            1.,
            0.,
            0.,
            0.,
            0.,
            1.,
            0.,
            0.,
            0.,
            0.,
            1.,
            0.,
            self.position.x,
            self.position.y,
            self.position.z,
            1.,
        );

        translate * rot * scale
    }
}