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;
15 pub struct CameraState {
25 moving_backward: bool,
38 pub fn new() -> CameraState {
40 aspect_ratio: 600.0 / 600.0,
41 pos: Vec3(0.0, 0.0, 0.0),
42 dir: Vec3(0.0, 0.0, -1.0),
47 pub fn set_pos(&mut self, pos: Vec3) {
51 pub fn get_pos(&self) -> Vec3 {
55 pub fn set_dir(&mut self, dir: Vec3) {
59 pub fn get_perspective(&self) -> [[f32; 4]; 4] {
60 let fov: f32 = 3.141592 / 2.0;
64 let f = 1.0 / (fov / 2.0).tan();
66 // note: remember that this is column-major, so the lines of code are actually columns
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],
75 pub fn get_view(&self) -> [[f32; 4]; 4] {
76 let f = norm(&self.dir);
78 let up = Vec3(0.0, 1.0, 0.0);
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);
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);
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);
93 // note: remember that this is column-major, so the lines of code are actually columns
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],
102 pub fn update(&mut self) {
103 let f = norm(&self.dir);
105 let up = Vec3(0.0, 1.0, 0.0);
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);
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);
117 let walk_speed = 0.01;
118 let strife_speed = 0.02;
119 let pan_speed = 0.01;
122 self.pos += u * strife_speed;
124 if self.moving_down {
125 self.pos -= u * strife_speed;
127 if self.moving_left {
128 self.pos -= s * strife_speed;
130 if self.moving_right {
131 self.pos += s * strife_speed;
133 if self.moving_forward {
134 self.pos += f * walk_speed;
136 if self.moving_backward {
137 self.pos -= f * walk_speed;
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());
152 //println!("camera_pos = {:?}", self.pos);
153 //println!("camera_dir = {:?}", self.dir);
156 pub fn process_input(&mut self, event: &glutin::Event) {
158 &MouseMoved(x, y) => {
159 self.rel_x += x - self.mouse_x;
160 self.rel_y += y - self.mouse_y;
164 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Up)) => {
165 self.moving_up = true;
167 &KeyboardInput(Released, _, Some(VirtualKeyCode::Up)) => {
168 self.moving_up = false;
170 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Down)) => {
171 self.moving_down = true;
173 &KeyboardInput(Released, _, Some(VirtualKeyCode::Down)) => {
174 self.moving_down = false;
176 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Left)) => {
177 self.moving_left = true;
179 &KeyboardInput(Released, _, Some(VirtualKeyCode::Left)) => {
180 self.moving_left = false;
182 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Right)) => {
183 self.moving_right = true;
185 &KeyboardInput(Released, _, Some(VirtualKeyCode::Right)) => {
186 self.moving_right = false;
188 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::A)) => {
189 self.turning_left = true;
191 &KeyboardInput(Released, _, Some(VirtualKeyCode::A)) => {
192 self.turning_left = false;
194 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::D)) => {
195 self.turning_right = true;
197 &KeyboardInput(Released, _, Some(VirtualKeyCode::D)) => {
198 self.turning_right = false;
200 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::W)) => {
201 self.moving_forward = true;
203 &KeyboardInput(Released, _, Some(VirtualKeyCode::W)) => {
204 self.moving_forward = false;
206 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::S)) => {
207 self.moving_backward = true;
209 &KeyboardInput(Released, _, Some(VirtualKeyCode::S)) => {
210 self.moving_backward = false;
212 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::R)) => {
213 self.turning_up = true;
215 &KeyboardInput(Released, _, Some(VirtualKeyCode::R)) => {
216 self.turning_up = false;
218 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
219 self.turning_down = true;
221 &KeyboardInput(Released, _, Some(VirtualKeyCode::F)) => {
222 self.turning_down = false;