Fix benchmarks for latest glium/glutin API
[mandelwow.git] / bounding_box.rs
1 use crate::cube::Cube;
2 use glium;
3 use glium::{Display, Program, Surface, implement_vertex};
4 use glium::index::{IndexBuffer, PrimitiveType};
5 use std::rc::Rc;
6
7 pub fn solid_fill_program(display: &Display) -> Program {
8     let vertex_shader_src = include_str!("shaders/solid.vert");
9     let fragment_shader_src = include_str!("shaders/solid.frag");
10     Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
11 }
12
13 #[derive(Copy, Clone)]
14 struct Vertex { position: [f32; 3] }
15 implement_vertex!(Vertex, position);
16
17 pub struct BoundingBox {
18     vertexes: glium::VertexBuffer<Vertex>,
19     program: Rc<Program>,
20     indices: IndexBuffer<u16>,
21 }
22
23 impl BoundingBox {
24     pub fn new(display: &Display, c: &Cube, program: Rc<Program>) -> BoundingBox {
25         let vertex_data = [
26             Vertex { position: [c.xmin, c.ymin, c.zmin] },
27             Vertex { position: [c.xmax, c.ymin, c.zmin] },
28             Vertex { position: [c.xmax, c.ymax, c.zmin] },
29             Vertex { position: [c.xmin, c.ymax, c.zmin] },
30             Vertex { position: [c.xmin, c.ymin, c.zmax] },
31             Vertex { position: [c.xmax, c.ymin, c.zmax] },
32             Vertex { position: [c.xmax, c.ymax, c.zmax] },
33             Vertex { position: [c.xmin, c.ymax, c.zmax] },
34         ];
35
36         const INDICES: &[u16] = &[0, 1, 1, 2, 2, 3, 3, 0,   // front
37                                   4, 5, 5, 6, 6, 7, 7, 4,   // back
38                                   0, 4, 1, 5, 2, 6, 3, 7];  // sides
39
40         BoundingBox {
41             vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
42             program: program,
43             indices:  IndexBuffer::new(display, PrimitiveType::LinesList, INDICES).unwrap(),
44         }
45     }
46
47     pub fn draw<U>(&self, frame: &mut glium::Frame,
48                    uniforms: &U) where U: glium::uniforms::Uniforms {
49         let params = glium::DrawParameters {
50             depth: glium::Depth {
51                 test: glium::draw_parameters::DepthTest::IfLess,
52                 write: true,
53                 ..Default::default()
54             },
55             blend: glium::Blend::alpha_blending(),
56             ..Default::default()
57         };
58         frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, &params).unwrap();
59     }
60 }