From e097b1c13128291636dac1e310a738849c716b3f Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Sat, 10 Oct 2020 18:09:11 -0700 Subject: [PATCH 1/3] Synchronize book 3 with source In addition, in book 2 we use `objects` to build up the scene, where book 3 and source still used the old `world`. This change also synchronizes between these two versions. --- books/RayTracingTheRestOfYourLife.html | 77 +++++++++++--------------- src/TheNextWeek/main.cc | 2 +- src/TheRestOfYourLife/main.cc | 25 +++++---- 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index 12a93601..e2fb1750 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -757,35 +757,35 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ ... - color ray_color( + color ray_color(...) ... hittable_list cornell_box() { - hittable_list world; + hittable_list objects; auto red = make_shared(color(.65, .05, .05)); auto white = make_shared(color(.73, .73, .73)); auto green = make_shared(color(.12, .45, .15)); auto light = make_shared(color(15, 15, 15)); - world.add(make_shared(0, 555, 0, 555, 555, green)); - world.add(make_shared(0, 555, 0, 555, 0, red)); - world.add(make_shared(213, 343, 227, 332, 554, light)); - world.add(make_shared(0, 555, 0, 555, 555, white)); - world.add(make_shared(0, 555, 0, 555, 0, white)); - world.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 555, green)); + objects.add(make_shared(0, 555, 0, 555, 0, red)); + objects.add(make_shared(213, 343, 227, 332, 554, light)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 0, white)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), white); box1 = make_shared(box1, 15); box1 = make_shared(box1, vec3(265,0,295)); - world.add(box1); + objects.add(box1); shared_ptr box2 = make_shared(point3(0,0,0), point3(165,165,165), white); box2 = make_shared(box2, -18); box2 = make_shared(box2, vec3(130,0,65)); - world.add(box2); + objects.add(box2); - return world; + return objects; } int main() { @@ -1524,21 +1524,21 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ hittable_list cornell_box() { - hittable_list world; + hittable_list objects; auto red = make_shared(color(.65, .05, .05)); auto white = make_shared(color(.73, .73, .73)); auto green = make_shared(color(.12, .45, .15)); auto light = make_shared(color(15, 15, 15)); - world.add(make_shared(0, 555, 0, 555, 555, green)); - world.add(make_shared(0, 555, 0, 555, 0, red)); + objects.add(make_shared(0, 555, 0, 555, 555, green)); + objects.add(make_shared(0, 555, 0, 555, 0, red)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - world.add(make_shared(make_shared(213, 343, 227, 332, 554, light))); + objects.add(make_shared(make_shared(213, 343, 227, 332, 554, light))); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - world.add(make_shared(0, 555, 0, 555, 555, white)); - world.add(make_shared(0, 555, 0, 555, 0, white)); - world.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 0, white)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -2227,23 +2227,23 @@
-We also need to change the block to metal. +We also need to change the block to metal. We'll also swap out the short block for a glass sphere. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - hittable_list cornell_box(camera& cam, double aspect) { - hittable_list world; + hittable_list cornell_box() { + hittable_list objects; auto red = make_shared(color(.65, .05, .05)); auto white = make_shared(color(.73, .73, .73)); auto green = make_shared(color(.12, .45, .15)); auto light = make_shared(color(15, 15, 15)); - world.add(make_shared(0, 555, 0, 555, 555, green)); - world.add(make_shared(0, 555, 0, 555, 0, red)); - world.add(make_shared(213, 343, 227, 332, 554, light)); - world.add(make_shared(0, 555, 0, 555, 555, white)); - world.add(make_shared(0, 555, 0, 555, 0, white)); - world.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 555, green)); + objects.add(make_shared(0, 555, 0, 555, 0, red)); + objects.add(make_shared(make_shared(213, 343, 227, 332, 554, light))); + objects.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 0, white)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -2252,25 +2252,14 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ box1 = make_shared(box1, 15); box1 = make_shared(box1, vec3(265,0,295)); - world.add(box1); - - shared_ptr box2 = make_shared(point3(0,0,0), point3(165,165,165), white); - box2 = make_shared(box2, -18); - box2 = make_shared(box2, vec3(130,0,65); - world.add(box2); - - point3 lookfrom(278, 278, -800); - point3 lookat(278, 278, 0); - vec3 vup(0, 1, 0); - auto dist_to_focus = 10.0; - auto aperture = 0.0; - auto vfov = 40.0; - auto time0 = 0.0; - auto time1 = 1.0; + objects.add(box1); - cam = camera(lookfrom, lookat, vup, vfov, aspect, aperture, dist_to_focus, time0, time1); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + auto glass = make_shared(1.5); + objects.add(make_shared(point3(190,90,190), 90 , glass)); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - return world; + return objects; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [scene-cornell-al]: [main.cc] Cornell box scene with aluminum material] diff --git a/src/TheNextWeek/main.cc b/src/TheNextWeek/main.cc index 1ff0094e..c8e57bb6 100644 --- a/src/TheNextWeek/main.cc +++ b/src/TheNextWeek/main.cc @@ -150,7 +150,7 @@ hittable_list cornell_box() { auto red = make_shared(color(.65, .05, .05)); auto white = make_shared(color(.73, .73, .73)); auto green = make_shared(color(.12, .45, .15)); - auto light = make_shared(color(15,15,15)); + auto light = make_shared(color(15, 15, 15)); objects.add(make_shared(0, 555, 0, 555, 555, green)); objects.add(make_shared(0, 555, 0, 555, 0, red)); diff --git a/src/TheRestOfYourLife/main.cc b/src/TheRestOfYourLife/main.cc index 1132f3b0..6faa549a 100644 --- a/src/TheRestOfYourLife/main.cc +++ b/src/TheRestOfYourLife/main.cc @@ -63,29 +63,30 @@ color ray_color( hittable_list cornell_box() { - hittable_list world; + hittable_list objects; auto red = make_shared(color(.65, .05, .05)); auto white = make_shared(color(.73, .73, .73)); auto green = make_shared(color(.12, .45, .15)); auto light = make_shared(color(15, 15, 15)); - world.add(make_shared(0, 555, 0, 555, 555, green)); - world.add(make_shared(0, 555, 0, 555, 0, red)); - world.add(make_shared(make_shared(213, 343, 227, 332, 554, light))); - world.add(make_shared(0, 555, 0, 555, 555, white)); - world.add(make_shared(0, 555, 0, 555, 0, white)); - world.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 555, green)); + objects.add(make_shared(0, 555, 0, 555, 0, red)); + objects.add(make_shared(make_shared(213, 343, 227, 332, 554, light))); + objects.add(make_shared(0, 555, 0, 555, 555, white)); + objects.add(make_shared(0, 555, 0, 555, 0, white)); + objects.add(make_shared(0, 555, 0, 555, 555, white)); - shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), white); + shared_ptr aluminum = make_shared(color(0.8, 0.85, 0.88), 0.0); + shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), aluminum); box1 = make_shared(box1, 15); box1 = make_shared(box1, vec3(265,0,295)); - world.add(box1); + objects.add(box1); auto glass = make_shared(1.5); - world.add(make_shared(point3(190,90,190), 90 , glass)); + objects.add(make_shared(point3(190,90,190), 90 , glass)); - return world; + return objects; } @@ -120,7 +121,7 @@ int main() { auto time1 = 1.0; camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, time0, time1); - + // Render std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; From ed166d2fc2d9410b5551114b4f4ba5177fbdc0b1 Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Sat, 10 Oct 2020 18:40:53 -0700 Subject: [PATCH 2/3] Book 3 introduces ray_color lights param too soon Resolves #740 --- books/RayTracingTheRestOfYourLife.html | 60 +++++++++++++++----------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index e2fb1750..38d3aac6 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -757,8 +757,9 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ ... - color ray_color(...) + color ray_color(...) { ... + } hittable_list cornell_box() { hittable_list objects; @@ -799,10 +800,6 @@ // World - auto lights = make_shared(); - lights->add(make_shared(213, 343, 227, 332, 554, shared_ptr())); - lights->add(make_shared(point3(190, 90, 190), 90, shared_ptr())); - auto world = cornell_box(); color background(0,0,0); @@ -1690,8 +1687,7 @@ return emitted + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) - * ray_color(scattered, background, world, depth-1) - / pdf_val; + * ray_color(scattered, background, world, depth-1) / pdf_val; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [ray-color-cos-pdf]: [main.cc] The ray_color function, using cosine pdf] @@ -1821,8 +1817,7 @@ return emitted + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) - * ray_color(scattered, background, world, depth-1) - / pdf_val; + * ray_color(scattered, background, world, depth-1) / pdf_val; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [ray-color-hittable-pdf]: [main.cc] ray_color function with hittable PDF] @@ -1872,15 +1867,8 @@
And plugging it into `ray_color()`: - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight - color ray_color( - const ray& r, - const color& background, - const hittable& world, - shared_ptr lights, - int depth - ) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + color ray_color(const ray& r, const color& background, const hittable& world, int depth) { hit_record rec; // If we've exceeded the ray bounce limit, no more light is gathered. @@ -1912,8 +1900,7 @@ return emitted + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered) - * ray_color(scattered, background, world, lights, depth-1) - / pdf_val; + * ray_color(scattered, background, world, depth-1) / pdf_val; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [ray-color-mixture]: [main.cc] The ray_color function, using mixture PDF] @@ -1972,7 +1959,7 @@ -Cleaning Up PDF Management. +Cleaning Up PDF Management ====================================================================================================
@@ -2077,7 +2064,7 @@
And `ray_color()` changes are small: - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight color ray_color( const ray& r, const color& background, @@ -2085,6 +2072,7 @@ shared_ptr lights, int depth ) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ hit_record rec; // If we've exceeded the ray bounce limit, no more light is gathered. @@ -2109,10 +2097,31 @@ return emitted + srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered) - * ray_color(scattered, background, world, lights, depth-1) - / pdf_val; + * ray_color(scattered, background, world, lights, depth-1) / pdf_val; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ } + + ... + + int main() { + ... + // World + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + auto lights = make_shared(); + lights->add(make_shared(213, 343, 227, 332, 554, shared_ptr())); + lights->add(make_shared(point3(190, 90, 190), 90, shared_ptr())); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ + + auto world = cornell_box(); + + color background(0,0,0); + + for (int j ...) { + for (int i ...) { + ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + pixel_color += ray_color(r, background, world, lights, max_depth); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [ray-color-scatter]: [main.cc] Ray color with scatter]
@@ -2218,8 +2227,7 @@ return emitted + srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered) - * ray_color(scattered, background, world, lights, depth-1) - / pdf_val; + * ray_color(scattered, background, world, lights, depth-1) / pdf_val; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [ray-color-implicit]: [main.cc] @@ -2254,6 +2262,7 @@ box1 = make_shared(box1, vec3(265,0,295)); objects.add(box1); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight auto glass = make_shared(1.5); objects.add(make_shared(point3(190,90,190), 90 , glass)); @@ -2500,6 +2509,7 @@ auto g = pixel_color.y(); auto b = pixel_color.z(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight // Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life. if (r != r) r = 0.0; From f898540a6cef1642015e0f92d8735a2af0d1b080 Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Sun, 11 Oct 2020 23:04:33 -0700 Subject: [PATCH 3/3] Fix for-loop ellipses in code listings Also add changelog entry for fixed light code introduction order. --- CHANGELOG.md | 2 ++ books/RayTracingTheRestOfYourLife.html | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd07e6a..b198c657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ Change Log -- Ray Tracing in One Weekend ### The Next Week ### The Rest of Your Life + - Fix: Synchronize book and source for `cornell_box()` function. + - Fix: Introduction of light code was introduced out of sequence (#738, #740) ---------------------------------------------------------------------------------------------------- diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index 38d3aac6..e0e9de96 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -2117,8 +2117,9 @@ color background(0,0,0); - for (int j ...) { - for (int i ...) { + for (int j = image_height-1; j >= 0; --j) { + std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush; + for (int i = 0; i < image_width; ++i) { ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight pixel_color += ray_color(r, background, world, lights, max_depth);