Hemispherical scattering

This commit is contained in:
Armin Friedl 2022-07-30 16:31:00 +02:00
parent 77e0bf6b4b
commit c551944e9d
2 changed files with 13 additions and 2 deletions

View file

@ -15,7 +15,7 @@ color ray_color(const ray &r, const hittable &world, int depth) {
return color(0, 0, 0); return color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) { 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); return 0.5 * ray_color(ray(rec.p, target - rec.p), world, depth - 1);
} }

View file

@ -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 #endif // RTIWW_VEC3_H