Skip to content

Commit 88f6dae

Browse files
authored
Merge pull request #934 from spkersten/fix-uniform-sampling
Fix uniform sampling
2 parents 627d2b0 + 33c39a6 commit 88f6dae

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,9 +1554,9 @@
15541554

15551555

15561556
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1557-
scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time());
1557+
scattered = ray(rec.p, scatter_direction, r_in.time());
15581558
alb = albedo->value(rec.u, rec.v, rec.p);
1559-
pdf = dot(rec.normal, scattered.direction()) / pi;
1559+
pdf = dot(rec.normal, unit_vector(scattered.direction())) / pi;
15601560
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15611561
return true;
15621562
}
@@ -1625,7 +1625,7 @@
16251625
reflected rays weighted by Lambertian, so $\cos(\theta_o)$, but we'll change the scattering PDF.
16261626
Instead of having our scattering PDF perfectly match the Lambertian distribution -- again --
16271627
$\cos(\theta_o)$, we'll just use a uniform pdf about the hemisphere, $1/2\pi$. This will still
1628-
converge on the correct answer, all we've done is change the PDF, but since the PDF is now less of a
1628+
converge to the correct answer, all we've done is change the PDF, but since the PDF is now less of a
16291629
perfect match for the real distribution, it will take longer to converge:
16301630

16311631
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1635,13 +1635,15 @@
16351635
bool scatter(
16361636
const ray& r_in, const hit_record& rec, color& alb, ray& scattered, double& pdf
16371637
) const override {
1638-
auto scatter_direction = rec.normal + random_unit_vector();
1638+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1639+
auto scatter_direction = random_in_hemisphere(rec.normal);
1640+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16391641

16401642
// Catch degenerate scatter direction
16411643
if (scatter_direction.near_zero())
16421644
scatter_direction = rec.normal;
16431645

1644-
scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time());
1646+
scattered = ray(rec.p, scatter_direction, r_in.time());
16451647
alb = albedo->value(rec.u, rec.v, rec.p);
16461648
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16471649
pdf = 0.5 / pi;
@@ -1650,9 +1652,8 @@
16501652
}
16511653

16521654
double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered) const {
1653-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1654-
return 0.5 / pi;
1655-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1655+
auto cosine = dot(rec.normal, unit_vector(scattered.direction()));
1656+
return cosine < 0 ? 0 : cosine/pi;
16561657
}
16571658
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16581659
[Listing [scatter-mod]: <kbd>[material.h]</kbd> Modified PDF]
@@ -1689,7 +1690,7 @@
16891690
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16901691
auto scatter_direction = random_in_hemisphere(rec.normal);
16911692
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1692-
scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time());
1693+
scattered = ray(rec.p, scatter_direction, r_in.time());
16931694
alb = albedo->value(rec.u, rec.v, rec.p);
16941695
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16951696
pdf = 0.5 / pi;

0 commit comments

Comments
 (0)