From 70c8f16193bd0b77a59cddc76c700b9d7f43b37c Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Mon, 26 Jun 2017 01:29:02 -0400 Subject: [PATCH] Draw the cubes using flat shading and fewer vertexes. Described in detail here: http://poniesandlight.co.uk/notes/flat_shading_on_osx/ --- main.rs | 6 ++--- shaded.frag | 2 +- shaded.vert | 4 +-- shaded_cube.rs | 67 ++++++++++++++++---------------------------------- 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/main.rs b/main.rs index d4e38b7..c673e02 100644 --- a/main.rs +++ b/main.rs @@ -98,7 +98,7 @@ fn main() { zmax: 1.1, }; let mandelwow_bbox = bounding_box::BoundingBox::new(&display, &bounds, &bounding_box_program); - let shaded_cube = ShadedCube::new(&display, &Cube::default(), &shaded_program); + let shaded_cube = ShadedCube::new(&display, &shaded_program); const SEA_XSIZE: usize = 24; const SEA_ZSIZE: usize = 20; @@ -226,8 +226,8 @@ fn main() { .with_depth_buffer(24) .rebuild_glium(&display).unwrap(); } else { - //glutin::WindowBuilder::new() - // .rebuild_glium(&display).unwrap(); + glutin::WindowBuilder::new() + .rebuild_glium(&display).unwrap(); } }, ev => camera.process_input(&ev), diff --git a/shaded.frag b/shaded.frag index e48c837..492644f 100644 --- a/shaded.frag +++ b/shaded.frag @@ -1,7 +1,7 @@ #version 300 es precision lowp float; -in vec4 color; +flat in vec4 color; out vec4 color_out; void main() { diff --git a/shaded.vert b/shaded.vert index e21f5fc..c7c7fa1 100644 --- a/shaded.vert +++ b/shaded.vert @@ -3,7 +3,7 @@ precision lowp float; in vec3 position; in vec3 normal; -out vec4 color; // Shaded color +flat out vec4 color; // Shaded color uniform mat4 model; uniform mat4 perspview; @@ -12,7 +12,7 @@ uniform vec3 col; void main() { mat4 m = perspview * model; vec3 dark = col * 0.1; - vec3 u_light = vec3(-0.5, -0.7, -0.6); + vec3 u_light = vec3( 0.5, -0.7, -0.6); vec3 v_normal = transpose(inverse(mat3(model))) * normal; float distance = model[3][2]; diff --git a/shaded_cube.rs b/shaded_cube.rs index f334d2f..560256f 100644 --- a/shaded_cube.rs +++ b/shaded_cube.rs @@ -1,4 +1,3 @@ -use cube::Cube; use glium; use glium::{Display, Program, Surface}; use glium::index::{IndexBuffer, PrimitiveType}; @@ -23,59 +22,35 @@ pub struct ShadedCube<'a> { } impl<'a> ShadedCube<'a> { - pub fn new(display: &Display, c: &Cube, program: &'a Program) -> ShadedCube<'a> { + pub fn new(display: &Display, program: &'a Program) -> ShadedCube<'a> { // x---> // 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 + // 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 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 + // 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,7 +68,7 @@ impl<'a> ShadedCube<'a> { write: true, ..Default::default() }, - backface_culling: glium::draw_parameters::BackfaceCullingMode::CullCounterClockwise, + backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, ..Default::default() }; frame.draw(&self.vertexes, &self.indices, &self.program, uniforms, ¶ms).unwrap(); -- 2.25.1