diff --git a/src/cube.frag b/src/cube.frag deleted file mode 100644 index f789f10..0000000 --- a/src/cube.frag +++ /dev/null @@ -1,8 +0,0 @@ -#version 330 - -in vec3 DIFFUSE; -out vec4 color; - -void main() { - color = vec4(DIFFUSE, 1.0); -} diff --git a/src/cube.vert b/src/cube.vert deleted file mode 100644 index 44b02d1..0000000 --- a/src/cube.vert +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 - -in vec3 position; - -uniform mat4 MVP; - -out vec3 DIFFUSE; - -void main() { - gl_Position = MVP * vec4(position, 1.0); - DIFFUSE = vec3(1.0, 0.0, 0.0); -} diff --git a/src/flat.frag b/src/flat.frag index f789f10..bf77901 100644 --- a/src/flat.frag +++ b/src/flat.frag @@ -1,6 +1,8 @@ #version 330 in vec3 DIFFUSE; +in vec3 NORMAL; + out vec4 color; void main() { diff --git a/src/flat.vert b/src/flat.vert index e1e2a64..20046be 100644 --- a/src/flat.vert +++ b/src/flat.vert @@ -1,13 +1,15 @@ #version 330 in vec3 position; -in vec3 color; +in vec3 normal; uniform mat4 MVP; out vec3 DIFFUSE; +out vec3 NORMAL; void main() { gl_Position = MVP * vec4(position, 1.0); - DIFFUSE = color; + DIFFUSE = vec3(1.0, 0.0, 0.0); + NORMAL = normal; } diff --git a/src/main.rs b/src/main.rs index dc9b289..e0be298 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,46 +7,27 @@ extern crate nalgebra_glm as glm; use model::Model; -#[derive(Copy, Clone)] -struct Vertex { - position: [f32; 3], - color: [f32; 3], -} - -implement_vertex!(Vertex, position, color); - fn main() { use glium::{glutin, Surface}; let events_loop = glutin::event_loop::EventLoop::new(); let wb = glutin::window::WindowBuilder::new() .with_title("Couch Game Engine"); - let cb = glutin::ContextBuilder::new(); + let cb = glutin::ContextBuilder::new() + .with_depth_buffer(24); let display = glium::Display::new(wb, cb, &events_loop).unwrap(); - let vertex1 = Vertex { position: [-0.5, -0.5 , 0.0], color: [1.0, 0.0, 0.0] }; - let vertex2 = Vertex { position: [ 0.0, 0.5 , 0.0], color: [0.0, 1.0, 0.0] }; - let vertex3 = Vertex { position: [ 0.5, -0.25, 0.0], color: [0.0, 0.0, 1.0] }; - let shape = vec![vertex1, vertex2, vertex3]; - - let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); - let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList); - let vertex_shader_src = include_str!("flat.vert"); let fragment_shader_src = include_str!("flat.frag"); - let flat_program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); - - let vertex_shader_src = include_str!("cube.vert"); - let fragment_shader_src = include_str!("cube.frag"); - let cube_program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); + let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); let mut device_manager = crate::device::DeviceManager::new(); let mut offset = 0.0; let mut rotate = 0.0; - let model = Model::new(&display, "Lantern.glb"); + let model = Model::new(&display, "Box.glb"); events_loop.run(move |ev, _, control_flow| { if device_manager.is_pressed(&device::Key::W) { @@ -77,9 +58,8 @@ fn main() { backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, .. Default::default() }; - target.clear_color(0.0, 0.0, 0.0, 1.0); - target.draw(&vertex_buffer, &indices, &flat_program, &uniforms, ¶ms).unwrap(); - model.draw(&mut target, &cube_program, &uniforms); + target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0); + model.draw(&mut target, &program, &uniforms, ¶ms); target.finish().unwrap(); diff --git a/src/model.rs b/src/model.rs index cd903e6..40b0d00 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,13 +1,14 @@ extern crate gltf; -use glium::{ VertexBuffer, IndexBuffer, Program }; +use glium::{ VertexBuffer, IndexBuffer }; #[derive(Copy, Clone)] struct Vertex { position: [f32; 3], + normal: [f32; 3], } -implement_vertex!(Vertex, position); +implement_vertex!(Vertex, position, normal); pub struct Model { vb: VertexBuffer, @@ -21,7 +22,12 @@ impl Model { let primitive = mesh.primitives().next().unwrap(); let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()])); - let vertices: Vec = reader.read_positions().unwrap().map(|position| Vertex{ position }).collect(); + let mut vertices: Vec = Vec::new(); + let positions = reader.read_positions().unwrap(); + let normals = reader.read_normals().unwrap(); + for (position, normal) in positions.zip(normals) { + vertices.push(Vertex { position, normal }); + } let vb = VertexBuffer::new(display, &vertices).unwrap(); let indices: Vec = reader.read_indices().unwrap().into_u32().collect(); @@ -31,10 +37,11 @@ impl Model { } pub fn draw(&self, - target: &mut impl glium::Surface, - program: &glium::Program, - uniforms: &impl glium::uniforms::Uniforms + target: &mut impl glium::Surface, + program: &glium::Program, + uniforms: &impl glium::uniforms::Uniforms, + params: &glium::DrawParameters ) { - target.draw(&self.vb, &self.ib, program, uniforms, &Default::default()).unwrap() + target.draw(&self.vb, &self.ib, program, uniforms, params).unwrap() } }