improve diffuse approximation

This commit is contained in:
K Tyl 2020-06-06 23:25:36 +01:00
parent 92329f633d
commit b827c777e0
2 changed files with 23 additions and 2 deletions

View File

@ -21,9 +21,9 @@ colour ray_colour(const ray& r, const hittable& world, int depth)
return colour(0,0,0); 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); return 0.5 * ray_colour(ray(rec.p, target - rec.p), world, depth-1);
} }

View File

@ -138,3 +138,24 @@ vec3 random_in_unit_sphere()
return p; 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;
}
}