Skip to content

Commit f0a3002

Browse files
authored
Merge pull request #601 from RayTracing/feat-516
Add alternative color constructors
2 parents 66ca0df + 9b57542 commit f0a3002

File tree

9 files changed

+110
-115
lines changed

9 files changed

+110
-115
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Change Log -- Ray Tracing in One Weekend
55

66
### Common
77
- Change: Renamed and explicitly numbered book images and figures (#495)
8+
- New: Added alternative constructors that take color arguments in addition to the constructors
9+
that take `shared_ptr<texture>` arguments, simplifying calling code. This applies to the
10+
following classes: `checker_texture`, `constant_medium`, `diffuse_light`, and `lambertian`.
11+
(#516)
812

913

1014
----------------------------------------------------------------------------------------------------

books/RayTracingTheNextWeek.html

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@
10411041
class lambertian : public material {
10421042
public:
10431043
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1044+
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
10441045
lambertian(shared_ptr<texture> a) : albedo(a) {}
10451046
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10461047

@@ -1064,24 +1065,6 @@
10641065
[Listing [lambertian-textured]: <kbd>[material.h]</kbd> Lambertian material with texture]
10651066
</div>
10661067

1067-
<div class='together'>
1068-
Where you used to have code like this:
1069-
1070-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1071-
...make_shared<lambertian>(color(0.5, 0.5, 0.5))
1072-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1073-
[Listing [lam-solid]: <kbd>[main.cc]</kbd> Lambertian material with solid color]
1074-
1075-
now you should replace the `color(...)` with `make_shared<solid_color>(...)`
1076-
1077-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1078-
...make_shared<lambertian>(make_shared<solid_color>(0.5, 0.5, 0.5))
1079-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1080-
[Listing [lam-textured]: <kbd>[main.cc]</kbd> Lambertian material with texture]
1081-
1082-
Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
1083-
</div>
1084-
10851068

10861069
A Checker Texture
10871070
------------------
@@ -1094,7 +1077,12 @@
10941077
class checker_texture : public texture {
10951078
public:
10961079
checker_texture() {}
1097-
checker_texture(shared_ptr<texture> t0, shared_ptr<texture> t1): even(t0), odd(t1) {}
1080+
1081+
checker_texture(shared_ptr<texture> t0, shared_ptr<texture> t1)
1082+
: even(t0), odd(t1) {}
1083+
1084+
checker_texture(color c1, color c2)
1085+
: even(make_shared<solid_color>(c1)) , odd(make_shared<solid_color>(c2)) {}
10981086

10991087
virtual color value(double u, double v, const point3& p) const {
11001088
auto sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z());
@@ -1119,10 +1107,7 @@
11191107
If we add this to our `random_scene()` function’s base sphere:
11201108

11211109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1122-
auto checker = make_shared<checker_texture>(
1123-
make_shared<solid_color>(0.2, 0.3, 0.1),
1124-
make_shared<solid_color>(0.9, 0.9, 0.9)
1125-
);
1110+
auto checker = make_shared<checker_texture>(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9));
11261111

11271112
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(checker)));
11281113
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1144,10 +1129,7 @@
11441129
hittable_list two_spheres() {
11451130
hittable_list objects;
11461131

1147-
auto checker = make_shared<checker_texture>(
1148-
make_shared<solid_color>(0.2, 0.3, 0.1),
1149-
make_shared<solid_color>(0.9, 0.9, 0.9)
1150-
);
1132+
auto checker = make_shared<checker_texture>(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9));
11511133

11521134
objects.add(make_shared<sphere>(point3(0,-10, 0), 10, make_shared<lambertian>(checker)));
11531135
objects.add(make_shared<sphere>(point3(0, 10, 0), 10, make_shared<lambertian>(checker)));
@@ -1843,6 +1825,7 @@
18431825
class diffuse_light : public material {
18441826
public:
18451827
diffuse_light(shared_ptr<texture> a) : emit(a) {}
1828+
diffuse_light(color c) : emit(make_shared<solid_color>(c)) {}
18461829

18471830
virtual bool scatter(
18481831
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
@@ -2028,7 +2011,7 @@
20282011
objects.add(make_shared<sphere>(point3(0,-1000,0), 1000, make_shared<lambertian>(pertext)));
20292012
objects.add(make_shared<sphere>(point3(0,2,0), 2, make_shared<lambertian>(pertext)));
20302013

2031-
auto difflight = make_shared<diffuse_light>(make_shared<solid_color>(4,4,4));
2014+
auto difflight = make_shared<diffuse_light>(color(4,4,4));
20322015
objects.add(make_shared<sphere>(point3(0,7,0), 2, difflight));
20332016
objects.add(make_shared<xy_rect>(3, 5, 1, 3, -2, difflight));
20342017

@@ -2163,10 +2146,10 @@
21632146
hittable_list cornell_box() {
21642147
hittable_list objects;
21652148

2166-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
2167-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2168-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
2169-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
2149+
auto red = make_shared<lambertian>(color(.65, .05, .05));
2150+
auto white = make_shared<lambertian>(color(.73, .73, .73));
2151+
auto green = make_shared<lambertian>(color(.12, .45, .15));
2152+
auto light = make_shared<diffuse_light>(color(15, 15, 15));
21702153

21712154
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
21722155
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
@@ -2248,10 +2231,10 @@
22482231
hittable_list cornell_box() {
22492232
hittable_list objects;
22502233

2251-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
2252-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2253-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
2254-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
2234+
auto red = make_shared<lambertian>(color(.65, .05, .05));
2235+
auto white = make_shared<lambertian>(color(.73, .73, .73));
2236+
auto green = make_shared<lambertian>(color(.12, .45, .15));
2237+
auto light = make_shared<diffuse_light>(color(15, 15, 15));
22552238

22562239

22572240
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2616,10 +2599,16 @@
26162599
class constant_medium : public hittable {
26172600
public:
26182601
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
2619-
: boundary(b), neg_inv_density(-1/d)
2620-
{
2621-
phase_function = make_shared<isotropic>(a);
2622-
}
2602+
: boundary(b),
2603+
neg_inv_density(-1/d),
2604+
phase_function(make_shared<isotropic>(a))
2605+
{}
2606+
2607+
constant_medium(shared_ptr<hittable> b, double d, color c)
2608+
: boundary(b),
2609+
neg_inv_density(-1/d),
2610+
phase_function(make_shared<isotropic>(color(c)))
2611+
{}
26232612

26242613
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
26252614

@@ -2734,10 +2723,10 @@
27342723
hittable_list cornell_smoke() {
27352724
hittable_list objects;
27362725

2737-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
2738-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2739-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
2740-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
2726+
auto red = make_shared<lambertian>(color(.65, .05, .05));
2727+
auto white = make_shared<lambertian>(color(.73, .73, .73));
2728+
auto green = make_shared<lambertian>(color(.12, .45, .15));
2729+
auto light = make_shared<diffuse_light>(color(7, 7, 7));
27412730

27422731
objects.add(make_shared<flip_face>(make_shared<yz_rect>(0, 555, 0, 555, 555, green)));
27432732
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
@@ -2754,8 +2743,8 @@
27542743
box2 = make_shared<rotate_y>(box2, -18);
27552744
box2 = make_shared<translate>(box2, vec3(130,0,65));
27562745

2757-
objects.add(make_shared<constant_medium>(box1, 0.01, make_shared<solid_color>(0,0,0)));
2758-
objects.add(make_shared<constant_medium>(box2, 0.01, make_shared<solid_color>(1,1,1)));
2746+
objects.add(make_shared<constant_medium>(box1, 0.01, color(0,0,0)));
2747+
objects.add(make_shared<constant_medium>(box2, 0.01, color(1,1,1)));
27592748

27602749
return objects;
27612750
}
@@ -2783,7 +2772,7 @@
27832772
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
27842773
hittable_list final_scene() {
27852774
hittable_list boxes1;
2786-
auto ground = make_shared<lambertian>(make_shared<solid_color>(0.48, 0.83, 0.53));
2775+
auto ground = make_shared<lambertian>(color(0.48, 0.83, 0.53));
27872776

27882777
const int boxes_per_side = 20;
27892778
for (int i = 0; i < boxes_per_side; i++) {
@@ -2804,13 +2793,12 @@
28042793

28052794
objects.add(make_shared<bvh_node>(boxes1, 0, 1));
28062795

2807-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
2796+
auto light = make_shared<diffuse_light>(color(7, 7, 7));
28082797
objects.add(make_shared<xz_rect>(123, 423, 147, 412, 554, light));
28092798

28102799
auto center1 = point3(400, 400, 200);
28112800
auto center2 = center1 + vec3(30,0,0);
2812-
auto moving_sphere_material =
2813-
make_shared<lambertian>(make_shared<solid_color>(0.7, 0.3, 0.1));
2801+
auto moving_sphere_material = make_shared<lambertian>(color(0.7, 0.3, 0.1));
28142802
objects.add(make_shared<moving_sphere>(center1, center2, 0, 1, 50, moving_sphere_material));
28152803

28162804
objects.add(make_shared<sphere>(point3(260, 150, 45), 50, make_shared<dielectric>(1.5)));
@@ -2820,20 +2808,17 @@
28202808

28212809
auto boundary = make_shared<sphere>(point3(360,150,145), 70, make_shared<dielectric>(1.5));
28222810
objects.add(boundary);
2823-
objects.add(make_shared<constant_medium>(
2824-
boundary, 0.2, make_shared<solid_color>(0.2, 0.4, 0.9)
2825-
));
2811+
objects.add(make_shared<constant_medium>(boundary, 0.2, color(0.2, 0.4, 0.9)));
28262812
boundary = make_shared<sphere>(point3(0, 0, 0), 5000, make_shared<dielectric>(1.5));
2827-
objects.add(make_shared<constant_medium>(
2828-
boundary, .0001, make_shared<solid_color>(1,1,1)));
2813+
objects.add(make_shared<constant_medium>(boundary, .0001, color(1,1,1)));
28292814

28302815
auto emat = make_shared<lambertian>(make_shared<image_texture>("earthmap.jpg"));
28312816
objects.add(make_shared<sphere>(point3(400,200,400), 100, emat));
28322817
auto pertext = make_shared<noise_texture>(0.1);
28332818
objects.add(make_shared<sphere>(point3(220,280,300), 80, make_shared<lambertian>(pertext)));
28342819

28352820
hittable_list boxes2;
2836-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2821+
auto white = make_shared<lambertian>(color(.73, .73, .73));
28372822
int ns = 1000;
28382823
for (int j = 0; j < ns; j++) {
28392824
boxes2.add(make_shared<sphere>(point3::random(0,165), 10, white));

books/RayTracingTheRestOfYourLife.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,10 @@
758758
hittable_list cornell_box(camera& cam, double aspect) {
759759
hittable_list world;
760760

761-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
762-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
763-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
764-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
761+
auto red = make_shared<lambertian>(color(.65, .05, .05));
762+
auto white = make_shared<lambertian>(color(.73, .73, .73));
763+
auto green = make_shared<lambertian>(color(.12, .45, .15));
764+
auto light = make_shared<diffuse_light>(color(15, 15, 15));
765765

766766
world.add(make_shared<flip_face>(make_shared<yz_rect>(0, 555, 0, 555, 555, green)));
767767
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
@@ -849,6 +849,7 @@
849849
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
850850
class lambertian : public material {
851851
public:
852+
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
852853
lambertian(shared_ptr<texture> a) : albedo(a) {}
853854

854855

@@ -1947,6 +1948,7 @@
19471948
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19481949
class lambertian : public material {
19491950
public:
1951+
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
19501952
lambertian(shared_ptr<texture> a) : albedo(a) {}
19511953

19521954
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -2108,10 +2110,10 @@
21082110
hittable_list cornell_box(camera& cam, double aspect) {
21092111
hittable_list world;
21102112

2111-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
2112-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2113-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
2114-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
2113+
auto red = make_shared<lambertian>(color(.65, .05, .05));
2114+
auto white = make_shared<lambertian>(color(.73, .73, .73));
2115+
auto green = make_shared<lambertian>(color(.12, .45, .15));
2116+
auto light = make_shared<diffuse_light>(color(15, 15, 15));
21152117

21162118
world.add(make_shared<flip_face>(make_shared<yz_rect>(0, 555, 0, 555, 555, green)));
21172119
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));

src/TheNextWeek/constant_medium.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
class constant_medium : public hittable {
2222
public:
2323
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
24-
: boundary(b), neg_inv_density(-1/d)
25-
{
26-
phase_function = make_shared<isotropic>(a);
27-
}
24+
: boundary(b),
25+
neg_inv_density(-1/d),
26+
phase_function(make_shared<isotropic>(a))
27+
{}
28+
29+
constant_medium(shared_ptr<hittable> b, double d, color c)
30+
: boundary(b),
31+
neg_inv_density(-1/d),
32+
phase_function(make_shared<isotropic>(make_shared<solid_color>(c)))
33+
{}
2834

2935
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const;
3036

0 commit comments

Comments
 (0)