From dde8b8450a6ccb556d4cc37eb345e1135b5b7140 Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Sun, 17 May 2020 14:09:04 -0700 Subject: [PATCH 1/2] Add alternative color constructors Add alternative constructors that take color arguments in addition to the constructors that take shared_ptr arguments, simplifying calling code. This applies to the following clases: - checker_texture - constant_medium - diffuse_light - lambertian Resolves #516 --- books/RayTracingTheNextWeek.html | 97 +++++++++++--------------- books/RayTracingTheRestOfYourLife.html | 18 ++--- src/TheNextWeek/constant_medium.h | 14 ++-- src/TheNextWeek/main.cc | 73 ++++++++----------- src/TheNextWeek/material.h | 2 + src/TheRestOfYourLife/main.cc | 8 +-- src/TheRestOfYourLife/material.h | 2 + src/common/texture.h | 7 +- 8 files changed, 106 insertions(+), 115 deletions(-) diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index 01eb1f6d..3c6a4a03 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -1041,6 +1041,7 @@ class lambertian : public material { public: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight + lambertian(const color& a) : albedo(make_shared(a)) {} lambertian(shared_ptr a) : albedo(a) {} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ @@ -1064,24 +1065,6 @@ [Listing [lambertian-textured]: [material.h] Lambertian material with texture] -
-Where you used to have code like this: - - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - ...make_shared(color(0.5, 0.5, 0.5)) - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - [Listing [lam-solid]: [main.cc] Lambertian material with solid color] - -now you should replace the `color(...)` with `make_shared(...)` - - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - ...make_shared(make_shared(0.5, 0.5, 0.5)) - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - [Listing [lam-textured]: [main.cc] Lambertian material with texture] - -Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`. -
- A Checker Texture ------------------ @@ -1094,7 +1077,12 @@ class checker_texture : public texture { public: checker_texture() {} - checker_texture(shared_ptr t0, shared_ptr t1): even(t0), odd(t1) {} + + checker_texture(shared_ptr t0, shared_ptr t1) + : even(t0), odd(t1) {} + + checker_texture(color c1, color c2) + : even(make_shared(c1)) , odd(make_shared(c2)) {} virtual color value(double u, double v, const point3& p) const { auto sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z()); @@ -1119,10 +1107,7 @@ If we add this to our `random_scene()` function’s base sphere: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ - auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), - make_shared(0.9, 0.9, 0.9) - ); + auto checker = make_shared(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9)); world.add(make_shared(point3(0,-1000,0), 1000, make_shared(checker))); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1144,10 +1129,7 @@ hittable_list two_spheres() { hittable_list objects; - auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), - make_shared(0.9, 0.9, 0.9) - ); + auto checker = make_shared(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9)); objects.add(make_shared(point3(0,-10, 0), 10, make_shared(checker))); objects.add(make_shared(point3(0, 10, 0), 10, make_shared(checker))); @@ -1843,6 +1825,7 @@ class diffuse_light : public material { public: diffuse_light(shared_ptr a) : emit(a) {} + diffuse_light(color c) : emit(make_shared(c)) {} virtual bool scatter( const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered @@ -2028,7 +2011,7 @@ objects.add(make_shared(point3(0,-1000,0), 1000, make_shared(pertext))); objects.add(make_shared(point3(0,2,0), 2, make_shared(pertext))); - auto difflight = make_shared(make_shared(4,4,4)); + auto difflight = make_shared(color(4,4,4)); objects.add(make_shared(point3(0,7,0), 2, difflight)); objects.add(make_shared(3, 5, 1, 3, -2, difflight)); @@ -2163,10 +2146,10 @@ hittable_list cornell_box() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15, 15, 15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15, 15, 15)); objects.add(make_shared(0, 555, 0, 555, 555, green)); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -2248,10 +2231,10 @@ hittable_list cornell_box() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15, 15, 15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15, 15, 15)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -2616,10 +2599,16 @@ class constant_medium : public hittable { public: constant_medium(shared_ptr b, double d, shared_ptr a) - : boundary(b), neg_inv_density(-1/d) - { - phase_function = make_shared(a); - } + : boundary(b), + neg_inv_density(-1/d), + phase_function(make_shared(a)) + {} + + constant_medium(shared_ptr b, double d, color c) + : boundary(b), + neg_inv_density(-1/d), + phase_function(make_shared(color(c))) + {} virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const; @@ -2734,10 +2723,10 @@ hittable_list cornell_smoke() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(7, 7, 7)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(7, 7, 7)); objects.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -2754,8 +2743,8 @@ box2 = make_shared(box2, -18); box2 = make_shared(box2, vec3(130,0,65)); - objects.add(make_shared(box1, 0.01, make_shared(0,0,0))); - objects.add(make_shared(box2, 0.01, make_shared(1,1,1))); + objects.add(make_shared(box1, 0.01, color(0,0,0))); + objects.add(make_shared(box2, 0.01, color(1,1,1))); return objects; } @@ -2783,7 +2772,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ hittable_list final_scene() { hittable_list boxes1; - auto ground = make_shared(make_shared(0.48, 0.83, 0.53)); + auto ground = make_shared(color(0.48, 0.83, 0.53)); const int boxes_per_side = 20; for (int i = 0; i < boxes_per_side; i++) { @@ -2804,13 +2793,12 @@ objects.add(make_shared(boxes1, 0, 1)); - auto light = make_shared(make_shared(7, 7, 7)); + auto light = make_shared(color(7, 7, 7)); objects.add(make_shared(123, 423, 147, 412, 554, light)); auto center1 = point3(400, 400, 200); auto center2 = center1 + vec3(30,0,0); - auto moving_sphere_material = - make_shared(make_shared(0.7, 0.3, 0.1)); + auto moving_sphere_material = make_shared(color(0.7, 0.3, 0.1)); objects.add(make_shared(center1, center2, 0, 1, 50, moving_sphere_material)); objects.add(make_shared(point3(260, 150, 45), 50, make_shared(1.5))); @@ -2820,12 +2808,9 @@ auto boundary = make_shared(point3(360,150,145), 70, make_shared(1.5)); objects.add(boundary); - objects.add(make_shared( - boundary, 0.2, make_shared(0.2, 0.4, 0.9) - )); + objects.add(make_shared(boundary, 0.2, color(0.2, 0.4, 0.9))); boundary = make_shared(point3(0, 0, 0), 5000, make_shared(1.5)); - objects.add(make_shared( - boundary, .0001, make_shared(1,1,1))); + objects.add(make_shared(boundary, .0001, color(1,1,1))); auto emat = make_shared(make_shared("earthmap.jpg")); objects.add(make_shared(point3(400,200,400), 100, emat)); @@ -2833,7 +2818,7 @@ objects.add(make_shared(point3(220,280,300), 80, make_shared(pertext))); hittable_list boxes2; - auto white = make_shared(make_shared(.73, .73, .73)); + auto white = make_shared(color(.73, .73, .73)); int ns = 1000; for (int j = 0; j < ns; j++) { boxes2.add(make_shared(point3::random(0,165), 10, white)); diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index 737e03b9..dd37cecb 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -758,10 +758,10 @@ hittable_list cornell_box(camera& cam, double aspect) { hittable_list world; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15, 15, 15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15, 15, 15)); world.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); world.add(make_shared(0, 555, 0, 555, 0, red)); @@ -849,6 +849,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ class lambertian : public material { public: + lambertian(const color& a) : albedo(make_shared(a)) {} lambertian(shared_ptr a) : albedo(a) {} @@ -1947,6 +1948,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ class lambertian : public material { public: + lambertian(const color& a) : albedo(make_shared(a)) {} lambertian(shared_ptr a) : albedo(a) {} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight @@ -2108,10 +2110,10 @@ hittable_list cornell_box(camera& cam, double aspect) { hittable_list world; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15, 15, 15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15, 15, 15)); world.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); world.add(make_shared(0, 555, 0, 555, 0, red)); diff --git a/src/TheNextWeek/constant_medium.h b/src/TheNextWeek/constant_medium.h index 903d4b8e..a6f119c5 100644 --- a/src/TheNextWeek/constant_medium.h +++ b/src/TheNextWeek/constant_medium.h @@ -21,10 +21,16 @@ class constant_medium : public hittable { public: constant_medium(shared_ptr b, double d, shared_ptr a) - : boundary(b), neg_inv_density(-1/d) - { - phase_function = make_shared(a); - } + : boundary(b), + neg_inv_density(-1/d), + phase_function(make_shared(a)) + {} + + constant_medium(shared_ptr b, double d, color c) + : boundary(b), + neg_inv_density(-1/d), + phase_function(make_shared(make_shared(c))) + {} virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const; diff --git a/src/TheNextWeek/main.cc b/src/TheNextWeek/main.cc index 90d35163..6600d102 100644 --- a/src/TheNextWeek/main.cc +++ b/src/TheNextWeek/main.cc @@ -50,10 +50,7 @@ color ray_color(const ray& r, const color& background, const hittable& world, in hittable_list random_scene() { hittable_list world; - auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), - make_shared(0.9, 0.9, 0.9) - ); + auto checker = make_shared(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9)); world.add(make_shared(point3(0,-1000,0), 1000, make_shared(checker))); @@ -68,7 +65,7 @@ hittable_list random_scene() { if (choose_mat < 0.8) { // diffuse auto albedo = color::random() * color::random(); - sphere_material = make_shared(make_shared(albedo)); + sphere_material = make_shared(albedo); auto center2 = center + vec3(0, random_double(0,.5), 0); world.add(make_shared( center, center2, 0.0, 1.0, 0.2, sphere_material)); @@ -90,7 +87,7 @@ hittable_list random_scene() { auto material1 = make_shared(1.5); world.add(make_shared(point3(0, 1, 0), 1.0, material1)); - auto material2 = make_shared(make_shared(color(0.4, 0.2, 0.1))); + auto material2 = make_shared(color(0.4, 0.2, 0.1)); world.add(make_shared(point3(-4, 1, 0), 1.0, material2)); auto material3 = make_shared(color(0.7, 0.6, 0.5), 0.0); @@ -103,10 +100,7 @@ hittable_list random_scene() { hittable_list two_spheres() { hittable_list objects; - auto checker = make_shared( - make_shared(0.2, 0.3, 0.1), - make_shared(0.9, 0.9, 0.9) - ); + auto checker = make_shared(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9)); objects.add(make_shared(point3(0,-10, 0), 10, make_shared(checker))); objects.add(make_shared(point3(0, 10, 0), 10, make_shared(checker))); @@ -142,7 +136,7 @@ hittable_list simple_light() { objects.add(make_shared(point3(0,-1000,0), 1000, make_shared(pertext))); objects.add(make_shared(point3(0,2,0), 2, make_shared(pertext))); - auto difflight = make_shared(make_shared(4,4,4)); + auto difflight = make_shared(color(4,4,4)); objects.add(make_shared(point3(0,7,0), 2, difflight)); objects.add(make_shared(3, 5, 1, 3, -2, difflight)); @@ -153,10 +147,10 @@ hittable_list simple_light() { hittable_list cornell_box() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15,15,15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15,15,15)); objects.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -182,10 +176,10 @@ hittable_list cornell_box() { hittable_list cornell_balls() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(5,5,5)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(5, 5, 5)); objects.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -196,7 +190,7 @@ hittable_list cornell_balls() { auto boundary = make_shared(point3(160,100,145), 100, make_shared(1.5)); objects.add(boundary); - objects.add(make_shared(boundary, 0.1, make_shared(1,1,1))); + objects.add(make_shared(boundary, 0.1, color(1,1,1))); shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), white); box1 = make_shared(box1, 15); @@ -210,10 +204,10 @@ hittable_list cornell_balls() { hittable_list cornell_smoke() { hittable_list objects; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(7, 7, 7)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(7, 7, 7)); objects.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -230,8 +224,8 @@ hittable_list cornell_smoke() { box2 = make_shared(box2, -18); box2 = make_shared(box2, vec3(130,0,65)); - objects.add(make_shared(box1, 0.01, make_shared(0,0,0))); - objects.add(make_shared(box2, 0.01, make_shared(1,1,1))); + objects.add(make_shared(box1, 0.01, color(0,0,0))); + objects.add(make_shared(box2, 0.01, color(1,1,1))); return objects; } @@ -244,10 +238,10 @@ hittable_list cornell_final() { auto mat = make_shared(make_shared("earthmap.jpg")); - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(7, 7, 7)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(7, 7, 7)); objects.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); objects.add(make_shared(0, 555, 0, 555, 0, red)); @@ -261,10 +255,8 @@ hittable_list cornell_final() { boundary2 = make_shared(boundary2, -18); boundary2 = make_shared(boundary2, vec3(130,0,65)); - auto tex = make_shared(0.9, 0.9, 0.9); - objects.add(boundary2); - objects.add(make_shared(boundary2, 0.2, tex)); + objects.add(make_shared(boundary2, 0.2, color(0.9, 0.9, 0.9))); return objects; } @@ -272,7 +264,7 @@ hittable_list cornell_final() { hittable_list final_scene() { hittable_list boxes1; - auto ground = make_shared(make_shared(0.48, 0.83, 0.53)); + auto ground = make_shared(color(0.48, 0.83, 0.53)); const int boxes_per_side = 20; for (int i = 0; i < boxes_per_side; i++) { @@ -293,13 +285,12 @@ hittable_list final_scene() { objects.add(make_shared(boxes1, 0, 1)); - auto light = make_shared(make_shared(7, 7, 7)); + auto light = make_shared(color(7, 7, 7)); objects.add(make_shared(123, 423, 147, 412, 554, light)); auto center1 = point3(400, 400, 200); auto center2 = center1 + vec3(30,0,0); - auto moving_sphere_material = - make_shared(make_shared(0.7, 0.3, 0.1)); + auto moving_sphere_material = make_shared(color(0.7, 0.3, 0.1)); objects.add(make_shared(center1, center2, 0, 1, 50, moving_sphere_material)); objects.add(make_shared(point3(260, 150, 45), 50, make_shared(1.5))); @@ -309,11 +300,9 @@ hittable_list final_scene() { auto boundary = make_shared(point3(360,150,145), 70, make_shared(1.5)); objects.add(boundary); - objects.add(make_shared( - boundary, 0.2, make_shared(0.2, 0.4, 0.9) - )); + objects.add(make_shared(boundary, 0.2, color(0.2, 0.4, 0.9))); boundary = make_shared(point3(0,0,0), 5000, make_shared(1.5)); - objects.add(make_shared(boundary, .0001, make_shared(1,1,1))); + objects.add(make_shared(boundary, .0001, color(1,1,1))); auto emat = make_shared(make_shared("earthmap.jpg")); objects.add(make_shared(point3(400,200,400), 100, emat)); @@ -321,7 +310,7 @@ hittable_list final_scene() { objects.add(make_shared(point3(220,280,300), 80, make_shared(pertext))); hittable_list boxes2; - auto white = make_shared(make_shared(.73, .73, .73)); + auto white = make_shared(color(.73, .73, .73)); int ns = 1000; for (int j = 0; j < ns; j++) { boxes2.add(make_shared(point3::random(0,165), 10, white)); diff --git a/src/TheNextWeek/material.h b/src/TheNextWeek/material.h index 3470c759..48f71dde 100644 --- a/src/TheNextWeek/material.h +++ b/src/TheNextWeek/material.h @@ -76,6 +76,7 @@ class dielectric : public material { class diffuse_light : public material { public: diffuse_light(shared_ptr a) : emit(a) {} + diffuse_light(color c) : emit(make_shared(c)) {} virtual bool scatter( const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered @@ -111,6 +112,7 @@ class isotropic : public material { class lambertian : public material { public: + lambertian(const color& a) : albedo(make_shared(a)) {} lambertian(shared_ptr a) : albedo(a) {} virtual bool scatter( diff --git a/src/TheRestOfYourLife/main.cc b/src/TheRestOfYourLife/main.cc index 34e675fe..7f5fe027 100644 --- a/src/TheRestOfYourLife/main.cc +++ b/src/TheRestOfYourLife/main.cc @@ -65,10 +65,10 @@ color ray_color( hittable_list cornell_box(camera& cam, double aspect) { hittable_list world; - auto red = make_shared(make_shared(.65, .05, .05)); - auto white = make_shared(make_shared(.73, .73, .73)); - auto green = make_shared(make_shared(.12, .45, .15)); - auto light = make_shared(make_shared(15, 15, 15)); + auto red = make_shared(color(.65, .05, .05)); + auto white = make_shared(color(.73, .73, .73)); + auto green = make_shared(color(.12, .45, .15)); + auto light = make_shared(color(15, 15, 15)); world.add(make_shared(make_shared(0, 555, 0, 555, 555, green))); world.add(make_shared(0, 555, 0, 555, 0, red)); diff --git a/src/TheRestOfYourLife/material.h b/src/TheRestOfYourLife/material.h index 42a077f9..d48f2281 100644 --- a/src/TheRestOfYourLife/material.h +++ b/src/TheRestOfYourLife/material.h @@ -97,6 +97,7 @@ class dielectric : public material { class diffuse_light : public material { public: diffuse_light(shared_ptr a) : emit(a) {} + diffuse_light(color c) : emit(make_shared(c)) {} virtual color emitted( const ray& r_in, const hit_record& rec, double u, double v, const point3& p @@ -130,6 +131,7 @@ class isotropic : public material { class lambertian : public material { public: + lambertian(const color& a) : albedo(make_shared(a)) {} lambertian(shared_ptr a) : albedo(a) {} virtual bool scatter( diff --git a/src/common/texture.h b/src/common/texture.h index 48c4b027..e4973afd 100644 --- a/src/common/texture.h +++ b/src/common/texture.h @@ -45,7 +45,12 @@ class solid_color : public texture { class checker_texture : public texture { public: checker_texture() {} - checker_texture(shared_ptr t0, shared_ptr t1): even(t0), odd(t1) {} + + checker_texture(shared_ptr t0, shared_ptr t1) + : even(t0), odd(t1) {} + + checker_texture(color c1, color c2) + : even(make_shared(c1)) , odd(make_shared(c2)) {} virtual color value(double u, double v, const vec3& p) const { auto sines = sin(10*p.x())*sin(10*p.y())*sin(10*p.z()); From 87a3b0c8356fd36942b1cf734ec7ca6a57656d5e Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Sun, 17 May 2020 14:58:15 -0700 Subject: [PATCH 2/2] changelog: add new color-parameter constructors --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68496ac6..d52782f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ Change Log -- Ray Tracing in One Weekend # v3.2.0 (in progress) +### Common + - New: Added alternative constructors that take color arguments in addition to the constructors + that take `shared_ptr` arguments, simplifying calling code. This applies to the + following classes: `checker_texture`, `constant_medium`, `diffuse_light`, and `lambertian`. + (#516) + ---------------------------------------------------------------------------------------------------- # v3.1.2 (in progress)