Aspect ratio for camera

This commit is contained in:
duck
2025-05-28 22:44:08 +05:00
parent 3ef02a97e5
commit 446a3b3d26
2 changed files with 9 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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;