Add a sea shaded cubes, just because. No big deal (yet).
[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             blend: glium::Blend::alpha_blending(),
97             ..Default::default()
98         };
99         frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, &params).unwrap();
100     }
101 }
102
103 /*
104 pub fn draw<U>(display: &Display,
105                frame: &mut glium::Frame,
106                program: &Program,
107                uniforms: &U,
108                cube: &Cube) where U: glium::uniforms::Uniforms {
109
110     implement_vertex!(Vertex, position, normal);
111
112     #[derive(Copy, Clone)]
113     struct Normal { normal: [f32; 3] }
114     implement_vertex!(Normal, normal);
115
116     //      x--->
117     //      4 ──────┐ 5   
118     //      ╱┆     ╱│
119     //   0 ┌─────┐1 │
120     // y   │ 7+┄┄│┄┄+ 6 
121     // |   │╱    │ ╱   /
122     // v   └─────┘    z
123     //    3       2
124     let cube = [
125         // Front face
126         Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  0.,  0.,  1.] },  // 0
127         Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [  0.,  0.,  1.] },  // 1
128         Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [  0.,  0.,  1.] },  // 2
129         Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  0.,  0.,  1.] },  // 3
130
131         // Back face
132         Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  0.,  0., -1.] },  // 7
133         Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [  0.,  0., -1.] },  // 6
134         Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [  0.,  0., -1.] },  // 5
135         Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  0.,  0., -1.] },  // 4
136
137         // Right face
138         Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [ -1.,  0.,  0.] },  // 1
139         Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [ -1.,  0.,  0.] },  // 5
140         Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [ -1.,  0.,  0.] },  // 6
141         Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [ -1.,  0.,  0.] },  // 2
142
143         // Left face
144         Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  1.,  0.,  0.] },  // 0
145         Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  1.,  0.,  0.] },  // 3
146         Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  1.,  0.,  0.] },  // 7
147         Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  1.,  0.,  0.] },  // 4
148
149         // Top face
150         Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  0.,  1.,  0.] },  // 0
151         Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  0.,  1.,  0.] },  // 4
152         Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [  0.,  1.,  0.] },  // 5
153         Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [  0.,  1.,  0.] },  // 1
154
155         // Bottom face
156         Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [  0., -1.,  0.] },  // 2
157         Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [  0., -1.,  0.] },  // 6
158         Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  0., -1.,  0.] },  // 7
159         Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  0., -1.,  0.] },  // 3
160     ];
161     let vb = glium::VertexBuffer::new(display, &cube).unwrap();
162
163     let params = glium::DrawParameters {
164         depth: glium::Depth {
165             test: glium::draw_parameters::DepthTest::IfLess,
166             write: true,
167             ..Default::default()
168         },
169         blend: glium::Blend::alpha_blending(),
170         ..Default::default()
171     };
172
173     let front_indices = IndexBuffer::new(display, PrimitiveType::TrianglesList,
174                                          &[  0,  1,  2,  0,  2,  3,  // Front
175                                              4,  5,  6,  4,  6,  7,  // Back
176                                              8,  9, 10,  8, 10, 11,  // Right
177                                             12, 13, 14, 12, 14, 15,  // Left
178                                             16, 17, 18, 16, 18, 19,  // Top
179                                             20, 21, 22, 20, 22, 23u16,  // Bottom
180                                         ]).unwrap();
181     frame.draw(&vb, &front_indices, program, uniforms, &params).unwrap();
182 }
183
184 */