Skip to content

Commit 77471af

Browse files
committed
Updates from book 3 complete progression
Also includes some minor fixes. Note that this shows the current state of some buggy renders, which render the entire scene as black, except for the single square light source. Resolves #988 Resolves #1317
1 parent 1473cba commit 77471af

30 files changed

+251
-135
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 182 additions & 83 deletions
Large diffs are not rendered by default.

images/img-3.06-cornell-ortho.jpg

75.8 KB
Loading
28.5 KB
Loading

images/img-3.08-cornell-lightdown.jpg

24 KB
Loading

images/img-3.09-cornell-cos-pdf.jpg

107 KB
Loading

images/img-3.10-hittable-light.jpg

-26.1 KB
Loading

images/img-3.11-cosine-and-light.jpg

-26.1 KB
Loading

images/img-3.12-arbitrary-pdf.jpg

-37.5 KB
Loading
-112 KB
Loading

images/img-3.14-glass-and-light.jpg

44 KB
Loading

images/img-3.15-book3-final.jpg

44.4 KB
Loading

src/InOneWeekend/color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using color = vec3;
1818

19+
1920
inline double linear_to_gamma(double linear_component)
2021
{
2122
if (linear_component > 0)
@@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
2425
return 0;
2526
}
2627

28+
2729
void write_color(std::ostream& out, const color& pixel_color) {
2830
auto r = pixel_color.x();
2931
auto g = pixel_color.y();

src/InOneWeekend/rtweekend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
using std::make_shared;
2222
using std::shared_ptr;
2323

24+
2425
// Constants
2526

2627
const double infinity = std::numeric_limits<double>::infinity();
2728
const double pi = 3.1415926535897932385;
2829

30+
2931
// Utility Functions
3032

3133
inline double degrees_to_radians(double degrees) {
@@ -42,6 +44,7 @@ inline double random_double(double min, double max) {
4244
return min + (max-min)*random_double();
4345
}
4446

47+
4548
// Common Headers
4649

4750
#include "color.h"

src/TheNextWeek/color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using color = vec3;
1818

19+
1920
inline double linear_to_gamma(double linear_component)
2021
{
2122
if (linear_component > 0)
@@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
2425
return 0;
2526
}
2627

28+
2729
void write_color(std::ostream& out, const color& pixel_color) {
2830
auto r = pixel_color.x();
2931
auto g = pixel_color.y();

src/TheNextWeek/quad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "hittable.h"
1515
#include "hittable_list.h"
1616

17+
1718
class quad : public hittable {
1819
public:
1920
quad(const point3& Q, const vec3& u, const vec3& v, shared_ptr<material> mat)

src/TheNextWeek/rtweekend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
using std::make_shared;
2222
using std::shared_ptr;
2323

24+
2425
// Constants
2526

2627
const double infinity = std::numeric_limits<double>::infinity();
2728
const double pi = 3.1415926535897932385;
2829

30+
2931
// Utility Functions
3032

3133
inline double degrees_to_radians(double degrees) {
@@ -47,6 +49,7 @@ inline int random_int(int min, int max) {
4749
return int(random_double(min, max+1));
4850
}
4951

52+
5053
// Common Headers
5154

5255
#include "color.h"

src/TheRestOfYourLife/camera.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "rtweekend.h"
1515

1616
#include "hittable.h"
17+
#include "pdf.h"
1718
#include "material.h"
1819

1920

@@ -175,12 +176,13 @@ class camera {
175176
mixture_pdf p(light_ptr, srec.pdf_ptr);
176177

177178
ray scattered = ray(rec.p, p.generate(), r.time());
178-
auto pdf_val = p.value(scattered.direction());
179+
auto pdf_value = p.value(scattered.direction());
179180

180181
double scattering_pdf = rec.mat->scattering_pdf(r, rec, scattered);
181182

182183
color sample_color = ray_color(scattered, depth-1, world, lights);
183-
color color_from_scatter = (srec.attenuation * scattering_pdf * sample_color) / pdf_val;
184+
color color_from_scatter =
185+
(srec.attenuation * scattering_pdf * sample_color) / pdf_value;
184186

185187
return color_from_emission + color_from_scatter;
186188
}

src/TheRestOfYourLife/color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using color = vec3;
1818

19+
1920
inline double linear_to_gamma(double linear_component)
2021
{
2122
if (linear_component > 0)
@@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
2425
return 0;
2526
}
2627

28+
2729
void write_color(std::ostream& out, const color& pixel_color) {
2830
auto r = pixel_color.x();
2931
auto g = pixel_color.y();

src/TheRestOfYourLife/cos_cubed.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include <iostream>
1515
#include <iomanip>
16-
#include <math.h>
16+
1717

1818
double f(double r2) {
1919
// auto x = std::cos(2*pi*r1) * 2 * std::sqrt(r2*(1-r2));
@@ -23,10 +23,12 @@ double f(double r2) {
2323
return cos_theta*cos_theta*cos_theta;
2424
}
2525

26+
2627
double pdf() {
2728
return 1.0 / (2.0*pi);
2829
}
2930

31+
3032
int main() {
3133
int N = 1000000;
3234

src/TheRestOfYourLife/cos_density.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313

1414
#include <iostream>
1515
#include <iomanip>
16-
#include <math.h>
16+
1717

1818
double f(const vec3& d) {
1919
auto cos_theta = d.z();
2020
return cos_theta*cos_theta*cos_theta;
2121
}
2222

23+
2324
double pdf(const vec3& d) {
2425
return d.z() / pi;
2526
}
2627

28+
2729
int main() {
2830
int N = 1000000;
2931

src/TheRestOfYourLife/estimate_halfway.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,42 @@
1313
#include <vector>
1414
#include <iostream>
1515
#include <iomanip>
16-
#include <math.h>
17-
#include <cmath>
18-
#include <stdlib.h>
16+
1917

2018
struct sample {
2119
double x;
2220
double p_x;
2321
};
2422

23+
2524
bool compare_by_x(const sample& a, const sample& b) {
2625
return a.x < b.x;
2726
}
2827

28+
2929
int main() {
3030
unsigned int N = 10000;
3131
double sum = 0.0;
3232

33-
// iterate through all of our samples
33+
// Iterate through all of our samples.
34+
3435
std::vector<sample> samples;
3536
for (unsigned int i = 0; i < N; i++) {
36-
// Get the area under the curve
37+
// Get the area under the curve.
3738
auto x = random_double(0, 2*pi);
3839
auto sin_x = std::sin(x);
3940
auto p_x = exp(-x / (2*pi)) * sin_x * sin_x;
4041
sum += p_x;
41-
// store this sample
42+
43+
// Store this sample.
4244
sample this_sample = {x, p_x};
4345
samples.push_back(this_sample);
4446
}
4547

46-
// Sort the samples by x
48+
// Sort the samples by x.
4749
std::sort(samples.begin(), samples.end(), compare_by_x);
4850

49-
// Find out the sample at which we have half of our area
51+
// Find out the sample at which we have half of our area.
5052
double half_sum = sum / 2.0;
5153
double halfway_point = 0.0;
5254
double accum = 0.0;

src/TheRestOfYourLife/integrate_x_sq.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@
1313

1414
#include <iostream>
1515
#include <iomanip>
16-
#include <math.h>
17-
#include <stdlib.h>
16+
1817

1918
double f(double d) {
2019
return 8.0 * std::pow(d, 1.0/3.0);
2120
}
2221

22+
2323
double pdf(double x) {
24-
return (3.0/8.0) * x*x;
24+
return (3.0/8.0) * x*x;
2525
}
2626

27+
2728
int main() {
2829
int N = 1;
29-
3030
auto sum = 0.0;
31+
3132
for (int i = 0; i < N; i++) {
32-
auto x = f(random_double());
33+
auto z = random_double();
34+
if (z == 0.0) // Ignore zero to avoid NaNs
35+
continue;
36+
37+
auto x = f(z);
3338
sum += x*x / pdf(x);
3439
}
3540

3641
std::cout << std::fixed << std::setprecision(12);
37-
std::cout << "I = " << sum / N << '\n';
42+
std::cout << "I = " << (sum / N) << '\n';
3843
}

src/TheRestOfYourLife/main.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "rtweekend.h"
1313

1414
#include "camera.h"
15-
#include "constant_medium.h"
16-
#include "hittable.h"
1715
#include "hittable_list.h"
1816
#include "material.h"
1917
#include "quad.h"

src/TheRestOfYourLife/onb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "rtweekend.h"
1515

16+
1617
class onb {
1718
public:
1819
onb() {}

src/TheRestOfYourLife/pdf.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
#ifndef PDF_H
22
#define PDF_H
3-
//==============================================================================================
4-
// Originally written in 2016 by Peter Shirley <[email protected]>
5-
//
6-
// To the extent possible under law, the author(s) have dedicated all copyright and related and
7-
// neighboring rights to this software to the public domain worldwide. This software is
8-
// distributed without any warranty.
9-
//
10-
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication
11-
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12-
//==============================================================================================
133

144
#include "rtweekend.h"
155

@@ -26,35 +16,35 @@ class pdf {
2616
};
2717

2818

29-
class cosine_pdf : public pdf {
19+
class sphere_pdf : public pdf {
3020
public:
31-
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
21+
sphere_pdf() {}
3222

3323
double value(const vec3& direction) const override {
34-
auto cosine_theta = dot(unit_vector(direction), uvw.w());
35-
return std::fmax(0, cosine_theta/pi);
24+
return 1/ (4 * pi);
3625
}
3726

3827
vec3 generate() const override {
39-
return uvw.local(random_cosine_direction());
28+
return random_unit_vector();
4029
}
41-
42-
private:
43-
onb uvw;
4430
};
4531

4632

47-
class sphere_pdf : public pdf {
33+
class cosine_pdf : public pdf {
4834
public:
49-
sphere_pdf() {}
35+
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
5036

5137
double value(const vec3& direction) const override {
52-
return 1/ (4 * pi);
38+
auto cosine_theta = dot(unit_vector(direction), uvw.w());
39+
return std::fmax(0, cosine_theta/pi);
5340
}
5441

5542
vec3 generate() const override {
56-
return random_unit_vector();
43+
return uvw.local(random_cosine_direction());
5744
}
45+
46+
private:
47+
onb uvw;
5848
};
5949

6050

src/TheRestOfYourLife/pi.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
#include <iostream>
1515
#include <iomanip>
16-
#include <stdlib.h>
1716

1817

1918
int main() {
19+
std::cout << std::fixed << std::setprecision(12);
20+
2021
int inside_circle = 0;
2122
int inside_circle_stratified = 0;
2223
int sqrt_N = 1000;
@@ -25,21 +26,19 @@ int main() {
2526
for (int j = 0; j < sqrt_N; j++) {
2627
auto x = random_double(-1,1);
2728
auto y = random_double(-1,1);
28-
2929
if (x*x + y*y < 1)
3030
inside_circle++;
3131

3232
x = 2*((i + random_double()) / sqrt_N) - 1;
3333
y = 2*((j + random_double()) / sqrt_N) - 1;
34-
3534
if (x*x + y*y < 1)
3635
inside_circle_stratified++;
3736
}
3837
}
3938

40-
std::cout << std::fixed << std::setprecision(12);
41-
std::cout << "Regular Estimate of Pi = "
42-
<< (4.0 * inside_circle) / (sqrt_N*sqrt_N) << '\n';
43-
std::cout << "Stratified Estimate of Pi = "
39+
std::cout
40+
<< "Regular Estimate of Pi = "
41+
<< (4.0 * inside_circle) / (sqrt_N*sqrt_N) << '\n'
42+
<< "Stratified Estimate of Pi = "
4443
<< (4.0 * inside_circle_stratified) / (sqrt_N*sqrt_N) << '\n';
4544
}

src/TheRestOfYourLife/quad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "hittable.h"
1515
#include "hittable_list.h"
1616

17+
1718
class quad : public hittable {
1819
public:
1920
quad(const point3& Q, const vec3& u, const vec3& v, shared_ptr<material> mat)

0 commit comments

Comments
 (0)