2 use glium::{Display, Program, Surface};
3 use glium::index::{IndexBuffer, PrimitiveType};
5 pub fn shaded_program(display: &Display) -> Program {
6 let vertex_shader_src = include_str!("shaders/shaded.vert");
7 let fragment_shader_src = include_str!("shaders/shaded.frag");
8 Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
11 #[derive(Copy, Clone)]
16 implement_vertex!(Vertex, position, normal);
18 pub struct ShadedCube<'a> {
19 vertexes: glium::VertexBuffer<Vertex>,
21 indices: IndexBuffer<u16>,
24 impl<'a> ShadedCube<'a> {
25 pub fn new(display: &Display, program: &'a Program) -> ShadedCube<'a> {
36 Vertex { position: [-0.5, -0.5, 0.5], normal: [ 0., 0., -1.] }, // 0
37 Vertex { position: [ 0.5, -0.5, 0.5], normal: [ -1., 0., 0.] }, // 1
38 Vertex { position: [ 0.5, 0.5, 0.5], normal: [ 0., -1., 0.] }, // 2
39 Vertex { position: [-0.5, 0.5, 0.5], normal: [ 1., 0., 0.] }, // 3
42 Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0., 1., 0.] }, // 4
43 Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0., 0., 1.] }, // 5
44 Vertex { position: [ 0.5, 0.5, -0.5], normal: [ 0., 0., 0.] }, // 6
45 Vertex { position: [-0.5, 0.5, -0.5], normal: [ 0., 0., 0.] }, // 7
47 const INDICES: &[u16] = &[
48 1, 2, 0, 2, 3, 0, // Front
49 5, 6, 1, 6, 2, 1, // Right
50 6, 7, 2, 7, 3, 2, // Top
51 4, 0, 3, 7, 4, 3, // Left
52 5, 1, 4, 1, 0, 4, // Top
53 7, 6, 5, 4, 7, 5u16 // Back
57 vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
59 indices: IndexBuffer::new(display, PrimitiveType::TrianglesList, INDICES).unwrap(),
63 pub fn draw<U>(&self, frame: &mut glium::Frame, uniforms: &U)
64 where U: glium::uniforms::Uniforms {
65 let params = glium::DrawParameters {
67 test: glium::draw_parameters::DepthTest::IfLess,
71 backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
74 frame.draw(&self.vertexes, &self.indices, self.program, uniforms, ¶ms).unwrap();