diff --git a/rtiww/src/main.cpp b/rtiww/src/main.cpp index 8d92978..0af61bf 100644 --- a/rtiww/src/main.cpp +++ b/rtiww/src/main.cpp @@ -15,7 +15,7 @@ color ray_color(const ray &r, const hittable &world, int depth) { return color(0, 0, 0); if (world.hit(r, 0.001, infinity, rec)) { - point3 target = rec.p + rec.normal + random_in_unit_sphere(); + point3 target = rec.p + random_in_hemisphere(rec.normal); return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth - 1); } @@ -58,7 +58,7 @@ int main() { pixel_color += ray_color(r, world, max_depth); } - p.set_pixel(j,i,pixel_color); + p.set_pixel(j, i, pixel_color); } } diff --git a/rtiww/src/vec3.h b/rtiww/src/vec3.h index e630b6d..b3028b3 100644 --- a/rtiww/src/vec3.h +++ b/rtiww/src/vec3.h @@ -104,4 +104,15 @@ vec3 random_in_unit_sphere() { } } +vec3 random_unit_vector() { return unit_vector(random_in_unit_sphere()); } + +vec3 random_in_hemisphere(const vec3 &normal) { + vec3 in_unit_sphere = random_in_unit_sphere(); + + if (dot(in_unit_sphere, normal) > 0.0) // In the same hemisphere as the normal + return in_unit_sphere; + else + return -in_unit_sphere; +} + #endif // RTIWW_VEC3_H