|
388 | 388 | Our Buffon Needle example is a way of calculating $\pi$ by solving for the ratio of the area of the
|
389 | 389 | circle and the area of the inscribing square:
|
390 | 390 |
|
391 |
| - $$ \frac{\text{area}(circle)}{\text{area}(square)} = \frac{\pi}{4} $$ |
| 391 | + $$ \frac{\text{area}(\mathit{circle})}{\text{area}(\mathit{square})} = \frac{\pi}{4} $$ |
392 | 392 |
|
393 | 393 | We picked a bunch of random points in the inscribing square and counted the fraction of them that
|
394 | 394 | were also in the unit circle. This fraction was an estimate that tended toward $\frac{\pi}{4}$ as
|
|
397 | 397 | $\frac{\pi}{4}$, and we know that the area of a inscribing square is $4r^2$, so we could then use
|
398 | 398 | those two quanties to get the area of a circle:
|
399 | 399 |
|
400 |
| - $$ \frac{\text{area}(circle)}{\text{area}(square)} = \frac{\pi}{4} $$ |
401 |
| - $$ \frac{\text{area}(circle)}{(2r)^2} = \frac{\pi}{4} $$ |
402 |
| - $$ \text{area}(circle) = \frac{\pi}{4} 4r^2 $$ |
403 |
| - $$ \text{area}(circle) = \pi r^2 $$ |
| 400 | + $$ \frac{\text{area}(\mathit{circle})}{\text{area}(\mathit{square})} = \frac{\pi}{4} $$ |
| 401 | + $$ \frac{\text{area}(\mathit{circle})}{(2r)^2} = \frac{\pi}{4} $$ |
| 402 | + $$ \text{area}(\mathit{circle}) = \frac{\pi}{4} 4r^2 $$ |
| 403 | + $$ \text{area}(\mathit{circle}) = \pi r^2 $$ |
404 | 404 |
|
405 | 405 | We choose a circle with radius $r = 1$ and get:
|
406 | 406 |
|
407 |
| - $$ \text{area}(circle) = \pi $$ |
| 407 | + $$ \text{area}(\mathit{circle}) = \pi $$ |
408 | 408 |
|
409 | 409 | Our work above is equally valid as a means to solve for $pi$ as it is a means to solve for the area
|
410 | 410 | of a circle:
|
|
1340 | 1340 | in the _LMS color space_ (long, medium, short).
|
1341 | 1341 |
|
1342 | 1342 | If the light does scatter, it will have a directional distribution that we can describe as a PDF
|
1343 |
| -over solid angle. I will refer to this as its _scattering PDF_: $pScatter()$. The scattering PDF |
1344 |
| -will vary with the outgoing direction: $pScatter(\omega_o)$. The scattering PDF can also vary with |
1345 |
| -_incident direction_: $pScatter(\omega_i, \omega_o)$. You can see this varying with incident |
1346 |
| -direction when you look at reflections off a road -- they become mirror-like as your viewing angle |
1347 |
| -(incident angle) approaches grazing. Lastly, the scattering PDF can also depend on the scattering |
1348 |
| -position: $pScatter(\textbf{x}, \omega_i, \omega_o)$. The $\textbf{x}$ is just a vector representing |
1349 |
| -the scattering position: $\textbf{x} = (x, y, z)$. The Albedo of an object can also depend on these |
1350 |
| -quantities: $A(\textbf{x}, \omega_i, \omega_o)$. |
| 1343 | +over solid angle. I will refer to this as its _scattering PDF_: $\mathit{pScatter}()$. The |
| 1344 | +scattering PDF will vary with the outgoing direction: $\mathit{pScatter}(\omega_o)$. The scattering |
| 1345 | +PDF can also vary with _incident direction_: $\mathit{pScatter}(\omega_i, \omega_o)$. You can see |
| 1346 | +this varying with incident direction when you look at reflections off a road -- they become |
| 1347 | +mirror-like as your viewing angle (incident angle) approaches grazing. Lastly, the scattering PDF |
| 1348 | +can also depend on the scattering position: $\mathit{pScatter}(\textbf{x}, \omega_i, \omega_o)$. The |
| 1349 | +$\textbf{x}$ is just a vector representing the scattering position: $\textbf{x} = (x, y, z)$. The |
| 1350 | +Albedo of an object can also depend on these quantities: $A(\textbf{x}, \omega_i, \omega_o)$. |
1351 | 1351 |
|
1352 | 1352 | <div class='together'>
|
1353 | 1353 | The color of a surface is found by integrating these terms over the unit hemisphere by the incident
|
1354 | 1354 | direction:
|
1355 | 1355 |
|
1356 | 1356 | $$ \text{Color}_o(\textbf{x}, \omega_o) = \int_{\omega_i}
|
1357 | 1357 | A(\textbf{x}, \omega_i, \omega_o) \cdot
|
1358 |
| - pScatter(\textbf{x}, \omega_i, \omega_o) \cdot |
| 1358 | + \mathit{pScatter}(\textbf{x}, \omega_i, \omega_o) \cdot |
1359 | 1359 | \text{Color}_i(\textbf{x}, \omega_i) $$
|
1360 | 1360 |
|
1361 | 1361 | We've added a $\text{Color}_i$ term. The scattering PDF and the albedo at the surface of an object
|
1362 | 1362 | are acting as filters to the light that is shining on that point. So we need to solve for the light
|
1363 | 1363 | that is shining on that point. This is a recursive algorithm, and is the reason our `ray_color`
|
1364 | 1364 | function returns the color of the current object multiplied by the color of the next ray.
|
1365 | 1365 |
|
1366 |
| -Note that $A()$, $pScatter()$, and $\text{Color}_i()$ may all depend on the wavelength of the light, |
1367 |
| -$\lambda$, but I've left it out of the equation because it's complicated enough as it is. |
| 1366 | +Note that $A()$, $\mathit{pScatter}()$, and $\text{Color}_i()$ may all depend on the wavelength of |
| 1367 | +the light, $\lambda$, but I've left it out of the equation because it's complicated enough as it is. |
1368 | 1368 | </div>
|
1369 | 1369 |
|
1370 | 1370 |
|
|
1374 | 1374 | If we apply the Monte Carlo basic formula we get the following statistical estimate:
|
1375 | 1375 |
|
1376 | 1376 | $$ \text{Color}_o = \frac{A(\textbf{x}, \omega_i, \omega_o) \cdot
|
1377 |
| - pScatter(\textbf{x}, \omega_i, \omega_o) \cdot |
| 1377 | + \mathit{pScatter}(\textbf{x}, \omega_i, \omega_o) \cdot |
1378 | 1378 | \text{Color}_i(\textbf{x}, \omega_i)}
|
1379 | 1379 | {p(\textbf{x}, \omega_o)} $$
|
1380 | 1380 |
|
|
1383 | 1383 | </div>
|
1384 | 1384 |
|
1385 | 1385 | For a Lambertian surface we already implicitly implemented this formula for the special case where
|
1386 |
| -$p()$ is a cosine density. The $pScatter()$ of a Lambertian surface is proportional to |
| 1386 | +$p()$ is a cosine density. The $\mathit{pScatter}()$ of a Lambertian surface is proportional to |
1387 | 1387 | $\cos(\theta_o)$, where $\theta_o$ is the angle relative to the surface normal. All two dimensional
|
1388 |
| -PDFs need to integrate to one over the whole surface (remember that $pScatter()$ is a PDF). We set |
1389 |
| -$pScatter(\theta_o < 0) = 0$ so that we don't scatter below the horizon. The integral of |
1390 |
| -$\cos(\theta_o)$ over the hemisphere is $\pi$. |
| 1388 | +PDFs need to integrate to one over the whole surface (remember that $\mathit{pScatter}()$ is a |
| 1389 | +PDF). We set $\mathit{pScatter}(\theta_o < 0) = 0$ so that we don't scatter below the horizon. The |
| 1390 | +integral of $\cos(\theta_o)$ over the hemisphere is $\pi$. |
1391 | 1391 |
|
1392 | 1392 | <div class='together'>
|
1393 | 1393 | To integrate over the hemisphere, remember that in spherical coordinates:
|
|
1404 | 1404 | If we have a scattering PDF that is $\cos(\theta_o)$ over the hemisphere, then we need to normalize
|
1405 | 1405 | by $\pi$. So for a Lambertian surface the scattering PDF is:
|
1406 | 1406 |
|
1407 |
| - $$ pScatter(\omega_o) = \frac{\cos(\theta_o)}{\pi} $$ |
| 1407 | + $$ \mathit{pScatter}(\omega_o) = \frac{\cos(\theta_o)}{\pi} $$ |
1408 | 1408 | </div>
|
1409 | 1409 |
|
1410 | 1410 | <div class='together'>
|
1411 | 1411 | We'll assume that the PDF is equal to the scattering PDF:
|
1412 | 1412 |
|
1413 |
| - $$ p(\omega_o) = pScatter() = \frac{\cos(\theta_o)}{\pi} $$ |
| 1413 | + $$ p(\omega_o) = \mathit{pScatter}() = \frac{\cos(\theta_o)}{\pi} $$ |
1414 | 1414 |
|
1415 | 1415 | The numerator and denominator cancel out, and we get:
|
1416 | 1416 |
|
|
1428 | 1428 | _Bidirectional Reflectance Distribution Function_ (BRDF). It relates pretty simply to our terms:
|
1429 | 1429 |
|
1430 | 1430 | $$ BRDF(\omega_i, \omega_o) = \frac{A(\textbf{x}, \omega_i, \omega_o) \cdot
|
1431 |
| - pScatter(\textbf{x}, \omega_i, \omega_o)}{\cos(\theta_o)} $$ |
| 1431 | + \mathit{pScatter}(\textbf{x}, \omega_i, \omega_o)}{\cos(\theta_o)} $$ |
1432 | 1432 |
|
1433 | 1433 | So for a Lambertian surface for example, $BRDF = A / \pi$. Translation between our terms and BRDF is
|
1434 | 1434 | easy. For participating media (volumes), our albedo is usually called the _scattering albedo_, and
|
|
1446 | 1446 | <div class='together'>
|
1447 | 1447 | Our goal over the next two chapters is to instrument our program to send a bunch of extra rays
|
1448 | 1448 | toward light sources so that our picture is less noisy. Let’s assume we can send a bunch of rays
|
1449 |
| -toward the light source using a PDF $pLight(\omega_o)$. Let’s also assume we have a PDF related to |
1450 |
| -$pScatter$, and let’s call that $pSurface(\omega_o)$. A great thing about PDFs is that you can just |
1451 |
| -use linear mixtures of them to form mixture densities that are also PDFs. For example, the simplest |
1452 |
| -would be: |
| 1449 | +toward the light source using a PDF $\mathit{pLight}(\omega_o)$. Let’s also assume we have a PDF |
| 1450 | +related to $\mathit{pScatter}$, and let’s call that $\mathit{pSurface}(\omega_o)$. A great thing |
| 1451 | +about PDFs is that you can just use linear mixtures of them to form mixture densities that are also |
| 1452 | +PDFs. For example, the simplest would be: |
1453 | 1453 |
|
1454 |
| - $$ p(\omega_o) = \frac{1}{2} pSurface(\omega_o) + \frac{1}{2} pLight(\omega_o)$$ |
| 1454 | + $$ p(\omega_o) = \frac{1}{2} \mathit{pSurface}(\omega_o) + \frac{1}{2} |
| 1455 | + \mathit{pLight}(\omega_o)$$ |
1455 | 1456 | </div>
|
1456 | 1457 |
|
1457 | 1458 | As long as the weights are positive and add up to one, any such mixture of PDFs is a PDF. Remember,
|
1458 | 1459 | we can use any PDF: _all PDFs eventually converge to the correct answer_. So, the game is to figure
|
1459 | 1460 | out how to make the PDF larger where the product
|
1460 | 1461 |
|
1461 |
| - $$ pScatter(\textbf{x}, \omega_i, \omega_o) \cdot \text{Color}_i(\textbf{x}, \omega_i) $$ |
| 1462 | + $$ \mathit{pScatter}(\textbf{x}, \omega_i, \omega_o) \cdot |
| 1463 | + \text{Color}_i(\textbf{x}, \omega_i) $$ |
1462 | 1464 |
|
1463 | 1465 | is largest. For diffuse surfaces, this is mainly a matter of guessing where
|
1464 | 1466 | $\text{Color}_i(\textbf{x}, \omega_i)$ is largest. Which is equivalent to guessing where the most
|
1465 | 1467 | light is coming from.
|
1466 | 1468 |
|
1467 |
| -For a mirror, $pScatter()$ is huge only near one direction, so $pScatter()$ matters a lot more. In |
1468 |
| -fact, most renderers just make mirrors a special case, and make the $pScatter()/p()$ implicit -- our |
1469 |
| -code currently does that. |
| 1469 | +For a mirror, $\mathit{pScatter}()$ is huge only near one direction, so $\mathit{pScatter}()$ |
| 1470 | +matters a lot more. In fact, most renderers just make mirrors a special case, and make the |
| 1471 | +$\mathit{pScatter}()/p()$ implicit -- our code currently does that. |
1470 | 1472 |
|
1471 | 1473 |
|
1472 | 1474 | Returning to the Cornell Box
|
|
1674 | 1676 | material, but Lambertian requires a $\cos(\theta_o)$ sampling pattern. So any differences that we
|
1675 | 1677 | see is because we are using a slightly different material. We're going to uniformly scatter random
|
1676 | 1678 | directions from the hemisphere and perfectly match the PDF, given by
|
1677 |
| -$pScatter(\omega_o) = p(\omega_o) = \frac{1}{2\pi}$. |
| 1679 | +$\mathit{pScatter}(\omega_o) = p(\omega_o) = \frac{1}{2\pi}$. |
1678 | 1680 |
|
1679 | 1681 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
1680 | 1682 | class lambertian : public material {
|
|
2416 | 2418 | </div>
|
2417 | 2419 |
|
2418 | 2420 | <div class='together'>
|
2419 |
| -The details of how this is done under the hood varies for $pSurface$ and $pLight$, but that is |
2420 |
| -exactly what class hierarchies were invented for! It’s never obvious what goes in an abstract class, |
2421 |
| -so my approach is to be greedy and hope a minimal interface works, and for `pdf` this implies: |
| 2421 | +The details of how this is done under the hood varies for $\mathit{pSurface}$ and $\mathit{pLight}$, |
| 2422 | +but that is exactly what class hierarchies were invented for! It’s never obvious what goes in an |
| 2423 | +abstract class, so my approach is to be greedy and hope a minimal interface works, and for `pdf` |
| 2424 | +this implies: |
2422 | 2425 |
|
2423 | 2426 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2424 | 2427 | #ifndef PDF_H
|
|
2735 | 2738 | mixtures of any PDFs to form mixture densities that are also PDFs. Any weighted average of PDFs is
|
2736 | 2739 | also a PDF. As long as the weights are positive and add up to any one, we have a new PDF.
|
2737 | 2740 |
|
2738 |
| - $$ pMixture() = w_0 p_0() + w_1 p_1() + w_2 p_2() + \ldots + w_{n-1} p_{n-1}() $$ |
| 2741 | + $$ \mathit{pMixture}() = w_0 p_0() + w_1 p_1() + w_2 p_2() + \ldots + w_{n-1} p_{n-1}() $$ |
2739 | 2742 |
|
2740 | 2743 | $$ 1 = w_0 + w_1 + w_2 + \ldots + w_{n-1} $$
|
2741 | 2744 |
|
2742 | 2745 | For example, we could just average the two densities:
|
2743 | 2746 |
|
2744 |
| - $$ pMixture(\omega_o) = \frac{1}{2} pSurface(\omega_o) + \frac{1}{2} pLight(\omega_o)$$ |
| 2747 | + $$ \mathit{pMixture}(\omega_o) = \frac{1}{2} \mathit{pSurface}(\omega_o) + \frac{1}{2} |
| 2748 | + \mathit{pLight}(\omega_o)$$ |
2745 | 2749 |
|
2746 | 2750 | <div class='together'>
|
2747 | 2751 | How would we instrument our code to do that? There is a very important detail that makes this not
|
|
2754 | 2758 | pick direction according to pLight
|
2755 | 2759 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2756 | 2760 |
|
2757 |
| -But solving for the PDF value of $pMixture$ is slightly more subtle. We can't just |
| 2761 | +But solving for the PDF value of $\mathit{pMixture}$ is slightly more subtle. We can't just |
2758 | 2762 |
|
2759 | 2763 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2760 | 2764 | if (direction is from pSurface)
|
|
2769 | 2773 | correct, we would have to solve backwards to figure which PDF the direction could come from. Which
|
2770 | 2774 | honestly sounds like a nightmare, but fortunately we don't need to do that. There are some
|
2771 | 2775 | directions that both PDFs could have generated. For example, a direction toward the light could have
|
2772 |
| -been generated by either $pLight$ _or_ $pSurface$. It is sufficient for us to solve for the pdf |
2773 |
| -value of $pSurface$ and of $pLight$ for a random direction and then take the PDF mixture weights to |
2774 |
| -solve for the total PDF value for that direction. The mixture density class is actually pretty |
2775 |
| -straightforward: |
| 2776 | +been generated by either $\mathit{pLight}$ _or_ $\mathit{pSurface}$. It is sufficient for us to |
| 2777 | +solve for the pdf value of $\mathit{pSurface}$ and of $\mathit{pLight}$ for a random direction and |
| 2778 | +then take the PDF mixture weights to solve for the total PDF value for that direction. The mixture |
| 2779 | +density class is actually pretty straightforward: |
2776 | 2780 |
|
2777 | 2781 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
2778 | 2782 | class mixture_pdf : public pdf {
|
|
0 commit comments