From c38b987a01800d49d70d425d111a9962cbefcd30 Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Mon, 28 Aug 2023 21:17:11 +0200 Subject: [PATCH] Fix the value range of the floating point depth buffer. Now I can make the near plane tiny without causing any z-fighting. --- src/main.zig | 2 ++ src/renderer.zig | 7 ++++--- src/vec.zig | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index b3bde17f..67e6ed89 100644 --- a/src/main.zig +++ b/src/main.zig @@ -714,6 +714,8 @@ pub fn main() !void { c.glCullFace(c.GL_BACK); c.glEnable(c.GL_BLEND); + c.glClipControl(c.GL_LOWER_LEFT, c.GL_ZERO_TO_ONE); + c.glDepthFunc(c.GL_GREATER); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); Window.GLFWCallbacks.framebufferSize(undefined, Window.width, Window.height); var lastTime = std.time.nanoTimestamp(); diff --git a/src/renderer.zig b/src/renderer.zig index 38eb825d..a6b2f00d 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -28,7 +28,7 @@ const Mat4f = vec.Mat4f; /// The number of milliseconds after which no more chunk meshes are created. This allows the game to run smoother on movement. const maximumMeshTime = 12; -const zNear = 0.1; // TODO: Handle closer surfaces in a special function. +const zNear = 1e-10; var fogShader: graphics.Shader = undefined; var fogUniforms: struct { @@ -131,6 +131,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo _ = world; worldFrameBuffer.bind(); gpu_performance_measuring.startQuery(.clear); + c.glClearDepth(0.0); worldFrameBuffer.clear(Vec4f{skyColor[0], skyColor[1], skyColor[2], 1}); gpu_performance_measuring.stopQuery(); game.camera.updateViewMatrix(); @@ -203,7 +204,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo c.glUniform1f(chunk.meshing.transparentUniforms.@"waterFog.density", waterFog.density); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_SRC1_COLOR); - c.glDepthFunc(c.GL_LEQUAL); + c.glDepthFunc(c.GL_GEQUAL); c.glDepthMask(c.GL_FALSE); { var i: usize = meshes.len; @@ -214,7 +215,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo } } c.glDepthMask(c.GL_TRUE); - c.glDepthFunc(c.GL_LESS); + c.glDepthFunc(c.GL_GREATER); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); gpu_performance_measuring.stopQuery(); // NormalChunkMesh.bindTransparentShader(ambientLight, directionalLight.getDirection(), time); diff --git a/src/vec.zig b/src/vec.zig index 71c90809..c6cdc717 100644 --- a/src/vec.zig +++ b/src/vec.zig @@ -175,10 +175,10 @@ pub const Mat4f = struct { const tanX = aspect*tanY; return Mat4f { // Taken from https://chaosinmotion.com/2010/09/06/goodbye-far-clipping-plane/ .columns = [4]Vec4f { // Keep in mind that this is the transpose! - Vec4f{1/tanX, 0, 0, 0}, - Vec4f{0, 1/tanY, 0, 0}, - Vec4f{0, 0, 0, -1}, - Vec4f{0, 0, -near, 0}, + Vec4f{1/tanX, 0, 0, 0}, + Vec4f{0, 1/tanY, 0, 0}, + Vec4f{0, 0, 0, -1}, + Vec4f{0, 0, near, 0}, } }; }