Some progress on factoring out rendering
Getty Ritter
9 years ago
| 1 | extern crate glium; | |
| 2 | ||
| 3 | use glium::{DisplayBuild, Display, Frame, Surface}; | |
| 4 | use glium::glutin::WindowBuilder; | |
| 5 | ||
| 1 | 6 | struct Graphics { |
| 2 | 7 | disp: Display, |
| 3 | 8 | } |
| 4 | 9 | |
| 5 | 10 | impl Graphics { |
| 6 |
fn |
|
| 11 | fn new() -> Self { | |
| 12 | let disp = WindowBuilder::new() | |
| 13 | .with_title(format!("utmyen")) | |
| 14 | .with_depth_buffer(24) | |
| 15 | .build_glium() | |
| 16 | .unwrap(); | |
| 17 | Graphics { disp: disp } | |
| 18 | } | |
| 19 | ||
| 20 | fn run(&mut self) { | |
| 21 | while (Target { frame: self.disp.draw() }).step() {} | |
| 7 | 22 | } |
| 8 | 23 | } |
| 24 | ||
| 25 | struct Target { | |
| 26 | frame: Frame, | |
| 27 | } | |
| 28 | ||
| 29 | impl Target { | |
| 30 | fn step(&mut self) -> bool { | |
| 31 | let light = [-1.0, 0.4, 0.9f32]; | |
| 32 | let uniforms = uniform! { | |
| 33 | perspective: self.perspective(), | |
| 34 | u_light: light, | |
| 35 | matrix: self.matrix(), | |
| 36 | }; | |
| 37 | ||
| 38 | let params = glium::DrawParameters { | |
| 39 | depth: glium::Depth { | |
| 40 | test: glium::draw_parameters::DepthTest::IfLess, | |
| 41 | write: true, | |
| 42 | .. Default::default() | |
| 43 | }, | |
| 44 | .. Default::default() | |
| 45 | }; | |
| 46 | return true; | |
| 47 | } | |
| 48 | ||
| 49 | fn matrix(&self) -> [[f32;4];4] { | |
| 50 | let rt = 0.0f32; | |
| 51 | let sx = 0.0f32; | |
| 52 | let px = 0.0f32; | |
| 53 | let py = 0.0f32; | |
| 54 | let pz = 0.0f32; | |
| 55 | [ [ rt.cos() * sx, 0.0, -rt.sin() * sx, 0.0 ], | |
| 56 | [ 0.0, sx, 0.0, 0.0 ], | |
| 57 | [ rt.sin() * sx, 0.0, rt.cos() * sx, 0.0 ], | |
| 58 | [ px, pz, py, sx ] | |
| 59 | ] | |
| 60 | } | |
| 61 | ||
| 62 | fn perspective(&self) -> [[f32;4];4] { | |
| 63 | let (width, height) = self.frame.get_dimensions(); | |
| 64 | let aspect_ratio = height as f32 / width as f32; | |
| 65 | ||
| 66 | let fov: f32 = 3.141592 / 3.0; | |
| 67 | let zfar = 1024.0; | |
| 68 | let znear = 0.1; | |
| 69 | ||
| 70 | let f = 1.0 / (fov / 2.0).tan(); | |
| 71 | ||
| 72 | [ | |
| 73 | [f * aspect_ratio , 0.0, 0.0 , 0.0], | |
| 74 | [ 0.0 , f , 0.0 , 0.0], | |
| 75 | [ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0], | |
| 76 | [ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0], | |
| 77 | ] | |
| 78 | } | |
| 79 | } |