Skip to content

Instantly share code, notes, and snippets.

@dimitriye98
Created August 25, 2016 15:50
Show Gist options
  • Save dimitriye98/c021df7475d91c9e64e5be41141d5f5a to your computer and use it in GitHub Desktop.
Save dimitriye98/c021df7475d91c9e64e5be41141d5f5a to your computer and use it in GitHub Desktop.
pub struct DrawService {
display: Rc<Display>,
frame: Frame,
program: Program,
perspective: PerspectiveMatrix3<f32>,
}
impl DrawService {
fn build_perspective(frame: &Frame) -> PerspectiveMatrix3<f32> {
let (width, height) = frame.get_dimensions();
let fov: f32 = ::std::f32::consts::PI / 3.0;
let zfar = 1024.0;
let znear = 0.001;
PerspectiveMatrix3::new(width as f32 / height as f32, fov, znear, zfar)
}
pub fn new(display: Rc<Display>, program: Program) -> DrawService {
let mut frame = display.draw();
frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
let perspective = DrawService::build_perspective(&frame);
DrawService {
display: display,
program: program,
frame: frame,
perspective: perspective,
}
}
// TODO: Switch to trait object when glium updates.
pub fn facade(&self) -> &Display {
&self.display
}
pub fn update_perspective(&mut self) {
self.perspective = DrawService::build_perspective(&self.frame);
}
pub fn flush(&mut self) {
// TODO: Update framerate
self.frame.set_finish();
self.frame = self.display.draw();
self.frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
}
pub fn draw_buffer<'a, 'b, I, V>(&mut self, model_view: &Matrix4<f32>, vertices: V, indices: I)
where I: Into<IndicesSource<'a>>, V: MultiVerticesSource<'b> {
let uniforms = uniform! {
model_view: model_view.as_ref().clone(),
perspective: self.perspective.as_matrix().as_ref().clone(),
};
use glium::{DrawParameters, Depth};
use glium::draw_parameters::{DepthTest, BackfaceCullingMode};
let params = DrawParameters {
depth: Depth {
test: DepthTest::IfLess,
write: true,
.. Default::default()
},
backface_culling: BackfaceCullingMode::CullClockwise,
.. Default::default()
};
self.frame.draw(vertices, indices, &self.program, &uniforms, &params).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment