logo
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
//! Camera implementations

use cgmath::Point3;

use crate::utils::clamp;

mod arcball;
pub use self::arcball::*;

mod first_person;
pub use self::first_person::*;

pub enum CameraBehaviour {
    FirstPerson,
    Spectator,
    Flight,
    Orbit,
}

// Spherical coordinate system
#[derive(Clone, Copy)]
pub struct Camera {
    theta: f32, // polar angle
    phi: f32,   // azimuthal angle
    r: f32,     // radial distance (distance to origin)
}

impl Camera {
    pub fn position(&self) -> Point3<f32> {
        Point3::new(
            self.r * self.phi.sin() * self.theta.sin(),
            self.r * self.phi.cos(),
            self.r * self.phi.sin() * self.theta.cos(),
        )
    }
}

impl Camera {
    pub fn rotate(&mut self, theta: f32, phi: f32) {
        self.theta += theta;
        let phi = self.phi + phi;
        self.phi = clamp(phi, 10.0_f32.to_radians(), 170.0_f32.to_radians());
    }

    pub fn forward(&mut self, r: f32) {
        self.r -= r;
    }
}

impl Default for Camera {
    fn default() -> Self {
        Camera {
            theta: 0.0_f32.to_radians(),
            phi: 45.0_f32.to_radians(),
            r: 3.0,
        }
    }
}