3 use glutin::ElementState::{Pressed, Released};
4 use glutin::Event::KeyboardInput;
5 use glutin::VirtualKeyCode;
6 use support::vec3::Vec3;
7 use support::vec3::norm;
15 pub struct CameraState {
25 moving_backward: bool,
33 pub fn new() -> CameraState {
35 aspect_ratio: 1024.0 / 768.0,
36 pos: Vec3(0.0, 0.0, 0.0),
37 dir: Vec3(0.0, 0.0, -1.0),
42 pub fn set_pos(&mut self, pos: Vec3) {
46 pub fn get_pos(&self) -> Vec3 {
50 pub fn set_dir(&mut self, dir: Vec3) {
54 pub fn get_perspective(&self) -> [[f32; 4]; 4] {
55 let fov: f32 = 3.141592 / 2.0;
59 let f = 1.0 / (fov / 2.0).tan();
61 // note: remember that this is column-major, so the lines of code are actually columns
63 [f / self.aspect_ratio, 0.0, 0.0 , 0.0],
64 [ 0.0 , f , 0.0 , 0.0],
65 [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
66 [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
70 pub fn get_view(&self) -> [[f32; 4]; 4] {
71 let f = norm(&self.dir);
73 let up = Vec3(0.0, 1.0, 0.0);
75 let s = Vec3(f.1 * up.2 - f.2 * up.1,
76 f.2 * up.0 - f.0 * up.2,
77 f.0 * up.1 - f.1 * up.0);
80 let u = (sn.1 * f.2 - sn.2 * f.1,
81 sn.2 * f.0 - sn.0 * f.2,
82 sn.0 * f.1 - sn.1 * f.0);
84 let p = (-self.pos.0 * s.0 - self.pos.1 * s.1 - self.pos.2 * s.2,
85 -self.pos.0 * u.0 - self.pos.1 * u.1 - self.pos.2 * u.2,
86 -self.pos.0 * f.0 - self.pos.1 * f.1 - self.pos.2 * f.2);
88 // note: remember that this is column-major, so the lines of code are actually columns
90 [sn.0, u.0, f.0, 0.0],
91 [sn.1, u.1, f.1, 0.0],
92 [sn.2, u.2, f.2, 0.0],
97 pub fn update(&mut self) {
98 let f = norm(&self.dir);
100 let up = Vec3(0.0, 1.0, 0.0);
102 let s = Vec3(f.1 * up.2 - f.2 * up.1,
103 f.2 * up.0 - f.0 * up.2,
104 f.0 * up.1 - f.1 * up.0);
108 let u = Vec3(s.1 * f.2 - s.2 * f.1,
109 s.2 * f.0 - s.0 * f.2,
110 s.0 * f.1 - s.1 * f.0);
113 self.pos += u * 0.01;
115 if self.moving_left {
116 self.pos.0 -= s.0 * 0.01;
117 self.pos.1 -= s.1 * 0.01;
118 self.pos.2 -= s.2 * 0.01;
120 if self.moving_down {
121 self.pos.0 -= u.0 * 0.01;
122 self.pos.1 -= u.1 * 0.01;
123 self.pos.2 -= u.2 * 0.01;
125 if self.moving_right {
126 self.pos += s * 0.01;
128 if self.moving_forward {
129 self.pos += f * 0.01;
131 if self.moving_backward {
132 self.pos.0 -= f.0 * 0.01;
133 self.pos.1 -= f.1 * 0.01;
134 self.pos.2 -= f.2 * 0.01;
136 if self.turning_left {
138 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
140 if self.turning_right {
142 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
146 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
148 if self.turning_down {
150 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
152 //println!("camera_pos = {:?}", self.pos);
153 //println!("camera_dir = {:?}", self.dir);
156 pub fn process_input(&mut self, event: &glutin::Event) {
158 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Up)) => {
159 self.moving_up = true;
161 &KeyboardInput(Released, _, Some(VirtualKeyCode::Up)) => {
162 self.moving_up = false;
164 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Down)) => {
165 self.moving_down = true;
167 &KeyboardInput(Released, _, Some(VirtualKeyCode::Down)) => {
168 self.moving_down = false;
170 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Left)) => {
171 self.moving_left = true;
173 &KeyboardInput(Released, _, Some(VirtualKeyCode::Left)) => {
174 self.moving_left = false;
176 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::Right)) => {
177 self.moving_right = true;
179 &KeyboardInput(Released, _, Some(VirtualKeyCode::Right)) => {
180 self.moving_right = false;
182 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::A)) => {
183 self.turning_left = true;
185 &KeyboardInput(Released, _, Some(VirtualKeyCode::A)) => {
186 self.turning_left = false;
188 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::D)) => {
189 self.turning_right = true;
191 &KeyboardInput(Released, _, Some(VirtualKeyCode::D)) => {
192 self.turning_right = false;
194 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::W)) => {
195 self.moving_forward = true;
197 &KeyboardInput(Released, _, Some(VirtualKeyCode::W)) => {
198 self.moving_forward = false;
200 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::S)) => {
201 self.moving_backward = true;
203 &KeyboardInput(Released, _, Some(VirtualKeyCode::S)) => {
204 self.moving_backward = false;
206 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::R)) => {
207 self.turning_up = true;
209 &KeyboardInput(Released, _, Some(VirtualKeyCode::R)) => {
210 self.turning_up = false;
212 &KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
213 self.turning_down = true;
215 &KeyboardInput(Released, _, Some(VirtualKeyCode::F)) => {
216 self.turning_down = false;