8000 Added isotropic changes for book 3. Made some changes to prior books. by trevordblack · Pull Request #885 · RayTracing/raytracing.github.io · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added isotropic changes for book 3. Made some changes to prior books. #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Change Log -- Ray Tracing in One Weekend
- Change: general rename of `mat_ptr` to `mat` (material)
- Change: hittable::bounding_box() signature has changed to always return a value (#859)
- Fix: Enabled compiler warnings for MSVC, Clang, GNU. Cleaned up warnings as fit (#865)
- Change: replaced random vector in `isotropic` with `random_unit_vector`

### In One Weekend
- Added: More commentary about the choice between `double` and `float` (#752)
Expand All @@ -35,7 +36,9 @@ Change Log -- Ray Tracing in One Weekend
- Fix: Fixed `bvh_node` constructor definition signature (#872)

### The Rest of Your Life
- Fix: Added missing functionality for `isotropic` (#664)
- Fix: Variable `direction` was used without being defined in listing 11 (#831)
- Fix: Added missing functionality for `isotropic` (#664)


----------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -3145,7 +3145,7 @@

bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)
const override {
scattered = ray(rec.p, random_in_unit_sphere(), r_in.time());
scattered = ray(rec.p, random_unit_vector(), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
return true;
}
Expand Down
99 changes: 96 additions & 3 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,7 @@
class lambertian : public material {
public:
...

bool scatter(
const ray& r_in, const hit_record& rec, color& alb, ray& scattered, double& pdf
) const override {
Expand All @@ -2158,6 +2159,9 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
return true;
}

...
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [scatter-onb]: <kbd>[material.h]</kbd> Scatter function, with orthonormal basis]
</div>
Expand All @@ -2171,6 +2175,44 @@
Let’s get rid of some of that noise.
</div>

<div class='together'>
But first, let's quickly update the `isotropic` material:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class isotropic : public material {
public:
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
isotropic(shared_ptr<texture> a) : albedo(a) {}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
bool scatter(
const ray& r_in, const hit_record& rec, color& alb, ray& scattered, double& pdf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
) const override {
scattered = ray(rec.p, random_unit_vector(), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
pdf = 1 / (4 * pi);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
return true;
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered)
const override {
return 1 / (4 * pi);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

public:
shared_ptr<texture> albedo;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [class-isotropic-impsample]: <kbd>[material.h]</kbd>
Isotropic material, modified for importance sampling]
</div>


Sampling Lights Directly
Expand Down Expand Up @@ -2438,8 +2480,26 @@
</div>

<div class='together'>
We’ll see if we need to add anything else to `pdf` by fleshing out the subclasses. First, let’s try
a cosine density:
We’ll see if we need to add anything else to `pdf` by fleshing out the subclasses. First, we'll
create a uniform density over the unit sphere:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class sphere_pdf : public pdf {
public:
sphere_pdf() { }

double value(const vec3& direction) const override {
return 1/ (4 * pi);
}

vec3 generate() const override {
return random_unit_vector();
}
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [class-uni-pdf]: <kbd>[pdf.h]</kbd> The uniform_pdf class]

Next, let’s try a cosine density:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class cosine_pdf : public pdf {
Expand Down Expand Up @@ -2903,14 +2963,15 @@
</div>

<div class='together'>
The Lambertian material becomes simpler:
The `lambertian` material becomes simpler:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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
bool scatter(const ray& r_in, const hit_record& rec, scatter_record& srec) const override {
srec.attenuation = albedo->value(rec.u, rec.v, rec.p);
Expand All @@ -2932,6 +2993,38 @@
[Listing [lambertian-scatter]: <kbd>[material.h]</kbd> New lambertian scatter() method]
</div>

<div class='together'>
As does the `isotropic` material:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class isotropic : public material {
public:
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
isotropic(shared_ptr<texture> a) : albedo(a) {}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
bool scatter(const ray& r_in, const hit_record& rec, scatter_record& srec) const override {
srec.attenuation = albedo->value(rec.u, rec.v, rec.p);
srec.pdf_ptr = make_shared<sphere_pdf>();
srec.skip_pdf = false;
return true;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++


double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered)
const override {
return 1 / (4 * pi);
}

public:
shared_ptr<texture> albedo;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [isotropic-scatter]: <kbd>[material.h]</kbd> New isotropic scatter() method]
</div>

<div class='together'>
And `ray_color()` changes are small:

Expand Down
2 changes: 1 addition & 1 deletion src/TheNextWeek/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class isotropic : public material {

bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)
const override {
scattered = ray(rec.p, random_in_unit_sphere(), r_in.time());
scattered = ray(rec.p, random_unit_vector(), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
return true;
}
Expand Down
87 changes: 87 additions & 0 deletions src/TheRestOfYourLife/constant_medium.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#ifndef CONSTANT_MEDIUM_H
#define CONSTANT_MEDIUM_H
//==============================================================================================
// Originally written in 2016 by Peter Shirley <ptrshrl@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==============================================================================================

#include "rtweekend.h"

#include "hittable.h"
#include "material.h"
#include "texture.h"


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))
{}

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

bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
// Print occasional samples when debugging. To enable, set enableDebug true.
const bool enableDebug = false;
const bool debugging = enableDebug && random_double() < 0.00001;

hit_record rec1, rec2;

if (!boundary->hit(r, interval::universe, rec1))
return false;

if (!boundary->hit(r, interval(rec1.t+0.0001, infinity), rec2))
return false;

if (debugging) std::cerr << "\nt_min=" << rec1.t << ", t_max=" << rec2.t << '\n';

if (rec1.t < ray_t.min) rec1.t = ray_t.min;
if (rec2.t > ray_t.max) rec2.t = ray_t.max;

if (rec1.t >= rec2.t)
return false;

if (rec1.t < 0)
rec1.t = 0;

const auto ray_length = r.direction().length();
const auto distance_inside_boundary = (rec2.t - rec1.t) * ray_length;
const auto hit_distance = neg_inv_density * log(random_double());

if (hit_distance > distance_inside_boundary)
return false;

rec.t = rec1.t + hit_distance / ray_length;
rec.p = r.at(rec.t);

if (debugging) {
std::cerr << "hit_distance = " << hit_distance << '\n'
<< "rec.t = " << rec.t << '\n'
<< "rec.p = " << rec.p << '\n';
}

rec.normal = vec3(1,0,0); // arbitrary
rec.front_face = true; // also arbitrary
rec.mat = phase_function;

return true;
}

aabb bounding_box() const override { return boundary->bounding_box(); }

public:
shared_ptr<hittable> boundary;
double neg_inv_density;
shared_ptr<material> phase_function;
};


#endif
43 changes: 43 additions & 0 deletions src/TheRestOfYourLife/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "box.h"
#include "camera.h"
#include "color.h"
#include "constant_medium.h"
#include "hittable_list.h"
#include "material.h"
#include "scene.h"
Expand Down Expand Up @@ -63,6 +64,48 @@ void cornell_box(scene& scene_desc) {
lights.add(make_shared<sphere>(point3(190, 90, 190), 90, shared_ptr<material>()));
}

void cornell_smoke(scene& scene_desc) {
scene_desc.image_width = 600;
scene_desc.aspect_ratio = 1.0;
scene_desc.samples_per_pixel = 100;
scene_desc.max_depth = 50;
scene_desc.background = color(0,0,0);

scene_desc.cam.lookfrom = point3(278, 278, -800);
scene_desc.cam.lookat = point3(278, 278, 0);
scene_desc.cam.vup = vec3(0, 1, 0);
scene_desc.cam.vfov = 40.0;
scene_desc.cam.aperture = 0.0;
scene_desc.cam.focus_dist = 10.0;

hittable_list& world = scene_desc.world;

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));
world.add(make_shared<flip_face>(make_shared<xz_rect>(213, 343, 227, 332, 554, light)));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
world.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
world.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));

shared_ptr<hittable> box1 = make_shared<box>(point3(0,0,0), point3(165,330,165), white);
box1 = make_shared<rotate_y>(box1, 15);
box1 = make_shared<translate>(box1, vec3(265,0,295));

shared_ptr<hittable> box2 = make_shared<box>(point3(0,0,0), point3(165,165,165), white);
box2 = make_shared<rotate_y>(box2, -18);
box2 = make_shared<translate>(box2, vec3(130,0,65));

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

hittable_list& lights = scene_desc.lights;
lights.add(make_shared<xz_rect>(213, 343, 227, 332, 554, shared_ptr<material>()));
}

int main() {
scene scene_desc;
Expand Down
17 changes: 8 additions & 9 deletions src/TheRestOfYourLife/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,17 @@ class isotropic : public material {
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
isotropic(shared_ptr<texture> a) : albedo(a) {}

#if 0
// Issue #669
// This method doesn't match the signature in the base `material` class, so this one's
// never actually called. Disabling this definition until we sort this out.
bool scatter(const ray& r_in, const hit_record& rec, scatter_record& srec) const override {
srec.attenuation = albedo->value(rec.u, rec.v, rec.p);
srec.pdf_ptr = make_shared<sphere_pdf>();
srec.skip_pdf = false;
return true;
}

bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)
double scattering_pdf(const ray& r_in, const hit_record& rec, const ray& scattered)
const override {
scattered = ray(rec.p, random_in_unit_sphere(), r_in.time());
attenuation = albedo->value(rec.u, rec.v, rec.p);
return true;
return 1 / (4 * pi);
}
#endif

public:
shared_ptr<texture> albedo;
Expand Down
14 changes: 14 additions & 0 deletions src/TheRestOfYourLife/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ class cosine_pdf : public pdf {
};


class sphere_pdf : public pdf {
public:
sphere_pdf() { }

double value(const vec3& direction) const override {
return 1/ (4 * pi);
}

vec3 generate() const override {
return random_unit_vector();
}
};


class hittable_pdf : public pdf {
public:
hittable_pdf(const hittable_list& _objects, const point3& _origin)
Expand Down
0