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