Skip to content

Commit ed166d2

Browse files
committed
Book 3 introduces ray_color lights param too soon
Resolves #740
1 parent e097b1c commit ed166d2

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,9 @@
757757

758758
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
759759
...
760-
color ray_color(...)
760+
color ray_color(...) {
761761
...
762+
}
762763

763764
hittable_list cornell_box() {
764765
hittable_list objects;
@@ -799,10 +800,6 @@
799800

800801
// World
801802

802-
auto lights = make_shared<hittable_list>();
803-
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
804-
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
805-
806803
auto world = cornell_box();
807804

808805
color background(0,0,0);
@@ -1690,8 +1687,7 @@
16901687

16911688
return emitted
16921689
+ albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)
1693-
* ray_color(scattered, background, world, depth-1)
1694-
/ pdf_val;
1690+
* ray_color(scattered, background, world, depth-1) / pdf_val;
16951691
}
16961692
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16971693
[Listing [ray-color-cos-pdf]: <kbd>[main.cc]</kbd> The ray_color function, using cosine pdf]
@@ -1821,8 +1817,7 @@
18211817

18221818
return emitted
18231819
+ albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)
1824-
* ray_color(scattered, background, world, depth-1)
1825-
/ pdf_val;
1820+
* ray_color(scattered, background, world, depth-1) / pdf_val;
18261821
}
18271822
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18281823
[Listing [ray-color-hittable-pdf]: <kbd>[main.cc]</kbd> ray_color function with hittable PDF]
@@ -1872,15 +1867,8 @@
18721867
<div class='together'>
18731868
And plugging it into `ray_color()`:
18741869

1875-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1876-
color ray_color(
1877-
const ray& r,
1878-
const color& background,
1879-
const hittable& world,
1880-
shared_ptr<hittable> lights,
1881-
int depth
1882-
) {
18831870
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1871+
color ray_color(const ray& r, const color& background, const hittable& world, int depth) {
18841872
hit_record rec;
18851873

18861874
// If we've exceeded the ray bounce limit, no more light is gathered.
@@ -1912,8 +1900,7 @@
19121900

19131901
return emitted
19141902
+ albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)
1915-
* ray_color(scattered, background, world, lights, depth-1)
1916-
/ pdf_val;
1903+
* ray_color(scattered, background, world, depth-1) / pdf_val;
19171904
}
19181905
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19191906
[Listing [ray-color-mixture]: <kbd>[main.cc]</kbd> The ray_color function, using mixture PDF]
@@ -1972,7 +1959,7 @@
19721959

19731960

19741961

1975-
Cleaning Up PDF Management.
1962+
Cleaning Up PDF Management
19761963
====================================================================================================
19771964

19781965
<div class='together'>
@@ -2077,14 +2064,15 @@
20772064
<div class='together'>
20782065
And `ray_color()` changes are small:
20792066

2080-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2067+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
20812068
color ray_color(
20822069
const ray& r,
20832070
const color& background,
20842071
const hittable& world,
20852072
shared_ptr<hittable> lights,
20862073
int depth
20872074
) {
2075+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
20882076
hit_record rec;
20892077

20902078
// If we've exceeded the ray bounce limit, no more light is gathered.
@@ -2109,10 +2097,31 @@
21092097

21102098
return emitted
21112099
+ srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered)
2112-
* ray_color(scattered, background, world, lights, depth-1)
2113-
/ pdf_val;
2100+
* ray_color(scattered, background, world, lights, depth-1) / pdf_val;
21142101
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
21152102
}
2103+
2104+
...
2105+
2106+
int main() {
2107+
...
2108+
// World
2109+
2110+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2111+
auto lights = make_shared<hittable_list>();
2112+
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
2113+
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
2114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2115+
2116+
auto world = cornell_box();
2117+
2118+
color background(0,0,0);
2119+
2120+
for (int j ...) {
2121+
for (int i ...) {
2122+
...
2123+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2124+
pixel_color += ray_color(r, background, world, lights, max_depth);
21162125
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21172126
[Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scatter]
21182127
</div>
@@ -2218,8 +2227,7 @@
22182227

22192228
return emitted + srec.attenuation
22202229
* rec.mat_ptr->scattering_pdf(r, rec, scattered)
2221-
* ray_color(scattered, background, world, lights, depth-1)
2222-
/ pdf_val;
2230+
* ray_color(scattered, background, world, lights, depth-1) / pdf_val;
22232231
}
22242232
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22252233
[Listing [ray-color-implicit]: <kbd>[main.cc]</kbd>
@@ -2254,6 +2262,7 @@
22542262
box1 = make_shared<translate>(box1, vec3(265,0,295));
22552263
objects.add(box1);
22562264

2265+
22572266
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
22582267
auto glass = make_shared<dielectric>(1.5);
22592268
objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
@@ -2500,6 +2509,7 @@
25002509
auto g = pixel_color.y();
25012510
auto b = pixel_color.z();
25022511

2512+
25032513
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
25042514
// Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
25052515
if (r != r) r = 0.0;

0 commit comments

Comments
 (0)