Implement mouse panning and streamline keyboard movement.
[mandelwow.git] / support / camera.rs
1 extern crate glutin;
2
3 use glutin::ElementState::{Pressed, Released};
4 use glutin::Event::{KeyboardInput, MouseMoved};
5 use glutin::VirtualKeyCode;
6 use support::vec3::Vec3;
7 use support::vec3::norm;
8
9 use std::f32;
10
11 //use glutin::Event;
12 //use VirtualKeyCode;
13
14 #[derive(Default)]
15 pub struct CameraState {
16     aspect_ratio: f32,
17     pos: Vec3,
18     dir: Vec3,
19
20     moving_up: bool,
21     moving_left: bool,
22     moving_down: bool,
23     moving_right: bool,
24     moving_forward: bool,
25     moving_backward: bool,
26     turning_up: bool,
27     turning_left: bool,
28     turning_down: bool,
29     turning_right: bool,
30
31     mouse_x: i32,
32     mouse_y: i32,
33     rel_x: i32,
34     rel_y: i32,
35 }
36
37 impl CameraState {
38     pub fn new() -> CameraState {
39         CameraState {
40             aspect_ratio: 1024.0 / 768.0,
41             pos: Vec3(0.0, 0.0, 0.0),
42             dir: Vec3(0.0, 0.0, -1.0),
43             .. Default::default()
44         }
45     }
46
47     pub fn set_pos(&mut self, pos: Vec3) {
48         self.pos = pos;
49     }
50
51     pub fn get_pos(&self) -> Vec3 {
52         self.pos
53     }
54
55     pub fn set_dir(&mut self, dir: Vec3) {
56         self.dir = dir;
57     }
58
59     pub fn get_perspective(&self) -> [[f32; 4]; 4] {
60         let fov: f32 = 3.141592 / 2.0;
61         let zfar = 1024.0;
62         let znear = 0.1;
63
64         let f = 1.0 / (fov / 2.0).tan();
65
66         // note: remember that this is column-major, so the lines of code are actually columns
67         [
68             [f / self.aspect_ratio,    0.0,              0.0              ,   0.0],
69             [         0.0         ,     f ,              0.0              ,   0.0],
70             [         0.0         ,    0.0,  (zfar+znear)/(zfar-znear)    ,   1.0],
71             [         0.0         ,    0.0, -(2.0*zfar*znear)/(zfar-znear),   0.0],
72         ]
73     }
74
75     pub fn get_view(&self) -> [[f32; 4]; 4] {
76         let f = norm(&self.dir);
77
78         let up = Vec3(0.0, 1.0, 0.0);
79
80         let s = Vec3(f.1 * up.2 - f.2 * up.1,
81                      f.2 * up.0 - f.0 * up.2,
82                      f.0 * up.1 - f.1 * up.0);
83         let sn = norm(&s);
84
85         let u = (sn.1 * f.2 - sn.2 * f.1,
86                  sn.2 * f.0 - sn.0 * f.2,
87                  sn.0 * f.1 - sn.1 * f.0);
88
89         let p = (-self.pos.0 * s.0 - self.pos.1 * s.1 - self.pos.2 * s.2,
90                  -self.pos.0 * u.0 - self.pos.1 * u.1 - self.pos.2 * u.2,
91                  -self.pos.0 * f.0 - self.pos.1 * f.1 - self.pos.2 * f.2);
92
93         // note: remember that this is column-major, so the lines of code are actually columns
94         [
95             [sn.0, u.0, f.0, 0.0],
96             [sn.1, u.1, f.1, 0.0],
97             [sn.2, u.2, f.2, 0.0],
98             [p.0,  p.1, p.2, 1.0],
99         ]
100     }
101
102     pub fn update(&mut self) {
103         let f = norm(&self.dir);
104
105         let up = Vec3(0.0, 1.0, 0.0);
106
107         let s = Vec3(f.1 * up.2 - f.2 * up.1,
108                      f.2 * up.0 - f.0 * up.2,
109                      f.0 * up.1 - f.1 * up.0);
110
111         let s = norm(&s);
112
113         let u = Vec3(s.1 * f.2 - s.2 * f.1,
114                      s.2 * f.0 - s.0 * f.2,
115                      s.0 * f.1 - s.1 * f.0);
116
117         let walk_speed = 0.01;
118         let strife_speed = 0.02;
119         let pan_speed = 0.01;
120
121         if self.moving_up {
122             self.pos += u * strife_speed;
123         }
124         if self.moving_down {
125             self.pos -= u * strife_speed;
126         }
127         if self.moving_left {
128             self.pos -= s * strife_speed;
129         }
130         if self.moving_right {
131             self.pos += s * strife_speed;
132         }
133         if self.moving_forward {
134             self.pos += f * walk_speed;
135         }
136         if self.moving_backward {
137             self.pos -= f * walk_speed;
138         }
139
140         if self.turning_left { self.rel_x -= 2; }
141         if self.turning_right { self.rel_x += 2; }
142         if self.turning_up { self.rel_y -= 2; }
143         if self.turning_down { self.rel_y += 2; }
144         let vx = -pan_speed * self.rel_x as f32;
145         let vy = -pan_speed * self.rel_y as f32;
146         self.dir = Vec3(f.0 * vx.cos() + f.2 * vx.sin(),
147                         f.1 * vy.cos() - f.2 * vy.sin(),
148                         f.1 * vy.sin() - f.0 * vx.sin() + f.2 * vx.cos() * vy.cos());
149         self.rel_x = 0;
150         self.rel_y = 0;
151
152         //println!("camera_pos = {:?}", self.pos);
153         //println!("camera_dir = {:?}", self.dir);
154     }
155
156     pub fn process_input(&mut self, event: &glutin::Event) {
157         match event {
158             &MouseMoved(x, y) => {
159                 self.rel_x += x - self.mouse_x;
160                 self.rel_y += y - self.mouse_y;
161                 self.mouse_x = x;
162                 self.mouse_y = y;
163             }
164             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Up)) => {
165                 self.moving_up = true;
166             },
167             &KeyboardInput(Released, _, Some(VirtualKeyCode::Up)) => {
168                 self.moving_up = false;
169             },
170             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Down)) => {
171                 self.moving_down = true;
172             },
173             &KeyboardInput(Released, _, Some(VirtualKeyCode::Down)) => {
174                 self.moving_down = false;
175             },
176             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Left)) => {
177                 self.moving_left = true;
178             },
179             &KeyboardInput(Released, _, Some(VirtualKeyCode::Left)) => {
180                 self.moving_left = false;
181             },
182             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Right)) => {
183                 self.moving_right = true;
184             },
185             &KeyboardInput(Released, _, Some(VirtualKeyCode::Right)) => {
186                 self.moving_right = false;
187             },
188             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::A)) => {
189                 self.turning_left = true;
190             },
191             &KeyboardInput(Released, _, Some(VirtualKeyCode::A)) => {
192                 self.turning_left = false;
193             },
194             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::D)) => {
195                 self.turning_right = true;
196             },
197             &KeyboardInput(Released, _, Some(VirtualKeyCode::D)) => {
198                 self.turning_right = false;
199             },
200             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::W)) => {
201                 self.moving_forward = true;
202             },
203             &KeyboardInput(Released, _, Some(VirtualKeyCode::W)) => {
204                 self.moving_forward = false;
205             },
206             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::S)) => {
207                 self.moving_backward = true;
208             },
209             &KeyboardInput(Released, _, Some(VirtualKeyCode::S)) => {
210                 self.moving_backward = false;
211             },
212             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::R)) => {
213                 self.turning_up = true;
214             },
215             &KeyboardInput(Released, _, Some(VirtualKeyCode::R)) => {
216                 self.turning_up = false;
217             },
218             &KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
219                 self.turning_down = true;
220             },
221             &KeyboardInput(Released, _, Some(VirtualKeyCode::F)) => {
222                 self.turning_down = false;
223             },
224             _ => {}
225         }
226     }
227 }