Skip to content

Commit 07eaa1f

Browse files
committed
Merge branch 'dev-minor' into remove-flip
2 parents ffee5aa + f0a3002 commit 07eaa1f

File tree

9 files changed

+106
-111
lines changed

9 files changed

+106
-111
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Change Log -- Ray Tracing in One Weekend
1111
- Removed: Deleted the section covering the old `flip_face` class, renumbered images as this
1212
eliminated the rendering with missing Cornell box faces (#482)
1313
- Change: Renamed and explicitly numbered book images and figures (#495)
14+
- New: Added alternative constructors that take color arguments in addition to the constructors
15+
that take `shared_ptr<texture>` arguments, simplifying calling code. This applies to the
16+
following classes: `checker_texture`, `constant_medium`, `diffuse_light`, and `lambertian`.
17+
(#516)
1418

1519

1620
----------------------------------------------------------------------------------------------------

books/RayTracingTheNextWeek.html

Lines changed: 37 additions & 52 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));
@@ -2540,10 +2523,16 @@
25402523
class constant_medium : public hittable {
25412524
public:
25422525
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
2543-
: boundary(b), neg_inv_density(-1/d)
2544-
{
2545-
phase_function = make_shared<isotropic>(a);
2546-
}
2526+
: boundary(b),
2527+
neg_inv_density(-1/d),
2528+
phase_function(make_shared<isotropic>(a))
2529+
{}
2530+
2531+
constant_medium(shared_ptr<hittable> b, double d, color c)
2532+
: boundary(b),
2533+
neg_inv_density(-1/d),
2534+
phase_function(make_shared<isotropic>(color(c)))
2535+
{}
25472536

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

@@ -2658,10 +2647,10 @@
26582647
hittable_list cornell_smoke() {
26592648
hittable_list objects;
26602649

2661-
auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
2662-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2663-
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
2664-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
2650+
auto red = make_shared<lambertian>(color(.65, .05, .05));
2651+
auto white = make_shared<lambertian>(color(.73, .73, .73));
2652+
auto green = make_shared<lambertian>(color(.12, .45, .15));
2653+
auto light = make_shared<diffuse_light>(color(7, 7, 7));
26652654

26662655
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
26672656
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
@@ -2678,8 +2667,8 @@
26782667
box2 = make_shared<rotate_y>(box2, -18);
26792668
box2 = make_shared<translate>(box2, vec3(130,0,65));
26802669

2681-
objects.add(make_shared<constant_medium>(box1, 0.01, make_shared<solid_color>(0,0,0)));
2682-
objects.add(make_shared<constant_medium>(box2, 0.01, make_shared<solid_color>(1,1,1)));
2670+
objects.add(make_shared<constant_medium>(box1, 0.01, color(0,0,0)));
2671+
objects.add(make_shared<constant_medium>(box2, 0.01, color(1,1,1)));
26832672

26842673
return objects;
26852674
}
@@ -2707,7 +2696,7 @@
27072696
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
27082697
hittable_list final_scene() {
27092698
hittable_list boxes1;
2710-
auto ground = make_shared<lambertian>(make_shared<solid_color>(0.48, 0.83, 0.53));
2699+
auto ground = make_shared<lambertian>(color(0.48, 0.83, 0.53));
27112700

27122701
const int boxes_per_side = 20;
27132702
for (int i = 0; i < boxes_per_side; i++) {
@@ -2728,13 +2717,12 @@
27282717

27292718
objects.add(make_shared<bvh_node>(boxes1, 0, 1));
27302719

2731-
auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
2720+
auto light = make_shared<diffuse_light>(color(7, 7, 7));
27322721
objects.add(make_shared<xz_rect>(123, 423, 147, 412, 554, light));
27332722

27342723
auto center1 = point3(400, 400, 200);
27352724
auto center2 = center1 + vec3(30,0,0);
2736-
auto moving_sphere_material =
2737-
make_shared<lambertian>(make_shared<solid_color>(0.7, 0.3, 0.1));
2725+
auto moving_sphere_material = make_shared<lambertian>(color(0.7, 0.3, 0.1));
27382726
objects.add(make_shared<moving_sphere>(center1, center2, 0, 1, 50, moving_sphere_material));
27392727

27402728
objects.add(make_shared<sphere>(point3(260, 150, 45), 50, make_shared<dielectric>(1.5)));
@@ -2744,20 +2732,17 @@
27442732

27452733
auto boundary = make_shared<sphere>(point3(360,150,145), 70, make_shared<dielectric>(1.5));
27462734
objects.add(boundary);
2747-
objects.add(make_shared<constant_medium>(
2748-
boundary, 0.2, make_shared<solid_color>(0.2, 0.4, 0.9)
2749-
));
2735+
objects.add(make_shared<constant_medium>(boundary, 0.2, color(0.2, 0.4, 0.9)));
27502736
boundary = make_shared<sphere>(point3(0, 0, 0), 5000, make_shared<dielectric>(1.5));
2751-
objects.add(make_shared<constant_medium>(
2752-
boundary, .0001, make_shared<solid_color>(1,1,1)));
2737+
objects.add(make_shared<constant_medium>(boundary, .0001, color(1,1,1)));
27532738

27542739
auto emat = make_shared<lambertian>(make_shared<image_texture>("earthmap.jpg"));
27552740
objects.add(make_shared<sphere>(point3(400,200,400), 100, emat));
27562741
auto pertext = make_shared<noise_texture>(0.1);
27572742
objects.add(make_shared<sphere>(point3(220,280,300), 80, make_shared<lambertian>(pertext)));
27582743

27592744
hittable_list boxes2;
2760-
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
2745+
auto white = make_shared<lambertian>(color(.73, .73, .73));
27612746
int ns = 1000;
27622747
for (int j = 0; j < ns; j++) {
27632748
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<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<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)