2021-05-13

Ray Tracing: The Rest of Your Life 

Peter Shirley

edited by Steve Hollasch and Trevor David Black

Version 3.2.3, 2020-12-07 

Copyright 2018-2020 Peter Shirley. All rights reserved. 

Contents

1 Overview

2 A Simple Monte Carlo Program

2.1 Estimating Pi

2.2 Showing Convergence

2.3 Stratified Samples (Jittering)

3 One Dimensional MC Integration

3.1 Integrating x²

3.2 Density Functions

3.3 Constructing a PDF

3.4 Importance Sampling

4 MC Integration on the Sphere of Directions

5 Light Scattering

5.1 Albedo

5.2 Scattering

5.3 The Scattering PDF

6 Importance Sampling Materials

6.1 Returning to the Cornell Box

6.2 Random Hemisphere Sampling

7 Generating Random Directions

7.1 Random Directions Relative to the Z Axis

7.2 Uniform Sampling a Hemisphere

8 Orthonormal Bases

8.1 Relative Coordinates

8.2 Generating an Orthonormal Basis

8.3 The ONB Class

9 Sampling Lights Directly

9.1 Getting the PDF of a Light

9.2 Light Sampling

9.3 Switching to Unidirectional Light

10 Mixture Densities

10.1 An Average of Lighting and Reflection

10.2 Sampling Directions towards a Hittable

10.3 The Mixture PDF Class

11 Some Architectural Decisions

12 Cleaning Up PDF Management

12.1 Diffuse Versus Specular

12.2 Handling Specular

12.3 Sampling a Sphere Object

12.4 Updating the Sphere Code

12.5 Adding PDF Functions to Hittable Lists

12.6 Handling Surface Acne

13 The Rest of Your Life

14 Acknowledgments

15 Citing This Book

15.1 Basic Data

15.2 Snippets

15.2.1 Markdown

15.2.2 HTML

15.2.3 LaTeX and BibTex

15.2.4 BibLaTeX

15.2.5 IEEE

15.2.6 MLA:

Overview

In Ray Tracing in One Weekend and Ray Tracing: the Next Week, you built a “real” ray tracer.

In this volume, I assume you will be pursuing a career related to ray tracing, and we will dive into the math of creating a very serious ray tracer. When you are done you should be ready to start messing with the many serious commercial ray tracers underlying the movie and product design industries. There are many many things I do not cover in this short volume; I dive into only one of many ways to write a Monte Carlo rendering program. I don’t do shadow rays (instead I make rays more likely to go toward lights), bidirectional methods, Metropolis methods, or photon mapping. What I do is speak in the language of the field that studies those methods. I think of this book as a deep exposure that can be your first of many, and it will equip you with some of the concepts, math, and terms you will need to study the others.

As before, https://in1weekend.blogspot.com/ will have further readings and references.

Thanks to everyone who lent a hand on this project. You can find them in the acknowledgments section at the end of this book.

A Simple Monte Carlo Program

Let’s start with one of the simplest Monte Carlo (MC) programs. MC programs give a statistical estimate of an answer, and this estimate gets more and more accurate the longer you run it. This basic characteristic of simple programs producing noisy but ever-better answers is what MC is all about, and it is especially good for applications like graphics where great accuracy is not needed.

Estimating Pi

As an example, let’s estimate \(\pi\). There are many ways to do this, with the Buffon Needle problem being a classic case study. We’ll do a variation inspired by that. Suppose you have a circle inscribed inside a square:

Figure 1: Estimating π with a circle inside a square

Now, suppose you pick random points inside the square. The fraction of those random points that end up inside the circle should be proportional to the area of the circle. The exact fraction should in fact be the ratio of the circle area to the square area. Fraction:

$$ \frac{\pi r^2}{(2r)^2} = \frac{\pi}{4} $$

Since the \(r\) cancels out, we can pick whatever is computationally convenient. Let’s go with \(r=1\), centered at the origin:

#include"rtweekend.h"#include#include#include#includeintmain(){intN =1000;intinside_circle =0;for(inti =0; i < N; i++) {autox = random_double(-1,1);autoy = random_double(-1,1);if(x*x + y*y <1)            inside_circle++;    }std::cout<

Listing 1: [pi.cc] Estimating π

The answer of \(\pi\) found will vary from computer to computer based on the initial random seed. On my computer, this gives me the answer Estimate of Pi = 3.0880000000

Showing Convergence

If we change the program to run forever and just print out a running estimate:

#include"rtweekend.h"#include#include#include#includeintmain(){intinside_circle =0;intruns =0;std::cout<

Listing 2: [pi.cc] Estimating π, v2

Stratified Samples (Jittering)

We get very quickly near \(\pi\), and then more slowly zero in on it. This is an example of the Law of Diminishing Returns, where each sample helps less than the last. This is the worst part of MC. We can mitigate this diminishing return by stratifying the samples (often called jittering), where instead of taking random samples, we take a grid and take one sample within each:

Figure 2: Sampling areas with jittered points

This changes the sample generation, but we need to know how many samples we are taking in advance because we need to know the grid. Let’s take a hundred million and try it both ways:

#include"rtweekend.h"#include#includeintmain(){intinside_circle =0;intinside_circle_stratified =0;intsqrt_N =10000;for(inti =0; i < sqrt_N; i++) {for(intj =0; j < sqrt_N; j++) {autox = random_double(-1,1);autoy = 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++;        }    }autoN =static_cast(sqrt_N) * sqrt_N;std::cout<

Listing 3: [pi.cc] Estimating π, v3

On my computer, I get:

RegularEstimateofPi=3.14151480StratifiedEstimateofPi=3.14158948

Interestingly, the stratified method is not only better, it converges with a better asymptotic rate! Unfortunately, this advantage decreases with the dimension of the problem (so for example, with the 3D sphere volume version the gap would be less). This is called the Curse of Dimensionality. We are going to be very high dimensional (each reflection adds two dimensions), so I won't stratify in this book, but if you are ever doing single-reflection or shadowing or some strictly 2D problem, you definitely want to stratify.

One Dimensional MC Integration

Integration is all about computing areas and volumes, so we could have framed chapter  2 in an integral form if we wanted to make it maximally confusing. But sometimes integration is the most natural and clean way to formulate things. Rendering is often such a problem.

Integrating x²

Let’s look at a classic integral:

$$ I = \int_{0}^{2} x^2 dx $$

In computer sciency notation, we might write this as:

$$ I = \text{area}( x^2, 0, 2 ) $$

We could also write it as:

$$ I = 2 \cdot \text{average}(x^2, 0, 2) $$

This suggests a MC approach:

#include"rtweekend.h"#include#include#include#includeintmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {autox = random_double(0,2);        sum += x*x;    }std::cout<

Listing 4: [integrate_x_sq.cc] Integrating \(x^2\)

This, as expected, produces approximately the exact answer we get with algebra, \(I = 8/3\). We could also do it for functions that we can’t analytically integrate like \(\log(\sin(x))\). In graphics, we often have functions we can evaluate but can’t write down explicitly, or functions we can only probabilistically evaluate. That is in fact what the ray tracing ray_color() function of the last two books is — we don’t know what color is seen in every direction, but we can statistically estimate it in any given dimension.

One problem with the random program we wrote in the first two books is that small light sources create too much noise. This is because our uniform sampling doesn’t sample these light sources often enough. Light sources are only sampled if a ray scatters toward them, but this can be unlikely for a small light, or a light that is far away. We could lessen this problem if we sent more random samples toward this light, but this will cause the scene to be inaccurately bright. We can remove this inaccuracy by downweighting these samples to adjust for the over-sampling. How we do that adjustment? To do that, we will need the concept of a probability density function.

Density Functions

First, what is a density function? It’s just a continuous form of a histogram. Here’s an example from the histogram Wikipedia page:

Figure 3: Histogram example

If we added data for more trees, the histogram would get taller. If we divided the data into more bins, it would get shorter. A discrete density function differs from a histogram in that it normalizes the frequency y-axis to a fraction or percentage (just a fraction times 100). A continuous histogram, where we take the number of bins to infinity, can’t be a fraction because the height of all the bins would drop to zero. A density function is one where we take the bins and adjust them so they don’t get shorter as we add more bins. For the case of the tree histogram above we might try:

$$ \text{bin-height} = \frac{(\text{Fraction of trees between height }H\text{ and }H’)}{(H-H’)} $$

That would work! We could interpret that as a statistical predictor of a tree’s height:

$$ \text{Probability a random tree is between } H \text{ and } H’ = \text{bin-height}\cdot(H-H’)$$

If we wanted to know about the chances of being in a span of multiple bins, we would sum.

probability density function, henceforth PDF, is that fractional histogram made continuous.

Constructing a PDF

Let’s make a PDF and use it a bit to understand it more. Suppose I want a random number \(r\) between 0 and 2 whose probability is proportional to itself: \(r\). We would expect the PDF \(p(r)\) to look something like the figure below, but how high should it be?

Figure 4: A linear PDF

The height is just \(p(2)\). What should that be? We could reasonably make it anything by convention, and we should pick something that is convenient. Just as with histograms we can sum up (integrate) the region to figure out the probability that \(r\) is in some interval \((x_0,x_1)\):

$$ \text{Probability } x_0 < r < x_1 = C \cdot \text{area}(p(r), x_0, x_1) $$

where \(C\) is a scaling constant. We may as well make \(C = 1\) for cleanliness, and that is exactly what is done in probability. We also know the probability \(r\) has the value 1 somewhere, so for this case

$$ \text{area}(p(r), 0, 2) = 1 $$

Since \(p(r)\) is proportional to \(r\), i.e., \(p = C' \cdot r\) for some other constant \(C'\)

$$ area(C'r, 0, 2) = \int_{0}^{2} C' r dr = \frac{C'r^2}{2} \biggr|_{r=0}^{r=2} = \frac{C' \cdot 2^2}{2} - \frac{C' \cdot 0^2}{2} = 2C' $$

So \(p(r) = r/2\).

How do we generate a random number with that PDF \(p(r)\)? For that we will need some more machinery. Don’t worry this doesn’t go on forever!

Given a random number from d = random_double() that is uniform and between 0 and 1, we should be able to find some function \(f(d)\) that gives us what we want. Suppose \(e = f(d) = d^2\). This is no longer a uniform PDF. The PDF of \(e\) will be bigger near 1 than it is near 0 (squaring a number between 0 and 1 makes it smaller). To convert this general observation to a function, we need the cumulative probability distribution function \(P(x)\):

$$ P(x) = \text{area}(p, -\infty, x) $$

Note that for \(x\) where we didn’t define \(p(x)\), \(p(x) = 0\), i.e., the probability of an \(x\) there is zero. For our example PDF \(p(r) = r/2\), the \(P(x)\) is:

$$ P(x) = 0 : x < 0 $$ $$ P(x) = \frac{x^2}{4} : 0 < x < 2 $$ $$ P(x) = 1 : x > 2 $$

One question is, what’s up with \(x\) versus \(r\)? They are dummy variables — analogous to the function arguments in a program. If we evaluate \(P\) at \(x = 1.0\), we get:

$$ P(1.0) = \frac{1}{4} $$

This says the probability that a random variable with our PDF is less than one is 25%. This gives rise to a clever observation that underlies many methods to generate non-uniform random numbers. We want a function f() that when we call it as f(random_double()) we get a return value with a PDF \(\frac{x^2}{4}\). We don’t know what that is, but we do know that 25% of what it returns should be less than 1.0, and 75% should be above 1.0. If \(f()\) is increasing, then we would expect \(f(0.25) = 1.0\). This can be generalized to figure out \(f()\) for every possible input:

$$ f(P(x)) = x $$

That means \(f\) just undoes whatever \(P\) does. So,

$$ f(x) = P^{-1}(x) $$

The −1 means “inverse function”. Ugly notation, but standard. For our purposes, if we have PDF \(p()\) and cumulative distribution function \(P()\), we can use this “inverse function” with a random number to get what we want:

$$ e = P^{-1} (\text{random_double}()) $$

For our PDF \(p(x) = x/2\), and corresponding \(P(x)\), we need to compute the inverse of \(P\). If we have

$$ y = \frac{x^2}{4} $$

we get the inverse by solving for \(x\) in terms of \(y\):

$$ x = \sqrt{4y} $$

Thus our random number with density \(p\) is found with:

$$ e = \sqrt{4\cdot\text{random_double}()} $$

Note that this ranges from 0 to 2 as hoped, and if we check our work by replacing random_double() with \(\frac{1}{4}\) we get 1 as expected.

We can now sample our old integral

$$ I = \int_{0}^{2} x^2 $$

We need to account for the non-uniformity of the PDF of \(x\). Where we sample too much we should down-weight. The PDF is a perfect measure of how much or little sampling is being done. So the weighting function should be proportional to \(1/pdf\). In fact it is exactly \(1/pdf\):

inlinedoublepdf(doublex){return0.5*x;}

intmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {

autox =sqrt(random_double(0,4));        sum += x*x / pdf(x);

    }std::cout<

Listing 5: [integrate_x_sq.cc] Integrating \(x^2\) with PDF

Importance Sampling

Since we are sampling more where the integrand is big, we might expect less noise and thus faster convergence. In effect, we are steering our samples toward the parts of the distribution that are more important. This is why using a carefully chosen non-uniform PDF is usually called importance sampling.

If we take that same code with uniform samples so the PDF = \(1/2\) over the range [0,2] we can use the machinery to get x = random_double(0,2), and the code is:

inlinedoublepdf(doublex){return0.5;}

intmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {

autox = random_double(0,2);

        sum += x*x / pdf(x);    }std::cout<

Listing 6: [integrate_x_sq.cc] Integrating \(x^2\), v3

Note that we don’t need that 2 in the 2*sum/N anymore — that is handled by the PDF, which is 2 when you divide by it. You’ll note that importance sampling helps a little, but not a ton. We could make the PDF follow the integrand exactly:

$$ p(x) = \frac{3}{8}x^2 $$

And we get the corresponding

$$ P(x) = \frac{x^3}{8} $$

and

$$ P^{-1}(x) = 8x^\frac{1}{3} $$

This perfect importance sampling is only possible when we already know the answer (we got \(P\) by integrating \(p\) analytically), but it’s a good exercise to make sure our code works. For just 1 sample we get:

inlinedoublepdf(doublex){return3*x*x/8;}

intmain(){

intN =1;

autosum =0.0;for(inti =0; i < N; i++) {

autox =pow(random_double(0,8),1./3.);

        sum += x*x / pdf(x);    }std::cout<

Listing 7: [integrate_x_sq.cc] Integrating \(x^2\), final version

Which always returns the exact answer.

Let’s review now because that was most of the concepts that underlie MC ray tracers.

You have an integral of \(f(x)\) over some domain \([a,b]\)

You pick a PDF \(p\) that is non-zero over \([a,b]\)

You average a whole ton of \(\frac{f(r)}{p(r)}\) where \(r\) is a random number with PDF \(p\).

Any choice of PDF \(p\) will always converge to the right answer, but the closer that \(p\) approximates \(f\), the faster that it will converge.

MC Integration on the Sphere of Directions

In our ray tracer we pick random directions, and directions can be represented as points on the unit sphere. The same methodology as before applies, but now we need to have a PDF defined over 2D.

Suppose we have this integral over all directions:

$$ \int cos^2(\theta) $$

By MC integration, we should just be able to sample \(\cos^2(\theta) / p(\text{direction})\), but what is direction in that context? We could make it based on polar coordinates, so \(p\) would be in terms of \((\theta, \phi)\). However you do it, remember that a PDF has to integrate to 1 and represent the relative probability of that direction being sampled. Recall that we have vec3 functions to take uniform random samples in (random_in_unit_sphere()) or on (random_unit_vector()) a unit sphere.

Now what is the PDF of these uniform points? As a density on the unit sphere, it is \(1/\text{area}\) of the sphere or \(1/(4\pi)\). If the integrand is \(\cos^2(\theta)\), and \(\theta\) is the angle with the z axis:

inlinedoublepdf(constvec3& p){return1/ (4*pi);}intmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {        vec3 d = random_unit_vector();autocosine_squared = d.z()*d.z();        sum += cosine_squared / pdf(d);    }std::cout<

Listing 8: [sphere_importance.cc] Generating importance-sampled points on the unit sphere

The analytic answer (if you remember enough advanced calc, check me!) is \(\frac{4}{3} \pi\), and the code above produces that. Next, we are ready to apply that in ray tracing!

The key point here is that all the integrals and probability and all that are over the unit sphere. The area on the unit sphere is how you measure the directions. Call it direction, solid angle, or area — it’s all the same thing. Solid angle is the term usually used. If you are comfortable with that, great! If not, do what I do and imagine the area on the unit sphere that a set of directions goes through. The solid angle \(\omega\) and the projected area \(A\) on the unit sphere are the same thing.

Figure 5: Solid angle / projected area of a sphere

Now let’s go on to the light transport equation we are solving.

Light Scattering

In this chapter we won't actually program anything. We will set up for a big lighting change in the next chapter.

Albedo

Our program from the last books already scatters rays from a surface or volume. This is the commonly used model for light interacting with a surface. One natural way to model this is with probability. First, is the light absorbed?

Probability of light scattering: \(A\)

Probability of light being absorbed: \(1-A\)

Here \(A\) stands for albedo (latin for whiteness). Albedo is a precise technical term in some disciplines, but in all cases it is used to define some form of fractional reflectance. This fractional reflectance (or albedo) will vary with color and (as we implemented for our glass in book one) can vary with incident direction.

Scattering

In most physically based renderers, we would use a set of wavelengths for the light color rather than RGB. We can extend our intuition by thinking of R, G, and B as specific algebraic mixtures of long, medium, and short wavelengths.

If the light does scatter, it will have a directional distribution that we can describe as a PDF over solid angle. I will refer to this as its scattering PDF: \(s(direction)\). The scattering PDF can also vary with incident direction, which is the direction of the incoming ray. You can see this varying with incident direction when you look at reflections off a road — they become mirror-like as your viewing angle (incident angle) approaches grazing.

The color of a surface in terms of these quantities is:

$$ Color = \int A \cdot s(direction) \cdot \text{color}(direction) $$

Note that \(A\) and \(s()\) may depend on the view direction or the scattering position (position on a surface or position within a volume). Therefore, the output color may also vary with view direction or scattering position.

The Scattering PDF

If we apply the MC basic formula we get the following statistical estimate:

$$ Color = \frac{A \cdot s(direction) \cdot \text{color}(direction)}{p(direction)} $$

where \(p(direction)\) is the PDF of whatever direction we randomly generate.

For a Lambertian surface we already implicitly implemented this formula for the special case where \(p()\) is a cosine density. The \(s()\) of a Lambertian surface is proportional to \(\cos(\theta)\), where \(\theta\) is the angle relative to the surface normal. Remember that all PDF need to integrate to one. For \(\cos(\theta) < 0\) we have \(s(direction) = 0\), and the integral of cos over the hemisphere is \(\pi\).

To see that, remember that in spherical coordinates:

$$ dA = \sin(\theta) d\theta d\phi $$

So:

$$ Area = \int_{0}^{2 \pi} \int_{0}^{\pi / 2} cos(\theta) sin(\theta) d\theta d\phi = 2 \pi \frac{1}{2} = \pi $$

So for a Lambertian surface the scattering PDF is:

$$ s(direction) = \frac{\cos(\theta)}{\pi} $$

If we sample using a PDF that equals the scattering PDF:

$$ p(direction) = s(direction) = \frac{\cos(\theta)}{\pi} $$

The numerator and denominator cancel out, and we get:

$$ Color = A \cdot color(direction) $$

This is exactly what we had in our original ray_color() function! However, we need to generalize so we can send extra rays in important directions, such as toward the lights.

The treatment above is slightly non-standard because I want the same math to work for surfaces and volumes. To do otherwise will make some ugly code.

If you read the literature, you’ll see reflection described by the bidirectional reflectance distribution function (BRDF). It relates pretty simply to our terms:

$$ BRDF = \frac{A \cdot s(direction)}{\cos(\theta)} $$

So for a Lambertian surface for example, \(BRDF = A / \pi\). Translation between our terms and BRDF is easy.

For participation media (volumes), our albedo is usually called scattering albedo, and our scattering PDF is usually called phase function.

Importance Sampling Materials

Our goal over the next two chapters is to instrument our program to send a bunch of extra rays toward light sources so that our picture is less noisy. Let’s assume we can send a bunch of rays toward the light source using a PDF \(pLight(direction)\). Let’s also assume we have a PDF related to \(s\), and let’s call that \(pSurface(direction)\). A great thing about PDFs is that you can just use linear mixtures of them to form mixture densities that are also PDFs. For example, the simplest would be:

$$ p(direction) = \frac{1}{2}\cdotp \text{Light}(direction) + \frac{1}{2}\cdot \text{pSurface}(direction) $$

As long as the weights are positive and add up to one, any such mixture of PDFs is a PDF. Remember, we can use any PDF: all PDFs eventually converge to the correct answer. So, the game is to figure out how to make the PDF larger where the product \(s(direction) \cdot color(direction)\) is large. For diffuse surfaces, this is mainly a matter of guessing where \(color(direction)\) is high.

For a mirror, \(s()\) is huge only near one direction, so it matters a lot more. Most renderers in fact make mirrors a special case, and just make the \(s/p\) implicit — our code currently does that.

Returning to the Cornell Box

Let’s do a simple refactoring and temporarily remove all materials that aren’t Lambertian. We can use our Cornell Box scene again, and let’s generate the camera in the function that generates the model.

...colorray_color(...){    ...}hittable_listcornell_box(){    hittable_list objects;autored  = make_shared(color(.65,.05,.05));autowhite = make_shared(color(.73,.73,.73));autogreen = make_shared(color(.12,.45,.15));autolight = make_shared(color(15,15,15));objects.add(make_shared(0,555,0,555,555, green));objects.add(make_shared(0,555,0,555,0, red));objects.add(make_shared(213,343,227,332,554, light));objects.add(make_shared(0,555,0,555,555, white));objects.add(make_shared(0,555,0,555,0, white));objects.add(make_shared(0,555,0,555,555, white));shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), white);box1 = make_shared(box1,15);box1 = make_shared(box1, vec3(265,0,295));    objects.add(box1);shared_ptr box2 = make_shared(point3(0,0,0), point3(165,165,165), white);box2 = make_shared(box2,-18);box2 = make_shared(box2, vec3(130,0,65));    objects.add(box2);returnobjects;}intmain(){// Imageconstautoaspect_ratio =1.0/1.0;constintimage_width =600;constintimage_height =static_cast(image_width / aspect_ratio);constintsamples_per_pixel =100;constintmax_depth =50;// Worldautoworld = cornell_box();colorbackground(0,0,0);// Camerapoint3lookfrom(278,278,-800);point3lookat(278,278,0);vec3vup(0,1,0);autodist_to_focus =10.0;autoaperture =0.0;autovfov =40.0;autotime0 =0.0;autotime1 =1.0;cameracam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, time0, time1);// Renderstd::cout<<"P3\n"<< image_width <<' '<< image_height <<"\n255\n";for(intj = image_height-1; j >=0; --j) {        ...}

Listing 9: [main.cc] Cornell box, refactored

At 500×500 my code produces this image in 10min on 1 core of my Macbook:

Image 1: Cornell box, refactored

Reducing that noise is our goal. We’ll do that by constructing a PDF that sends more rays to the light.

First, let’s instrument the code so that it explicitly samples some PDF and then normalizes for that. Remember MC basics: \(\int f(x) \approx f(r)/p(r)\). For the Lambertian material, let’s sample like we do now: \(p(direction) = \cos(\theta) / \pi\).

We modify the base-class material to enable this importance sampling:

classmaterial{public:virtualboolscatter(constray& r_in,consthit_record& rec, color& albedo, ray& scattered,double& pdf

)const{returnfalse;        }

virtualdoublescattering_pdf(constray& r_in,consthit_record& rec,constray& scattered        )const{return0;        }

virtualcoloremitted(doubleu,doublev,constpoint3& p)const{returncolor(0,0,0);        }};

Listing 10: [material.h] The material class, adding importance sampling

And Lambertian material becomes:

classlambertian:publicmaterial {public:

lambertian(constcolor& a) : albedo(make_shared(a)) {}lambertian(shared_ptr a) : albedo(a) {}

virtualboolscatter(

constray& r_in,consthit_record& rec, color& alb, ray& scattered,double& pdf

)constoverride{autoscatter_direction = rec.normal + random_unit_vector();// Catch degenerate scatter directionif(scatter_direction.near_zero())                scatter_direction = rec.normal;

            scattered = ray(rec.p, unit_vector(direction), r_in.time());            alb = albedo->value(rec.u, rec.v, rec.p);            pdf = dot(rec.normal, scattered.direction()) / pi;

returntrue;        }

doublescattering_pdf(constray& r_in,consthit_record& rec,constray& scattered        )const{autocosine = dot(rec.normal, unit_vector(scattered.direction()));returncosine <0?0: cosine/pi;        }

public:shared_ptr albedo;};

Listing 11: [material.h] Lambertian material, modified for importance sampling

And the ray_color function gets a minor modification:

colorray_color(constray& r,constcolor& background,consthittable& world,intdepth){    hit_record rec;// If we've exceeded the ray bounce limit, no more light is gathered.if(depth <=0)returncolor(0,0,0);// If the ray hits nothing, return the background color.if(!world.hit(r,0.001, infinity, rec))returnbackground;    ray scattered;    color attenuation;    color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);

doublepdf;    color albedo;if(!rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf))returnemitted;returnemitted        + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)* ray_color(scattered, background, world, depth-1) / pdf;

}

Listing 12: [main.cc] The ray_color function, modified for importance sampling

You should get exactly the same picture.

Random Hemisphere Sampling

Now, just for the experience, try a different sampling strategy. As in the first book, Let’s choose randomly from the hemisphere above the surface. This would be \(p(direction) = \frac{1}{2\pi}\).

virtualboolscatter(constray& r_in,consthit_record& rec, color& alb, ray& scattered,double& pdf)constoverride{

autodirection = random_in_hemisphere(rec.normal);

    scattered = ray(rec.p, unit_vector(direction), r_in.time());    alb = albedo->value(rec.u, rec.v, rec.p);

pdf =0.5/ pi;

returntrue;}

Listing 13: [material.h] Modified scatter function

And again I should get the same picture except with different variance, but I don’t!

Image 2: Cornell box, with different sampling strategy

It’s pretty close to our old picture, but there are differences that are not noise. The front of the tall box is much more uniform in color. So I have the most difficult kind of bug to find in a Monte Carlo program — a bug that produces a reasonable looking image. I also don’t know if the bug is the first version of the program, or the second, or both!

Let’s build some infrastructure to address this.

Generating Random Directions

In this and the next two chapters, let’s harden our understanding and tools and figure out which Cornell Box is right.

Random Directions Relative to the Z Axis

Let’s first figure out how to generate random directions. To simplify things, let’s assume the z-axis is the surface normal, and \(\theta\) is the angle from the normal. We’ll get them oriented to the surface normal vector in the next chapter. We will only deal with distributions that are rotationally symmetric about \(z\). So \(p(direction) = f(\theta)\). If you have had advanced calculus, you may recall that on the sphere in spherical coordinates \(dA = \sin(\theta) \cdot d\theta \cdot d\phi\). If you haven’t, you’ll have to take my word for the next step, but you’ll get it when you take advanced calculus.

Given a directional PDF, \(p(direction) = f(\theta)\) on the sphere, the 1D PDFs on \(\theta\) and \(\phi\) are:

$$ a(\phi) = \frac{1}{2\pi} $$ (uniform) $$ b(\theta) = 2\pi f(\theta)\sin(\theta) $$

For uniform random numbers \(r_1\) and \(r_2\), the material presented in the One Dimensional MC Integration chapter leads to:

$$ r_1 = \int_{0}^{\phi} \frac{1}{2\pi} dt = \frac{\phi}{2\pi} $$

Solving for \(\phi\) we get:

$$ \phi = 2 \pi \cdot r_1 $$

For \(\theta\) we have:

$$ r_2 = \int_{0}^{\theta} 2 \pi f(t) \sin(t) dt $$

Here, \(t\) is a dummy variable. Let’s try some different functions for \(f()\). Let’s first try a uniform density on the sphere. The area of the unit sphere is \(4\pi\), so a uniform \(p(direction) = \frac{1}{4\pi}\) on the unit sphere.

$$ r_2 = \int_{0}^{\theta} 2 \pi \frac{1}{4\pi} \sin(t) dt $$ $$ = \int_{0}^{\theta} \frac{1}{2} \sin(t) dt $$ $$ = \frac{-\cos(\theta)}{2} - \frac{-\cos(0)}{2} $$ $$ = \frac{1 - \cos(\theta)}{2} $$

Solving for \(\cos(\theta)\) gives:

$$ \cos(\theta) = 1 - 2 r_2 $$

We don’t solve for theta because we probably only need to know \(\cos(\theta)\) anyway, and don’t want needless \(\arccos()\) calls running around.

To generate a unit vector direction toward \((\theta,\phi)\) we convert to Cartesian coordinates:

$$ x = \cos(\phi) \cdot \sin(\theta) $$ $$ y = \sin(\phi) \cdot \sin(\theta) $$ $$ z = \cos(\theta) $$

And using the identity that \(\cos^2 + \sin^2 = 1\), we get the following in terms of random \((r_1,r_2)\):

$$ x = \cos(2\pi \cdot r_1)\sqrt{1 - (1-2 r_2)^2} $$ $$ y = \sin(2\pi \cdot r_1)\sqrt{1 - (1-2 r_2)^2} $$ $$ z = 1 - 2 r_2 $$

Simplifying a little, \((1 - 2 r_2)^2 = 1 - 4r_2 + 4r_2^2\), so:

$$ x = \cos(2 \pi r_1) \cdot 2 \sqrt{r_2(1 - r_2)} $$ $$ y = \sin(2 \pi r_1) \cdot 2 \sqrt{r_2(1 - r_2)} $$ $$ z = 1 - 2 r_2 $$

We can output some of these:

intmain(){for(inti =0; i <200; i++) {autor1 = random_double();autor2 = random_double();autox =cos(2*pi*r1)*2*sqrt(r2*(1-r2));autoy =sin(2*pi*r1)*2*sqrt(r2*(1-r2));autoz =1-2*r2;std::cout<< x <<" "<< y <<" "<< z <<'\n';    }}

Listing 14: [sphere_plot.cc] Random points on the unit sphere

And plot them for free on plot.ly (a great site with 3D scatterplot support):

Figure 6: Random points on the unit sphere

On the plot.ly website you can rotate that around and see that it appears uniform.

Uniform Sampling a Hemisphere

Now let’s derive uniform on the hemisphere. The density being uniform on the hemisphere means \(p(direction) = \frac{1}{2\pi}\). Just changing the constant in the theta equations yields:

$$ \cos(\theta) = 1 - r_2 $$

It is comforting that \(\cos(\theta)\) will vary from 1 to 0, and thus theta will vary from 0 to \(\pi/2\). Rather than plot it, let’s do a 2D integral with a known solution. Let’s integrate cosine cubed over the hemisphere (just picking something arbitrary with a known solution). First let’s do it by hand:

$$ \int \cos^3(\theta) dA $$ $$ = \int_{0}^{2 \pi} \int_{0}^{\pi /2} \cos^3(\theta) \sin(\theta) d\theta d\phi $$ $$ = 2 \pi \int_{0}^{\pi/2} \cos^3(\theta) \sin(\theta) = \frac{\pi}{2} $$

Now for integration with importance sampling. \(p(direction) = \frac{1}{2\pi}\), so we average \(f/p\) which is \(\cos^3(\theta) / (1/2\pi)\), and we can test this:

intmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {autor1 = random_double();autor2 = random_double();autox =cos(2*pi*r1)*2*sqrt(r2*(1-r2));autoy =sin(2*pi*r1)*2*sqrt(r2*(1-r2));autoz =1- r2;sum += z*z*z / (1.0/(2.0*pi));    }std::cout<

Listing 15: [cos_cubed.cc] Integration using \(cos^3(x)\)

Now let’s generate directions with \(p(directions) = \cos(\theta) / \pi\).

$$ r_2 = \int_{0}^{\theta} 2 \pi \frac{\cos(t)}{\pi} \sin(t) = 1 - \cos^2(\theta) $$

So,

$$ \cos(\theta) = \sqrt{1 - r_2} $$

We can save a little algebra on specific cases by noting

$$ z = \cos(\theta) = \sqrt{1 - r_2} $$ $$ x = \cos(\phi) \sin(\theta) = \cos(2 \pi r_1) \sqrt{1 - z^2} = \cos(2 \pi r_1) \sqrt{r_2} $$ $$ y = \sin(\phi) \sin(\theta) = \sin(2 \pi r_1) \sqrt{1 - z^2} = \sin(2 \pi r_1) \sqrt{r_2} $$

Let’s also start generating them as random vectors:

#include"rtweekend.h"#include#includeinlinevec3random_cosine_direction(){autor1 = random_double();autor2 = random_double();autoz =sqrt(1-r2);autophi =2*pi*r1;autox =cos(phi)*sqrt(r2);autoy =sin(phi)*sqrt(r2);returnvec3(x, y, z);}intmain(){intN =1000000;autosum =0.0;for(inti =0; i < N; i++) {autov = random_cosine_direction();        sum += v.z()*v.z()*v.z() / (v.z()/pi);    }std::cout<

Listing 16: [cos_density.cc] Integration with cosine density function

We can generate other densities later as we need them. In the next chapter we’ll get them aligned to the surface normal vector.

Orthonormal Bases

In the last chapter we developed methods to generate random directions relative to the Z-axis. We’d like to be able to do that relative to a surface normal vector.

Relative Coordinates

An orthonormal basis (ONB) is a collection of three mutually orthogonal unit vectors. The Cartesian XYZ axes are one such ONB, and I sometimes forget that it has to sit in some real place with real orientation to have meaning in the real world, and some virtual place and orientation in the virtual world. A picture is a result of the relative positions/orientations of the camera and scene, so as long as the camera and scene are described in the same coordinate system, all is well.

Suppose we have an origin \(\mathbf{O}\) and cartesian unit vectors \(\mathbf{x}\), \(\mathbf{y}\), and \(\mathbf{z}\). When we say a location is (3,-2,7), we really are saying:

$$ \text{Location is } \mathbf{O} + 3\mathbf{x} - 2\mathbf{y} + 7\mathbf{z} $$

If we want to measure coordinates in another coordinate system with origin \(\mathbf{O}'\) and basis vectors \(\mathbf{u}\), \(\mathbf{v}\), and \(\mathbf{w}\), we can just find the numbers \((u,v,w)\) such that:

$$ \text{Location is } \mathbf{O}' + u\mathbf{u} + v\mathbf{v} + w\mathbf{w} $$

Generating an Orthonormal Basis

If you take an intro graphics course, there will be a lot of time spent on coordinate systems and 4×4 coordinate transformation matrices. Pay attention, it’s important stuff in graphics! But we won’t need it. What we need to is generate random directions with a set distribution relative to \(\mathbf{n}\). We don’t need an origin because a direction is relative to no specified origin. We do need two cotangent vectors that are mutually perpendicular to \(\mathbf{n}\) and to each other.

Some models will come with one or more cotangent vectors. If our model has only one cotangent vector, then the process of making an ONB is a nontrivial one. Suppose we have any vector \(\mathbf{a}\) that is of nonzero length and not parallel to \(\mathbf{n}\). We can get vectors \(\mathbf{s}\) and \(\mathbf{t}\) perpendicular to \(\mathbf{n}\) by using the property of the cross product that \(\mathbf{c} \times \mathbf{d}\) is perpendicular to both \(\mathbf{c}\) and \(\mathbf{d}\):

$$ \mathbf{t} = \text{unit_vector}(\mathbf{a} \times \mathbf{n}) $$

$$ \mathbf{s} = \mathbf{t} \times \mathbf{n} $$

This is all well and good, but the catch is that we may not be given an \(\mathbf{a}\) when we load a model, and we don't have an \(\mathbf{a}\) with our existing program. If we went ahead and picked an arbitrary \(\mathbf{a}\) to use as our initial vector we may get an \(\mathbf{a}\) that is parallel to \(\mathbf{n}\). A common method is to use an if-statement to determine whether \(\mathbf{n}\) is a particular axis, and if not, use that axis.

if absolute(n.x > 0.9)    a ← (0, 1, 0)else    a ← (1, 0, 0)

Once we have an ONB of \(\mathbf{s}\), \(\mathbf{t}\), and \(\mathbf{n}\), and we have a \(random(x,y,z)\) relative to the Z-axis, we can get the vector relative to \(\mathbf{n}\) as:

$$ \text{Random vector} = x \mathbf{s} + y \mathbf{t} + z \mathbf{n} $$

You may notice we used similar math to get rays from a camera. That could be viewed as a change to the camera’s natural coordinate system.

The ONB Class

Should we make a class for ONBs, or are utility functions enough? I’m not sure, but let’s make a class because it won't really be more complicated than utility functions:

#ifndefONB_H#defineONB_Hclassonb{public:        onb() {}inlinevec3operator[](inti)const{returnaxis[i]; }vec3u()const{returnaxis[0]; }vec3v()const{returnaxis[1]; }vec3w()const{returnaxis[2]; }vec3local(doublea,doubleb,doublec)const{returna*u() + b*v() + c*w();        }vec3local(constvec3& a)const{returna.x()*u() + a.y()*v() + a.z()*w();        }voidbuild_from_w(constvec3&);public:vec3 axis[3];};voidonb::build_from_w(constvec3& n){axis[2] = unit_vector(n);vec3 a = (fabs(w().x()) >0.9) ? vec3(0,1,0) : vec3(1,0,0);axis[1] = unit_vector(cross(w(), a));axis[0] = cross(w(), v());}#endif

Listing 17: [onb.h] Orthonormal basis class

We can rewrite our Lambertian material using this to get:

virtualboolscatter(constray& r_in,consthit_record& rec, color& alb, ray& scattered,double& pdf)constoverride{

    onb uvw;    uvw.build_from_w(rec.normal);autodirection = uvw.local(random_cosine_direction());

    scattered = ray(rec.p, unit_vector(direction), r_in.time());    alb = albedo->value(rec.u, rec.v, rec.p);

    pdf = dot(uvw.w(), scattered.direction()) / pi;

returntrue;}

Listing 18: [material.h] Scatter function, with orthonormal basis

Which produces:

Image 3: Cornell box, with orthonormal basis scatter function

Is that right? We still don’t know for sure. Tracking down bugs is hard in the absence of reliable reference solutions. Let’s table that for now and get rid of some of that noise.

Sampling Lights Directly

The problem with sampling almost uniformly over directions is that lights are not sampled any more than unimportant directions. We could use shadow rays and separate out direct lighting. Instead, I’ll just send more rays to the light. We can then use that later to send more rays in whatever direction we want.

It’s really easy to pick a random direction toward the light; just pick a random point on the light and send a ray in that direction. We also need to know the PDF, \(p(direction)\). What is that?

Getting the PDF of a Light

For a light of area \(A\), if we sample uniformly on that light, the PDF on the surface of the light is \(\frac{1}{A}\). What is it on the area of the unit sphere that defines directions? Fortunately, there is a simple correspondence, as outlined in the diagram:

Figure 7: Projection of light shape onto PDF

If we look at a small area \(dA\) on the light, the probability of sampling it is \(p_q(q) \cdot dA\). On the sphere, the probability of sampling the small area \(dw\) on the sphere is \(p(direction) \cdot dw\). There is a geometric relationship between \(dw\) and \(dA\):

$$ dw = \frac{dA \cdot \cos(alpha)}{distance^2(p,q)} $$

Since the probability of sampling dw and dA must be the same, we have

$$ p(direction) \cdot \frac{dA \cdot \cos(alpha)}{distance^2(p,q)} = p_q(q) \cdot dA = \frac{dA}{A} $$

So

$$ p(direction) = \frac{distance^2(p,q)}{\cos(alpha) \cdot A} $$

Light Sampling

If we hack our ray_color() function to sample the light in a very hard-coded fashion just to check that math and get the concept, we can add it (see the highlighted region):

colorray_color(constray& r,constcolor& background,consthittable& world,intdepth){    hit_record rec;// If we've exceeded the ray bounce limit, no more light is gathered.if(depth <=0)returncolor(0,0,0);// If the ray hits nothing, return the background color.if(!world.hit(r,0.001, infinity, rec))returnbackground;    ray scattered;    color attenuation;    color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);doublepdf;    color albedo;if(!rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf))returnemitted;

autoon_light = point3(random_double(213,343),554, random_double(227,332));autoto_light = on_light - rec.p;autodistance_squared = to_light.length_squared();    to_light = unit_vector(to_light);if(dot(to_light, rec.normal) <0)returnemitted;doublelight_area = (343-213)*(332-227);autolight_cosine =fabs(to_light.y());if(light_cosine <0.000001)returnemitted;    pdf = distance_squared / (light_cosine * light_area);    scattered = ray(rec.p, to_light, r.time());

returnemitted        + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)* ray_color(scattered, background, world, depth-1) / pdf;}

Listing 19: [main.cc] Ray color with light sampling

With 10 samples per pixel this yields:

Image 4: Cornell box, sampling only the light, 10 samples per pixel

This is about what we would expect from something that samples only the light sources, so this appears to work.

Switching to Unidirectional Light

The noisy pops around the light on the ceiling are because the light is two-sided and there is a small space between light and ceiling. We probably want to have the light just emit down. We can do that by letting the emitted member function of hittable take extra information:

virtualcoloremitted(constray& r_in,consthit_record& rec,doubleu,doublev,constpoint3& p)constoverride{if(rec.front_face)returnemit->value(u, v, p);elsereturncolor(0,0,0);}

Listing 20: [material.h] Material emission, directional

We also need to flip the light so its normals point in the \(-y\) direction:

classflip_face:publichittable {public:flip_face(shared_ptr p) : ptr(p) {}virtualboolhit(constray& r,doublet_min,doublet_max, hit_record& rec)constoverride{if(!ptr->hit(r, t_min, t_max, rec))returnfalse;            rec.front_face = !rec.front_face;returntrue;        }virtualboolbounding_box(doubletime0,doubletime1, aabb& output_box)constoverride{returnptr->bounding_box(time0, time1, output_box);        }public:shared_ptr ptr;};

Listing 21: [hittable.h] We use a hittable object to flip the light

Making sure to call this in our world definition:

hittable_listcornell_box(){    hittable_list objects;autored  = make_shared(color(.65,.05,.05));autowhite = make_shared(color(.73,.73,.73));autogreen = make_shared(color(.12,.45,.15));autolight = make_shared(color(15,15,15));objects.add(make_shared(0,555,0,555,555, green));objects.add(make_shared(0,555,0,555,0, red));

objects.add(make_shared(make_shared(213,343,227,332,554, light)));

objects.add(make_shared(0,555,0,555,555, white));objects.add(make_shared(0,555,0,555,0, white));objects.add(make_shared(0,555,0,555,555, white));    ...

Listing 22: [main.cc] Flip the light in our cornell box scene

This gives us:

Image 5: Cornell box, light emitted only in the downward direction

Mixture Densities

We have used a PDF related to \(\cos(\theta)\), and a PDF related to sampling the light. We would like a PDF that combines these.

An Average of Lighting and Reflection

A common tool in probability is to mix the densities to form a mixture density. Any weighted average of PDFs is a PDF. For example, we could just average the two densities:

$$ \text{mixture}_\text{pdf}(direction) = \frac{1}{2} \text{reflection}_\text{pdf}(direction) + \frac{1}{2} \text{light}_\text{pdf}(direction) $$

How would we instrument our code to do that? There is a very important detail that makes this not quite as easy as one might expect. Choosing the random direction is simple:

if (random_double() < 0.5)    pick direction according to pdf_reflectionelse    pick direction according to pdf_light

But evaluating \(\text{mixture}_\text{pdf}\) is slightly more subtle. We need to evaluate both \(\text{reflection}_\text{pdf}\) and \(\text{light}_\text{pdf}\) because there are some directions where either PDF could have generated the direction. For example, we might generate a direction toward the light using \(\text{reflection}_\text{pdf}\).

If we step back a bit, we see that there are two functions a PDF needs to support:

What is your value at this location?

Return a random number that is distributed appropriately.

The details of how this is done under the hood varies for the \(\text{reflection}_\text{pdf}\) and the \(\text{light}_\text{pdf}\) and the mixture density of the two of them, but that is exactly what class hierarchies were invented for! It’s never obvious what goes in an abstract class, so my approach is to be greedy and hope a minimal interface works, and for the PDF this implies:

#ifndefPDF_H#definePDF_Hclasspdf{public:virtual~pdf() {}virtualdoublevalue(constvec3& direction)const=0;virtualvec3generate()const=0;};#endif

Listing 23: [pdf.h] The pdf class

We’ll see if that works by fleshing out the subclasses. For sampling the light, we will need hittable to answer some queries that it doesn’t have an interface for. We’ll probably need to mess with it too, but we can start by seeing if we can put something in hittable involving sampling the bounding box that works with all its subclasses.

First, let’s try a cosine density:

inlinevec3random_cosine_direction(){autor1 = random_double();autor2 = random_double();autoz =sqrt(1-r2);autophi =2*pi*r1;autox =cos(phi)*sqrt(r2);autoy =sin(phi)*sqrt(r2);returnvec3(x, y, z);}classcosine_pdf:publicpdf {public:cosine_pdf(constvec3& w) { uvw.build_from_w(w); }virtualdoublevalue(constvec3& direction)constoverride{autocosine = dot(unit_vector(direction), uvw.w());return(cosine <=0) ?0: cosine/pi;        }virtualvec3generate()constoverride{returnuvw.local(random_cosine_direction());        }public:        onb uvw;};

Listing 24: [pdf.h] The cosine_pdf class

We can try this in the ray_color() function, with the main changes highlighted. We also need to change variable pdfto some other variable name to avoid a name conflict with the new pdf class.

colorray_color(constray& r,constcolor& background,consthittable& world,intdepth){    hit_record rec;// If we've exceeded the ray bounce limit, no more light is gathered.if(depth <=0)returncolor(0,0,0);// If the ray hits nothing, return the background color.if(!world.hit(r,0.001, infinity, rec))returnbackground;    ray scattered;    color attenuation;    color emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p);doublepdf_val;    color albedo;if(!rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf_val))returnemitted;

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

returnemitted        + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)* ray_color(scattered, background, world, depth-1) / pdf_val;}

Listing 25: [main.cc] The ray_color function, using cosine pdf

This yields an apparently matching result so all we’ve done so far is refactor where pdf is computed:

Image 6: Cornell box with a cosine density pdf

Sampling Directions towards a Hittable

Now we can try sampling directions toward a hittable, like the light.

classhittable_pdf:publicpdf {public:hittable_pdf(shared_ptr p,constpoint3& origin) : ptr(p), o(origin) {}virtualdoublevalue(constvec3& direction)constoverride{returnptr->pdf_value(o, direction);        }virtualvec3generate()constoverride{returnptr->random(o);        }public:        point3 o;shared_ptr ptr;};

Listing 26: [pdf.h] The hittable_pdf class

This assumes two as-yet not implemented functions in the hittable class. To avoid having to add instrumentation to all hittable subclasses, we’ll add two dummy functions to the hittable class:

classhittable{public:virtualboolhit(constray& r,doublet_min,doublet_max, hit_record& rec)const=0;virtualboolbounding_box(doubletime0,doubletime1, aabb& output_box)const=0;

virtualdoublepdf_value(constpoint3& o,constvec3& v)const{return0.0;        }virtualvec3random(constvec3& o)const{returnvec3(1,0,0);        }

};

Listing 27: [hittable.h] The hittable class, with two new methods

And we change xz_rect to implement those functions:

classxz_rect:publichittable {public:        ...

virtualdoublepdf_value(constpoint3& origin,constvec3& v)constoverride{            hit_record rec;if(!this->hit(ray(origin, v),0.001, infinity, rec))return0;autoarea = (x1-x0)*(z1-z0);autodistance_squared = rec.t * rec.t * v.length_squared();autocosine =fabs(dot(v, rec.normal) / v.length());returndistance_squared / (cosine * area);        }virtualvec3random(constpoint3& origin)constoverride{autorandom_point = point3(random_double(x0,x1), k, random_double(z0,z1));returnrandom_point - origin;        }

Listing 28: [aarect.h] XZ rect with pdf

And then change ray_color():

colorray_color(constray& r,constcolor& background,consthittable& world,shared_ptr& lights,intdepth){

    ...    ray scattered;    color attenuation;    color emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p);doublepdf_val;    color albedo;if(!rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf_val))returnemitted;

hittable_pdflight_pdf(lights, rec.p);    scattered = ray(rec.p, light_pdf.generate(), r.time());    pdf_val = light_pdf.value(scattered.direction());

returnemitted        + albedo * rec.mat_ptr->scattering_pdf(r, rec, scattered)

* ray_color(scattered, background, world, lights, depth-1) / pdf_val;

}...intmain(){    ...// Worldautoworld = cornell_box();

shared_ptr lights =make_shared(213,343,227,332,554,shared_ptr());

    ...for(intj = image_height-1; j >=0; --j) {std::cerr<<"\rScanlines remaining: "<< j <<' '<

            pixel_color += ray_color(r, background, world, lights, max_depth);

    ...

Listing 29: [main.cc] ray_color function with hittable PDF

At 10 samples per pixel we get:

Image 7: Cornell box, sampling a hittable light, 10 samples per pixel

The Mixture PDF Class

Now we would like to do a mixture density of the cosine and light sampling. The mixture density class is straightforward:

classmixture_pdf:publicpdf {public:mixture_pdf(shared_ptr p0,shared_ptr p1) {p[0] = p0;p[1] = p1;        }virtualdoublevalue(constvec3& direction)constoverride{return0.5* p[0]->value(direction) +0.5*p[1]->value(direction);        }virtualvec3generate()constoverride{if(random_double() <0.5)returnp[0]->generate();elsereturnp[1]->generate();        }public:shared_ptr p[2];};

Listing 30: [pdf.h] The mixture_pdf class

And plugging it into ray_color():

colorray_color(constray& r,constcolor& background,consthittable& world,shared_ptr& lights,intdepth){    ...    ray scattered;    color attenuation;    color emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p);doublepdf_val;    color albedo;if(!rec.mat_ptr->scatter(r, rec, albedo, scattered, pdf_val))returnemitted;

autop0 = make_shared(lights, rec.p);autop1 = make_shared(rec.normal);mixture_pdfmixed_pdf(p0, p1);    scattered = ray(rec.p, mixed_pdf.generate(), r.time());    pdf_val = mixed_pdf.value(scattered.direction());

    ...}

Listing 31: [main.cc] The ray_color function, using mixture PDF

1000 samples per pixel yields:

Image 8: Cornell box, mixture density of cosine and light sampling

We’ve basically gotten this same picture (with different levels of noise) with several different sampling patterns. It looks like the original picture was slightly wrong! Note by “wrong” here I mean not a correct Lambertian picture. Yet Lambertian is just an ideal approximation to matte, so our original picture was some other accidental approximation to matte. I don’t think the new one is any better, but we can at least compare it more easily with other Lambertian renderers.

Some Architectural Decisions

I won't write any code in this chapter. We’re at a crossroads where I need to make some architectural decisions. The mixture-density approach is to not have traditional shadow rays, and is something I personally like, because in addition to lights you can sample windows or bright cracks under doors or whatever else you think might be bright. But most programs branch, and send one or more terminal rays to lights explicitly, and one according to the reflective distribution of the surface. This could be a time you want faster convergence on more restricted scenes and add shadow rays; that’s a personal design preference.

There are some other issues with the code.

The PDF construction is hard coded in the ray_color() function. We should clean that up, probably by passing something into color about the lights. Unlike BVH construction, we should be careful about memory leaks as there are an unbounded number of samples.

The specular rays (glass and metal) are no longer supported. The math would work out if we just made their scattering function a delta function. But that would be floating point disaster. We could either separate out specular reflections, or have surface roughness never be zero and have almost-mirrors that look perfectly smooth but don’t generate NaNs. I don’t have an opinion on which way to do it (I have tried both and they both have their advantages), but we have smooth metal and glass code anyway, so I add perfect specular surfaces that do not do explicit f()/p() calculations.

We also lack a real background function infrastructure in case we want to add an environment map or more interesting functional background. Some environment maps are HDR (the RGB components are floats rather than 0–255 bytes usually interpreted as 0-1). Our output has been HDR all along; we’ve just been truncating it.

Finally, our renderer is RGB and a more physically based one — like an automobile manufacturer might use — would probably need to use spectral colors and maybe even polarization. For a movie renderer, you would probably want RGB. You can make a hybrid renderer that has both modes, but that is of course harder. I’m going to stick to RGB for now, but I will revisit this near the end of the book.

Cleaning Up PDF Management

So far I have the ray_color() function create two hard-coded PDFs:

p0() related to the shape of the light

p1() related to the normal vector and type of surface

We can pass information about the light (or whatever hittable we want to sample) into the ray_color() function, and we can ask the material function for a PDF (we would have to instrument it to do that). We can also either ask hitfunction or the material class to supply whether there is a specular vector.

Diffuse Versus Specular

One thing we would like to allow for is a material like varnished wood that is partially ideal specular (the polish) and partially diffuse (the wood). Some renderers have the material generate two rays: one specular and one diffuse. I am not fond of branching, so I would rather have the material randomly decide whether it is diffuse or specular. The catch with that approach is that we need to be careful when we ask for the PDF value and be aware of whether for this evaluation of ray_color() it is diffuse or specular. Fortunately, we know that we should only call the pdf_value() if it is diffuse so we can handle that implicitly.

We can redesign material and stuff all the new arguments into a struct like we did for hittable:

structscatter_record{    ray specular_ray;boolis_specular;    color attenuation;shared_ptr pdf_ptr;};

classmaterial{public:virtualcoloremitted(

constray& r_in,consthit_record& rec,doubleu,doublev,constpoint3& p

)const{returncolor(0,0,0);        }virtualboolscatter(

constray& r_in,consthit_record& rec, scatter_record& srec

)const{returnfalse;        }virtualdoublescattering_pdf(constray& r_in,consthit_record& rec,constray& scattered        )const{return0;        }};

Listing 32: [material.h] Refactoring the material class

The Lambertian material becomes simpler:

classlambertian:publicmaterial {public:lambertian(constcolor& a) : albedo(make_shared(a)) {}lambertian(shared_ptr a) : albedo(a) {}

virtualboolscatter(constray& r_in,consthit_record& rec, scatter_record& srec        )constoverride{srec.is_specular =false;            srec.attenuation = albedo->value(rec.u, rec.v, rec.p);srec.pdf_ptr =newcosine_pdf(rec.normal);returntrue;        }

doublescattering_pdf(constray& r_in,consthit_record& rec,constray& scattered        )const{autocosine = dot(rec.normal, unit_vector(scattered.direction()));returncosine <0?0: cosine/pi;        }public:shared_ptr albedo;};

Listing 33: [material.h] New lambertian scatter() method

And ray_color() changes are small:

colorray_color(constray& r,constcolor& background,consthittable& world,

shared_ptr& lights,

intdepth) {    hit_record rec;// If we've exceeded the ray bounce limit, no more light is gathered.if(depth <=0)returncolor(0,0,0);// If the ray hits nothing, return the background color.if(!world.hit(r,0.001, infinity, rec))returnbackground;

    scatter_record srec;    color emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p);if(!rec.mat_ptr->scatter(r, rec, srec))returnemitted;autolight_ptr = make_shared(lights, rec.p);mixture_pdfp(light_ptr, srec.pdf_ptr);    ray scattered = ray(rec.p, p.generate(), r.time());autopdf_val = p.value(scattered.direction());returnemitted        + srec.attenuation * rec.mat_ptr->scattering_pdf(r, rec, scattered)* ray_color(scattered, background, world, lights, depth-1) / pdf_val;

}...intmain(){    ...// Worldautoworld = cornell_box();

autolights = make_shared();lights->add(make_shared(213,343,227,332,554,shared_ptr()));lights->add(make_shared(point3(190,90,190),90,shared_ptr()));

    ...

Listing 34: [main.cc] Ray color with scatter

Handling Specular

We have not yet dealt with specular surfaces, nor instances that mess with the surface normal. But this design is clean overall, and those are all fixable. For now, I will just fix specular. Metal and dielectric materials are easy to fix.

classmetal:publicmaterial {public:metal(constcolor& a,doublef) : albedo(a), fuzz(f <1? f :1) {}

virtualboolscatter(constray& r_in,consthit_record& rec, scatter_record& srec        )constoverride{            vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);            srec.specular_ray = ray(rec.p, reflected+fuzz*random_in_unit_sphere());            srec.attenuation = albedo;srec.is_specular =true;srec.pdf_ptr =0;returntrue;        }

public:        color albedo;doublefuzz;};...classdielectric:publicmaterial {public:        ...virtualboolscatter(constray& r_in,consthit_record& rec, scatter_record& srec        )constoverride{

srec.is_specular =true;srec.pdf_ptr =nullptr;srec.attenuation = color(1.0,1.0,1.0);

doublerefraction_ratio = rec.front_face ? (1.0/ir) : ir;            ...

            srec.specular_ray = ray(rec.p, direction, r_in.time());

returntrue;        }    ...};

Listing 35: [material.h] The metal and dielectric scatter methods

Note that if fuzziness is high, this surface isn’t ideally specular, but the implicit sampling works just like it did before.

ray_color() just needs a new case to generate an implicitly sampled ray:

colorray_color(constray& r,constcolor& background,consthittable& world,shared_ptr& lights,intdepth){    ...    scatter_record srec;    color emitted = rec.mat_ptr->emitted(r, rec, rec.u, rec.v, rec.p);if(!rec.mat_ptr->scatter(r, rec, srec))returnemitted;

if(srec.is_specular) {returnsrec.attenuation* ray_color(srec.specular_ray, background, world, lights, depth-1);    }

    ...}

Listing 36: [main.cc] Ray color function with implicitly-sampled rays

We also need to change the block to metal. We'll also swap out the short block for a glass sphere.

hittable_listcornell_box(){    hittable_list objects;autored  = make_shared(color(.65,.05,.05));autowhite = make_shared(color(.73,.73,.73));autogreen = make_shared(color(.12,.45,.15));autolight = make_shared(color(15,15,15));objects.add(make_shared(0,555,0,555,555, green));objects.add(make_shared(0,555,0,555,0, red));objects.add(make_shared(make_shared(213,343,227,332,554, light)));objects.add(make_shared(0,555,0,555,555, white));objects.add(make_shared(0,555,0,555,0, white));objects.add(make_shared(0,555,0,555,555, white));

shared_ptr aluminum = make_shared(color(0.8,0.85,0.88),0.0);shared_ptr box1 = make_shared(point3(0,0,0), point3(165,330,165), aluminum);

box1 = make_shared(box1,15);box1 = make_shared(box1, vec3(265,0,295));    objects.add(box1);

autoglass = make_shared(1.5);objects.add(make_shared(point3(190,90,190),90, glass));

returnobjects;}

Listing 37: [main.cc] Cornell box scene with aluminum material

The resulting image has a noisy reflection on the ceiling because the directions toward the box are not sampled with more density.

Image 9: Cornell box with arbitrary PDF functions

We could make the PDF include the block. Let’s do that instead with a glass sphere because it’s easier.

Sampling a Sphere Object

When we sample a sphere’s solid angle uniformly from a point outside the sphere, we are really just sampling a cone uniformly (the cone is tangent to the sphere). Let’s say the code has theta_max. Recall from the Generating Random Directions chapter that to sample \(\theta\) we have:

$$ r_2 = \int_{0}^{\theta} 2\pi \cdot f(t) \cdot \sin(t) dt $$

Here \(f(t)\) is an as yet uncalculated constant \(C\), so:

$$ r_2 = \int_{0}^{\theta} 2\pi \cdot C \cdot \sin(t) dt $$

Doing some algebra/calculus this yields:

$$ r_2 = 2\pi \cdot C \cdot (1-\cos(\theta)) $$

So

$$ cos(\theta) = 1 - \frac{r_2}{2 \pi \cdot C} $$

We know that for \(r_2 = 1\) we should get \(\theta_{max}\), so we can solve for \(C\):

$$ \cos(\theta) = 1 + r_2 \cdot (\cos(\theta_{max})-1) $$

\(\phi\) we sample like before, so:

$$ z = \cos(\theta) = 1 + r_2 \cdot (\cos(\theta_{max}) - 1) $$ $$ x = \cos(\phi) \cdot \sin(\theta) = \cos(2\pi \cdot r_1) \cdot \sqrt{1-z^2} $$ $$ y = \sin(\phi) \cdot \sin(\theta) = \sin(2\pi \cdot r_1) \cdot \sqrt{1-z^2} $$

Now what is \(\theta_{max}\)?

Figure 8: A sphere-enclosing cone

We can see from the figure that \(\sin(\theta_{max}) = R / length(\mathbf{c} - \mathbf{p})\). So:

$$ \cos(\theta_{max}) = \sqrt{1 - \frac{R^2}{length^2(\mathbf{c} - \mathbf{p})}} $$

We also need to evaluate the PDF of directions. For directions toward the sphere this is \(1/solid\_angle\). What is the solid angle of the sphere? It has something to do with the \(C\) above. It, by definition, is the area on the unit sphere, so the integral is

$$ solid\_angle = \int_{0}^{2\pi} \int_{0}^{\theta_{max}} \sin(\theta) = 2 \pi \cdot (1-\cos(\theta_{max})) $$

It’s good to check the math on all such calculations. I usually plug in the extreme cases (thank you for that concept, Mr. Horton — my high school physics teacher). For a zero radius sphere \(\cos(\theta_{max}) = 0\), and that works. For a sphere tangent at \(\mathbf{p}\), \(\cos(\theta_{max}) = 0\), and \(2\pi\) is the area of a hemisphere, so that works too.

Updating the Sphere Code

The sphere class needs the two PDF-related functions:

doublesphere::pdf_value(constpoint3& o,constvec3& v)const{    hit_record rec;if(!this->hit(ray(o, v),0.001, infinity, rec))return0;autocos_theta_max =sqrt(1- radius*radius/(center-o).length_squared());autosolid_angle =2*pi*(1-cos_theta_max);return1/ solid_angle;}vec3sphere::random(constpoint3& o)const{    vec3 direction = center - o;autodistance_squared = direction.length_squared();    onb uvw;    uvw.build_from_w(direction);returnuvw.local(random_to_sphere(radius, distance_squared));}

Listing 38: [sphere.h] Sphere with PDF

With the utility function:

inlinevec3random_to_sphere(doubleradius,doubledistance_squared){autor1 = random_double();autor2 = random_double();autoz =1+ r2*(sqrt(1-radius*radius/distance_squared) -1);autophi =2*pi*r1;autox =cos(phi)*sqrt(1-z*z);autoy =sin(phi)*sqrt(1-z*z);returnvec3(x, y, z);}

Listing 39: [pdf.h] The random_to_sphere utility function

We can first try just sampling the sphere rather than the light:

intmain(){    ...// Worldautoworld = cornell_box();

shared_ptr lights =//  make_shared(213, 343, 227, 332, 554, shared_ptr());make_shared(point3(190,90,190),90,shared_ptr());

    ...

Listing 40: [main.cc] Sampling just the sphere

This yields a noisy box, but the caustic under the sphere is good. It took five times as long as sampling the light did for my code. This is probably because those rays that hit the glass are expensive!

Image 10: Cornell box with glass sphere, using new PDF functions

Adding PDF Functions to Hittable Lists

We should probably just sample both the sphere and the light. We can do that by creating a mixture density of their two densities. We could do that in the ray_color() function by passing a list of hittables in and building a mixture PDF, or we could add PDF functions to hittable_list. I think both tactics would work fine, but I will go with instrumenting hittable_list.

doublehittable_list::pdf_value(constpoint3& o,constvec3& v)const{autoweight =1.0/objects.size();autosum =0.0;for(constauto& object : objects)        sum += weight * object->pdf_value(o, v);returnsum;}vec3hittable_list::random(constvec3& o)const{autoint_size =static_cast(objects.size());returnobjects[random_int(0, int_size-1)]->random(o);}

Listing 41: [hittable_list.h] Creating a mixture of densities

We assemble a list to pass to ray_color() from main():

hittable_list lights;lights.add(make_shared(213,343,227,332,554,0));lights.add(make_shared(point3(190,90,190),90,0));

Listing 42: [main.cc] Updating the scene

And we get a decent image with 1000 samples as before:

Image 11: Cornell Cornell box, using a mixture of glass & light PDFs

Handling Surface Acne

An astute reader pointed out there are some black specks in the image above. All Monte Carlo Ray Tracers have this as a main loop:

pixel_color = average(manymany samples)

If you find yourself getting some form of acne in the images, and this acne is white or black, so one “bad” sample seems to kill the whole pixel, that sample is probably a huge number or a NaN (Not A Number). This particular acne is probably a NaN. Mine seems to come up once in every 10–100 million rays or so.

So big decision: sweep this bug under the rug and check for NaNs, or just kill NaNs and hope this doesn't come back to bite us later. I will always opt for the lazy strategy, especially when I know floating point is hard. First, how do we check for a NaN? The one thing I always remember for NaNs is that a NaN does not equal itself. Using this trick, we update the write_color() function to replace any NaN components with zero:

voidwrite_color(std::ostream &out, color pixel_color,intsamples_per_pixel){autor = pixel_color.x();autog = pixel_color.y();autob = pixel_color.z();

// Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.if(r != r) r =0.0;if(g != g) g =0.0;if(b != b) b =0.0;

// Divide the color by the number of samples and gamma-correct for gamma=2.0.autoscale =1.0/ samples_per_pixel;r =sqrt(scale * r);g =sqrt(scale * g);b =sqrt(scale * b);// Write the translated [0,255] value of each color component.out <(256* clamp(r,0.0,0.999)) <<' '<(256* clamp(g,0.0,0.999)) <<' '<(256* clamp(b,0.0,0.999)) <<'\n';}

Listing 43: [color.h] NaN-tolerant write_color function

Happily, the black specks are gone:

Image 12: Cornell box with anti-acne color function

The Rest of Your Life

The purpose of this book was to show the details of dotting all the i’s of the math on one way of organizing a physically based renderer’s sampling approach. Now you can explore a lot of different potential paths.

If you want to explore Monte Carlo methods, look into bidirectional and path spaced approaches such as Metropolis. Your probability space won't be over solid angle, but will instead be over path space, where a path is a multidimensional point in a high-dimensional space. Don’t let that scare you — if you can describe an object with an array of numbers, mathematicians call it a point in the space of all possible arrays of such points. That’s not just for show. Once you get a clean abstraction like that, your code can get clean too. Clean abstractions are what programming is all about!

If you want to do movie renderers, look at the papers out of studios and Solid Angle. They are surprisingly open about their craft.

If you want to do high-performance ray tracing, look first at papers from Intel and NVIDIA. Again, they are surprisingly open.

If you want to do hard-core physically based renderers, convert your renderer from RGB to spectral. I am a big fan of each ray having a random wavelength and almost all the RGBs in your program turning into floats. It sounds inefficient, but it isn’t!

Regardless of what direction you take, add a glossy BRDF model. There are many to choose from, and each has its advantages.

Have fun!

Peter Shirley

Salt Lake City, March, 2016

Acknowledgments

Original Manuscript Help

Dave Hart

Jean Buckley

Web Release

Berna Kabadayı

Lorenzo Mancini

Lori Whippler Hollasch

Ronald Wotzlaw

Corrections and Improvements

Aaryaman Vasishta

Andrew Kensler

Antonio Gamiz

Apoorva Joshi

Aras Pranckevičius

Becker

Ben Kerl

Benjamin Summerton

Bennett Hardwick

Dan Drummond

David Chambers

David Hart

Eric Haines

Fabio Sancinetti

Filipe Scur

Frank He

Gerrit Wessendorf

Grue Debry

Ingo Wald

Jason Stone

Jean Buckley

Joey Cho

John Kilpatrick

Kaan Eraslan

Lorenzo Mancini

Manas Kale

Marcus Ottosson

Mark Craig

Matthew Heimlich

Nakata Daisuke

Paul Melis

Phil Cristensen

Ronald Wotzlaw

Shaun P. Lee

Shota Kawajiri

Tatsuya Ogawa

Thiago Ize

Vahan Sosoyan

ZeHao Chen

Special Thanks

Thanks to the team at Limnu for help on the figures.

These books are entirely written in Morgan McGuire's fantastic and free Markdeep library. To see what this looks like, view the page source from your browser.

Thanks to Helen Hu for graciously donating her https://github.com/RayTracing/ GitHub organization to this project.

Citing This Book

Consistent citations make it easier to identify the source, location and versions of this work. If you are citing this book, we ask that you try to use one of the following forms if possible.

Basic Data

Title (series): “Ray Tracing in One Weekend Series”

Title (book): “Ray Tracing: The Rest of Your Life”

Author: Peter Shirley

Editors: Steve Hollasch, Trevor David Black

Version/Edition: v3.2.3

Date: 2020-12-07

URL (series): https://raytracing.github.io/

URL (book): https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html

Snippets

Markdown

[_Ray Tracing: The Rest of Your Life_](https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html)

HTML

Ray Tracing: The Rest of Your Life

LaTeX and BibTex

~\cite{Shirley2020RTW3}@misc{Shirley2020RTW3,  title = {Ray Tracing: The Rest of Your Life},  author = {Peter Shirley},year = {2020},  month = {December}note = {\small \texttt{https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html}},url = {https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html}}

BibLaTeX

\usepackage{biblatex}~\cite{Shirley2020RTW3}@online{Shirley2020RTW3,  title = {Ray Tracing: The Rest of Your Life},  author = {Peter Shirley},year = {2020},  month = {December}url = {https://raytracing.github.io/books/RayTracingTheRestOfYourLife.html}}

IEEE

“Ray Tracing: The Rest of Your Life.”raytracing.github.io/books/RayTracingTheRestOfYourLife.html(accessed MMM. DD, YYYY)

MLA:

Ray Tracing: The Rest of Your Life. raytracing.github.io/books/RayTracingTheRestOfYourLife.htmlAccessed DD MMM. YYYY.

formatted by Markdeep 1.11  

你可能感兴趣的:(2021-05-13)