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 backface_culling: glium::draw_parameters::BackfaceCullingMode::CullCounterClockwise,
99 frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, ¶ms).unwrap();