Cleanup, proper depth testing

This commit is contained in:
Dane Johnson 2022-04-08 16:49:31 -05:00
parent 3c7d6ad2f4
commit 299daf4e87
6 changed files with 26 additions and 55 deletions

View File

@ -1,8 +0,0 @@
#version 330
in vec3 DIFFUSE;
out vec4 color;
void main() {
color = vec4(DIFFUSE, 1.0);
}

View File

@ -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);
}

View File

@ -1,6 +1,8 @@
#version 330 #version 330
in vec3 DIFFUSE; in vec3 DIFFUSE;
in vec3 NORMAL;
out vec4 color; out vec4 color;
void main() { void main() {

View File

@ -1,13 +1,15 @@
#version 330 #version 330
in vec3 position; in vec3 position;
in vec3 color; in vec3 normal;
uniform mat4 MVP; uniform mat4 MVP;
out vec3 DIFFUSE; out vec3 DIFFUSE;
out vec3 NORMAL;
void main() { void main() {
gl_Position = MVP * vec4(position, 1.0); gl_Position = MVP * vec4(position, 1.0);
DIFFUSE = color; DIFFUSE = vec3(1.0, 0.0, 0.0);
NORMAL = normal;
} }

View File

@ -7,46 +7,27 @@ extern crate nalgebra_glm as glm;
use model::Model; use model::Model;
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3],
color: [f32; 3],
}
implement_vertex!(Vertex, position, color);
fn main() { fn main() {
use glium::{glutin, Surface}; use glium::{glutin, Surface};
let events_loop = glutin::event_loop::EventLoop::new(); let events_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new() let wb = glutin::window::WindowBuilder::new()
.with_title("Couch Game Engine"); .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 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 vertex_shader_src = include_str!("flat.vert");
let fragment_shader_src = include_str!("flat.frag"); 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 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 mut device_manager = crate::device::DeviceManager::new(); let mut device_manager = crate::device::DeviceManager::new();
let mut offset = 0.0; let mut offset = 0.0;
let mut rotate = 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| { events_loop.run(move |ev, _, control_flow| {
if device_manager.is_pressed(&device::Key::W) { if device_manager.is_pressed(&device::Key::W) {
@ -77,9 +58,8 @@ fn main() {
backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise, backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
.. Default::default() .. Default::default()
}; };
target.clear_color(0.0, 0.0, 0.0, 1.0); target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
target.draw(&vertex_buffer, &indices, &flat_program, &uniforms, &params).unwrap(); model.draw(&mut target, &program, &uniforms, &params);
model.draw(&mut target, &cube_program, &uniforms);
target.finish().unwrap(); target.finish().unwrap();

View File

@ -1,13 +1,14 @@
extern crate gltf; extern crate gltf;
use glium::{ VertexBuffer, IndexBuffer, Program }; use glium::{ VertexBuffer, IndexBuffer };
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct Vertex { struct Vertex {
position: [f32; 3], position: [f32; 3],
normal: [f32; 3],
} }
implement_vertex!(Vertex, position); implement_vertex!(Vertex, position, normal);
pub struct Model { pub struct Model {
vb: VertexBuffer<Vertex>, vb: VertexBuffer<Vertex>,
@ -21,7 +22,12 @@ impl Model {
let primitive = mesh.primitives().next().unwrap(); let primitive = mesh.primitives().next().unwrap();
let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()])); let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
let vertices: Vec<Vertex> = reader.read_positions().unwrap().map(|position| Vertex{ position }).collect(); let mut vertices: Vec<Vertex> = 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 vb = VertexBuffer::new(display, &vertices).unwrap();
let indices: Vec<u32> = reader.read_indices().unwrap().into_u32().collect(); let indices: Vec<u32> = reader.read_indices().unwrap().into_u32().collect();
@ -31,10 +37,11 @@ impl Model {
} }
pub fn draw(&self, pub fn draw(&self,
target: &mut impl glium::Surface, target: &mut impl glium::Surface,
program: &glium::Program, program: &glium::Program,
uniforms: &impl glium::uniforms::Uniforms 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()
} }
} }