Use reversed infinite projection for camera

This commit is contained in:
duck
2025-09-20 19:03:36 +05:00
parent c59d833542
commit fc6ebb73b8
5 changed files with 12 additions and 17 deletions

View File

@@ -13,6 +13,6 @@ layout(set = 1, binding = 1) uniform Object{
void main() { void main() {
gl_Position = vec4(inCoord, 1.0) * object.transform * camera.transform; 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; outUV = inUV;
} }

View File

@@ -142,7 +142,6 @@ pub fn create() void {
Graphics.camera = Camera{ Graphics.camera = Camera{
.transform = .{}, .transform = .{},
.near = 1.0 / 16.0, .near = 1.0 / 16.0,
.far = 128.0,
.lens = 1.5, .lens = 1.5,
.aspect = 16.0 / 9.0, .aspect = 16.0 / 9.0,
.matrix = undefined, .matrix = undefined,
@@ -193,7 +192,7 @@ pub fn beginDraw() bool {
.mip_level = 0, .mip_level = 0,
.texture = Graphics.fsaa_target, .texture = Graphics.fsaa_target,
}, 1, &.{ }, 1, &.{
.clear_depth = 1.0, .clear_depth = 0.0,
.load_op = sdl.GPU_LOADOP_CLEAR, .load_op = sdl.GPU_LOADOP_CLEAR,
.store_op = sdl.GPU_STOREOP_DONT_CARE, .store_op = sdl.GPU_STOREOP_DONT_CARE,
.stencil_load_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, .mip_level = 0,
.texture = Graphics.fsaa_target, .texture = Graphics.fsaa_target,
}, 1, &.{ }, 1, &.{
.clear_depth = 1.0, .clear_depth = 0.0,
.load_op = sdl.GPU_LOADOP_CLEAR, .load_op = sdl.GPU_LOADOP_CLEAR,
.store_op = sdl.GPU_STOREOP_DONT_CARE, .store_op = sdl.GPU_STOREOP_DONT_CARE,
.stencil_load_op = sdl.GPU_STOREOP_DONT_CARE, .stencil_load_op = sdl.GPU_STOREOP_DONT_CARE,

View File

@@ -8,7 +8,6 @@ transform: Transform,
/// tangent of the half of the view angle (90 degress = 1 "lens") /// tangent of the half of the view angle (90 degress = 1 "lens")
lens: f32, lens: f32,
near: f32, near: f32,
far: f32,
/// width = height * aspect /// width = height * aspect
aspect: f32, aspect: f32,
@@ -19,13 +18,10 @@ pub fn computeMatrix(camera: *Camera) void {
const xx = 1.0 / (camera.lens * camera.aspect); const xx = 1.0 / (camera.lens * camera.aspect);
const yy = 1.0 / camera.lens; 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){ const projection = @Vector(16, f32){
xx, 0, 0, 0, xx, 0, 0, 0,
0, yy, 0, 0, 0, yy, 0, 0,
0, 0, -zz, wz, 0, 0, 0, camera.near,
0, 0, -1, 0, 0, 0, -1, 0,
}; };
camera.matrix = Transform.multiplyMatrix(projection, camera.transform.inverseMatrix()); camera.matrix = Transform.multiplyMatrix(projection, camera.transform.inverseMatrix());

View File

@@ -12,7 +12,7 @@ pub const BLEND_NORMAL = sdl.GPUColorTargetBlendState{
}; };
pub const DEPTH_ENABLED = sdl.GPUDepthStencilState{ pub const DEPTH_ENABLED = sdl.GPUDepthStencilState{
.compare_op = sdl.GPU_COMPAREOP_LESS, .compare_op = sdl.GPU_COMPAREOP_GREATER,
.enable_depth_test = true, .enable_depth_test = true,
.enable_depth_write = true, .enable_depth_write = true,
}; };

View File

@@ -459,7 +459,7 @@ pub fn draw() void {
); );
Graphics.drawObject(&World.cubemap, .{ Graphics.drawObject(&World.cubemap, .{
.scale = Graphics.camera.far, .scale = 1e20,
.position = Graphics.camera.transform.position, .position = Graphics.camera.transform.position,
}); });
Graphics.clearDepth(); Graphics.clearDepth();