Update to rust-rocket 0.7.2
[mandelwow.git] / shaded_cube.rs
index fb4fb1dafa105261c0823b4def54e7f8f0da7f2b..211f23fa17ad30609ddda9a9c5cbcf0c8c5d0e00 100644 (file)
@@ -1,12 +1,12 @@
-use cube::Cube;
 use glium;
-use glium::{Display, Program, Surface};
+use glium::{Display, Program, Surface, implement_vertex};
 use glium::index::{IndexBuffer, PrimitiveType};
+use std::rc::Rc;
 
 pub fn shaded_program(display: &Display) -> Program {
-    let vertex_shader_src = include_str!("shaded.vert");
-    let fragment_shader_src = include_str!("shaded.frag");
-    return Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap();
+    let vertex_shader_src = include_str!("shaders/shaded.vert");
+    let fragment_shader_src = include_str!("shaders/shaded.frag");
+    Program::from_source(display, vertex_shader_src, fragment_shader_src, None).unwrap()
 }
 
 #[derive(Copy, Clone)]
@@ -16,66 +16,42 @@ struct Vertex {
 }
 implement_vertex!(Vertex, position, normal);
 
-pub struct ShadedCube<'a> {
+pub struct ShadedCube {
     vertexes: glium::VertexBuffer<Vertex>,
-    program: &'a Program,
+    program: Rc<Program>,
     indices: IndexBuffer<u16>,
 }
 
-impl<'a> ShadedCube<'a> {
-    pub fn new(display: &Display, c: &Cube, program: &'a Program) -> ShadedCube<'a> {
+impl<'a> ShadedCube {
+    pub fn new(display: &Display, program: Rc<Program>) -> ShadedCube {
         //      x--->
-        //      4 ──────┐ 5   
+        //      4 ──────┐ 5
         //      ╱┆     ╱│
         //   0 ┌─────┐1 │
-        // y   │ 7+┄┄│┄┄+ 6 
-        // |   │╱    │ ╱   /
-        // v   └─────┘    z
+        // y   │ 7+┄┄│┄┄+ 6    z
+        // |   │╱    │ ╱    ╱
+        // v   └─────┘
         //    3       2
         let vertex_data = [
-            // Front face
-            Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  0.,  0.,  1.] },  // 0
-            Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [  0.,  0.,  1.] },  // 1
-            Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [  0.,  0.,  1.] },  // 2
-            Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  0.,  0.,  1.] },  // 3
-
-            // Back face
-            Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  0.,  0., -1.] },  // 7
-            Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [  0.,  0., -1.] },  // 6
-            Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [  0.,  0., -1.] },  // 5
-            Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  0.,  0., -1.] },  // 4
-
-            // Right face
-            Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [ -1.,  0.,  0.] },  // 1
-            Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [ -1.,  0.,  0.] },  // 5
-            Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [ -1.,  0.,  0.] },  // 6
-            Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [ -1.,  0.,  0.] },  // 2
-
-            // Left face
-            Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  1.,  0.,  0.] },  // 0
-            Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  1.,  0.,  0.] },  // 3
-            Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  1.,  0.,  0.] },  // 7
-            Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  1.,  0.,  0.] },  // 4
-
-            // Top face
-            Vertex { position: [c.xmin, c.ymin, c.zmin], normal: [  0.,  1.,  0.] },  // 0
-            Vertex { position: [c.xmin, c.ymin, c.zmax], normal: [  0.,  1.,  0.] },  // 4
-            Vertex { position: [c.xmax, c.ymin, c.zmax], normal: [  0.,  1.,  0.] },  // 5
-            Vertex { position: [c.xmax, c.ymin, c.zmin], normal: [  0.,  1.,  0.] },  // 1
-
-            // Bottom face
-            Vertex { position: [c.xmax, c.ymax, c.zmin], normal: [  0., -1.,  0.] },  // 2
-            Vertex { position: [c.xmax, c.ymax, c.zmax], normal: [  0., -1.,  0.] },  // 6
-            Vertex { position: [c.xmin, c.ymax, c.zmax], normal: [  0., -1.,  0.] },  // 7
-            Vertex { position: [c.xmin, c.ymax, c.zmin], normal: [  0., -1.,  0.] },  // 3
+            // Front
+            Vertex { position: [-0.5, -0.5,  0.5], normal: [  0.,  0., -1.] },  // 0
+            Vertex { position: [ 0.5, -0.5,  0.5], normal: [ -1.,  0.,  0.] },  // 1
+            Vertex { position: [ 0.5,  0.5,  0.5], normal: [  0., -1.,  0.] },  // 2
+            Vertex { position: [-0.5,  0.5,  0.5], normal: [  1.,  0.,  0.] },  // 3
+
+            // Back
+            Vertex { position: [-0.5, -0.5, -0.5], normal: [  0.,  1.,  0.] },  // 4
+            Vertex { position: [ 0.5, -0.5, -0.5], normal: [  0.,  0.,  1.] },  // 5
+            Vertex { position: [ 0.5,  0.5, -0.5], normal: [  0.,  0.,  0.] },  // 6
+            Vertex { position: [-0.5,  0.5, -0.5], normal: [  0.,  0.,  0.] },  // 7
         ];
         const INDICES: &[u16] = &[
-             0,  1,  2,  0,  2,  3,  // Front
-             4,  5,  6,  4,  6,  7,  // Back
-             8,  9, 10,  8, 10, 11,  // Right
-            12, 13, 14, 12, 14, 15,  // Left
-            16, 17, 18, 16, 18, 19,  // Top
-            20, 21, 22, 20, 22, 23u16  // Bottom
+             1,  2,  0,  2,  3,  0,    // Front
+             5,  6,  1,  6,  2,  1,    // Right
+             6,  7,  2,  7,  3,  2,    // Top
+             4,  0,  3,  7,  4,  3,    // Left
+             5,  1,  4,  1,  0,  4,    // Top
+             7,  6,  5,  4,  7,  5u16  // Back
         ];
 
         ShadedCube {
@@ -93,92 +69,9 @@ impl<'a> ShadedCube<'a> {
                 write: true,
                 ..Default::default()
             },
-            blend: glium::Blend::alpha_blending(),
+            backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
             ..Default::default()
         };
         frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, &params).unwrap();
     }
 }
-
-/*
-pub fn draw<U>(display: &Display,
-               frame: &mut glium::Frame,
-               program: &Program,
-               uniforms: &U,
-               cube: &Cube) where U: glium::uniforms::Uniforms {
-
-    implement_vertex!(Vertex, position, normal);
-
-    #[derive(Copy, Clone)]
-    struct Normal { normal: [f32; 3] }
-    implement_vertex!(Normal, normal);
-
-    //      x--->
-    //      4 ──────┐ 5   
-    //      ╱┆     ╱│
-    //   0 ┌─────┐1 │
-    // y   │ 7+┄┄│┄┄+ 6 
-    // |   │╱    │ ╱   /
-    // v   └─────┘    z
-    //    3       2
-    let cube = [
-        // Front face
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  0.,  0.,  1.] },  // 0
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [  0.,  0.,  1.] },  // 1
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [  0.,  0.,  1.] },  // 2
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  0.,  0.,  1.] },  // 3
-
-        // Back face
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  0.,  0., -1.] },  // 7
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [  0.,  0., -1.] },  // 6
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [  0.,  0., -1.] },  // 5
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  0.,  0., -1.] },  // 4
-
-        // Right face
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [ -1.,  0.,  0.] },  // 1
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [ -1.,  0.,  0.] },  // 5
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [ -1.,  0.,  0.] },  // 6
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [ -1.,  0.,  0.] },  // 2
-
-        // Left face
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  1.,  0.,  0.] },  // 0
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  1.,  0.,  0.] },  // 3
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  1.,  0.,  0.] },  // 7
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  1.,  0.,  0.] },  // 4
-
-        // Top face
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmin], normal: [  0.,  1.,  0.] },  // 0
-        Vertex { position: [cube.xmin, cube.ymin, cube.zmax], normal: [  0.,  1.,  0.] },  // 4
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmax], normal: [  0.,  1.,  0.] },  // 5
-        Vertex { position: [cube.xmax, cube.ymin, cube.zmin], normal: [  0.,  1.,  0.] },  // 1
-
-        // Bottom face
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmin], normal: [  0., -1.,  0.] },  // 2
-        Vertex { position: [cube.xmax, cube.ymax, cube.zmax], normal: [  0., -1.,  0.] },  // 6
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmax], normal: [  0., -1.,  0.] },  // 7
-        Vertex { position: [cube.xmin, cube.ymax, cube.zmin], normal: [  0., -1.,  0.] },  // 3
-    ];
-    let vb = glium::VertexBuffer::new(display, &cube).unwrap();
-
-    let params = glium::DrawParameters {
-        depth: glium::Depth {
-            test: glium::draw_parameters::DepthTest::IfLess,
-            write: true,
-            ..Default::default()
-        },
-        blend: glium::Blend::alpha_blending(),
-        ..Default::default()
-    };
-
-    let front_indices = IndexBuffer::new(display, PrimitiveType::TrianglesList,
-                                         &[  0,  1,  2,  0,  2,  3,  // Front
-                                             4,  5,  6,  4,  6,  7,  // Back
-                                             8,  9, 10,  8, 10, 11,  // Right
-                                            12, 13, 14, 12, 14, 15,  // Left
-                                            16, 17, 18, 16, 18, 19,  // Top
-                                            20, 21, 22, 20, 22, 23u16,  // Bottom
-                                        ]).unwrap();
-    frame.draw(&vb, &front_indices, program, uniforms, &params).unwrap();
-}
-
-*/