Skip to content

Commit d4af17a

Browse files
celephhollasch
authored andcommitted
Markdeep cleanup pass on TheRestOfYourLife: Fix line length, indentations, spaces to meet style guidelines.
1 parent 558156b commit d4af17a

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

books/RayTracingTheRestOfYourLife.html

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
basic characteristic of simple programs producing noisy but ever-better answers is what MC is all
5858
about, and it is especially good for applications like graphics where great accuracy is not needed.
5959

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

6464
![Figure 2-1](../images/fig-3-02-1.jpg)
@@ -67,7 +67,7 @@
6767
up inside the circle should be proportional to the area of the circle. The exact fraction should in
6868
fact be the ratio of the circle area to the square area. Fraction:
6969

70-
$$ \frac{Pi \cdot R^2}{(2R)^2} = \frac{Pi}{4} $$
70+
$$ \frac{Pi \cdot R^2}{(2R)^2} = \frac{Pi}{4} $$
7171

7272
Since the *R* cancels out, we can pick whatever is computationally convenient. Let’s go with R=1
7373
centered at the origin:
@@ -272,8 +272,8 @@
272272
How do we generate a random number with that pdf $p(r)$? For that we will need some more machinery.
273273
Don’t worry this doesn’t go on forever!
274274

275-
Given a random number from `d = random_double()` that is uniform and between 0 and 1, we should be able
276-
to find some function $f(d)$ that gives us what we want. Suppose $e = f(d) = d^2$. That is no
275+
Given a random number from `d = random_double()` that is uniform and between 0 and 1, we should be
276+
able to find some function $f(d)$ that gives us what we want. Suppose $e = f(d) = d^2$. That is no
277277
longer a uniform _pdf_ . The _pdf_ of $e$ will be bigger near 0 than it is near 1 (squaring a
278278
number between 0 and 1 makes it smaller). To take this general observation to a function, we need
279279
the cumulative probability distribution function $P(x)$:
@@ -294,8 +294,8 @@
294294

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

@@ -324,7 +324,8 @@
324324

325325
$$ e = \sqrt{4*random_double()} $$
326326

327-
Note that does range 0 to 2 as hoped, and if we send in $1/4$ for `random_double()` we get 1 as desired.
327+
Note that does range 0 to 2 as hoped, and if we send in $1/4$ for `random_double()` we get 1 as
328+
desired.
328329

329330
We can now sample our old integral
330331

@@ -448,7 +449,7 @@
448449
unit-sphere. The same methodology as before applies. But now we need to have a pdf defined over 2D.
449450
Suppose we have this integral over all directions:
450451

451-
$$ \int cos^2(\theta) $$
452+
$$ \int cos^2(\theta) $$
452453

453454
By MC integration, we should just be able to sample $cos^2(\theta) / p(direction)$. But what is
454455
direction in that context? We could make it based on polar coordinates, so $p$ would be in terms of
@@ -498,14 +499,15 @@
498499
}
499500
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
500501

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

504505
The key point here is that all the integrals and probability and all that are over the unit sphere.
505506
The area on the unit sphere is how you measure the directions. Call it direction, solid angle, or
506-
area-- it’s all the same thing. Solid angle is the term usually used. If you are comfortable with
507+
area -- it’s all the same thing. Solid angle is the term usually used. If you are comfortable with
507508
that, great! If not, do what I do and imagine the area on the unit sphere that a set of directions
508-
goes through. The solid angle $\omega$ and the projected area $A$ on the unit sphere are the same thing.
509+
goes through. The solid angle $\omega$ and the projected area $A$ on the unit sphere are the same
510+
thing.
509511

510512
![Figure 4-1](../images/fig-3-04-1.jpg)
511513

@@ -524,6 +526,7 @@
524526
First, is the light absorbed?
525527

526528
Probability of light scattering: $A$
529+
527530
Probability of light being absorbed: $1-A$
528531

529532
Here $A$ stands for _albedo_ (latin for _whiteness_). Albedo is a precise technical term in some
@@ -535,19 +538,19 @@
535538

536539
If the light does scatter, it will have a directional distribution that we can describe as a pdf
537540
over solid angle. I will refer to this as its _scattering pdf_: $s(direction)$. The scattering pdf
538-
can also vary with incident direction, as you will notice when you look at reflections off a road--
541+
can also vary with incident direction, as you will notice when you look at reflections off a road --
539542
they become mirror-like as your viewing angle approaches grazing.
540543

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

543-
$$ Color = \int A \cdot s(direction) \cdot color(direction) $$
546+
$$ Color = \int A \cdot s(direction) \cdot color(direction) $$
544547

545548
Note that $A$ and $s()$ might depend on the view direction, so of course color can vary with view
546549
direction. Also $A$ and $s()$ may vary with position on the surface or within the volume.
547550

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

550-
$$ Color = (A \cdot s(direction) \cdot color(direction)) / p(direction) $$
553+
$$ Color = (A \cdot s(direction) \cdot color(direction)) / p(direction) $$
551554

552555
Where $p(direction)$ is the pdf of whatever direction we randomly generate.
553556

@@ -559,21 +562,21 @@
559562

560563
To see that remember that in spherical coordinates remember that:
561564

562-
$$ \delta A = sin(\theta) \delta \theta \delta \phi $$
565+
$$ \delta A = sin(\theta) \delta \theta \delta \phi $$
563566

564567
So:
565568

566-
$$ Area = \int_{0}^{2 \pi} \int_{0}^{\pi / 2} cos(\theta)sin(\theta) \delta \theta \delta \phi =
569+
$$ Area = \int_{0}^{2 \pi} \int_{0}^{\pi / 2} cos(\theta)sin(\theta) \delta \theta \delta \phi =
567570
2 \pi \cdot \frac{1}{2} = \pi $$
568571

569572
So for a Lambertian surface the scattering pdf is:
570573

571-
$$ s(direction) = cos(\theta) / \pi $$
574+
$$ s(direction) = cos(\theta) / \pi $$
572575

573576
If we sample using the same pdf, so $p(direction) = cos(\theta) / \pi$, the numerator and
574577
denominator cancel out and we get:
575578

576-
$$ Color = A * s(direction) $$
579+
$$ Color = A * s(direction) $$
577580

578581
This is exactly what we had in our original color() function! But we need to generalize now so we
579582
can send extra rays in important directions such as toward the lights.
@@ -584,7 +587,7 @@
584587
If you read the literature, you’ll see reflection described by the bidirectional reflectance
585588
distribution function (BRDF). It relates pretty simply to our terms:
586589

587-
$$ BRDF = A * s(direction) / cos(\theta) $$
590+
$$ BRDF = A * s(direction) / cos(\theta) $$
588591

589592
So for a Lambertian surface for example, $BRDF = A / \pi$. Translation between our terms and BRDF is
590593
easy.
@@ -604,15 +607,15 @@
604607
linear mixtures of them to form mixture densities that are also pdfs. For example, the simplest
605608
would be:
606609

607-
$$ p(direction) = 0.5 \cdot pLight(direction) + 0.5* \cdot pSurface(direction) $$
610+
$$ p(direction) = 0.5 \cdot pLight(direction) + 0.5* \cdot pSurface(direction) $$
608611

609612
As long as the weights are positive and add up to one, any such mixture of pdfs is a pdf. And
610-
remember, we can use any pdf-- it doesn’t change the answer we converge to! So, the game is to
611-
figure out how to make the pdf larger where the product $s(direction)*color(direction)$ is large. For
612-
diffuse surfaces, this is mainly a matter of guessing where $color(direction)$ is high.
613+
remember, we can use any pdf -- it doesn’t change the answer we converge to! So, the game is to
614+
figure out how to make the pdf larger where the product $s(direction)*color(direction)$ is large.
615+
For diffuse surfaces, this is mainly a matter of guessing where $color(direction)$ is high.
613616

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

617620
Let’s do simple refactoring and temporarily remove all materials that aren’t Lambertian. We can use
618621
our Cornell Box scene again, and let’s generate the camera in the function that generates the model:
@@ -654,7 +657,7 @@
654657
light.
655658

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

660663
We modify the base-class _material_ to enable this importance sampling:
@@ -749,7 +752,7 @@
749752

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

755758
Let’s build some infrastructure to address this.
@@ -783,7 +786,7 @@
783786

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

786-
For theta we have:
789+
For $\theta$ we have:
787790

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

@@ -841,7 +844,7 @@
841844

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

844-
Now let’s derive uniform on the hemisphere . The density being uniform on the hemisphere means
847+
Now let’s derive uniform on the hemisphere. The density being uniform on the hemisphere means
845848
$p(direction) = 1 / (2 \pi)$ and just changing the constant in the theta equations yields:
846849

847850
$$ \cos(\theta) = 1 - r_2 $$
@@ -1139,9 +1142,9 @@
11391142
form a mixture density. Any weighted average of _pdf_ s is a _pdf_. For example, we could just
11401143
average the two densities:
11411144

1142-
$$ pdf\_mixture(direction) = \frac{1}{2} pdf\_reflection(direction) +
1145+
$$ pdf\_mixture(direction) = \frac{1}{2} pdf\_reflection(direction) +
11431146
\frac{1}{2}pdf\_light(direction)
1144-
$$
1147+
$$
11451148

11461149
How would we instrument our code to do that? There is a very important detail that makes this not
11471150
quite as easy as one might expect. Choosing the random direction is simple:
@@ -1630,47 +1633,47 @@
16301633
really just sampling a cone uniformly (the cone is tangent to the sphere). Let’s say the code has
16311634
`theta_max`. Recall from Chapter 9, that to sample $\theta$ we have:
16321635

1633-
$$ r2 = \int_{0}^{\theta} 2 \pi \cdot f(t) \cdot sin(t) dt $$
1636+
$$ r2 = \int_{0}^{\theta} 2 \pi \cdot f(t) \cdot sin(t) dt $$
16341637

16351638
Here $f(t)$ is an as yet uncalculated constant $C$, so:
16361639

1637-
$$ r2 = \int_{0}^{\theta} 2 \pi \cdot C \cdot sin(t) dt $$
1640+
$$ r2 = \int_{0}^{\theta} 2 \pi \cdot C \cdot sin(t) dt $$
16381641

16391642
Doing some algebra/calculus this yields:
16401643

1641-
$$ r2 = 2 \pi \cdot C \cdot (1-cos(\theta)) $$
1644+
$$ r2 = 2 \pi \cdot C \cdot (1-cos(\theta)) $$
16421645

16431646
So
16441647

1645-
$$ cos(\theta) = 1 - r2/(2 \pi \cdot C) $$
1648+
$$ cos(\theta) = 1 - r2/(2 \pi \cdot C) $$
16461649

16471650
We know that for $r2 = 1$ we should get $\theta_{max}$, so we can solve for $C$:
16481651

1649-
$$ cos(\theta) = 1 + r2 \cdot (cos(\theta_{max})-1) $$
1652+
$$ cos(\theta) = 1 + r2 \cdot (cos(\theta_{max})-1) $$
16501653

16511654
$\phi$ we sample like before, so:
16521655

1653-
$$ z = cos(\theta) = 1 + r2 \cdot (cos(\theta_{max})-1) $$
1654-
$$ x = cos(\phi) \cdot sin(\theta) = cos(2 \pi \cdot r1) \cdot \sqrt{1-z^2} $$
1655-
$$ y = sin(\phi) \cdot sin(\theta) = sin(2 \pi \cdot r1) \cdot \sqrt{1-z^2} $$
1656+
$$ z = cos(\theta) = 1 + r2 \cdot (cos(\theta_{max})-1) $$
1657+
$$ x = cos(\phi) \cdot sin(\theta) = cos(2 \pi \cdot r1) \cdot \sqrt{1-z^2} $$
1658+
$$ y = sin(\phi) \cdot sin(\theta) = sin(2 \pi \cdot r1) \cdot \sqrt{1-z^2} $$
16561659

16571660
Now what is $\theta_{max}$?
16581661

16591662
![Figure 12-1](../images/fig-3-12-1.jpg)
16601663

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

1663-
$$ cos(\theta_{max}) = \sqrt{1- (R / length( c - p ))^2} $$
1666+
$$ cos(\theta_{max}) = \sqrt{1- (R / length( c - p ))^2} $$
16641667

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

1669-
$$ solid\_angle = \int_{0}^{2 \pi} \int_{0}^{\theta_{max}} sin(\theta) d \theta d \phi
1672+
$$ solid\_angle = \int_{0}^{2 \pi} \int_{0}^{\theta_{max}} sin(\theta) d \theta d \phi
16701673
= 2 \pi \cdot (1-cos(\theta_{max})) $$
16711674

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

0 commit comments

Comments
 (0)