summaryrefslogtreecommitdiff
path: root/engine/src/camera.rs
blob: 4a7768b33f5bfc2872ae6f57a9eabc9e95214dca (plain)
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
use crate::ecs::Component;
use crate::matrix::Matrix;
use crate::projection::{Perspective, Projection};
use crate::reflection::Reflection;
use crate::vector::Vec3;

pub mod fly;

#[derive(Debug, Clone, Component, Reflection)]
pub struct Camera
{
    pub target: Vec3<f32>,
    pub global_up: Vec3<f32>,
    pub projection: Projection,
}

impl Camera
{
    pub fn to_view_matrix(&self, camera_world_pos: Vec3<f32>) -> Matrix<f32, 4, 4>
    {
        let mut view = Matrix::new();

        view.look_at(camera_world_pos, self.target, self.global_up);

        view
    }
}

impl Default for Camera
{
    fn default() -> Self
    {
        Self {
            target: Vec3::FRONT,
            global_up: Vec3::UP,
            projection: Projection::Perspective(Perspective::default()),
        }
    }
}

/// Marker component for cameras that are active.
#[derive(Debug, Default, Clone, Copy, Component, Reflection)]
pub struct Active;

/// Cameras that can be controlled have this component.
#[derive(Debug, Clone, Copy, Component, Reflection)]
pub struct Controllable
{
    pub control_enabled: bool,
}

impl Default for Controllable
{
    fn default() -> Self
    {
        Self { control_enabled: true }
    }
}