From 6d7998ad9f0547e316a10a257d71ce3a8e496c99 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 5 Aug 2022 18:31:09 -0700 Subject: [PATCH] zigrtiow: fix acne --- zigrtiow/src/main.zig | 7 ++++--- zigrtiow/src/vec.zig | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/zigrtiow/src/main.zig b/zigrtiow/src/main.zig index 23a461d..3c794d4 100644 --- a/zigrtiow/src/main.zig +++ b/zigrtiow/src/main.zig @@ -15,6 +15,7 @@ const Point3 = vec.Point3; const Ray = ray.Ray; const Sphere = sphere.Sphere; const Vec3 = vec.Vec3; +const random_unit_vector = vec.random_unit_vector; const random_in_unit_sphere = vec.random_in_unit_sphere; const info = std.log.info; const write_color = color.write_color; @@ -23,10 +24,10 @@ fn ray_color(r: Ray, world: Hittable, depth: isize) Color { // If we've exceeded the ray bounce limit, no more light is gathered. if (depth <= 0) return Color.init(0, 0, 0); - var hit = world.hit(r, 0, std.math.inf(f32)); + var hit = world.hit(r, 0.0001, std.math.inf(f32)); if (hit) |rec| { - const target = rec.p.add(rec.normal.add(random_in_unit_sphere())); - return ray_color(Ray.init(rec.p, target.sub(rec.p)), world, depth - 1); + const target = rec.p.add(rec.normal.add(random_unit_vector())); + return ray_color(Ray.init(rec.p, target.sub(rec.p)), world, depth - 1).scale(0.5); } var unit_direction = r.direction().unit(); const t = 0.5 * (unit_direction.y() + 1); diff --git a/zigrtiow/src/vec.zig b/zigrtiow/src/vec.zig index 332c7f4..df190da 100644 --- a/zigrtiow/src/vec.zig +++ b/zigrtiow/src/vec.zig @@ -47,9 +47,9 @@ pub const Vec3 = struct { /// Return value in unit cube from -1, 1 pub fn random() Vec3 { return Vec3.init( - rand.float(f32) * 2 - 0.5, - rand.float(f32) * 2 - 0.5, - rand.float(f32) * 2 - 0.5, + rand.float(f32) * 2 - 1, + rand.float(f32) * 2 - 1, + rand.float(f32) * 2 - 1, ); } }; @@ -61,6 +61,9 @@ pub fn random_in_unit_sphere() Vec3 { return p; } } +pub fn random_unit_vector() Vec3 { + return random_in_unit_sphere().unit(); +} pub const Color = Vec3; pub const Point3 = Vec3;