From 33c39a6bb015c52b227ed5c3f873c031133a3f80 Mon Sep 17 00:00:00 2001 From: Sander Kersten Date: Sun, 26 Sep 2021 12:10:36 +0200 Subject: [PATCH] Fix uniform sampling example --- books/RayTracingTheRestOfYourLife.html | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index b9a83477..e7c71b95 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -1553,9 +1553,9 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time()); + scattered = ray(rec.p, scatter_direction, r_in.time()); alb = albedo->value(rec.u, rec.v, rec.p); - pdf = dot(rec.normal, scattered.direction()) / pi; + pdf = dot(rec.normal, unit_vector(scattered.direction())) / pi; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ return true; } @@ -1624,7 +1624,7 @@ reflected rays weighted by Lambertian, so $\cos(\theta_o)$, but we'll change the scattering PDF. Instead of having our scattering PDF perfectly match the Lambertian distribution -- again -- $\cos(\theta_o)$, we'll just use a uniform pdf about the hemisphere, $1/2\pi$. This will still -converge on the correct answer, all we've done is change the PDF, but since the PDF is now less of a +converge to the correct answer, all we've done is change the PDF, but since the PDF is now less of a perfect match for the real distribution, it will take longer to converge: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ @@ -1634,13 +1634,15 @@ bool scatter( const ray& r_in, const hit_record& rec, color& alb, ray& scattered, double& pdf ) const override { - auto scatter_direction = rec.normal + random_unit_vector(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + auto scatter_direction = random_in_hemisphere(rec.normal); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ // Catch degenerate scatter direction if (scatter_direction.near_zero()) scatter_direction = rec.normal; - scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time()); + scattered = ray(rec.p, scatter_direction, r_in.time()); alb = albedo->value(rec.u, rec.v, rec.p); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight pdf = 0.5 / pi; @@ -1649,9 +1651,8 @@ } double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered) const { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - return 0.5 / pi; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + auto cosine = dot(rec.normal, unit_vector(scattered.direction())); + return cosine < 0 ? 0 : cosine/pi; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [scatter-mod]: [material.h] Modified PDF] @@ -1688,7 +1689,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight auto scatter_direction = random_in_hemisphere(rec.normal); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time()); + scattered = ray(rec.p, scatter_direction, r_in.time()); alb = albedo->value(rec.u, rec.v, rec.p); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight pdf = 0.5 / pi;