reve/src/camera.rs 3.8 KiB raw
1
//! Camera abstraction for creating 2D projections from a 3D scene.
2
use std::f32::consts::PI;
3
4
use ombre::prelude::*;
5
6
/// Camera input sensitivty settings.
7
#[derive(Debug)]
8
pub struct CameraSensitivity {
9
    /// Orbit sensitivty.
10
    pub orbit: f32,
11
    /// Dolly sensitivty.
12
    pub dolly: f32,
13
    /// Pan sensitivty.
14
    pub pan: f32,
15
}
16
17
impl CameraSensitivity {
18
    pub fn fov(angle: f32) -> Self {
19
        Self {
20
            orbit: 0.0015 * angle / 90.,
21
            dolly: 0.0020 * angle / 90.,
22
            pan: 0.0015 * angle / 90.,
23
        }
24
    }
25
}
26
27
impl Default for CameraSensitivity {
28
    fn default() -> Self {
29
        Self {
30
            orbit: 0.0025,
31
            dolly: 0.0050,
32
            pan: 0.0025,
33
        }
34
    }
35
}
36
37
/// Camera object.
38
#[derive(Debug)]
39
pub struct Camera {
40
    /// Camera "eye" position.
41
    pub eye: Vector3D<f32, World>,
42
    /// Where the camera is looking at.
43
    pub target: Vector3D<f32, World>,
44
    /// An optional object position that is in focus.
45
    /// When resetting the camera, we set the target to this object.
46
    pub object: Option<Vector3D<f32, World>>,
47
    /// Up vector (roll).
48
    pub up: Vector3D<f32, World>,
49
    /// Controls the sensitivty to user input.
50
    pub sensitivity: CameraSensitivity,
51
    /// Near Z-plane.
52
    pub near: f32,
53
    /// Far Z-plane.
54
    pub far: f32,
55
    /// Field of view (degrees).
56
    pub fov: f32,
57
}
58
59
impl Camera {
60
    /// Reset the camera so that it looks at its object straight on.
61
    pub fn reset(&mut self) {
62
        self.target = self.object.unwrap_or_default();
63
        self.eye = self.target + Vector3D::new(0., 0., 5.); // TODO: Compute distance.
64
    }
65
66
    /// Pan the camera.
67
    pub fn pan(&mut self, delta: Vector2D<f32, Screen>) {
68
        let scale = self.sensitivity.pan * (self.target - self.eye).magnitude();
69
        let forward = (self.target - self.eye).normalize();
70
        let right = Vector3D::cross(forward, self.up).normalize();
71
        let up = Vector3D::cross(right, forward).normalize();
72
        let horizontal = right * -delta.x * scale;
73
        let vertical = up * delta.y * scale;
74
75
        self.eye = self.eye + horizontal + vertical;
76
        self.target = self.target + horizontal + vertical;
77
    }
78
79
    /// Dolly the camera.
80
    pub fn dolly(&mut self, delta: Vector2D<f32, Screen>) {
81
        let mut sph = self.eye.to_spherical(self.target);
82
83
        // Scale sensitivty based on distance from object.
84
        sph.x -= (delta.y * self.sensitivity.dolly) * sph.x;
85
        sph.x = sph.x.max(0.1);
86
87
        // Convert back to cartesian coordinates.
88
        self.eye = sph.to_world() + self.target;
89
    }
90
91
    /// Orbit the camera around its target.
92
    pub fn orbit(&mut self, delta: impl Into<Vector2D<f32, Screen>>) {
93
        let delta = delta.into();
94
        let mut sph = self.eye.to_spherical(self.target);
95
        let max = self.sensitivity.orbit * 10.;
96
97
        sph.y += (delta.x * self.sensitivity.orbit * sph.x).clamp(-max, max);
98
        sph.z -= (delta.y * self.sensitivity.orbit * sph.x).clamp(-max, max);
99
100
        // Clamp phi to avoid going over the poles.
101
        sph.z = sph.z.clamp(0.01, PI - 0.01);
102
        // Convert back to cartesian coordinates.
103
        self.eye = sph.to_world() + self.target;
104
    }
105
106
    /// Return a projection transform based on the camera, ie. a perspective matrix.
107
    pub fn projection(&self, size: Size<f32>) -> Transform3D<f32, World, Screen> {
108
        Transform3D::<f32, World, Screen>::perspective(
109
            self.fov,
110
            size.w / size.h,
111
            self.near,
112
            self.far,
113
        ) * Transform3D::<f32, Screen, Screen>::from_nonuniform_scale(1.0, -1.0, 1.0)
114
    }
115
116
    /// Return a view transform based on the camera, ie. a "look at" matrix.
117
    pub fn view(&self) -> Transform3D<f32, World> {
118
        Transform3D::<f32, World>::look_at(self.eye, self.target, self.up)
119
    }
120
}