diff --git a/src/main.cpp b/src/main.cpp index f07d189..c3aa001 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,9 +21,9 @@ colour ray_colour(const ray& r, const hittable& world, int depth) return colour(0,0,0); } - if (world.hit(r, 0, infinity, rec)) + if (world.hit(r, 0.001, infinity, rec)) { - point3 target = rec.p + rec.normal + random_in_unit_sphere(); + point3 target = rec.p + rec.normal + random_in_hemisphere(rec.normal); return 0.5 * ray_colour(ray(rec.p, target - rec.p), world, depth-1); } diff --git a/src/vec3.h b/src/vec3.h index f562294..b2abc05 100755 --- a/src/vec3.h +++ b/src/vec3.h @@ -138,3 +138,24 @@ vec3 random_in_unit_sphere() return p; } } + +vec3 random_unit_vector() +{ + auto a = random_double(0, 2*pi); + auto z = random_double(-1,1); + auto r = sqrt(1 - z*z); + return vec3(r*cos(a), r*sin(a), z); +} + +vec3 random_in_hemisphere(const vec3& normal) +{ + vec3 in_unit_sphere = random_in_unit_sphere(); + if (dot(in_unit_sphere, normal) > 0.0) + { + return in_unit_sphere; + } + else + { + return -in_unit_sphere; + } +}