Skip to content

Commit 4e7d7af

Browse files
committed
Add parameterized background color
This allows the scenes to specify their own background color, so they match the results in the book. Resolves #158
1 parent e19ceb3 commit 4e7d7af

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,10 +1730,12 @@
17301730
</div>
17311731

17321732
<div class='together'>
1733-
Next, let’s make the background black in our `ray_color` function, and pay attention to emitted:
1733+
Next, we want a pure black background so the only light in the scene is coming from the emitters. To
1734+
do this, we’ll add a background color parameter to our `ray_color` function, and pay attention to
1735+
the new `emitted` value.
17341736

17351737
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1736-
vec3 ray_color(const ray& r, hittable& world, int depth) {
1738+
vec3 ray_color(const ray& r, hittable& world, const vec3& background, int depth) {
17371739
hit_record rec;
17381740

17391741
// If we've exceeded the ray bounce limit, no more light is gathered.
@@ -1742,15 +1744,22 @@
17421744

17431745
// If the ray hits nothing, return the background color.
17441746
if (!world.hit(r, 0.001, infinity, rec))
1745-
return vec3(0,0,0);
1747+
return background;
17461748

17471749
ray scattered;
17481750
vec3 attenuation;
17491751
vec3 emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
17501752
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
17511753
return emitted;
17521754

1753-
return emitted + attenuation * ray_color(scattered, world, depth-1);
1755+
return emitted + attenuation * ray_color(scattered, world, background, depth-1);
1756+
}
1757+
...
1758+
1759+
int main() {
1760+
...
1761+
color += ray_color(r, world, vec3(0,0,0), max_depth);
1762+
...
17541763
}
17551764
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17561765
[Listing [ray-color-emitted]: <kbd>[main.cc]</kbd> ray_color function for emitting materials]

src/TheNextWeek/main.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <iostream>
2424

2525

26-
vec3 ray_color(const ray& r, hittable& world, int depth) {
26+
vec3 ray_color(const ray& r, hittable& world, const vec3& background, int depth) {
2727
hit_record rec;
2828

2929
// If we've exceeded the ray bounce limit, no more light is gathered.
@@ -32,7 +32,7 @@ vec3 ray_color(const ray& r, hittable& world, int depth) {
3232

3333
// If the ray hits nothing, return the background color.
3434
if (!world.hit(r, 0.001, infinity, rec))
35-
return vec3(0.5,0.5,0.5);
35+
return background;
3636

3737
ray scattered;
3838
vec3 attenuation;
@@ -41,7 +41,7 @@ vec3 ray_color(const ray& r, hittable& world, int depth) {
4141
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
4242
return emitted;
4343

44-
return emitted + attenuation * ray_color(scattered, world, depth-1);
44+
return emitted + attenuation * ray_color(scattered, world, background, depth-1);
4545
}
4646

4747

@@ -360,34 +360,39 @@ int main() {
360360
auto vfov = 40.0;
361361
auto aperture = 0.0;
362362
auto dist_to_focus = 10.0;
363+
vec3 background(0,0,0);
363364

364365
switch (0) {
365366
case 1:
366367
world = random_scene();
367368
lookfrom = vec3(13,2,3);
368369
lookat = vec3(0,0,0);
369370
vfov = 20.0;
371+
background = vec3(0.70, 0.80, 1.00);
370372
break;
371373

372374
case 2:
373375
world = two_spheres();
374376
lookfrom = vec3(13,2,3);
375377
lookat = vec3(0,0,0);
376378
vfov = 20.0;
379+
background = vec3(0.70, 0.80, 1.00);
377380
break;
378381

379382
case 3:
380383
world = two_perlin_spheres();
381384
lookfrom = vec3(13,2,3);
382385
lookat = vec3(0,0,0);
383386
vfov = 20.0;
387+
background = vec3(0.70, 0.80, 1.00);
384388
break;
385389

386390
case 4:
387391
world = earth();
388392
lookfrom = vec3(0,0,12);
389393
lookat = vec3(0,0,0);
390394
vfov = 20.0;
395+
background = vec3(0.70, 0.80, 1.00);
391396
break;
392397

393398
case 5:
@@ -446,7 +451,7 @@ int main() {
446451
auto u = (i + random_double()) / image_width;
447452
auto v = (j + random_double()) / image_height;
448453
ray r = cam.get_ray(u, v);
449-
color += ray_color(r, world, max_depth);
454+
color += ray_color(r, world, background, max_depth);
450455
}
451456
color.write_color(std::cout, samples_per_pixel);
452457
}

0 commit comments

Comments
 (0)