From fc6ebb73b8556b9b5456b18044ed0e3141775d48 Mon Sep 17 00:00:00 2001 From: duck Date: Sat, 20 Sep 2025 19:03:36 +0500 Subject: [PATCH] Use reversed infinite projection for camera --- data/shaders/basic.vert | 2 +- src/graphics.zig | 5 ++--- src/graphics/camera.zig | 18 +++++++----------- src/graphics/presets.zig | 2 +- src/world.zig | 2 +- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/data/shaders/basic.vert b/data/shaders/basic.vert index 3797185..f5e3dec 100644 --- a/data/shaders/basic.vert +++ b/data/shaders/basic.vert @@ -13,6 +13,6 @@ layout(set = 1, binding = 1) uniform Object{ void main() { gl_Position = vec4(inCoord, 1.0) * object.transform * camera.transform; - gl_ClipDistance[0] = gl_Position.z; + gl_ClipDistance[0] = gl_Position.w - gl_Position.z; outUV = inUV; } diff --git a/src/graphics.zig b/src/graphics.zig index 56b39be..a43f353 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -142,7 +142,6 @@ pub fn create() void { Graphics.camera = Camera{ .transform = .{}, .near = 1.0 / 16.0, - .far = 128.0, .lens = 1.5, .aspect = 16.0 / 9.0, .matrix = undefined, @@ -193,7 +192,7 @@ pub fn beginDraw() bool { .mip_level = 0, .texture = Graphics.fsaa_target, }, 1, &.{ - .clear_depth = 1.0, + .clear_depth = 0.0, .load_op = sdl.GPU_LOADOP_CLEAR, .store_op = sdl.GPU_STOREOP_DONT_CARE, .stencil_load_op = sdl.GPU_STOREOP_DONT_CARE, @@ -219,7 +218,7 @@ pub fn clearDepth() void { .mip_level = 0, .texture = Graphics.fsaa_target, }, 1, &.{ - .clear_depth = 1.0, + .clear_depth = 0.0, .load_op = sdl.GPU_LOADOP_CLEAR, .store_op = sdl.GPU_STOREOP_DONT_CARE, .stencil_load_op = sdl.GPU_STOREOP_DONT_CARE, diff --git a/src/graphics/camera.zig b/src/graphics/camera.zig index 8790923..ebbd7e2 100644 --- a/src/graphics/camera.zig +++ b/src/graphics/camera.zig @@ -8,7 +8,6 @@ transform: Transform, /// tangent of the half of the view angle (90 degress = 1 "lens") lens: f32, near: f32, -far: f32, /// width = height * aspect aspect: f32, @@ -19,14 +18,11 @@ pub fn computeMatrix(camera: *Camera) void { const xx = 1.0 / (camera.lens * camera.aspect); const yy = 1.0 / camera.lens; - const fnmod = 1.0 / (camera.far - camera.near); - const zz = camera.far * fnmod; - const wz = -camera.near * camera.far * fnmod; const projection = @Vector(16, f32){ - xx, 0, 0, 0, - 0, yy, 0, 0, - 0, 0, -zz, wz, - 0, 0, -1, 0, + xx, 0, 0, 0, + 0, yy, 0, 0, + 0, 0, 0, camera.near, + 0, 0, -1, 0, }; camera.matrix = Transform.multiplyMatrix(projection, camera.transform.inverseMatrix()); } @@ -61,9 +57,9 @@ pub fn mouse_in_quad(camera: Camera, mouse: @Vector(2, f32), quad_transform: Tra const hh = height * 0.5; const pi: [4]@Vector(2, f32) = .{ .{ -hw, -hh }, - .{ -hw, hh }, - .{ hw, hh }, - .{ hw, -hh }, + .{ -hw, hh }, + .{ hw, hh }, + .{ hw, -hh }, }; var po: [4]@Vector(2, f32) = undefined; for (0..4) |i| { diff --git a/src/graphics/presets.zig b/src/graphics/presets.zig index d9673f0..e4e008b 100644 --- a/src/graphics/presets.zig +++ b/src/graphics/presets.zig @@ -12,7 +12,7 @@ pub const BLEND_NORMAL = sdl.GPUColorTargetBlendState{ }; pub const DEPTH_ENABLED = sdl.GPUDepthStencilState{ - .compare_op = sdl.GPU_COMPAREOP_LESS, + .compare_op = sdl.GPU_COMPAREOP_GREATER, .enable_depth_test = true, .enable_depth_write = true, }; diff --git a/src/world.zig b/src/world.zig index 950de95..670578a 100644 --- a/src/world.zig +++ b/src/world.zig @@ -459,7 +459,7 @@ pub fn draw() void { ); Graphics.drawObject(&World.cubemap, .{ - .scale = Graphics.camera.far, + .scale = 1e20, .position = Graphics.camera.transform.position, }); Graphics.clearDepth();