3 use support::vec3::Vec3;
4 use support::vec3::norm;
9 //use glutin::VirtualKeyCode;
12 pub struct CameraState {
22 moving_backward: bool,
30 pub fn new() -> CameraState {
32 aspect_ratio: 1024.0 / 768.0,
33 pos: Vec3(0.0, 0.0, 0.0),
34 dir: Vec3(0.0, 0.0, -1.0),
39 pub fn set_pos(&mut self, pos: Vec3) {
43 pub fn get_pos(&self) -> Vec3 {
47 pub fn set_dir(&mut self, dir: Vec3) {
51 pub fn get_perspective(&self) -> [[f32; 4]; 4] {
52 let fov: f32 = 3.141592 / 2.0;
56 let f = 1.0 / (fov / 2.0).tan();
58 // note: remember that this is column-major, so the lines of code are actually columns
60 [f / self.aspect_ratio, 0.0, 0.0 , 0.0],
61 [ 0.0 , f , 0.0 , 0.0],
62 [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
63 [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
67 pub fn get_view(&self) -> [[f32; 4]; 4] {
68 let f = norm(&self.dir);
70 let up = Vec3(0.0, 1.0, 0.0);
72 let s = Vec3(f.1 * up.2 - f.2 * up.1,
73 f.2 * up.0 - f.0 * up.2,
74 f.0 * up.1 - f.1 * up.0);
77 let u = (sn.1 * f.2 - sn.2 * f.1,
78 sn.2 * f.0 - sn.0 * f.2,
79 sn.0 * f.1 - sn.1 * f.0);
81 let p = (-self.pos.0 * s.0 - self.pos.1 * s.1 - self.pos.2 * s.2,
82 -self.pos.0 * u.0 - self.pos.1 * u.1 - self.pos.2 * u.2,
83 -self.pos.0 * f.0 - self.pos.1 * f.1 - self.pos.2 * f.2);
85 // note: remember that this is column-major, so the lines of code are actually columns
87 [sn.0, u.0, f.0, 0.0],
88 [sn.1, u.1, f.1, 0.0],
89 [sn.2, u.2, f.2, 0.0],
94 pub fn update(&mut self) {
95 let f = norm(&self.dir);
97 let up = Vec3(0.0, 1.0, 0.0);
99 let s = Vec3(f.1 * up.2 - f.2 * up.1,
100 f.2 * up.0 - f.0 * up.2,
101 f.0 * up.1 - f.1 * up.0);
105 let u = Vec3(s.1 * f.2 - s.2 * f.1,
106 s.2 * f.0 - s.0 * f.2,
107 s.0 * f.1 - s.1 * f.0);
110 self.pos += u * 0.01;
112 if self.moving_left {
113 self.pos.0 -= s.0 * 0.01;
114 self.pos.1 -= s.1 * 0.01;
115 self.pos.2 -= s.2 * 0.01;
117 if self.moving_down {
118 self.pos.0 -= u.0 * 0.01;
119 self.pos.1 -= u.1 * 0.01;
120 self.pos.2 -= u.2 * 0.01;
122 if self.moving_right {
123 self.pos += s * 0.01;
125 if self.moving_forward {
126 self.pos += f * 0.01;
128 if self.moving_backward {
129 self.pos.0 -= f.0 * 0.01;
130 self.pos.1 -= f.1 * 0.01;
131 self.pos.2 -= f.2 * 0.01;
133 if self.turning_left {
135 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
137 if self.turning_right {
139 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
143 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
145 if self.turning_down {
147 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
149 //println!("camera_pos = {:?}", self.pos);
150 //println!("camera_dir = {:?}", self.dir);
153 pub fn process_input(&mut self, event: &glutin::Event) {
155 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Up)) => {
156 self.moving_up = true;
158 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Up)) => {
159 self.moving_up = false;
161 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Down)) => {
162 self.moving_down = true;
164 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Down)) => {
165 self.moving_down = false;
167 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Left)) => {
168 self.moving_left = true;
170 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Left)) => {
171 self.moving_left = false;
173 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Right)) => {
174 self.moving_right = true;
176 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Right)) => {
177 self.moving_right = false;
179 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::A)) => {
180 self.turning_left = true;
182 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::A)) => {
183 self.turning_left = false;
185 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::D)) => {
186 self.turning_right = true;
188 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::D)) => {
189 self.turning_right = false;
191 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::W)) => {
192 self.moving_forward = true;
194 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::W)) => {
195 self.moving_forward = false;
197 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::S)) => {
198 self.moving_backward = true;
200 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::S)) => {
201 self.moving_backward = false;
203 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::R)) => {
204 self.turning_up = true;
206 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::R)) => {
207 self.turning_up = false;
209 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::F)) => {
210 self.turning_down = true;
212 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::F)) => {
213 self.turning_down = false;