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