Skip to content

Commit

Permalink
Updates from book 3 complete progression
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hollasch committed Apr 23, 2024
1 parent 8539118 commit 07b339c
Show file tree
Hide file tree
Showing 30 changed files with 251 additions and 135 deletions.
265 changes: 182 additions & 83 deletions books/RayTracingTheRestOfYourLife.html

Large diffs are not rendered by default.

Binary file modified images/img-3.06-cornell-ortho.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.07-cornell-sample-light.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.08-cornell-lightdown.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.09-cornell-cos-pdf.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.10-hittable-light.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.11-cosine-and-light.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.12-arbitrary-pdf.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.13-cornell-glass-sphere.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.14-glass-and-light.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/img-3.15-book3-final.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/InOneWeekend/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using color = vec3;


inline double linear_to_gamma(double linear_component)
{
if (linear_component > 0)
Expand All @@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
return 0;
}


void write_color(std::ostream& out, const color& pixel_color) {
auto r = pixel_color.x();
auto g = pixel_color.y();
Expand Down
3 changes: 3 additions & 0 deletions src/InOneWeekend/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
using std::make_shared;
using std::shared_ptr;


// Constants

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


// Utility Functions

inline double degrees_to_radians(double degrees) {
Expand All @@ -42,6 +44,7 @@ inline double random_double(double min, double max) {
return min + (max-min)*random_double();
}


// Common Headers

#include "color.h"
Expand Down
2 changes: 2 additions & 0 deletions src/TheNextWeek/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using color = vec3;


inline double linear_to_gamma(double linear_component)
{
if (linear_component > 0)
Expand All @@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
return 0;
}


void write_color(std::ostream& out, const color& pixel_color) {
auto r = pixel_color.x();
auto g = pixel_color.y();
Expand Down
1 change: 1 addition & 0 deletions src/TheNextWeek/quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "hittable.h"
#include "hittable_list.h"


class quad : public hittable {
public:
quad(const point3& Q, const vec3& u, const vec3& v, shared_ptr<material> mat)
Expand Down
3 changes: 3 additions & 0 deletions src/TheNextWeek/rtweekend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
using std::make_shared;
using std::shared_ptr;


// Constants

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


// Utility Functions

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


// Common Headers

#include "color.h"
Expand Down
6 changes: 4 additions & 2 deletions src/TheRestOfYourLife/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "rtweekend.h"

#include "hittable.h"
#include "pdf.h"
#include "material.h"


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

ray scattered = ray(rec.p, p.generate(), r.time());
auto pdf_val = p.value(scattered.direction());
auto pdf_value = p.value(scattered.direction());

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

color sample_color = ray_color(scattered, depth-1, world, lights);
color color_from_scatter = (srec.attenuation * scattering_pdf * sample_color) / pdf_val;
color color_from_scatter =
(srec.attenuation * scattering_pdf * sample_color) / pdf_value;

return color_from_emission + color_from_scatter;
}
Expand Down
2 changes: 2 additions & 0 deletions src/TheRestOfYourLife/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using color = vec3;


inline double linear_to_gamma(double linear_component)
{
if (linear_component > 0)
Expand All @@ -24,6 +25,7 @@ inline double linear_to_gamma(double linear_component)
return 0;
}


void write_color(std::ostream& out, const color& pixel_color) {
auto r = pixel_color.x();
auto g = pixel_color.y();
Expand Down
4 changes: 3 additions & 1 deletion src/TheRestOfYourLife/cos_cubed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <iostream>
#include <iomanip>
#include <math.h>


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


double pdf() {
return 1.0 / (2.0*pi);
}


int main() {
int N = 1000000;

Expand Down
4 changes: 3 additions & 1 deletion src/TheRestOfYourLife/cos_density.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

#include <iostream>
#include <iomanip>
#include <math.h>


double f(const vec3& d) {
auto cos_theta = d.z();
return cos_theta*cos_theta*cos_theta;
}


double pdf(const vec3& d) {
return d.z() / pi;
}


int main() {
int N = 1000000;

Expand Down
18 changes: 10 additions & 8 deletions src/TheRestOfYourLife/estimate_halfway.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,42 @@
#include <vector>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <stdlib.h>


struct sample {
double x;
double p_x;
};


bool compare_by_x(const sample& a, const sample& b) {
return a.x < b.x;
}


int main() {
unsigned int N = 10000;
double sum = 0.0;

// iterate through all of our samples
// Iterate through all of our samples.

std::vector<sample> samples;
for (unsigned int i = 0; i < N; i++) {
// Get the area under the curve
// Get the area under the curve.
auto x = random_double(0, 2*pi);
auto sin_x = std::sin(x);
auto p_x = exp(-x / (2*pi)) * sin_x * sin_x;
sum += p_x;
// store this sample

// Store this sample.
sample this_sample = {x, p_x};
samples.push_back(this_sample);
}

// Sort the samples by x
// Sort the samples by x.
std::sort(samples.begin(), samples.end(), compare_by_x);

// Find out the sample at which we have half of our area
// Find out the sample at which we have half of our area.
double half_sum = sum / 2.0;
double halfway_point = 0.0;
double accum = 0.0;
Expand Down
17 changes: 11 additions & 6 deletions src/TheRestOfYourLife/integrate_x_sq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@

#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>


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


double pdf(double x) {
return (3.0/8.0) * x*x;
return (3.0/8.0) * x*x;
}


int main() {
int N = 1;

auto sum = 0.0;

for (int i = 0; i < N; i++) {
auto x = f(random_double());
auto z = random_double();
if (z == 0.0) // Ignore zero to avoid NaNs
continue;

auto x = f(z);
sum += x*x / pdf(x);
}

std::cout << std::fixed << std::setprecision(12);
std::cout << "I = " << sum / N << '\n';
std::cout << "I = " << (sum / N) << '\n';
}
2 changes: 0 additions & 2 deletions src/TheRestOfYourLife/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "rtweekend.h"

#include "camera.h"
#include "constant_medium.h"
#include "hittable.h"
#include "hittable_list.h"
#include "material.h"
#include "quad.h"
Expand Down
1 change: 1 addition & 0 deletions src/TheRestOfYourLife/onb.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "rtweekend.h"


class onb {
public:
onb() {}
Expand Down
34 changes: 12 additions & 22 deletions src/TheRestOfYourLife/pdf.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
#ifndef PDF_H
#define PDF_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"

Expand All @@ -26,35 +16,35 @@ class pdf {
};


class cosine_pdf : public pdf {
class sphere_pdf : public pdf {
public:
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
sphere_pdf() {}

double value(const vec3& direction) const override {
auto cosine_theta = dot(unit_vector(direction), uvw.w());
return std::fmax(0, cosine_theta/pi);
return 1/ (4 * pi);
}

vec3 generate() const override {
return uvw.local(random_cosine_direction());
return random_unit_vector();
}

private:
onb uvw;
};


class sphere_pdf : public pdf {
class cosine_pdf : public pdf {
public:
sphere_pdf() {}
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }

double value(const vec3& direction) const override {
return 1/ (4 * pi);
auto cosine_theta = dot(unit_vector(direction), uvw.w());
return std::fmax(0, cosine_theta/pi);
}

vec3 generate() const override {
return random_unit_vector();
return uvw.local(random_cosine_direction());
}

private:
onb uvw;
};


Expand Down
13 changes: 6 additions & 7 deletions src/TheRestOfYourLife/pi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

#include <iostream>
#include <iomanip>
#include <stdlib.h>


int main() {
std::cout << std::fixed << std::setprecision(12);

int inside_circle = 0;
int inside_circle_stratified = 0;
int sqrt_N = 1000;
Expand All @@ -25,21 +26,19 @@ int main() {
for (int j = 0; j < sqrt_N; j++) {
auto x = random_double(-1,1);
auto y = random_double(-1,1);

if (x*x + y*y < 1)
inside_circle++;

x = 2*((i + random_double()) / sqrt_N) - 1;
y = 2*((j + random_double()) / sqrt_N) - 1;

if (x*x + y*y < 1)
inside_circle_stratified++;
}
}

std::cout << std::fixed << std::setprecision(12);
std::cout << "Regular Estimate of Pi = "
<< (4.0 * inside_circle) / (sqrt_N*sqrt_N) << '\n';
std::cout << "Stratified Estimate of Pi = "
std::cout
<< "Regular Estimate of Pi = "
<< (4.0 * inside_circle) / (sqrt_N*sqrt_N) << '\n'
<< "Stratified Estimate of Pi = "
<< (4.0 * inside_circle_stratified) / (sqrt_N*sqrt_N) << '\n';
}
1 change: 1 addition & 0 deletions src/TheRestOfYourLife/quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "hittable.h"
#include "hittable_list.h"


class quad : public hittable {
public:
quad(const point3& Q, const vec3& u, const vec3& v, shared_ptr<material> mat)
Expand Down
Loading

0 comments on commit 07b339c

Please sign in to comment.