wasm: Enable backtraces in browser console
[mandelwow.git] / shaded_cube.rs
1 use glium;
2 use glium::{Display, Program, Surface};
3 use glium::index::{IndexBuffer, PrimitiveType};
4
5 pub fn shaded_program(display: &Display) -> Program {
6     let vertex_shader_src = include_str!("shaders/shaded.vert");
7     let fragment_shader_src = include_str!("shaders/shaded.frag");
8     Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
9 }
10
11 #[derive(Copy, Clone)]
12 struct Vertex {
13     position: [f32; 3],
14     normal: [f32; 3],
15 }
16 implement_vertex!(Vertex, position, normal);
17
18 pub struct ShadedCube<'a> {
19     vertexes: glium::VertexBuffer<Vertex>,
20     program: &'a Program,
21     indices: IndexBuffer<u16>,
22 }
23
24 impl<'a> ShadedCube<'a> {
25     pub fn new(display: &Display, program: &'a Program) -> ShadedCube<'a> {
26         //      x--->
27         //      4 ──────┐ 5
28         //      ╱┆     ╱│
29         //   0 ┌─────┐1 │
30         // y   │ 7+┄┄│┄┄+ 6    z
31         // |   │╱    │ ╱    ╱
32         // v   └─────┘
33         //    3       2
34         let vertex_data = [
35             // Front
36             Vertex { position: [-0.5, -0.5,  0.5], normal: [  0.,  0., -1.] },  // 0
37             Vertex { position: [ 0.5, -0.5,  0.5], normal: [ -1.,  0.,  0.] },  // 1
38             Vertex { position: [ 0.5,  0.5,  0.5], normal: [  0., -1.,  0.] },  // 2
39             Vertex { position: [-0.5,  0.5,  0.5], normal: [  1.,  0.,  0.] },  // 3
40
41             // Back
42             Vertex { position: [-0.5, -0.5, -0.5], normal: [  0.,  1.,  0.] },  // 4
43             Vertex { position: [ 0.5, -0.5, -0.5], normal: [  0.,  0.,  1.] },  // 5
44             Vertex { position: [ 0.5,  0.5, -0.5], normal: [  0.,  0.,  0.] },  // 6
45             Vertex { position: [-0.5,  0.5, -0.5], normal: [  0.,  0.,  0.] },  // 7
46         ];
47         const INDICES: &[u16] = &[
48              1,  2,  0,  2,  3,  0,    // Front
49              5,  6,  1,  6,  2,  1,    // Right
50              6,  7,  2,  7,  3,  2,    // Top
51              4,  0,  3,  7,  4,  3,    // Left
52              5,  1,  4,  1,  0,  4,    // Top
53              7,  6,  5,  4,  7,  5u16  // Back
54         ];
55
56         ShadedCube {
57             vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
58             program: program,
59             indices:  IndexBuffer::new(display, PrimitiveType::TrianglesList, INDICES).unwrap(),
60         }
61     }
62
63     pub fn draw<U>(&self, frame: &mut glium::Frame, uniforms: &U)
64             where U: glium::uniforms::Uniforms {
65         let params = glium::DrawParameters {
66             depth: glium::Depth {
67                 test: glium::draw_parameters::DepthTest::IfLess,
68                 write: true,
69                 ..Default::default()
70             },
71             backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
72             ..Default::default()
73         };
74         frame.draw(&self.vertexes, &self.indices, self.program, uniforms, &params).unwrap();
75     }
76 }