Skip to content

Commit

Permalink
Merge branch 'dev-minor' into remove-flip
Browse files Browse the repository at this point in the history
  • Loading branch information
hollasch committed May 20, 2020
2 parents ffee5aa + f0a3002 commit 07eaa1f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 111 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Change Log -- Ray Tracing in One Weekend
- Removed: Deleted the section covering the old `flip_face` class, renumbered images as this
eliminated the rendering with missing Cornell box faces (#482)
- Change: Renamed and explicitly numbered book images and figures (#495)
- New: Added alternative constructors that take color arguments in addition to the constructors
that take `shared_ptr<texture>` arguments, simplifying calling code. This applies to the
following classes: `checker_texture`, `constant_medium`, `diffuse_light`, and `lambertian`.
(#516)


----------------------------------------------------------------------------------------------------
Expand Down
89 changes: 37 additions & 52 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@
class lambertian : public material {
public:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
lambertian(shared_ptr<texture> a) : albedo(a) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

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

<div class='together'>
Where you used to have code like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
...make_shared<lambertian>(color(0.5, 0.5, 0.5))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [lam-solid]: <kbd>[main.cc]</kbd> Lambertian material with solid color]

now you should replace the `color(...)` with `make_shared<solid_color>(...)`

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
...make_shared<lambertian>(make_shared<solid_color>(0.5, 0.5, 0.5))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [lam-textured]: <kbd>[main.cc]</kbd> Lambertian material with texture]

Update all three occurrences of lambertian in the `random_scene()` function in `main.cc`.
</div>


A Checker Texture
------------------
Expand All @@ -1094,7 +1077,12 @@
class checker_texture : public texture {
public:
checker_texture() {}
checker_texture(shared_ptr<texture> t0, shared_ptr<texture> t1): even(t0), odd(t1) {}

checker_texture(shared_ptr<texture> t0, shared_ptr<texture> t1)
: even(t0), odd(t1) {}

checker_texture(color c1, color c2)
: even(make_shared<solid_color>(c1)) , odd(make_shared<solid_color>(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());
Expand All @@ -1119,10 +1107,7 @@
If we add this to our `random_scene()` function’s base sphere:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
auto checker = make_shared<checker_texture>(
make_shared<solid_color>(0.2, 0.3, 0.1),
make_shared<solid_color>(0.9, 0.9, 0.9)
);
auto checker = make_shared<checker_texture>(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9));

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

auto checker = make_shared<checker_texture>(
make_shared<solid_color>(0.2, 0.3, 0.1),
make_shared<solid_color>(0.9, 0.9, 0.9)
);
auto checker = make_shared<checker_texture>(color(0.2, 0.3, 0.1), color(0.9, 0.9, 0.9));

objects.add(make_shared<sphere>(point3(0,-10, 0), 10, make_shared<lambertian>(checker)));
objects.add(make_shared<sphere>(point3(0, 10, 0), 10, make_shared<lambertian>(checker)));
Expand Down Expand Up @@ -1843,6 +1825,7 @@
class diffuse_light : public material {
public:
diffuse_light(shared_ptr<texture> a) : emit(a) {}
diffuse_light(color c) : emit(make_shared<solid_color>(c)) {}

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

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

Expand Down Expand Up @@ -2163,10 +2146,10 @@
hittable_list cornell_box() {
hittable_list objects;

auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
Expand Down Expand Up @@ -2540,10 +2523,16 @@
class constant_medium : public hittable {
public:
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
: boundary(b), neg_inv_density(-1/d)
{
phase_function = make_shared<isotropic>(a);
}
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(a))
{}

constant_medium(shared_ptr<hittable> b, double d, color c)
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(color(c)))
{}

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

Expand Down Expand Up @@ -2658,10 +2647,10 @@
hittable_list cornell_smoke() {
hittable_list objects;

auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(7, 7, 7));

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

objects.add(make_shared<constant_medium>(box1, 0.01, make_shared<solid_color>(0,0,0)));
objects.add(make_shared<constant_medium>(box2, 0.01, make_shared<solid_color>(1,1,1)));
objects.add(make_shared<constant_medium>(box1, 0.01, color(0,0,0)));
objects.add(make_shared<constant_medium>(box2, 0.01, color(1,1,1)));

return objects;
}
Expand Down Expand Up @@ -2707,7 +2696,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
hittable_list final_scene() {
hittable_list boxes1;
auto ground = make_shared<lambertian>(make_shared<solid_color>(0.48, 0.83, 0.53));
auto ground = make_shared<lambertian>(color(0.48, 0.83, 0.53));

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

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

auto light = make_shared<diffuse_light>(make_shared<solid_color>(7, 7, 7));
auto light = make_shared<diffuse_light>(color(7, 7, 7));
objects.add(make_shared<xz_rect>(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<lambertian>(make_shared<solid_color>(0.7, 0.3, 0.1));
auto moving_sphere_material = make_shared<lambertian>(color(0.7, 0.3, 0.1));
objects.add(make_shared<moving_sphere>(center1, center2, 0, 1, 50, moving_sphere_material));

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

auto boundary = make_shared<sphere>(point3(360,150,145), 70, make_shared<dielectric>(1.5));
objects.add(boundary);
objects.add(make_shared<constant_medium>(
boundary, 0.2, make_shared<solid_color>(0.2, 0.4, 0.9)
));
objects.add(make_shared<constant_medium>(boundary, 0.2, color(0.2, 0.4, 0.9)));
boundary = make_shared<sphere>(point3(0, 0, 0), 5000, make_shared<dielectric>(1.5));
objects.add(make_shared<constant_medium>(
boundary, .0001, make_shared<solid_color>(1,1,1)));
objects.add(make_shared<constant_medium>(boundary, .0001, color(1,1,1)));

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

hittable_list boxes2;
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
auto white = make_shared<lambertian>(color(.73, .73, .73));
int ns = 1000;
for (int j = 0; j < ns; j++) {
boxes2.add(make_shared<sphere>(point3::random(0,165), 10, white));
Expand Down
18 changes: 10 additions & 8 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@
hittable_list cornell_box(camera& cam, double aspect) {
hittable_list world;

auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
Expand Down Expand Up @@ -849,6 +849,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class lambertian : public material {
public:
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
lambertian(shared_ptr<texture> a) : albedo(a) {}


Expand Down Expand Up @@ -1947,6 +1948,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class lambertian : public material {
public:
lambertian(const color& a) : albedo(make_shared<solid_color>(a)) {}
lambertian(shared_ptr<texture> a) : albedo(a) {}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand Down Expand Up @@ -2108,10 +2110,10 @@
hittable_list cornell_box(camera& cam, double aspect) {
hittable_list world;

auto red = make_shared<lambertian>(make_shared<solid_color>(.65, .05, .05));
auto white = make_shared<lambertian>(make_shared<solid_color>(.73, .73, .73));
auto green = make_shared<lambertian>(make_shared<solid_color>(.12, .45, .15));
auto light = make_shared<diffuse_light>(make_shared<solid_color>(15, 15, 15));
auto red = make_shared<lambertian>(color(.65, .05, .05));
auto white = make_shared<lambertian>(color(.73, .73, .73));
auto green = make_shared<lambertian>(color(.12, .45, .15));
auto light = make_shared<diffuse_light>(color(15, 15, 15));

world.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
world.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
Expand Down
14 changes: 10 additions & 4 deletions src/TheNextWeek/constant_medium.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
class constant_medium : public hittable {
public:
constant_medium(shared_ptr<hittable> b, double d, shared_ptr<texture> a)
: boundary(b), neg_inv_density(-1/d)
{
phase_function = make_shared<isotropic>(a);
}
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(a))
{}

constant_medium(shared_ptr<hittable> b, double d, color c)
: boundary(b),
neg_inv_density(-1/d),
phase_function(make_shared<isotropic>(make_shared<solid_color>(c)))
{}

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

Expand Down
Loading

0 comments on commit 07eaa1f

Please sign in to comment.