Skip to content

Commit d467cd3

Browse files
committed
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
1 parent 2f12e6b commit d467cd3

File tree

5 files changed

+102
-81
lines changed

5 files changed

+102
-81
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ Change Log -- Ray Tracing in One Weekend
55

66
### _In One Weekend_
77
- Change: The C++ `<random>` version of `random_double()` no longer depends on `<functional>` header
8+
- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with source. (#489)
9+
10+
### _The Next Week_
11+
- Fix: Added clarification about updating lambertian variables from `color` to `solid_color`.
12+
- Fix: Corrected for-loop indices (they differed from the version in book 1) in `random_scene()`.
13+
- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with version in
14+
_In One Weekend_ and with source. Added highlight for update from last version in book 1. (#489)
815

916

1017
----------------------------------------------------------------------------------------------------

books/RayTracingInOneWeekend.html

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,41 +2846,45 @@
28462846
hittable_list random_scene() {
28472847
hittable_list world;
28482848

2849-
world.add(make_shared<sphere>(
2850-
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5))));
2849+
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
2850+
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
28512851

2852-
int i = 1;
28532852
for (int a = -11; a < 11; a++) {
28542853
for (int b = -11; b < 11; b++) {
28552854
auto choose_mat = random_double();
28562855
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
2856+
28572857
if ((center - vec3(4, 0.2, 0)).length() > 0.9) {
2858+
shared_ptr<material> sphere_material;
2859+
28582860
if (choose_mat < 0.8) {
28592861
// diffuse
28602862
auto albedo = color::random() * color::random();
2861-
world.add(
2862-
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
2863+
sphere_material = make_shared<lambertian>(albedo);
2864+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
28632865
} else if (choose_mat < 0.95) {
28642866
// metal
2865-
auto albedo = color::random(.5, 1);
2866-
auto fuzz = random_double(0, .5);
2867-
world.add(
2868-
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
2867+
auto albedo = color::random(0.5, 1);
2868+
auto fuzz = random_double(0, 0.5);
2869+
sphere_material = make_shared<metal>(albedo, fuzz);
2870+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
28692871
} else {
28702872
// glass
2871-
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
2873+
sphere_material = make_shared<dielectric>(1.5);
2874+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
28722875
}
28732876
}
28742877
}
28752878
}
28762879

2877-
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
2880+
auto material1 = make_shared<dielectric>(1.5);
2881+
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
28782882

2879-
world.add(
2880-
make_shared<sphere>(point3(-4, 1, 0), 1.0, make_shared<lambertian>(color(.4, .2, .1))));
2883+
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
2884+
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
28812885

2882-
world.add(
2883-
make_shared<sphere>(point3(4, 1, 0), 1.0, make_shared<metal>(color(.7, .6, .5), 0.0)));
2886+
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
2887+
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
28842888

28852889
return world;
28862890
}

books/RayTracingTheNextWeek.html

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,40 +292,49 @@
292292
hittable_list random_scene() {
293293
hittable_list world;
294294

295-
world.add(make_shared<sphere>(
296-
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5))));
295+
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
296+
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
297297

298-
int i = 1;
299-
for (int a = -10; a < 10; a++) {
300-
for (int b = -10; b < 10; b++) {
298+
for (int a = -11; a < 11; a++) {
299+
for (int b = -11; b < 11; b++) {
301300
auto choose_mat = random_double();
302301
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
303-
if ((center - vec3(4, .2, 0)).length() > 0.9) {
302+
303+
if ((center - vec3(4, 0.2, 0)).length() > 0.9) {
304+
shared_ptr<material> sphere_material;
305+
304306
if (choose_mat < 0.8) {
305307
// diffuse
306308
auto albedo = color::random() * color::random();
309+
sphere_material = make_shared<lambertian>(albedo);
310+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
311+
auto center2 = center + vec3(0, random_double(0,.5), 0);
307312
world.add(make_shared<moving_sphere>(
308-
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
309-
make_shared<lambertian>(albedo)));
313+
center, center2, 0.0, 1.0, 0.2, sphere_material));
314+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
310315
} else if (choose_mat < 0.95) {
311316
// metal
312-
auto albedo = color::random(.5, 1);
313-
auto fuzz = random_double(0, .5);
314-
world.add(
315-
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
317+
auto albedo = color::random(0.5, 1);
318+
auto fuzz = random_double(0, 0.5);
319+
sphere_material = make_shared<metal>(albedo, fuzz);
320+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
316321
} else {
317322
// glass
318-
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
323+
sphere_material = make_shared<dielectric>(1.5);
324+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
319325
}
320326
}
321327
}
322328
}
323329

324-
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
325-
world.add(make_shared<sphere>(
326-
point3(-4, 1, 0), 1.0, make_shared<lambertian>(color(0.4, 0.2, 0.1))));
327-
world.add(make_shared<sphere>(
328-
point3(4, 1, 0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)));
330+
auto material1 = make_shared<dielectric>(1.5);
331+
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
332+
333+
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
334+
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
335+
336+
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
337+
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
329338

330339
return world;
331340
}
@@ -988,7 +997,7 @@
988997
</div>
989998

990999
<div class='together'>
991-
Where you used to have
1000+
Where you used to have code like this:
9921001

9931002
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
9941003
...make_shared<lambertian>(color(0.5, 0.5, 0.5))
@@ -1001,6 +1010,8 @@
10011010
...make_shared<lambertian>(make_shared<solid_color>(0.5, 0.5, 0.5))
10021011
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10031012
[Listing [lam-textured]: <kbd>[main.cc]</kbd> Lambertian material with texture]
1013+
1014+
Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
10041015
</div>
10051016

10061017

@@ -1041,7 +1052,8 @@
10411052

10421053
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10431054
auto checker = make_shared<checker_texture>(
1044-
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
1055+
make_shared<solid_color>(0.2, 0.3, 0.1),
1056+
make_shared<solid_color>(0.9, 0.9, 0.9)
10451057
);
10461058

10471059
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
@@ -1065,7 +1077,8 @@
10651077
hittable_list objects;
10661078

10671079
auto checker = make_shared<checker_texture>(
1068-
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
1080+
make_shared<solid_color>(0.2, 0.3, 0.1),
1081+
make_shared<solid_color>(0.9, 0.9, 0.9)
10691082
);
10701083

10711084
objects.add(make_shared<sphere>(point3(0,-10, 0), 10, make_shared<lambertian>(checker)));

src/InOneWeekend/main.cc

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,45 @@ color ray_color(const ray& r, const hittable& world, int depth) {
4444
hittable_list random_scene() {
4545
hittable_list world;
4646

47-
world.add(
48-
make_shared<sphere>(
49-
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5)))
50-
);
47+
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
48+
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
5149

52-
int i = 1;
5350
for (int a = -11; a < 11; a++) {
5451
for (int b = -11; b < 11; b++) {
5552
auto choose_mat = random_double();
5653
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
54+
5755
if ((center - vec3(4, 0.2, 0)).length() > 0.9) {
56+
shared_ptr<material> sphere_material;
57+
5858
if (choose_mat < 0.8) {
5959
// diffuse
6060
auto albedo = color::random() * color::random();
61-
world.add(
62-
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
61+
sphere_material = make_shared<lambertian>(albedo);
62+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
6363
} else if (choose_mat < 0.95) {
6464
// metal
65-
auto albedo = color::random(.5, 1);
66-
auto fuzz = random_double(0, .5);
67-
world.add(
68-
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
65+
auto albedo = color::random(0.5, 1);
66+
auto fuzz = random_double(0, 0.5);
67+
sphere_material = make_shared<metal>(albedo, fuzz);
68+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
6969
} else {
7070
// glass
71-
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
71+
sphere_material = make_shared<dielectric>(1.5);
72+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
7273
}
7374
}
7475
}
7576
}
7677

77-
world.add(
78-
make_shared<sphere>(
79-
point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
78+
auto material1 = make_shared<dielectric>(1.5);
79+
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
8080

81-
world.add(
82-
make_shared<sphere>(
83-
point3(-4, 1, 0), 1.0, make_shared<lambertian>(color(0.4, 0.2, 0.1))));
81+
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
82+
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
8483

85-
world.add(
86-
make_shared<sphere>(
87-
point3(4, 1, 0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)));
84+
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
85+
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
8886

8987
return world;
9088
}

src/TheNextWeek/main.cc

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,44 @@ hittable_list random_scene() {
5757

5858
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
5959

60-
for (int a = -10; a < 10; a++) {
61-
for (int b = -10; b < 10; b++) {
60+
for (int a = -11; a < 11; a++) {
61+
for (int b = -11; b < 11; b++) {
6262
auto choose_mat = random_double();
6363
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
64-
if ((center - vec3(4, .2, 0)).length() > 0.9) {
64+
65+
if ((center - vec3(4, 0.2, 0)).length() > 0.9) {
66+
shared_ptr<material> sphere_material;
67+
6568
if (choose_mat < 0.8) {
6669
// diffuse
6770
auto albedo = color::random() * color::random();
71+
sphere_material = make_shared<lambertian>(make_shared<solid_color>(albedo));
72+
auto center2 = center + vec3(0, random_double(0,.5), 0);
6873
world.add(make_shared<moving_sphere>(
69-
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
70-
make_shared<lambertian>(make_shared<solid_color>(albedo))
71-
));
74+
center, center2, 0.0, 1.0, 0.2, sphere_material));
7275
} else if (choose_mat < 0.95) {
7376
// metal
74-
auto albedo = color::random(.5, 1);
75-
auto fuzz = random_double(0, .5);
76-
world.add(
77-
make_shared<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
77+
auto albedo = color::random(0.5, 1);
78+
auto fuzz = random_double(0, 0.5);
79+
sphere_material = make_shared<metal>(albedo, fuzz);
80+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
7881
} else {
7982
// glass
80-
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
83+
sphere_material = make_shared<dielectric>(1.5);
84+
world.add(make_shared<sphere>(center, 0.2, sphere_material));
8185
}
8286
}
8387
}
8488
}
8589

86-
world.add(make_shared<sphere>(point3(0,1,0), 1.0, make_shared<dielectric>(1.5)));
90+
auto material1 = make_shared<dielectric>(1.5);
91+
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
8792

88-
world.add(
89-
make_shared<sphere>(
90-
point3(-4,1,0), 1.0,
91-
make_shared<lambertian>(make_shared<solid_color>(0.4, 0.2, 0.1))
92-
)
93-
);
93+
auto material2 = make_shared<lambertian>(make_shared<solid_color>(color(0.4, 0.2, 0.1)));
94+
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
9495

95-
world.add(
96-
make_shared<sphere>(
97-
point3(4,1,0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)
98-
)
99-
);
96+
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
97+
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
10098

10199
return hittable_list(make_shared<bvh_node>(world, 0.0, 1.0));
102100
}
@@ -106,7 +104,8 @@ hittable_list two_spheres() {
106104
hittable_list objects;
107105

108106
auto checker = make_shared<checker_texture>(
109-
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
107+
make_shared<solid_color>(0.2, 0.3, 0.1),
108+
make_shared<solid_color>(0.9, 0.9, 0.9)
110109
);
111110

112111
objects.add(make_shared<sphere>(point3(0,-10, 0), 10, make_shared<lambertian>(checker)));

0 commit comments

Comments
 (0)