sRGB correction for C64 palette.
[mandelwow.git] / shaded_cube.rs
1 use cube::Cube;
2 use glium;
3 use glium::{Display, Program, Surface};
4 use glium::index::{IndexBuffer, PrimitiveType};
5
6 pub fn shaded_program(display: &Display) -> Program {
7     let vertex_shader_src = include_str!("shaded.vert");
8     let fragment_shader_src = include_str!("shaded.frag");
9     return 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<'a> {
20     vertexes: glium::VertexBuffer<Vertex>,
21     program: &'a Program,
22     indices: IndexBuffer<u16>,
23 }
24
25 impl<'a> ShadedCube<'a> {
26     pub fn new(display: &Display, c: &Cube, program: &'a Program) -> ShadedCube<'a> {
27         //      x--->
28         //      4 ──────┐ 5
29         //      ╱┆     ╱│
30         //   0 ┌─────┐1 │
31         // y   │ 7+┄┄│┄┄+ 6
32         // |   │╱    │ ╱   /
33         // v   └─────┘    z
34         //    3       2
35         let vertex_data = [
36             // Front face
37             Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  0.,  0.,  1.] },  // 0
38             Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [  0.,  0.,  1.] },  // 1
39             Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [  0.,  0.,  1.] },  // 2
40             Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  0.,  0.,  1.] },  // 3
41
42             // Back face
43             Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  0.,  0., -1.] },  // 7
44             Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [  0.,  0., -1.] },  // 6
45             Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [  0.,  0., -1.] },  // 5
46             Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  0.,  0., -1.] },  // 4
47
48             // Right face
49             Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [ -1.,  0.,  0.] },  // 1
50             Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [ -1.,  0.,  0.] },  // 5
51             Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [ -1.,  0.,  0.] },  // 6
52             Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [ -1.,  0.,  0.] },  // 2
53
54             // Left face
55             Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  1.,  0.,  0.] },  // 0
56             Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  1.,  0.,  0.] },  // 3
57             Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  1.,  0.,  0.] },  // 7
58             Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  1.,  0.,  0.] },  // 4
59
60             // Top face
61             Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  0.,  1.,  0.] },  // 0
62             Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  0.,  1.,  0.] },  // 4
63             Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [  0.,  1.,  0.] },  // 5
64             Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [  0.,  1.,  0.] },  // 1
65
66             // Bottom face
67             Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [  0., -1.,  0.] },  // 2
68             Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [  0., -1.,  0.] },  // 6
69             Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  0., -1.,  0.] },  // 7
70             Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  0., -1.,  0.] },  // 3
71         ];
72         const INDICES: &[u16] = &[
73              0,  1,  2,  0,  2,  3,  // Front
74              4,  5,  6,  4,  6,  7,  // Back
75              8,  9, 10,  8, 10, 11,  // Right
76             12, 13, 14, 12, 14, 15,  // Left
77             16, 17, 18, 16, 18, 19,  // Top
78             20, 21, 22, 20, 22, 23u16  // Bottom
79         ];
80
81         ShadedCube {
82             vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
83             program: program,
84             indices:  IndexBuffer::new(display, PrimitiveType::TrianglesList, INDICES).unwrap(),
85         }
86     }
87
88     pub fn draw<U>(&self, frame: &mut glium::Frame, uniforms: &U)
89             where U: glium::uniforms::Uniforms {
90         let params = glium::DrawParameters {
91             depth: glium::Depth {
92                 test: glium::draw_parameters::DepthTest::IfLess,
93                 write: true,
94                 ..Default::default()
95             },
96             backface_culling: glium::draw_parameters::BackfaceCullingMode::CullCounterClockwise,
97             ..Default::default()
98         };
99         frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, &params).unwrap();
100     }
101 }