1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
extern crate nalgebra; use self::nalgebra::Matrix4; use self::nalgebra::Orthographic3; use self::nalgebra::Rotation3; use self::nalgebra::Vector3; pub struct OrthoCamera { position: Vector3<f32>, width: f32, height: f32, zoom: f32, near: f32, far: f32, rotation: f32, ppu: f32, } impl OrthoCamera { pub fn new(width: f32, height: f32) -> Self { OrthoCamera { position: Vector3::new(0.0, 0.0, 0.0), width: width, height: height, zoom: 1.0, near: -100.0, far: 100.0, ppu: 256.0, rotation: 0.0, } } pub fn set_dimensions(&mut self, width: f32, height: f32) { self.width = width; self.height = height; } pub fn set_zoom(&mut self, zoom: f32) { self.zoom = zoom; } pub fn translate(&mut self, dx: f32, dy: f32) { self.position.x += dx; self.position.y += dy; } pub fn rotate(&mut self, rads: f32) { self.rotation += rads; } pub fn set_rotation(&mut self, rad: f32) { self.rotation = rad; } fn projection(&self) -> Matrix4<f32> { let effective_width = self.width / (self.zoom * self.ppu); let effective_height = self.height / (self.zoom * self.ppu); let half_width = effective_width / 2.0; let half_height = effective_height / 2.0; Orthographic3::new( -half_width, half_width, -half_height, half_height, self.near, self.far, ).unwrap() } fn rotation(&self) -> Matrix4<f32> { Rotation3::from_axis_angle(&Vector3::z_axis(), self.rotation).to_homogeneous() } fn view(&self) -> Matrix4<f32> { Matrix4::new_translation(&self.position) } pub fn combined(&self) -> [[f32; 4]; 4] { (self.projection() * (self.view() * self.rotation())).into() } }