From f2ade1eee29f71195f4f921bc50bc6894e8efb57 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 13 Aug 2022 20:35:15 -0700 Subject: [PATCH] zigrtiow: partially configurable camera. --- zigrtiow/src/camera.zig | 21 ++++++++++++++++++--- zigrtiow/src/main.zig | 19 +++++++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/zigrtiow/src/camera.zig b/zigrtiow/src/camera.zig index f7d2170..fce465b 100644 --- a/zigrtiow/src/camera.zig +++ b/zigrtiow/src/camera.zig @@ -1,9 +1,19 @@ const vec = @import("./vec.zig"); +const std = @import("std"); const Ray = @import("./ray.zig").Ray; const Point3 = vec.Point3; const Vec3 = vec.Vec3; +const math = std.math; +const pi = math.pi; +const tan = math.tan; + +// Utility Functions + +fn degrees_to_radians(degrees: f32) f32 { + return degrees * pi / 180.0; +} pub const Camera = struct { origin: Point3, @@ -11,10 +21,15 @@ pub const Camera = struct { horizontal: Vec3, vertical: Vec3, - pub fn init() Camera { - const aspect_ratio = 16.0 / 9.0; - const viewport_height = 2.0; + pub fn init( + vfov: f32, // vertical field-of-view in degrees + aspect_ratio: f32, + ) Camera { + const theta = degrees_to_radians(vfov); + const h = tan(theta / 2); + const viewport_height = 2 * h; const viewport_width = aspect_ratio * viewport_height; + const focal_length = 1.0; const origin = Point3.init(0, 0, 0); const horizontal = Vec3.init(viewport_width, 0.0, 0.0); diff --git a/zigrtiow/src/main.zig b/zigrtiow/src/main.zig index d543205..1fdd1cc 100644 --- a/zigrtiow/src/main.zig +++ b/zigrtiow/src/main.zig @@ -25,6 +25,9 @@ const Vec3 = vec.Vec3; const info = std.log.info; const random_in_hemisphere = vec.random_in_hemisphere; const write_color = color.write_color; +const math = std.math; +const pi = math.pi; +const cos = math.cos; fn ray_color(r: Ray, world: Hittable, depth: isize) Color { // If we've exceeded the ray bounce limit, no more light is gathered. @@ -87,22 +90,18 @@ pub fn main() anyerror!void { const max_depth = 50; // World + const r = cos(pi / @as(f32, 4.0)); var tmp_world = HittableList.init(); - const material_ground = Material{ .labertian = Labertian{ .albedo = Color.init(0.8, 0.8, 0.0) } }; - const material_center = Material{ .labertian = Labertian{ .albedo = Color.init(0.1, 0.2, 0.5) } }; - const material_left = Material{ .dielectric = Dielectric{ .ir = 1.5 } }; - const material_right = Material{ .metal = Metal{ .albedo = Color.init(0.8, 0.6, 0.2), .fuzz = 0.0 } }; + const material_left = Material{ .labertian = Labertian{ .albedo = Color.init(0, 0, 1) } }; + const material_right = Material{ .labertian = Labertian{ .albedo = Color.init(1, 0, 0) } }; - try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, -100.5, -1), 100, material_ground) }); - try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(0, 0, -1), 0.5, material_center) }); - try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(-1, 0, -1), 0.5, material_left) }); - try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(-1, 0, -1), -0.4, material_left) }); - try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(1, 0, -1), 0.5, material_right) }); + try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(-r, 0, -1), r, material_left) }); + try tmp_world.add(Hittable{ .sphere = Sphere.init(Point3.init(r, 0, -1), r, material_right) }); const world = Hittable{ .hittable_list = tmp_world }; // Camera - const cam = Camera.init(); + const cam = Camera.init(90, aspect_ratio); // Render const Node = Queue(Task).Node;