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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use std::f32;
use glfw::{Key, Action, MouseButton};
use glfw;
use glfw::WindowEvent;
use na::{Point3, Vector2, Vector3, Matrix4, Isometry3, Perspective3};
use na;
use camera::Camera;
#[derive(Clone, Debug)]
pub struct ArcBall {
at: Point3<f32>,
yaw: f32,
pitch: f32,
dist: f32,
yaw_step: f32,
pitch_step: f32,
dist_step: f32,
rotate_button: Option<MouseButton>,
drag_button: Option<MouseButton>,
reset_key: Option<Key>,
projection: Perspective3<f32>,
proj_view: Matrix4<f32>,
inverse_proj_view: Matrix4<f32>,
last_cursor_pos: Vector2<f32>
}
impl ArcBall {
pub fn new(eye: Point3<f32>, at: Point3<f32>) -> ArcBall {
ArcBall::new_with_frustrum(f32::consts::PI / 4.0, 0.1, 1024.0, eye, at)
}
pub fn new_with_frustrum(fov: f32,
znear: f32,
zfar: f32,
eye: Point3<f32>,
at: Point3<f32>) -> ArcBall {
let mut res = ArcBall {
at: Point3::new(0.0, 0.0, 0.0),
yaw: 0.0,
pitch: 0.0,
dist: 0.0,
yaw_step: 0.005,
pitch_step: 0.005,
dist_step: 40.0,
rotate_button: Some(glfw::MouseButtonLeft),
drag_button: Some(glfw::MouseButtonRight),
reset_key: Some(Key::Enter),
projection: Perspective3::new(800.0 / 600.0, fov, znear, zfar),
proj_view: na::zero(),
inverse_proj_view: na::zero(),
last_cursor_pos: na::zero()
};
res.look_at(eye, at);
res
}
pub fn at(&self) -> Point3<f32> {
self.at
}
pub fn set_at(&mut self, at: Point3<f32>) {
self.at = at;
self.update_projviews();
}
pub fn yaw(&self) -> f32 {
self.yaw
}
pub fn set_yaw(&mut self, yaw: f32) {
self.yaw = yaw;
self.update_restrictions();
self.update_projviews();
}
pub fn pitch(&self) -> f32 {
self.pitch
}
pub fn set_pitch(&mut self, pitch: f32) {
self.pitch = pitch;
self.update_restrictions();
self.update_projviews();
}
pub fn dist(&self) -> f32 {
self.dist
}
pub fn set_dist(&mut self, dist: f32) {
self.dist = dist;
self.update_restrictions();
self.update_projviews();
}
pub fn look_at(&mut self, eye: Point3<f32>, at: Point3<f32>) {
let dist = na::norm(&(eye - at));
let pitch = ((eye.y - at.y) / dist).acos();
let yaw = (eye.z - at.z).atan2(eye.x - at.x);
self.at = at;
self.dist = dist;
self.yaw = yaw;
self.pitch = pitch;
self.update_restrictions();
self.update_projviews();
}
fn update_restrictions(&mut self) {
if self.dist < 0.00001 {
self.dist = 0.00001
}
if self.pitch <= 0.01 {
self.pitch = 0.01
}
let _pi: f32 = f32::consts::PI;
if self.pitch > _pi - 0.01 {
self.pitch = _pi - 0.01
}
}
pub fn rotate_button(&self) -> Option<MouseButton> {
self.rotate_button
}
pub fn rebind_rotate_button(&mut self, new_button: Option<MouseButton>) {
self.rotate_button = new_button;
}
pub fn drag_button(&self) -> Option<MouseButton> {
self.drag_button
}
pub fn rebind_drag_button(&mut self, new_button: Option<MouseButton>) {
self.drag_button = new_button;
}
pub fn reset_key(&self) -> Option<Key> {
self.reset_key
}
pub fn rebind_reset_key(&mut self, new_key : Option<Key>) {
self.reset_key = new_key;
}
fn handle_left_button_displacement(&mut self, dpos: &Vector2<f32>) {
self.yaw = self.yaw + dpos.x * self.yaw_step;
self.pitch = self.pitch - dpos.y * self.pitch_step;
self.update_restrictions();
self.update_projviews();
}
fn handle_right_button_displacement(&mut self, dpos: &Vector2<f32>) {
let eye = self.eye();
let dir = na::normalize(&(self.at - eye));
let tangent = na::normalize(&Vector3::y().cross(&dir));
let bitangent = dir.cross(&tangent);
let mult = self.dist / 1000.0;
self.at = self.at + tangent * (dpos.x * mult) + bitangent * (dpos.y * mult);
self.update_projviews();
}
fn handle_scroll(&mut self, off: f32) {
self.dist = self.dist + self.dist_step * (off) / 120.0;
self.update_restrictions();
self.update_projviews();
}
fn update_projviews(&mut self) {
self.proj_view = *self.projection.as_matrix() * self.view_transform().to_homogeneous();
self.inverse_proj_view = self.proj_view.try_inverse().unwrap();
}
}
impl Camera for ArcBall {
fn clip_planes(&self) -> (f32, f32) {
(self.projection.znear(), self.projection.zfar())
}
fn view_transform(&self) -> Isometry3<f32> {
Isometry3::look_at_rh(&self.eye(), &self.at, &Vector3::y())
}
fn eye(&self) -> Point3<f32> {
let px = self.at.x + self.dist * self.yaw.cos() * self.pitch.sin();
let py = self.at.y + self.dist * self.pitch.cos();
let pz = self.at.z + self.dist * self.yaw.sin() * self.pitch.sin();
Point3::new(px, py, pz)
}
fn handle_event(&mut self, window: &glfw::Window, event: &WindowEvent) {
match *event {
WindowEvent::CursorPos(x, y) => {
let curr_pos = Vector2::new(x as f32, y as f32);
if let Some(rotate_button) = self.rotate_button {
if window.get_mouse_button(rotate_button) == Action::Press {
let dpos = curr_pos - self.last_cursor_pos;
self.handle_left_button_displacement(&dpos)
}
}
if let Some(drag_button) = self.drag_button {
if window.get_mouse_button(drag_button) == Action::Press {
let dpos = curr_pos - self.last_cursor_pos;
self.handle_right_button_displacement(&dpos)
}
}
self.last_cursor_pos = curr_pos;
},
WindowEvent::Key(key, _, Action::Press, _) if Some(key) == self.reset_key => {
self.at = Point3::origin();
self.update_projviews();
},
WindowEvent::Scroll(_, off) => self.handle_scroll(off as f32),
WindowEvent::FramebufferSize(w, h) => {
self.projection.set_aspect(w as f32 / h as f32);
self.update_projviews();
},
_ => { }
}
}
fn transformation(&self) -> Matrix4<f32> {
self.proj_view
}
fn inverse_transformation(&self) -> Matrix4<f32> {
self.inverse_proj_view
}
fn update(&mut self, _: &glfw::Window) { }
}