From 6e9d12217bc4c327306275fee9665eb74dbd134d Mon Sep 17 00:00:00 2001 From: Altagos Date: Fri, 1 Mar 2024 11:20:54 +0100 Subject: [PATCH] added camera options --- src/camera.zig | 9 ++++++++- src/main.zig | 2 +- src/rayray.zig | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/camera.zig b/src/camera.zig index 60a251c..f20737f 100644 --- a/src/camera.zig +++ b/src/camera.zig @@ -8,6 +8,11 @@ const log = std.log.scoped(.camera); const Camera = @This(); +pub const Options = struct { + image_width: usize, + aspect_ratio: f32, +}; + image_height: usize, image_width: usize, aspect_ratio: f32, @@ -27,7 +32,9 @@ pixel00_loc: zm.Vec, image: zigimg.Image, -pub fn init(allocator: std.mem.Allocator, image_width: usize, aspect_ratio: f32) !Camera { +pub fn init(allocator: std.mem.Allocator, opts: Options) !Camera { + const image_width = opts.image_width; + const aspect_ratio = opts.aspect_ratio; const image_height = @as(usize, @intFromFloat(@as(f32, @floatFromInt(image_width)) / aspect_ratio)); if (image_height < 1) return error.ImageWidthLessThanOne; diff --git a/src/main.zig b/src/main.zig index 9e1630d..211397e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -33,7 +33,7 @@ pub fn main() !void { const s = spall.trace(@src(), "Raytracer", .{}); // Raytracing part - var raytracer = try rayray.Raytracer.init(allocator, world); + var raytracer = try rayray.Raytracer.init(allocator, world, .{ .aspect_ratio = 16.0 / 9.0, .image_width = 400 }); defer raytracer.deinit(); const img = try raytracer.render(); diff --git a/src/rayray.zig b/src/rayray.zig index 4c9b1d2..7c10dcb 100644 --- a/src/rayray.zig +++ b/src/rayray.zig @@ -24,10 +24,10 @@ pub const Raytracer = struct { camera: Camera, world: hittable.HittableList, - pub fn init(allocator: std.mem.Allocator, world: hittable.HittableList) !Self { + pub fn init(allocator: std.mem.Allocator, world: hittable.HittableList, camera_opts: Camera.Options) !Self { return .{ .allocator = allocator, - .camera = try Camera.init(allocator, 400, 16.0 / 9.0), + .camera = try Camera.init(allocator, camera_opts), .world = world, }; } -- 2.51.2