moai_render/camera/
mod.rs

1/// ### Quaternion Camera
2pub struct Camera {
3    projection: glam::Mat4,
4
5    orientation: glam::Quat, // Rotation
6    position: glam::Mat4,    // Translation
7
8    pub pitch: f32,
9    pub yaw: f32,
10    //roll: f32
11}
12
13impl Camera {
14    pub fn new(fov: f32, aspect: f32, near_clip: f32, far_clip: f32) -> Self{
15        let projection = glam::Mat4::perspective_rh(
16            fov.to_radians(),
17            aspect,
18            near_clip,
19            far_clip
20        );
21        let position = glam::Mat4::from_translation(glam::vec3(0.0, 0.0, 0.0));
22        return Camera { 
23            projection,
24            orientation: glam::quat(0.0, 0.0, 0.0, 0.0),
25            position,
26            pitch: 0.0,
27            yaw: 0.0 
28        };
29    }
30
31    // Update Camera -------
32    pub fn translate(&mut self, translate_vec: glam::Vec3){
33        self.position *= glam::Mat4::from_translation(translate_vec);
34    }
35
36    pub fn update(&mut self){
37        let pitch_quat = glam::Quat::from_rotation_x(self.pitch.to_radians());
38        let yaw_quat = glam::Quat::from_rotation_y(self.yaw.to_radians());
39        self.orientation = (pitch_quat*yaw_quat).normalize();
40    }
41
42    // Setters -------
43    pub fn set_position(&mut self, new_position: glam::Mat4){
44        self.position = new_position;
45    }
46
47    // Getters -------
48    pub fn get_view_projection(&mut self) -> glam::Mat4{
49        let view = glam::Mat4::from_quat(self.orientation) * self.position;
50        return self.projection * view;
51    }
52
53    pub fn get_front(&self) -> glam::Vec3{
54        return self.orientation.conjugate() * glam::vec3(0.0, 0.0, 1.0);
55    }
56
57    pub fn get_back(&self) -> glam::Vec3{
58        return self.orientation.conjugate() * glam::vec3(0.0, 0.0, -1.0);
59    }
60
61    pub fn get_left(&self) -> glam::Vec3{
62        return self.orientation.conjugate() * glam::vec3(1.0, 0.0, 0.0);
63    }
64
65    pub fn get_right(&self) -> glam::Vec3{
66        return self.orientation.conjugate() * glam::vec3(-1.0, 0.0, 0.0);
67    }
68}