2 use glium::{Display, Program, Surface, implement_vertex};
3 use glium::index::{IndexBuffer, PrimitiveType};
6 pub fn shaded_program(display: &Display) -> Program {
7 let vertex_shader_src = include_str!("shaders/shaded.vert");
8 let fragment_shader_src = include_str!("shaders/shaded.frag");
9 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 {
20 vertexes: glium::VertexBuffer<Vertex>,
22 indices: IndexBuffer<u16>,
26 pub fn new(display: &Display, program: Rc<Program>) -> ShadedCube {
37 Vertex { position: [-0.5, -0.5, 0.5], normal: [ 0., 0., -1.] }, // 0
38 Vertex { position: [ 0.5, -0.5, 0.5], normal: [ -1., 0., 0.] }, // 1
39 Vertex { position: [ 0.5, 0.5, 0.5], normal: [ 0., -1., 0.] }, // 2
40 Vertex { position: [-0.5, 0.5, 0.5], normal: [ 1., 0., 0.] }, // 3
43 Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0., 1., 0.] }, // 4
44 Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0., 0., 1.] }, // 5
45 Vertex { position: [ 0.5, 0.5, -0.5], normal: [ 0., 0., 0.] }, // 6
46 Vertex { position: [-0.5, 0.5, -0.5], normal: [ 0., 0., 0.] }, // 7
48 const INDICES: &[u16] = &[
49 1, 2, 0, 2, 3, 0, // Front
50 5, 6, 1, 6, 2, 1, // Right
51 6, 7, 2, 7, 3, 2, // Top
52 4, 0, 3, 7, 4, 3, // Left
53 5, 1, 4, 1, 0, 4, // Top
54 7, 6, 5, 4, 7, 5u16 // Back
58 vertexes: glium::VertexBuffer::new(display, &vertex_data).unwrap(),
60 indices: IndexBuffer::new(display, PrimitiveType::TrianglesList, INDICES).unwrap(),
64 pub fn draw<U>(&self, frame: &mut glium::Frame, uniforms: &U)
65 where U: glium::uniforms::Uniforms {
66 let params = glium::DrawParameters {
68 test: glium::draw_parameters::DepthTest::IfLess,
72 backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
75 frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, ¶ms).unwrap();