Fix the value range of the floating point depth buffer.

Now I can make the near plane tiny without causing any z-fighting.
This commit is contained in:
IntegratedQuantum 2023-08-28 21:17:11 +02:00
parent 4327a50c5e
commit c38b987a01
3 changed files with 10 additions and 7 deletions

View File

@ -714,6 +714,8 @@ pub fn main() !void {
c.glCullFace(c.GL_BACK); c.glCullFace(c.GL_BACK);
c.glEnable(c.GL_BLEND); 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); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
Window.GLFWCallbacks.framebufferSize(undefined, Window.width, Window.height); Window.GLFWCallbacks.framebufferSize(undefined, Window.width, Window.height);
var lastTime = std.time.nanoTimestamp(); var lastTime = std.time.nanoTimestamp();

View File

@ -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. /// 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 maximumMeshTime = 12;
const zNear = 0.1; // TODO: Handle closer surfaces in a special function. const zNear = 1e-10;
var fogShader: graphics.Shader = undefined; var fogShader: graphics.Shader = undefined;
var fogUniforms: struct { var fogUniforms: struct {
@ -131,6 +131,7 @@ pub fn renderWorld(world: *World, ambientLight: Vec3f, skyColor: Vec3f, playerPo
_ = world; _ = world;
worldFrameBuffer.bind(); worldFrameBuffer.bind();
gpu_performance_measuring.startQuery(.clear); gpu_performance_measuring.startQuery(.clear);
c.glClearDepth(0.0);
worldFrameBuffer.clear(Vec4f{skyColor[0], skyColor[1], skyColor[2], 1}); worldFrameBuffer.clear(Vec4f{skyColor[0], skyColor[1], skyColor[2], 1});
gpu_performance_measuring.stopQuery(); gpu_performance_measuring.stopQuery();
game.camera.updateViewMatrix(); 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.glUniform1f(chunk.meshing.transparentUniforms.@"waterFog.density", waterFog.density);
c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_SRC1_COLOR); 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); c.glDepthMask(c.GL_FALSE);
{ {
var i: usize = meshes.len; 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.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); c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
gpu_performance_measuring.stopQuery(); gpu_performance_measuring.stopQuery();
// NormalChunkMesh.bindTransparentShader(ambientLight, directionalLight.getDirection(), time); // NormalChunkMesh.bindTransparentShader(ambientLight, directionalLight.getDirection(), time);

View File

@ -178,7 +178,7 @@ pub const Mat4f = struct {
Vec4f{1/tanX, 0, 0, 0}, Vec4f{1/tanX, 0, 0, 0},
Vec4f{0, 1/tanY, 0, 0}, Vec4f{0, 1/tanY, 0, 0},
Vec4f{0, 0, 0, -1}, Vec4f{0, 0, 0, -1},
Vec4f{0, 0, -near, 0}, Vec4f{0, 0, near, 0},
} }
}; };
} }