Skip to content

Commit 0b4004e

Browse files
authored
Merge pull request #757 from RayTracing/book3-fixes
Book3 fixes
2 parents 6eda475 + f898540 commit 0b4004e

File tree

4 files changed

+83
-80
lines changed

4 files changed

+83
-80
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Change Log -- Ray Tracing in One Weekend
1212
### The Next Week
1313

1414
### The Rest of Your Life
15+
- Fix: Synchronize book and source for `cornell_box()` function.
16+
- Fix: Introduction of light code was introduced out of sequence (#738, #740)
1517

1618

1719
----------------------------------------------------------------------------------------------------

books/RayTracingTheRestOfYourLife.html

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -757,35 +757,36 @@
757757

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

763764
hittable_list cornell_box() {
764-
hittable_list world;
765+
hittable_list objects;
765766

766767
auto red = make_shared<lambertian>(color(.65, .05, .05));
767768
auto white = make_shared<lambertian>(color(.73, .73, .73));
768769
auto green = make_shared<lambertian>(color(.12, .45, .15));
769770
auto light = make_shared<diffuse_light>(color(15, 15, 15));
770771

771-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
772-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
773-
world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
774-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
775-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
776-
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
772+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
773+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
774+
objects.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
775+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
776+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
777+
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
777778

778779
shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white);
779780
box1 = make_shared<rotate_y>(box1, 15);
780781
box1 = make_shared<translate>(box1, vec3(265,0,295));
781-
world.add(box1);
782+
objects.add(box1);
782783

783784
shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white);
784785
box2 = make_shared<rotate_y>(box2, -18);
785786
box2 = make_shared<translate>(box2, vec3(130,0,65));
786-
world.add(box2);
787+
objects.add(box2);
787788

788-
return world;
789+
return objects;
789790
}
790791

791792
int main() {
@@ -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);
@@ -1524,21 +1521,21 @@
15241521

15251522
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15261523
hittable_list cornell_box() {
1527-
hittable_list world;
1524+
hittable_list objects;
15281525

15291526
auto red = make_shared<lambertian>(color(.65, .05, .05));
15301527
auto white = make_shared<lambertian>(color(.73, .73, .73));
15311528
auto green = make_shared<lambertian>(color(.12, .45, .15));
15321529
auto light = make_shared<diffuse_light>(color(15, 15, 15));
15331530

1534-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
1535-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
1531+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
1532+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
15361533
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1537-
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
1534+
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
15381535
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1539-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
1540-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
1541-
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
1536+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
1537+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
1538+
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
15421539

15431540
...
15441541
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -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,32 @@
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 = image_height-1; j >= 0; --j) {
2121+
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
2122+
for (int i = 0; i < image_width; ++i) {
2123+
...
2124+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2125+
pixel_color += ray_color(r, background, world, lights, max_depth);
21162126
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21172127
[Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scatter]
21182128
</div>
@@ -2218,32 +2228,31 @@
22182228

22192229
return emitted + srec.attenuation
22202230
* rec.mat_ptr->scattering_pdf(r, rec, scattered)
2221-
* ray_color(scattered, background, world, lights, depth-1)
2222-
/ pdf_val;
2231+
* ray_color(scattered, background, world, lights, depth-1) / pdf_val;
22232232
}
22242233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22252234
[Listing [ray-color-implicit]: <kbd>[main.cc]</kbd>
22262235
Ray color function with implicitly-sampled rays]
22272236
</div>
22282237

22292238
<div class='together'>
2230-
We also need to change the block to metal.
2239+
We also need to change the block to metal. We'll also swap out the short block for a glass sphere.
22312240

22322241
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2233-
hittable_list cornell_box(camera& cam, double aspect) {
2234-
hittable_list world;
2242+
hittable_list cornell_box() {
2243+
hittable_list objects;
22352244

22362245
auto red = make_shared<lambertian>(color(.65, .05, .05));
22372246
auto white = make_shared<lambertian>(color(.73, .73, .73));
22382247
auto green = make_shared<lambertian>(color(.12, .45, .15));
22392248
auto light = make_shared<diffuse_light>(color(15, 15, 15));
22402249

2241-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
2242-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
2243-
world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
2244-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
2245-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
2246-
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
2250+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
2251+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
2252+
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
2253+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
2254+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
2255+
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
22472256

22482257

22492258
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2252,25 +2261,15 @@
22522261
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
22532262
box1 = make_shared<rotate_y>(box1, 15);
22542263
box1 = make_shared<translate>(box1, vec3(265,0,295));
2255-
world.add(box1);
2256-
2257-
shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white);
2258-
box2 = make_shared<rotate_y>(box2, -18);
2259-
box2 = make_shared<translate>(box2, vec3(130,0,65);
2260-
world.add(box2);
2264+
objects.add(box1);
22612265

2262-
point3 lookfrom(278, 278, -800);
2263-
point3 lookat(278, 278, 0);
2264-
vec3 vup(0, 1, 0);
2265-
auto dist_to_focus = 10.0;
2266-
auto aperture = 0.0;
2267-
auto vfov = 40.0;
2268-
auto time0 = 0.0;
2269-
auto time1 = 1.0;
22702266

2271-
cam = camera(lookfrom, lookat, vup, vfov, aspect, aperture, dist_to_focus, time0, time1);
2267+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2268+
auto glass = make_shared<dielectric>(1.5);
2269+
objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
2270+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
22722271

2273-
return world;
2272+
return objects;
22742273
}
22752274
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22762275
[Listing [scene-cornell-al]: <kbd>[main.cc]</kbd> Cornell box scene with aluminum material]
@@ -2511,6 +2510,7 @@
25112510
auto g = pixel_color.y();
25122511
auto b = pixel_color.z();
25132512

2513+
25142514
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
25152515
// Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
25162516
if (r != r) r = 0.0;

src/TheNextWeek/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ hittable_list cornell_box() {
150150
auto red = make_shared<lambertian>(color(.65, .05, .05));
151151
auto white = make_shared<lambertian>(color(.73, .73, .73));
152152
auto green = make_shared<lambertian>(color(.12, .45, .15));
153-
auto light = make_shared<diffuse_light>(color(15,15,15));
153+
auto light = make_shared<diffuse_light>(color(15, 15, 15));
154154

155155
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
156156
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));

src/TheRestOfYourLife/main.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,30 @@ color ray_color(
6363

6464

6565
hittable_list cornell_box() {
66-
hittable_list world;
66+
hittable_list objects;
6767

6868
auto red = make_shared<lambertian>(color(.65, .05, .05));
6969
auto white = make_shared<lambertian>(color(.73, .73, .73));
7070
auto green = make_shared<lambertian>(color(.12, .45, .15));
7171
auto light = make_shared<diffuse_light>(color(15, 15, 15));
7272

73-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
74-
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
75-
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
76-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
77-
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
78-
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
73+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
74+
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
75+
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
76+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
77+
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
78+
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
7979

80-
shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white);
80+
shared_ptr<material> aluminum = make_shared<metal>(color(0.8, 0.85, 0.88), 0.0);
81+
shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), aluminum);
8182
box1 = make_shared<rotate_y>(box1, 15);
8283
box1 = make_shared<translate>(box1, vec3(265,0,295));
83-
world.add(box1);
84+
objects.add(box1);
8485

8586
auto glass = make_shared<dielectric>(1.5);
86-
world.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
87+
objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
8788

88-
return world;
89+
return objects;
8990
}
9091

9192

@@ -120,7 +121,7 @@ int main() {
120121
auto time1 = 1.0;
121122

122123
camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, time0, time1);
123-
124+
124125
// Render
125126

126127
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";

0 commit comments

Comments
 (0)