Skip to content

Commit 33c39a6

Browse files
committed
Fix uniform sampling example
1 parent 62ce2e6 commit 33c39a6

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
@@ -1553,9 +1553,9 @@
15531553

15541554

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

16301630
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -1634,13 +1634,15 @@
16341634
bool scatter(
16351635
const ray& r_in, const hit_record& rec, color& alb, ray& scattered, double& pdf
16361636
) const override {
1637-
auto scatter_direction = rec.normal + random_unit_vector();
1637+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1638+
auto scatter_direction = random_in_hemisphere(rec.normal);
1639+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
16381640

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

1643-
scattered = ray(rec.p, unit_vector(scatter_direction), r_in.time());
1645+
scattered = ray(rec.p, scatter_direction, r_in.time());
16441646
alb = albedo->value(rec.u, rec.v, rec.p);
16451647
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
16461648
pdf = 0.5 / pi;
@@ -1649,9 +1651,8 @@
16491651
}
16501652

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

0 commit comments

Comments
 (0)