Skip to content

Commit

Permalink
Update random_scene() in books 1 and 2
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
hollasch committed May 6, 2020
1 parent 2f12e6b commit d467cd3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 81 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Change Log -- Ray Tracing in One Weekend

### _In One Weekend_
- Change: The C++ `<random>` version of `random_double()` no longer depends on `<functional>` 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)


----------------------------------------------------------------------------------------------------
Expand Down
34 changes: 19 additions & 15 deletions books/RayTracingInOneWeekend.html
Original file line number Diff line number Diff line change
Expand Up @@ -2846,41 +2846,45 @@
hittable_list random_scene() {
hittable_list world;

world.add(make_shared<sphere>(
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5))));
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
world.add(make_shared<sphere>(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<material> sphere_material;

if (choose_mat < 0.8) {
// diffuse
auto albedo = color::random() * color::random();
world.add(
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
sphere_material = make_shared<lambertian>(albedo);
world.add(make_shared<sphere>(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<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
auto albedo = color::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
} else {
// glass
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
}
}
}
}

world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));

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

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

return world;
}
Expand Down
55 changes: 34 additions & 21 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,40 +292,49 @@
hittable_list random_scene() {
hittable_list world;

world.add(make_shared<sphere>(
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5))));
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
world.add(make_shared<sphere>(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<material> sphere_material;

if (choose_mat < 0.8) {
// diffuse
auto albedo = color::random() * color::random();
sphere_material = make_shared<lambertian>(albedo);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto center2 = center + vec3(0, random_double(0,.5), 0);
world.add(make_shared<moving_sphere>(
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
make_shared<lambertian>(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<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
auto albedo = color::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
} else {
// glass
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
}
}
}
}

world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
world.add(make_shared<sphere>(
point3(-4, 1, 0), 1.0, make_shared<lambertian>(color(0.4, 0.2, 0.1))));
world.add(make_shared<sphere>(
point3(4, 1, 0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)));
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));

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

auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));

return world;
}
Expand Down Expand Up @@ -988,7 +997,7 @@
</div>

<div class='together'>
Where you used to have
Where you used to have code like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
...make_shared<lambertian>(color(0.5, 0.5, 0.5))
Expand All @@ -1001,6 +1010,8 @@
...make_shared<lambertian>(make_shared<solid_color>(0.5, 0.5, 0.5))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [lam-textured]: <kbd>[main.cc]</kbd> Lambertian material with texture]

Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
</div>


Expand Down Expand Up @@ -1041,7 +1052,8 @@

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto checker = make_shared<checker_texture>(
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
make_shared<solid_color>(0.2, 0.3, 0.1),
make_shared<solid_color>(0.9, 0.9, 0.9)
);

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

auto checker = make_shared<checker_texture>(
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
make_shared<solid_color>(0.2, 0.3, 0.1),
make_shared<solid_color>(0.9, 0.9, 0.9)
);

objects.add(make_shared<sphere>(point3(0,-10, 0), 10, make_shared<lambertian>(checker)));
Expand Down
40 changes: 19 additions & 21 deletions src/InOneWeekend/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<sphere>(
point3(0,-1000,0), 1000, make_shared<lambertian>(color(0.5, 0.5, 0.5)))
);
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
world.add(make_shared<sphere>(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<material> sphere_material;

if (choose_mat < 0.8) {
// diffuse
auto albedo = color::random() * color::random();
world.add(
make_shared<sphere>(center, 0.2, make_shared<lambertian>(albedo)));
sphere_material = make_shared<lambertian>(albedo);
world.add(make_shared<sphere>(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<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
auto albedo = color::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
} else {
// glass
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
}
}
}
}

world.add(
make_shared<sphere>(
point3(0, 1, 0), 1.0, make_shared<dielectric>(1.5)));
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));

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

world.add(
make_shared<sphere>(
point3(4, 1, 0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)));
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));

return world;
}
Expand Down
47 changes: 23 additions & 24 deletions src/TheNextWeek/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,44 @@ hittable_list random_scene() {

world.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(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<material> sphere_material;

if (choose_mat < 0.8) {
// diffuse
auto albedo = color::random() * color::random();
sphere_material = make_shared<lambertian>(make_shared<solid_color>(albedo));
auto center2 = center + vec3(0, random_double(0,.5), 0);
world.add(make_shared<moving_sphere>(
center, center + vec3(0, random_double(0,.5), 0), 0.0, 1.0, 0.2,
make_shared<lambertian>(make_shared<solid_color>(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<sphere>(center, 0.2, make_shared<metal>(albedo, fuzz)));
auto albedo = color::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
} else {
// glass
world.add(make_shared<sphere>(center, 0.2, make_shared<dielectric>(1.5)));
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
}
}
}
}

world.add(make_shared<sphere>(point3(0,1,0), 1.0, make_shared<dielectric>(1.5)));
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));

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

world.add(
make_shared<sphere>(
point3(4,1,0), 1.0, make_shared<metal>(color(0.7, 0.6, 0.5), 0.0)
)
);
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));

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

auto checker = make_shared<checker_texture>(
make_shared<solid_color>(0.2, 0.3, 0.1), make_shared<solid_color>(0.9, 0.9, 0.9)
make_shared<solid_color>(0.2, 0.3, 0.1),
make_shared<solid_color>(0.9, 0.9, 0.9)
);

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

0 comments on commit d467cd3

Please sign in to comment.