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 set_dir(&mut self, dir: Vec3) {
47 pub fn get_perspective(&self) -> [[f32; 4]; 4] {
48 let fov: f32 = 3.141592 / 2.0;
52 let f = 1.0 / (fov / 2.0).tan();
54 // note: remember that this is column-major, so the lines of code are actually columns
56 [f / self.aspect_ratio, 0.0, 0.0 , 0.0],
57 [ 0.0 , f , 0.0 , 0.0],
58 [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
59 [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
64 pub fn get_view(&self) -> [[f32; 4]; 4] {
65 let f = norm(&self.dir);
67 let up = Vec3(0.0, 1.0, 0.0);
69 let s = Vec3(f.1 * up.2 - f.2 * up.1,
70 f.2 * up.0 - f.0 * up.2,
71 f.0 * up.1 - f.1 * up.0);
74 let u = (sn.1 * f.2 - sn.2 * f.1,
75 sn.2 * f.0 - sn.0 * f.2,
76 sn.0 * f.1 - sn.1 * f.0);
78 let p = (-self.pos.0 * s.0 - self.pos.1 * s.1 - self.pos.2 * s.2,
79 -self.pos.0 * u.0 - self.pos.1 * u.1 - self.pos.2 * u.2,
80 -self.pos.0 * f.0 - self.pos.1 * f.1 - self.pos.2 * f.2);
82 // note: remember that this is column-major, so the lines of code are actually columns
84 [sn.0, u.0, f.0, 0.0],
85 [sn.1, u.1, f.1, 0.0],
86 [sn.2, u.2, f.2, 0.0],
91 pub fn update(&mut self) {
92 let f = norm(&self.dir);
94 let up = Vec3(0.0, 1.0, 0.0);
96 let s = Vec3(f.1 * up.2 - f.2 * up.1,
97 f.2 * up.0 - f.0 * up.2,
98 f.0 * up.1 - f.1 * up.0);
102 let u = Vec3(s.1 * f.2 - s.2 * f.1,
103 s.2 * f.0 - s.0 * f.2,
104 s.0 * f.1 - s.1 * f.0);
107 self.pos += u * 0.01;
109 if self.moving_left {
110 self.pos.0 -= s.0 * 0.01;
111 self.pos.1 -= s.1 * 0.01;
112 self.pos.2 -= s.2 * 0.01;
114 if self.moving_down {
115 self.pos.0 -= u.0 * 0.01;
116 self.pos.1 -= u.1 * 0.01;
117 self.pos.2 -= u.2 * 0.01;
119 if self.moving_right {
120 self.pos += s * 0.01;
122 if self.moving_forward {
123 self.pos += f * 0.01;
125 if self.moving_backward {
126 self.pos.0 -= f.0 * 0.01;
127 self.pos.1 -= f.1 * 0.01;
128 self.pos.2 -= f.2 * 0.01;
130 if self.turning_left {
132 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
134 if self.turning_right {
136 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
140 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
142 if self.turning_down {
144 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
146 //println!("camera_pos = {:?}", self.pos);
147 //println!("camera_dir = {:?}", self.dir);
150 pub fn process_input(&mut self, event: &glutin::Event) {
152 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Up)) => {
153 self.moving_up = true;
155 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Up)) => {
156 self.moving_up = false;
158 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Down)) => {
159 self.moving_down = true;
161 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Down)) => {
162 self.moving_down = false;
164 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Left)) => {
165 self.moving_left = true;
167 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Left)) => {
168 self.moving_left = false;
170 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Right)) => {
171 self.moving_right = true;
173 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Right)) => {
174 self.moving_right = false;
176 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::A)) => {
177 self.turning_left = true;
179 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::A)) => {
180 self.turning_left = false;
182 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::D)) => {
183 self.turning_right = true;
185 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::D)) => {
186 self.turning_right = false;
188 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::W)) => {
189 self.moving_forward = true;
191 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::W)) => {
192 self.moving_forward = false;
194 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::S)) => {
195 self.moving_backward = true;
197 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::S)) => {
198 self.moving_backward = false;
200 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::R)) => {
201 self.turning_up = true;
203 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::R)) => {
204 self.turning_up = false;
206 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::F)) => {
207 self.turning_down = true;
209 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::F)) => {
210 self.turning_down = false;