From 446a3b3d26f29c3214092726c9caa7252df52a61 Mon Sep 17 00:00:00 2001 From: duck Date: Wed, 28 May 2025 22:44:08 +0500 Subject: [PATCH] Aspect ratio for camera --- src/graphics.zig | 5 +++-- src/graphics/camera.zig | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/graphics.zig b/src/graphics.zig index 0186229..8660b05 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -191,7 +191,8 @@ pub fn create() GameError!Self { .transform = .{}, .near = 1.0, .far = 1024.0, - .lens = .{ 1.5 * 16.0 / 9.0, 1.5 }, + .lens = 1.5, + .aspect = 16.0 / 9.0, }, }; } @@ -321,7 +322,7 @@ pub fn beginDraw(self: *Self) GameError!bool { self.command_buffer = sdl.AcquireGPUCommandBuffer(self.device) orelse return GameError.SdlError; if (self.to_resize) |new_size| { try self.resetTextures(new_size[0], new_size[1]); - self.camera.lens[0] = self.camera.lens[1] * @as(f32, @floatFromInt(new_size[0])) / @as(f32, @floatFromInt(new_size[1])); + self.camera.aspect = @as(f32, @floatFromInt(new_size[0])) / @as(f32, @floatFromInt(new_size[1])); self.window_size = new_size; self.to_resize = null; } diff --git a/src/graphics/camera.zig b/src/graphics/camera.zig index 3cd7fdb..20502fc 100644 --- a/src/graphics/camera.zig +++ b/src/graphics/camera.zig @@ -4,13 +4,16 @@ const Transform = @import("transform.zig"); const Camera = @This(); transform: Transform, -lens: @Vector(2, f32), +/// tangent of the half of the view angle (90 degress = 1 "lens") +lens: f32, near: f32, far: f32, +/// width = height * aspect +aspect: f32, pub fn matrix(camera: Camera) @Vector(16, f32) { - const xx = 1.0 / camera.lens[0]; - const yy = 1.0 / camera.lens[1]; + 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;