3 use glium::{Display, Program, Surface};
4 use glium::index::{IndexBuffer, PrimitiveType};
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();
12 #[derive(Copy, Clone)]
17 implement_vertex!(Vertex, position, normal);
19 pub struct ShadedCube<'a> {
20 vertexes: glium::VertexBuffer<Vertex>,
22 indices: IndexBuffer<u16>,
25 impl<'a> ShadedCube<'a> {
26 pub fn new(display: &Display, c: &Cube, program: &'a Program) -> ShadedCube<'a> {
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
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
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
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
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
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
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
82 vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
84 indices: IndexBuffer::new(display, PrimitiveType::TrianglesList, INDICES).unwrap(),
88 pub fn draw<U>(&self, frame: &mut glium::Frame, uniforms: &U)
89 where U: glium::uniforms::Uniforms {
90 let params = glium::DrawParameters {
92 test: glium::draw_parameters::DepthTest::IfLess,
96 blend: glium::Blend::alpha_blending(),
99 frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, ¶ms).unwrap();
104 pub fn draw<U>(display: &Display,
105 frame: &mut glium::Frame,
108 cube: &Cube) where U: glium::uniforms::Uniforms {
110 implement_vertex!(Vertex, position, normal);
112 #[derive(Copy, Clone)]
113 struct Normal { normal: [f32; 3] }
114 implement_vertex!(Normal, normal);
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
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
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
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
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
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
161 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
163 let params = glium::DrawParameters {
164 depth: glium::Depth {
165 test: glium::draw_parameters::DepthTest::IfLess,
169 blend: glium::Blend::alpha_blending(),
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
181 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();