Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


----------------------------------------------------------------------------------------------------
Expand Down
134 changes: 67 additions & 67 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -757,35 +757,36 @@

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
...
color ray_color(
color ray_color(...) {
...
}

hittable_list cornell_box() {
hittable_list world;
hittable_list objects;

auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));

shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white);
box1 = make_shared<rotate_y>(box1, 15);
box1 = make_shared<translate>(box1, vec3(265,0,295));
world.add(box1);
objects.add(box1);

shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white);
box2 = make_shared<rotate_y>(box2, -18);
box2 = make_shared<translate>(box2, vec3(130,0,65));
world.add(box2);
objects.add(box2);

return world;
return objects;
}

int main() {
Expand All @@ -799,10 +800,6 @@

// World

auto lights = make_shared<hittable_list>();
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));

auto world = cornell_box();

color background(0,0,0);
Expand Down Expand Up @@ -1524,21 +1521,21 @@

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
hittable_list cornell_box() {
hittable_list world;
hittable_list objects;

auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));

...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -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]: <kbd>[main.cc]</kbd> The ray_color function, using cosine pdf]
Expand Down Expand Up @@ -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]: <kbd>[main.cc]</kbd> ray_color function with hittable PDF]
Expand Down Expand Up @@ -1872,15 +1867,8 @@
<div class='together'>
And plugging it into `ray_color()`:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
color ray_color(
const ray& r,
const color& background,
const hittable& world,
shared_ptr<hittable> 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.
Expand Down Expand Up @@ -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]: <kbd>[main.cc]</kbd> The ray_color function, using mixture PDF]
Expand Down Expand Up @@ -1972,7 +1959,7 @@



Cleaning Up PDF Management.
Cleaning Up PDF Management
====================================================================================================

<div class='together'>
Expand Down Expand Up @@ -2077,14 +2064,15 @@
<div class='together'>
And `ray_color()` changes are small:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
color ray_color(
const ray& r,
const color& background,
const hittable& world,
shared_ptr<hittable> lights,
int depth
) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
hit_record rec;

// If we've exceeded the ray bounce limit, no more light is gathered.
Expand All @@ -2109,10 +2097,32 @@

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<hittable_list>();
lights->add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
lights->add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

auto world = cornell_box();

color background(0,0,0);

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);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scatter]
</div>
Expand Down Expand Up @@ -2218,32 +2228,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;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [ray-color-implicit]: <kbd>[main.cc]</kbd>
Ray color function with implicitly-sampled rays]
</div>

<div class='together'>
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<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
world.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand All @@ -2252,25 +2261,15 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
box1 = make_shared<rotate_y>(box1, 15);
box1 = make_shared<translate>(box1, vec3(265,0,295));
world.add(box1);

shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white);
box2 = make_shared<rotate_y>(box2, -18);
box2 = make_shared<translate>(box2, vec3(130,0,65);
world.add(box2);
objects.add(box1);

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;

cam = camera(lookfrom, lookat, vup, vfov, aspect, aperture, dist_to_focus, time0, time1);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto glass = make_shared<dielectric>(1.5);
objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

return world;
return objects;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [scene-cornell-al]: <kbd>[main.cc]</kbd> Cornell box scene with aluminum material]
Expand Down Expand Up @@ -2511,6 +2510,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;
Expand Down
2 changes: 1 addition & 1 deletion src/TheNextWeek/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ hittable_list cornell_box() {
auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15,15,15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
Expand Down
25 changes: 13 additions & 12 deletions src/TheRestOfYourLife/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,30 @@ color ray_color(


hittable_list cornell_box() {
hittable_list world;
hittable_list objects;

auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));

shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white);
shared_ptr<material> aluminum = make_shared<metal>(color(0.8, 0.85, 0.88), 0.0);
shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), aluminum);
box1 = make_shared<rotate_y>(box1, 15);
box1 = make_shared<translate>(box1, vec3(265,0,295));
world.add(box1);
objects.add(box1);

auto glass = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(190,90,190), 90 , glass));
objects.add(make_shared<sphere>(point3(190,90,190), 90 , glass));

return world;
return objects;
}


Expand Down Expand Up @@ -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";
Expand Down