Skip to content

Commit 55abc5f

Browse files
authored
Merge pull request #517 from RayTracing/fix-489
Update random_scene() in books 1 and 2
2 parents 7281850 + 27655ab commit 55abc5f

File tree

5 files changed

+104
-82
lines changed

5 files changed

+104
-82
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ 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>`
8-
header
8+
header.
9+
- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with source.
10+
(#489)
911

1012
### _The Next Week_
13+
- Fix: Added clarification about updating lambertian variables from `color` to `solid_color`.
14+
- Fix: Corrected for-loop indices (they differed from the version in book 1) in `random_scene()`.
1115
- Fix: Introduce "Texture Coordinates for Spheres" in Chapter 4 to support (u,v) coordinates in
1216
`hit_record` (#496)
17+
- Change: Refactored `random_scene()`. More named intermediate values, sync'ed with version in
18+
_In One Weekend_ and with source. Added highlight for update from last version in book 1. (#489)
19+
- Change: The C++ `<random>` version of `random_double()` no longer depends on `<functional>`
20+
header
1321

1422

1523
----------------------------------------------------------------------------------------------------

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
}
@@ -1053,7 +1062,7 @@
10531062
</div>
10541063

10551064
<div class='together'>
1056-
Where you used to have
1065+
Where you used to have code like this:
10571066

10581067
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10591068
...make_shared<lambertian>(color(0.5, 0.5, 0.5))
@@ -1066,6 +1075,8 @@
10661075
...make_shared<lambertian>(make_shared<solid_color>(0.5, 0.5, 0.5))
10671076
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10681077
[Listing [lam-textured]: <kbd>[main.cc]</kbd> Lambertian material with texture]
1078+
1079+
Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
10691080
</div>
10701081

10711082

@@ -1106,7 +1117,8 @@
11061117

11071118
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
11081119
auto checker = make_shared<checker_texture>(
1109-
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
1120+
make_shared<solid_color>(0.2, 0.3, 0.1),
1121+
make_shared<solid_color>(0.9, 0.9, 0.9)
11101122
);
11111123

11121124
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
@@ -1130,7 +1142,8 @@
11301142
hittable_list objects;
11311143

11321144
auto checker = make_shared<checker_texture>(
1133-
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
1145+
make_shared<solid_color>(0.2, 0.3, 0.1),
1146+
make_shared<solid_color>(0.9, 0.9, 0.9)
11341147
);
11351148

11361149
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)