5 use std::ops::AddAssign;
10 //use glutin::VirtualKeyCode;
12 #[derive(Default, PartialEq, Debug, Clone, Copy)]
13 pub struct Vec3 (f32, f32, f32);
17 fn add(self, other: Vec3) -> Vec3 {
18 Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
22 impl AddAssign for Vec3 {
23 fn add_assign(&mut self, other: Vec3) {
24 *self = Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
30 fn sub(self, other: Vec3) -> Vec3 {
31 Vec3(self.0 - other.0, self.1 - other.1, self.2 - other.2)
35 impl Mul<f32> for Vec3 {
37 fn mul(self, f: f32) -> Vec3 {
38 Vec3(self.0 * f, self.1 * f, self.2 * f)
43 pub struct CameraState {
53 moving_backward: bool,
60 fn norm(v: &Vec3) -> Vec3 {
61 let len = (v.0 * v.0 + v.1 * v.1 + v.2 * v.2).sqrt();
62 Vec3(v.0 / len, v.1 / len, v.2 / len)
66 pub fn new() -> CameraState {
68 aspect_ratio: 1024.0 / 768.0,
69 pos: Vec3(0.0, 0.0, 0.0),
70 dir: Vec3(0.0, 0.0, 1.0),
75 pub fn set_pos(&mut self, pos: Vec3) {
79 pub fn set_dir(&mut self, dir: Vec3) {
83 pub fn get_perspective(&self) -> [[f32; 4]; 4] {
84 let fov: f32 = 3.141592 / 2.0;
88 let f = 1.0 / (fov / 2.0).tan();
90 // note: remember that this is column-major, so the lines of code are actually columns
92 [f / self.aspect_ratio, 0.0, 0.0 , 0.0],
93 [ 0.0 , f , 0.0 , 0.0],
94 [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
95 [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
100 pub fn get_view(&self) -> [[f32; 4]; 4] {
101 let f = norm(&self.dir);
103 let up = Vec3(0.0, 1.0, 0.0);
105 let s = Vec3(f.1 * up.2 - f.2 * up.1,
106 f.2 * up.0 - f.0 * up.2,
107 f.0 * up.1 - f.1 * up.0);
110 let u = (sn.1 * f.2 - sn.2 * f.1,
111 sn.2 * f.0 - sn.0 * f.2,
112 sn.0 * f.1 - sn.1 * f.0);
114 let p = (-self.pos.0 * s.0 - self.pos.1 * s.1 - self.pos.2 * s.2,
115 -self.pos.0 * u.0 - self.pos.1 * u.1 - self.pos.2 * u.2,
116 -self.pos.0 * f.0 - self.pos.1 * f.1 - self.pos.2 * f.2);
118 // note: remember that this is column-major, so the lines of code are actually columns
120 [sn.0, u.0, f.0, 0.0],
121 [sn.1, u.1, f.1, 0.0],
122 [sn.2, u.2, f.2, 0.0],
123 [p.0, p.1, p.2, 1.0],
127 pub fn update(&mut self) {
128 let f = norm(&self.dir);
130 let up = Vec3(0.0, 1.0, 0.0);
132 let s = Vec3(f.1 * up.2 - f.2 * up.1,
133 f.2 * up.0 - f.0 * up.2,
134 f.0 * up.1 - f.1 * up.0);
138 let u = Vec3(s.1 * f.2 - s.2 * f.1,
139 s.2 * f.0 - s.0 * f.2,
140 s.0 * f.1 - s.1 * f.0);
143 self.pos += u * 0.01;
145 if self.moving_left {
146 self.pos.0 -= s.0 * 0.01;
147 self.pos.1 -= s.1 * 0.01;
148 self.pos.2 -= s.2 * 0.01;
150 if self.moving_down {
151 self.pos.0 -= u.0 * 0.01;
152 self.pos.1 -= u.1 * 0.01;
153 self.pos.2 -= u.2 * 0.01;
155 if self.moving_right {
156 self.pos += s * 0.01;
158 if self.moving_forward {
159 self.pos += f * 0.01;
161 if self.moving_backward {
162 self.pos.0 -= f.0 * 0.01;
163 self.pos.1 -= f.1 * 0.01;
164 self.pos.2 -= f.2 * 0.01;
166 if self.turning_left {
168 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
170 if self.turning_right {
172 self.dir = Vec3(f.0 * a.cos() + f.2 * a.sin(), f.1, - f.0 * a.sin() + f.2 * a.cos());
176 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
178 if self.turning_down {
180 self.dir = Vec3(f.0, f.1 * a.cos() - f.2 * a.sin(), f.1 * a.sin() + f.2 * a.cos());
182 //println!("camera_pos = {:?}", self.pos);
183 //println!("camera_dir = {:?}", self.dir);
186 pub fn process_input(&mut self, event: &glutin::Event) {
188 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Up)) => {
189 self.moving_up = true;
191 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Up)) => {
192 self.moving_up = false;
194 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Down)) => {
195 self.moving_down = true;
197 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Down)) => {
198 self.moving_down = false;
200 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Left)) => {
201 self.moving_left = true;
203 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Left)) => {
204 self.moving_left = false;
206 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::Right)) => {
207 self.moving_right = true;
209 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::Right)) => {
210 self.moving_right = false;
212 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::A)) => {
213 self.turning_left = true;
215 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::A)) => {
216 self.turning_left = false;
218 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::D)) => {
219 self.turning_right = true;
221 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::D)) => {
222 self.turning_right = false;
224 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::W)) => {
225 self.moving_forward = true;
227 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::W)) => {
228 self.moving_forward = false;
230 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::S)) => {
231 self.moving_backward = true;
233 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::S)) => {
234 self.moving_backward = false;
236 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::R)) => {
237 self.turning_up = true;
239 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::R)) => {
240 self.turning_up = false;
242 &glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::F)) => {
243 self.turning_down = true;
245 &glutin::Event::KeyboardInput(glutin::ElementState::Released, _, Some(glutin::VirtualKeyCode::F)) => {
246 self.turning_down = false;