Skip to content

Commit b078f8c

Browse files
committed
Convert ray_color to use color type
1 parent bb11cea commit b078f8c

File tree

6 files changed

+95
-92
lines changed

6 files changed

+95
-92
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Peter Shirley
88
edited by Steve Hollasch and Trevor David Black
99
<br>
10-
Version 3.0.1, 2020-03-31
10+
Version 3.1.0-wip, 2020-03-31
1111
<br>
1212
Copyright 2018-2020 Peter Shirley. All rights reserved.
1313

@@ -355,7 +355,7 @@
355355
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
356356
for (int i = 0; i < image_width; ++i) {
357357
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
358-
vec3 pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25);
358+
color pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25);
359359
pixel_color.write_color(std::cout);
360360
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
361361
}
@@ -444,10 +444,10 @@
444444

445445
#include <iostream>
446446

447-
vec3 ray_color(const ray& r) {
447+
color ray_color(const ray& r) {
448448
vec3 unit_direction = unit_vector(r.direction());
449449
auto t = 0.5*(unit_direction.y() + 1.0);
450-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
450+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
451451
}
452452

453453
int main() {
@@ -470,7 +470,7 @@
470470
auto u = double(i) / (image_width-1);
471471
auto v = double(j) / (image_height-1);
472472
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
473-
vec3 pixel_color = ray_color(r);
473+
color pixel_color = ray_color(r);
474474
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
475475
pixel_color.write_color(std::cout);
476476
}
@@ -593,11 +593,11 @@
593593
vec3 ray_color(const ray& r) {
594594
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
595595
if (hit_sphere(vec3(0,0,-1), 0.5, r))
596-
return vec3(1, 0, 0);
596+
return color(1, 0, 0);
597597
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
598598
vec3 unit_direction = unit_vector(r.direction());
599599
auto t = 0.5*(unit_direction.y() + 1.0);
600-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
600+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
601601
}
602602
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
603603
[Listing [main-red-sphere]: <kbd>[main.cc]</kbd> Rendering a red sphere]
@@ -667,14 +667,14 @@
667667
auto t = hit_sphere(vec3(0,0,-1), 0.5, r);
668668
if (t > 0.0) {
669669
vec3 N = unit_vector(r.at(t) - vec3(0,0,-1));
670-
return 0.5*vec3(N.x()+1, N.y()+1, N.z()+1);
670+
return 0.5*color(N.x()+1, N.y()+1, N.z()+1);
671671
}
672672
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
673673
vec3 unit_direction = unit_vector(r.direction());
674674
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
675675
t = 0.5*(unit_direction.y() + 1.0);
676676
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
677-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
677+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
678678
}
679679
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
680680
[Listing [render-surface-normal]: <kbd>[main.cc]</kbd> Rendering surface normals on a sphere]
@@ -1124,18 +1124,18 @@
11241124
#include <iostream>
11251125

11261126
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1127-
vec3 ray_color(const ray& r, const hittable& world) {
1127+
color ray_color(const ray& r, const hittable& world) {
11281128
hit_record rec;
11291129
if (world.hit(r, 0, infinity, rec)) {
1130-
return 0.5 * (rec.normal + vec3(1,1,1));
1130+
return 0.5 * (rec.normal + color(1,1,1));
11311131
}
11321132

11331133
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
11341134
vec3 unit_direction = unit_vector(r.direction());
11351135
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
11361136
auto t = 0.5*(unit_direction.y() + 1.0);
11371137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1138-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1138+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
11391139
}
11401140

11411141
int main() {
@@ -1165,7 +1165,7 @@
11651165

11661166

11671167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1168-
vec3 pixel_color = ray_color(r, world);
1168+
color pixel_color = ray_color(r, world);
11691169
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
11701170

11711171
pixel_color.write_color(std::cout);
@@ -1345,7 +1345,7 @@
13451345
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
13461346
for (int i = 0; i < image_width; ++i) {
13471347
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1348-
vec3 pixel_color(0, 0, 0);
1348+
color pixel_color(0, 0, 0);
13491349
for (int s = 0; s < samples_per_pixel; ++s) {
13501350
auto u = (i + random_double()) / (image_width-1);
13511351
auto v = (j + random_double()) / (image_height-1);
@@ -1455,7 +1455,7 @@
14551455
Then update the `ray_color()` function to use the new random direction generator:
14561456

14571457
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1458-
vec3 ray_color(const ray& r, const hittable& world) {
1458+
color ray_color(const ray& r, const hittable& world) {
14591459
hit_record rec;
14601460

14611461
if (world.hit(r, 0, infinity, rec)) {
@@ -1467,7 +1467,7 @@
14671467

14681468
vec3 unit_direction = unit_vector(r.direction());
14691469
auto t = 0.5*(unit_direction.y() + 1.0);
1470-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1470+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
14711471
}
14721472
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14731473
[Listing [ray-color-random-unit]: <kbd>[main.cc]</kbd> ray_color() using a random ray direction]
@@ -1480,12 +1480,12 @@
14801480
depth, returning no light contribution at the maximum depth:
14811481

14821482
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1483-
vec3 ray_color(const ray& r, const hittable& world, int depth) {
1483+
color ray_color(const ray& r, const hittable& world, int depth) {
14841484
hit_record rec;
14851485

14861486
// If we've exceeded the ray bounce limit, no more light is gathered.
14871487
if (depth <= 0)
1488-
return vec3(0,0,0);
1488+
return color(0,0,0);
14891489

14901490
if (world.hit(r, 0, infinity, rec)) {
14911491
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
@@ -1495,9 +1495,11 @@
14951495

14961496
vec3 unit_direction = unit_vector(r.direction());
14971497
auto t = 0.5*(unit_direction.y() + 1.0);
1498-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1498+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
14991499
}
1500+
15001501
...
1502+
15011503
int main() {
15021504
const int image_width = 200;
15031505
const int image_height = 100;
@@ -1510,7 +1512,7 @@
15101512
for (int j = image_height-1; j >= 0; --j) {
15111513
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
15121514
for (int i = 0; i < image_width; ++i) {
1513-
vec3 pixel_color(0, 0, 0);
1515+
color pixel_color(0, 0, 0);
15141516
for (int s = 0; s < samples_per_pixel; ++s) {
15151517
auto u = (i + random_double()) / (image_width-1);
15161518
auto v = (j + random_double()) / (image_height-1);
@@ -1629,12 +1631,12 @@
16291631
function.
16301632

16311633
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1632-
vec3 ray_color(const ray& r, const hittable& world, int depth) {
1634+
color ray_color(const ray& r, const hittable& world, int depth) {
16331635
hit_record rec;
16341636

16351637
// If we've exceeded the ray bounce limit, no more light is gathered.
16361638
if (depth <= 0)
1637-
return vec3(0,0,0);
1639+
return color(0,0,0);
16381640

16391641
if (world.hit(r, 0.001, infinity, rec)) {
16401642
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1645,7 +1647,7 @@
16451647

16461648
vec3 unit_direction = unit_vector(r.direction());
16471649
auto t = 0.5*(unit_direction.y() + 1.0);
1648-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1650+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
16491651
}
16501652
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16511653
[Listing [ray-color-unit-sphere]: <kbd>[main.cc]</kbd> ray_color() with replacement diffuse]
@@ -1711,12 +1713,12 @@
17111713
Plugging the new formula into the `ray_color()` function:
17121714

17131715
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1714-
vec3 ray_color(const ray& r, const hittable& world, int depth) {
1716+
color ray_color(const ray& r, const hittable& world, int depth) {
17151717
hit_record rec;
17161718

17171719
// If we've exceeded the ray bounce limit, no more light is gathered.
17181720
if (depth <= 0)
1719-
return vec3(0,0,0);
1721+
return color(0,0,0);
17201722

17211723
if (world.hit(r, 0.001, infinity, rec)) {
17221724
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1727,7 +1729,7 @@
17271729

17281730
vec3 unit_direction = unit_vector(r.direction());
17291731
auto t = 0.5*(unit_direction.y() + 1.0);
1730-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1732+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
17311733
}
17321734
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17331735
[Listing [ray-color-hemisphere]: <kbd>[main.cc]</kbd> ray_color() with hemispherical scattering]
@@ -1965,26 +1967,26 @@
19651967
We need to modify the `ray_color()` function to use this:
19661968

19671969
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1968-
vec3 ray_color(const ray& r, const hittable& world, int depth) {
1970+
color ray_color(const ray& r, const hittable& world, int depth) {
19691971
hit_record rec;
19701972

19711973
// If we've exceeded the ray bounce limit, no more light is gathered.
19721974
if (depth <= 0)
1973-
return vec3(0,0,0);
1975+
return color(0,0,0);
19741976

19751977
if (world.hit(r, 0.001, infinity, rec)) {
19761978
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
19771979
ray scattered;
1978-
vec3 attenuation;
1980+
color attenuation;
19791981
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
19801982
return attenuation * ray_color(scattered, world, depth-1);
1981-
return vec3(0,0,0);
1983+
return color(0,0,0);
19821984
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19831985
}
19841986

19851987
vec3 unit_direction = unit_vector(r.direction());
19861988
auto t = 0.5*(unit_direction.y() + 1.0);
1987-
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
1989+
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
19881990
}
19891991
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19901992
[Listing [ray-color-scatter]: <kbd>[main.cc]</kbd> Ray color with scattered reflectance]
@@ -2020,7 +2022,7 @@
20202022
for (int j = image_height-1; j >= 0; --j) {
20212023
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
20222024
for (int i = 0; i < image_width; ++i) {
2023-
vec3 pixel_color(0, 0, 0);
2025+
color pixel_color(0, 0, 0);
20242026
for (int s = 0; s < samples_per_pixel; ++s) {
20252027
auto u = (i + random_double()) / (image_width-1);
20262028
auto v = (j + random_double()) / (image_height-1);

books/RayTracingTheNextWeek.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Peter Shirley
88
edited by Steve Hollasch and Trevor David Black
99
<br>
10-
Version 3.0.1, 2020-03-31
10+
Version 3.1.0-wip, 2020-03-31
1111
<br>
1212
Copyright 2018-2020 Peter Shirley. All rights reserved.
1313

@@ -1802,20 +1802,21 @@
18021802
the new `emitted` value.
18031803

18041804
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1805-
vec3 ray_color(const ray& r, const vec3& background, const hittable& world, int depth) {
1805+
color ray_color(const ray& r, const color& background, const hittable& world, int depth) {
18061806
hit_record rec;
18071807

18081808
// If we've exceeded the ray bounce limit, no more light is gathered.
18091809
if (depth <= 0)
1810-
return vec3(0,0,0);
1810+
return color(0,0,0);
18111811

18121812
// If the ray hits nothing, return the background color.
18131813
if (!world.hit(r, 0.001, infinity, rec))
18141814
return background;
18151815

18161816
ray scattered;
1817-
vec3 attenuation;
1818-
vec3 emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
1817+
color attenuation;
1818+
color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
1819+
18191820
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
18201821
return emitted;
18211822

@@ -1825,7 +1826,7 @@
18251826

18261827
int main() {
18271828
...
1828-
const vec3 background(0,0,0);
1829+
const color background(0,0,0);
18291830
...
18301831
pixel_color += ray_color(r, background, world, max_depth);
18311832
...

0 commit comments

Comments
 (0)