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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use base::math::*;
use std::f32::consts;
#[derive(Clone, Copy)]
pub struct Camera {
pub position: Point3f,
pub theta: f32,
pub phi: f32,
pub aspect_ratio: f32,
proj: Matrix4<f32>,
}
impl Camera {
pub fn new(aspect_ratio: f32) -> Camera {
Camera {
position: Point3::new(15.0, 10.0, 50.0),
phi: -0.27,
theta: 2.6,
aspect_ratio: aspect_ratio,
proj: perspective(deg(60.0), aspect_ratio, 0.1, 3_000.0),
}
}
pub fn new_from_vector(pos: Point3f, look: Vector3f, aspect_ratio: f32) -> Self {
let mut look_phi = ((look.x) / ((look.x * look.x + look.y * look.y).sqrt())).acos();
if look.y < 0.0 {
look_phi = 2.0 * consts::PI - look_phi;
}
let look_theta = (consts::PI / 2.0) -
((look.z) / ((look.x * look.x + look.y * look.y).sqrt())).atan();
Camera {
position: pos,
phi: look_phi,
theta: look_theta,
aspect_ratio: aspect_ratio,
proj: perspective(deg(60.0), aspect_ratio, 0.1, 3_000.0),
}
}
pub fn proj_matrix(&self) -> Matrix4<f32> {
self.proj
}
pub fn set_proj_matrix(&mut self, proj: Matrix4<f32>) {
self.proj = proj;
}
pub fn view_matrix(&self) -> Matrix4<f32> {
Matrix4::look_at(self.position, self.get_look_at_point(), Vector3::unit_z())
}
pub fn get_look_at_point(&self) -> Point3f {
self.position + self.get_look_at_vector()
}
pub fn get_look_at_vector(&self) -> Vector3f {
Vector3f::new(self.theta.sin() * self.phi.cos(),
self.theta.sin() * self.phi.sin(),
self.theta.cos())
}
pub fn move_by(&mut self, pos_diff: Vector3f) {
self.position += pos_diff;
}
pub fn move_forward(&mut self, factor: f32) {
let mut lookatvector = self.get_look_at_vector();
lookatvector.z = 0.0;
lookatvector = lookatvector.normalize();
lookatvector *= factor;
self.move_by(lookatvector);
}
pub fn move_backward(&mut self, factor: f32) {
let mut lookatvector = self.get_look_at_vector();
lookatvector.z = 0.0;
lookatvector = lookatvector.normalize();
lookatvector *= -factor;
self.move_by(lookatvector);
}
pub fn move_left(&mut self, factor: f32) {
let mut lookatvector = self.get_look_at_vector();
lookatvector.z = 0.0;
lookatvector = lookatvector.normalize();
let mut move_dir = Vector3f::new(-lookatvector.y, lookatvector.x, 0.0);
move_dir *= factor;
self.move_by(move_dir);
}
pub fn move_right(&mut self, factor: f32) {
let mut lookatvector = self.get_look_at_vector();
lookatvector.z = 0.0;
lookatvector = lookatvector.normalize();
let mut move_dir = Vector3f::new(lookatvector.y, -lookatvector.x, 0.0);
move_dir *= factor;
self.move_by(move_dir);
}
pub fn move_up(&mut self, factor: f32) {
self.move_by(Vector3f::new(0.0, 0.0, factor));
}
pub fn move_down(&mut self, factor: f32) {
self.move_by(Vector3f::new(0.0, 0.0, -factor));
}
pub fn change_dir(&mut self, theta_diff: f32, phi_diff: f32) {
if self.theta < 0.1 {
if theta_diff > 0.0 {
self.theta += theta_diff;
}
} else if self.theta > consts::PI - 0.1 {
if theta_diff < 0.0 {
self.theta += theta_diff;
}
} else {
self.theta += theta_diff;
}
self.phi += phi_diff;
}
}