From 661a8655088fc1f4177ac299e34c7fb7a148d2f9 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 15 Feb 2019 19:07:42 -0800 Subject: [PATCH] Fill depth when drawing the ground plane --- demo/common/src/lib.rs | 61 +++++++++++++++++++++++++++++------------- gl/src/renderer.rs | 14 ++++++++++ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/demo/common/src/lib.rs b/demo/common/src/lib.rs index 8abb433b..e201bd29 100644 --- a/demo/common/src/lib.rs +++ b/demo/common/src/lib.rs @@ -108,6 +108,8 @@ impl DemoApp { let gl_attributes = sdl_video.gl_attr(); gl_attributes.set_context_profile(GLProfile::Core); gl_attributes.set_context_version(3, 3); + gl_attributes.set_depth_size(24); + gl_attributes.set_stencil_size(8); let window = sdl_video.window("Pathfinder Demo", MAIN_FRAMEBUFFER_WIDTH, MAIN_FRAMEBUFFER_HEIGHT) @@ -370,24 +372,7 @@ impl DemoApp { let perspective = transform.to_perspective(drawable_size, false); unsafe { - let mut transform = perspective.transform; - transform = - transform.post_mul(&Transform3DF32::from_scale(GROUND_SCALE, 1.0, GROUND_SCALE)); - gl::BindVertexArray(self.ground_solid_vertex_array.vertex_array.gl_vertex_array); - gl::UseProgram(self.ground_program.program.gl_program); - gl::UniformMatrix4fv(self.ground_program.transform_uniform.location, - 1, - gl::FALSE, - transform.as_ptr()); - let color = GROUND_SOLID_COLOR.to_f32(); - gl::Uniform4f(self.ground_program.color_uniform.location, - color.r(), - color.g(), - color.b(), - color.a()); - gl::Disable(gl::BLEND); - gl::DrawArrays(gl::TRIANGLE_FAN, 0, 4); - + // Use the stencil buffer to avoid Z-fighting with the gridlines. let mut transform = perspective.transform; let gridline_scale = GROUND_SCALE / GRIDLINE_COUNT as f32; transform = transform.post_mul(&Transform3DF32::from_scale(gridline_scale, @@ -405,8 +390,42 @@ impl DemoApp { color.g(), color.b(), color.a()); + gl::DepthFunc(gl::LESS); + gl::DepthMask(gl::FALSE); + gl::Enable(gl::DEPTH_TEST); + gl::StencilFunc(gl::ALWAYS, 1, !0); + gl::StencilOp(gl::KEEP, gl::KEEP, gl::REPLACE); + gl::Enable(gl::STENCIL_TEST); gl::Disable(gl::BLEND); gl::DrawArrays(gl::LINES, 0, (GRIDLINE_COUNT as GLsizei + 1) * 4); + gl::Disable(gl::DEPTH_TEST); + gl::Disable(gl::STENCIL_TEST); + + let mut transform = perspective.transform; + transform = + transform.post_mul(&Transform3DF32::from_scale(GROUND_SCALE, 1.0, GROUND_SCALE)); + gl::BindVertexArray(self.ground_solid_vertex_array.vertex_array.gl_vertex_array); + gl::UseProgram(self.ground_program.program.gl_program); + gl::UniformMatrix4fv(self.ground_program.transform_uniform.location, + 1, + gl::FALSE, + transform.as_ptr()); + let color = GROUND_SOLID_COLOR.to_f32(); + gl::Uniform4f(self.ground_program.color_uniform.location, + color.r(), + color.g(), + color.b(), + color.a()); + gl::DepthFunc(gl::LESS); + gl::DepthMask(gl::TRUE); + gl::Enable(gl::DEPTH_TEST); + gl::StencilFunc(gl::NOTEQUAL, 1, !0); + gl::StencilOp(gl::KEEP, gl::KEEP, gl::KEEP); + gl::Enable(gl::STENCIL_TEST); + gl::Disable(gl::BLEND); + gl::DrawArrays(gl::TRIANGLE_FAN, 0, 4); + gl::Disable(gl::DEPTH_TEST); + gl::Disable(gl::STENCIL_TEST); } } @@ -732,7 +751,11 @@ impl DemoDevice { unsafe { gl::BindFramebuffer(gl::FRAMEBUFFER, 0); gl::ClearColor(color.r(), color.g(), color.b(), color.a()); - gl::Clear(gl::COLOR_BUFFER_BIT); + gl::ClearDepth(1.0); + gl::ClearStencil(0); + gl::DepthMask(gl::TRUE); + gl::StencilMask(!0); + gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT); } } } diff --git a/gl/src/renderer.rs b/gl/src/renderer.rs index ad68150a..0b5c7d41 100644 --- a/gl/src/renderer.rs +++ b/gl/src/renderer.rs @@ -235,6 +235,8 @@ impl Renderer { TILE_HEIGHT as GLfloat); self.area_lut_texture.bind(0); gl::Uniform1i(self.fill_program.area_lut_uniform.location, 0); + gl::Disable(gl::DEPTH_TEST); + gl::Disable(gl::STENCIL_TEST); gl::BlendEquation(gl::FUNC_ADD); gl::BlendFunc(gl::ONE, gl::ONE); gl::Enable(gl::BLEND); @@ -269,6 +271,7 @@ impl Renderer { // FIXME(pcwalton): Fill this in properly! gl::Uniform2f(self.mask_tile_program.view_box_origin_uniform.location, 0.0, 0.0); self.enable_blending(); + self.enable_depth_test(); gl::DrawArraysInstanced(gl::TRIANGLE_FAN, 0, 4, batch.mask_tiles.len() as GLint); gl::Disable(gl::BLEND); } @@ -295,6 +298,9 @@ impl Renderer { // FIXME(pcwalton): Fill this in properly! gl::Uniform2f(self.solid_tile_program.view_box_origin_uniform.location, 0.0, 0.0); gl::Disable(gl::BLEND); + gl::DepthMask(gl::FALSE); + gl::Enable(gl::DEPTH_TEST); + gl::Disable(gl::STENCIL_TEST); gl::DrawArraysInstanced(gl::TRIANGLE_FAN, 0, 4, solid_tiles.len() as GLint); } } @@ -401,6 +407,14 @@ impl Renderer { gl::Enable(gl::BLEND); } } + + fn enable_depth_test(&self) { + unsafe { + gl::DepthMask(gl::FALSE); + gl::Enable(gl::DEPTH_TEST); + gl::Disable(gl::STENCIL_TEST); + } + } } #[derive(Clone, Copy, Default)]