Split bounding_box to its own module.
[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 = r#"
8         #version 140
9         in vec3 position;
10         uniform mat4 perspective;
11         uniform mat4 view;
12         uniform mat4 model;
13
14         void main() {
15             mat4 modelview = view * model;
16             gl_Position = perspective * modelview * vec4(position, 1.0);
17         }
18     "#;
19
20     let fragment_shader_src = r#"
21         #version 140
22
23         out vec4 color;
24
25         void main() {
26             color = vec4(1.0, 1.0, 1.0, 1.0);
27         }
28     "#;
29
30     return Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap();
31 }
32
33 pub fn draw<U>(display: &Display,
34                frame: &mut glium::Frame,
35                program: &Program,
36                uniforms: &U,
37                cube: &Cube) where U: glium::uniforms::Uniforms {
38
39     #[derive(Copy, Clone)]
40     struct Vertex { position: [f32; 3] }
41     implement_vertex!(Vertex, position);
42
43     let cube = [
44         Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
45         Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
46         Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
47         Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
48         Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
49         Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
50         Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
51         Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
52     ];
53     let vb = glium::VertexBuffer::new(display, &cube).unwrap();
54
55     let params = glium::DrawParameters {
56         depth: glium::Depth {
57             test: glium::draw_parameters::DepthTest::IfLess,
58             write: true,
59             ..Default::default()
60         },
61         blend: glium::Blend::alpha_blending(),
62         ..Default::default()
63     };
64
65     let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
66                                          &[0, 1, 2, 3u16]).unwrap();
67     frame.draw(&vb, &front_indices, program, uniforms, &params).unwrap();
68
69     let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
70                                         &[4, 5, 6, 7u16]).unwrap();
71     frame.draw(&vb, &back_indices, program, uniforms, &params).unwrap();
72
73     let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
74                                          &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
75     frame.draw(&vb, &sides_indices, program, uniforms, &params).unwrap();
76 }