Skip to content

Commit

Permalink
Merge pull request #934 from spkersten/fix-uniform-sampling
Browse files Browse the repository at this point in the history
Fix uniform sampling
  • Loading branch information
trevordblack committed Dec 18, 2021
2 parents 627d2b0 + 33c39a6 commit 88f6dae
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -1554,9 +1554,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;
}
Expand Down Expand Up @@ -1625,7 +1625,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++
Expand All @@ -1635,13 +1635,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;
Expand All @@ -1650,9 +1652,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]: <kbd>[material.h]</kbd> Modified PDF]
Expand Down Expand Up @@ -1689,7 +1690,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;
Expand Down

0 comments on commit 88f6dae

Please sign in to comment.