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
104
105
106
|
use crate::matrix::Matrix;
use crate::vector::Vec3;
#[derive(Debug)]
pub struct Camera
{
position: Vec3<f32>,
target: Vec3<f32>,
}
impl Camera
{
pub fn set_position(&mut self, position: Vec3<f32>)
{
self.position = position;
}
#[must_use]
pub fn position(&self) -> &Vec3<f32>
{
&self.position
}
#[must_use]
pub fn position_mut(&mut self) -> &mut Vec3<f32>
{
&mut self.position
}
pub fn set_target(&mut self, target: Vec3<f32>)
{
self.target = target;
}
#[must_use]
pub fn target(&self) -> &Vec3<f32>
{
&self.target
}
/// Returns the normalized direction to the current target.
#[must_use]
pub fn target_direction(&self) -> Vec3<f32>
{
-(&self.position - &self.target).normalize()
}
/// Returns the right direction (normalized) relative from where the camera is
/// currently looking.
#[must_use]
pub fn right(&self) -> Vec3<f32>
{
let rev_target_direction = (&self.position - &self.target).normalize();
Vec3::UP.cross(&rev_target_direction).normalize()
}
/// Returns the left direction (normalized) relative from where the camera is
/// currently looking.
#[must_use]
pub fn left(&self) -> Vec3<f32>
{
-self.right()
}
/// Returns the up direction (normalized) relative from where the camera is currently
/// looking.
#[must_use]
pub fn up(&self) -> Vec3<f32>
{
let rev_target_direction = (&self.position - &self.target).normalize();
rev_target_direction.cross(&self.right())
}
/// Returns the down direction (normalized) relative from where the camera is
/// currently looking.
#[must_use]
pub fn down(&self) -> Vec3<f32>
{
-self.up()
}
pub(crate) fn new() -> Self
{
let position = Vec3 {
x: 0.0,
y: 0.0,
z: 3.0,
};
Self {
position,
target: Vec3::default(),
}
}
pub(crate) fn as_matrix(&self) -> Matrix<f32, 4, 4>
{
let mut matrix = Matrix::new();
matrix.look_at(&self.position, &self.target, &Vec3::UP);
matrix
}
}
|