From d467cd31914431ca8a3b472f9128ead9061e6ae8 Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Wed, 6 May 2020 00:36:35 -0700 Subject: [PATCH] Update random_scene() in books 1 and 2 - Refactored book 1 and 2 `random_scene()` functions. More named intermediate values, sync'ed with each other and with source. - Added highlight for update from random_scene() in book 1. - Added clarification about updating lambertian variables from `color` to `solid_color`. - Corrected book 2 random_scene() for-loop indices (they differed from the version in book 1). Resolves #489 --- CHANGELOG.md | 7 ++++ books/RayTracingInOneWeekend.html | 34 ++++++++++--------- books/RayTracingTheNextWeek.html | 55 +++++++++++++++++++------------ src/InOneWeekend/main.cc | 40 +++++++++++----------- src/TheNextWeek/main.cc | 47 +++++++++++++------------- 5 files changed, 102 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85058fee..b96603bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ Change Log -- Ray Tracing in One Weekend ### _In One Weekend_ - Change: The C++ `` version of `random_double()` no longer depends on `` header +- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with source. (#489) + +### _The Next Week_ +- Fix: Added clarification about updating lambertian variables from `color` to `solid_color`. +- Fix: Corrected for-loop indices (they differed from the version in book 1) in `random_scene()`. +- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with version in + _In One Weekend_ and with source. Added highlight for update from last version in book 1. (#489) ---------------------------------------------------------------------------------------------------- diff --git a/books/RayTracingInOneWeekend.html b/books/RayTracingInOneWeekend.html index 92239408..ebd77a1f 100644 --- a/books/RayTracingInOneWeekend.html +++ b/books/RayTracingInOneWeekend.html @@ -2846,41 +2846,45 @@ hittable_list random_scene() { hittable_list world; - world.add(make_shared( - point3(0,-1000,0), 1000, make_shared(color(0.5, 0.5, 0.5)))); + auto ground_material = make_shared(color(0.5, 0.5, 0.5)); + world.add(make_shared(point3(0,-1000,0), 1000, ground_material)); - int i = 1; for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { auto choose_mat = random_double(); point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double()); + if ((center - vec3(4, 0.2, 0)).length() > 0.9) { + shared_ptr sphere_material; + if (choose_mat < 0.8) { // diffuse auto albedo = color::random() * color::random(); - world.add( - make_shared(center, 0.2, make_shared(albedo))); + sphere_material = make_shared(albedo); + world.add(make_shared(center, 0.2, sphere_material)); } else if (choose_mat < 0.95) { // metal - auto albedo = color::random(.5, 1); - auto fuzz = random_double(0, .5); - world.add( - make_shared(center, 0.2, make_shared(albedo, fuzz))); + auto albedo = color::random(0.5, 1); + auto fuzz = random_double(0, 0.5); + sphere_material = make_shared(albedo, fuzz); + world.add(make_shared(center, 0.2, sphere_material)); } else { // glass - world.add(make_shared(center, 0.2, make_shared(1.5))); + sphere_material = make_shared(1.5); + world.add(make_shared(center, 0.2, sphere_material)); } } } } - world.add(make_shared(point3(0, 1, 0), 1.0, make_shared(1.5))); + auto material1 = make_shared(1.5); + world.add(make_shared(point3(0, 1, 0), 1.0, material1)); - world.add( - make_shared(point3(-4, 1, 0), 1.0, make_shared(color(.4, .2, .1)))); + auto material2 = make_shared(color(0.4, 0.2, 0.1)); + world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); - world.add( - make_shared(point3(4, 1, 0), 1.0, make_shared(color(.7, .6, .5), 0.0))); + auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); + world.add(make_shared(point3(4, 1, 0), 1.0, material3)); return world; } diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index eb6aa558..5a78b16a 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -292,40 +292,49 @@ hittable_list random_scene() { hittable_list world; - world.add(make_shared( - point3(0,-1000,0), 1000, make_shared(color(0.5, 0.5, 0.5)))); + auto ground_material = make_shared(color(0.5, 0.5, 0.5)); + world.add(make_shared(point3(0,-1000,0), 1000, ground_material)); - int i = 1; - for (int a = -10; a < 10; a++) { - for (int b = -10; b < 10; b++) { + for (int a = -11; a < 11; a++) { + for (int b = -11; b < 11; b++) { auto choose_mat = random_double(); point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double()); - if ((center - vec3(4, .2, 0)).length() > 0.9) { + + if ((center - vec3(4, 0.2, 0)).length() > 0.9) { + shared_ptr sphere_material; + if (choose_mat < 0.8) { // diffuse auto albedo = color::random() * color::random(); + sphere_material = make_shared(albedo); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + auto center2 = center + vec3(0, random_double(0,.5), 0); world.add(make_shared( - center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2, - make_shared(albedo))); + center, center2, 0.0, 1.0, 0.2, sphere_material)); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ } else if (choose_mat < 0.95) { // metal - auto albedo = color::random(.5, 1); - auto fuzz = random_double(0, .5); - world.add( - make_shared(center, 0.2, make_shared(albedo, fuzz))); + auto albedo = color::random(0.5, 1); + auto fuzz = random_double(0, 0.5); + sphere_material = make_shared(albedo, fuzz); + world.add(make_shared(center, 0.2, sphere_material)); } else { // glass - world.add(make_shared(center, 0.2, make_shared(1.5))); + sphere_material = make_shared(1.5); + world.add(make_shared(center, 0.2, sphere_material)); } } } } - world.add(make_shared(point3(0, 1, 0), 1.0, make_shared(1.5))); - world.add(make_shared( - point3(-4, 1, 0), 1.0, make_shared(color(0.4, 0.2, 0.1)))); - world.add(make_shared( - point3(4, 1, 0), 1.0, make_shared(color(0.7, 0.6, 0.5), 0.0))); + auto material1 = make_shared(1.5); + world.add(make_shared(point3(0, 1, 0), 1.0, material1)); + + auto material2 = make_shared(color(0.4, 0.2, 0.1)); + world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); + + auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); + world.add(make_shared(point3(4, 1, 0), 1.0, material3)); return world; } @@ -988,7 +997,7 @@
-Where you used to have +Where you used to have code like this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ ...make_shared(color(0.5, 0.5, 0.5)) @@ -1001,6 +1010,8 @@ ...make_shared(make_shared(0.5, 0.5, 0.5)) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Listing [lam-textured]: [main.cc] Lambertian material with texture] + +Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
@@ -1041,7 +1052,8 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), make_shared(0.9, 0.9, 0.9) + make_shared(0.2, 0.3, 0.1), + make_shared(0.9, 0.9, 0.9) ); world.add(make_shared(point3(0,-1000,0), 1000, make_shared(checker))); @@ -1065,7 +1077,8 @@ hittable_list objects; auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), make_shared(0.9, 0.9, 0.9) + make_shared(0.2, 0.3, 0.1), + make_shared(0.9, 0.9, 0.9) ); objects.add(make_shared(point3(0,-10, 0), 10, make_shared(checker))); diff --git a/src/InOneWeekend/main.cc b/src/InOneWeekend/main.cc index 86f271b1..eee2bda0 100644 --- a/src/InOneWeekend/main.cc +++ b/src/InOneWeekend/main.cc @@ -44,47 +44,45 @@ color ray_color(const ray& r, const hittable& world, int depth) { hittable_list random_scene() { hittable_list world; - world.add( - make_shared( - point3(0,-1000,0), 1000, make_shared(color(0.5, 0.5, 0.5))) - ); + auto ground_material = make_shared(color(0.5, 0.5, 0.5)); + world.add(make_shared(point3(0,-1000,0), 1000, ground_material)); - int i = 1; for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { auto choose_mat = random_double(); point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double()); + if ((center - vec3(4, 0.2, 0)).length() > 0.9) { + shared_ptr sphere_material; + if (choose_mat < 0.8) { // diffuse auto albedo = color::random() * color::random(); - world.add( - make_shared(center, 0.2, make_shared(albedo))); + sphere_material = make_shared(albedo); + world.add(make_shared(center, 0.2, sphere_material)); } else if (choose_mat < 0.95) { // metal - auto albedo = color::random(.5, 1); - auto fuzz = random_double(0, .5); - world.add( - make_shared(center, 0.2, make_shared(albedo, fuzz))); + auto albedo = color::random(0.5, 1); + auto fuzz = random_double(0, 0.5); + sphere_material = make_shared(albedo, fuzz); + world.add(make_shared(center, 0.2, sphere_material)); } else { // glass - world.add(make_shared(center, 0.2, make_shared(1.5))); + sphere_material = make_shared(1.5); + world.add(make_shared(center, 0.2, sphere_material)); } } } } - world.add( - make_shared( - point3(0, 1, 0), 1.0, make_shared(1.5))); + auto material1 = make_shared(1.5); + world.add(make_shared(point3(0, 1, 0), 1.0, material1)); - world.add( - make_shared( - point3(-4, 1, 0), 1.0, make_shared(color(0.4, 0.2, 0.1)))); + auto material2 = make_shared(color(0.4, 0.2, 0.1)); + world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); - world.add( - make_shared( - point3(4, 1, 0), 1.0, make_shared(color(0.7, 0.6, 0.5), 0.0))); + auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); + world.add(make_shared(point3(4, 1, 0), 1.0, material3)); return world; } diff --git a/src/TheNextWeek/main.cc b/src/TheNextWeek/main.cc index f371c572..90d35163 100644 --- a/src/TheNextWeek/main.cc +++ b/src/TheNextWeek/main.cc @@ -57,46 +57,44 @@ hittable_list random_scene() { world.add(make_shared(point3(0,-1000,0), 1000, make_shared(checker))); - for (int a = -10; a < 10; a++) { - for (int b = -10; b < 10; b++) { + for (int a = -11; a < 11; a++) { + for (int b = -11; b < 11; b++) { auto choose_mat = random_double(); point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double()); - if ((center - vec3(4, .2, 0)).length() > 0.9) { + + if ((center - vec3(4, 0.2, 0)).length() > 0.9) { + shared_ptr sphere_material; + if (choose_mat < 0.8) { // diffuse auto albedo = color::random() * color::random(); + sphere_material = make_shared(make_shared(albedo)); + auto center2 = center + vec3(0, random_double(0,.5), 0); world.add(make_shared( - center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2, - make_shared(make_shared(albedo)) - )); + center, center2, 0.0, 1.0, 0.2, sphere_material)); } else if (choose_mat < 0.95) { // metal - auto albedo = color::random(.5, 1); - auto fuzz = random_double(0, .5); - world.add( - make_shared(center, 0.2, make_shared(albedo, fuzz))); + auto albedo = color::random(0.5, 1); + auto fuzz = random_double(0, 0.5); + sphere_material = make_shared(albedo, fuzz); + world.add(make_shared(center, 0.2, sphere_material)); } else { // glass - world.add(make_shared(center, 0.2, make_shared(1.5))); + sphere_material = make_shared(1.5); + world.add(make_shared(center, 0.2, sphere_material)); } } } } - world.add(make_shared(point3(0,1,0), 1.0, make_shared(1.5))); + auto material1 = make_shared(1.5); + world.add(make_shared(point3(0, 1, 0), 1.0, material1)); - world.add( - make_shared( - point3(-4,1,0), 1.0, - make_shared(make_shared(0.4, 0.2, 0.1)) - ) - ); + auto material2 = make_shared(make_shared(color(0.4, 0.2, 0.1))); + world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); - world.add( - make_shared( - point3(4,1,0), 1.0, make_shared(color(0.7, 0.6, 0.5), 0.0) - ) - ); + auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); + world.add(make_shared(point3(4, 1, 0), 1.0, material3)); return hittable_list(make_shared(world, 0.0, 1.0)); } @@ -106,7 +104,8 @@ hittable_list two_spheres() { hittable_list objects; auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), make_shared(0.9, 0.9, 0.9) + make_shared(0.2, 0.3, 0.1), + make_shared(0.9, 0.9, 0.9) ); objects.add(make_shared(point3(0,-10, 0), 10, make_shared(checker)));