# First-Met-With-RayTracing

Hey guys,speaking of RayTracing,I have to tell u that I’m really confused at the first time–What the hell is RayTracing?! Well u know I’m not the kind of person who is addicted to games, all I know about Graphics begins with the course UnityGame in my first year of Graduate,before that I am the one who doesn’t even know what pixel is! So not to mention those much more advanced technologies such as RayTracing!

I assume readers are the same with me! You just learned the really really basic graphics—！ coordinates transformation,！ uv mapping,！ something about graphicspipeline,！ unity games,(ok I admit that I’m a little irritable at present, cuz a bug cannot be fixed for a few days)suddenly a voice told you time to learn RayTracing, and it seems a mess with the existing knowledges in your head. Don’t worry, learn with me.

This article is going to be very very long, it’s about RayTracing,Acceleration,BRDF,PathTracing,Calculus,Global illumination,Unity,VScode etc, there will be lots pics and formulas. If you are patient enough to read through it, I promise you can learn something, but before this article, please scan the two articles:Lighting-Shading-Texture,Graphicspipeline,cuz we have lots things to link up.

## RAY-TRACING

### Acceleration

Radiometry is closely related to calculus. If you know nothing about Calculus, please go to learn it and later come back, at least have the conception in your head. Though I’ll begin with mathematics first.

### Spherical Coordinates

We all know Cartesian coordinates, cuz we define the points and vectors in Cartesian coordinate system since high school. Actually,we can also use Spherical coordinates to define them. In spherical coordinate system,the position of a point or direction and length of a vector are defined by two angles(denoted $\theta$ and $\phi$) and a radial distance($r$). The angle $\theta$ is called polar angle and is measured from the fixed zenith direction.The zenith direction in relation to which this polar angle will be measured is the y-axis.

To go from spherical coordinates to cartesian coordinates,we can use the following equation:

$$x = r sin(\theta)cos(\phi)$$
$$y = r cos(\theta)$$
$$z = r sin(\theta)sin(\phi) \tag{spherical2cartesian}$$

Also to go from cartesian coordinates to spherical coordinates,we can use the following equation:

$$r = \sqrt{x^2+y^2+z^2}$$
$$\theta = cos^{-1}(\frac{y}{r})$$
$$\phi = tan^{-1}(\frac{z}{x}) \tag{catesian2spherical}$$

### Differential and Integral Calculus

Calculus includes differential and integral. Since this is really basic math,I’ll just make a rough review.

The first fundamental theorem of calculus:

$$F = \int f(x)dx \tag{1}$$

The second fundamental theorem of calculus:

$$\int_{a}^{b}f(x)dx = F(b)-F(a) \tag{2}$$

The above is about functions defined in one dimension($f(x)$ takes one variable only, $x$). How about the functions in two dimension,even three dimension?

So the integral of the function in the above pic is :
$$\int_{ax}^{bx}\int_{ay}^{by}f(x,y)dxdy \tag{2d integral}$$

So the integral of the function in the above pic is :
$$\rho = \int_{ax}^{bx}\int_{ay}^{by}\int_{az}^{bz}f(x,y,z)dxdydz \tag{3d integral}$$
$\rho$ means the density of the volume.

Previously on the RayTracing part, the Whitted style ray tracing cannot give us correct results. That’s because the shading part only used empirical model.

Blinn-phong Model cannot give us correct resutls, it’s only an empirical model, not based on physics, there’s lots materials blinn-phong cannot present. We want to simulate much more materials in our real life on computer, which means we need the PBS(physically based shading),and Radiometry is the basic concept of PBS.

To begin with radiometry, we have new terms to remember:

• intensity

#### Radiant Energy and Flux(Power)

We all know what Energy and Power mean in physics. They are almost the same in CG.

Radiant energy is the energy of electromagnetic radiation. It is measured in units of joules, and denoted by the symbol:

$$Q[J = Joule]$$

Radiant flux(power) is the energy emitted,reflected,transmitted or received, per unit time.

$$\Phi = \frac{dQ}{dt} [W = Watt][lm = lumen] \tag{flux}$$

Radiant intensity is the power per unit solid angle emitted by a point light source.

$$I(\omega) = \frac{d\Phi}{d\omega} [\frac{W}{sr}][\frac{lm}{sr}=cd=candela] \tag{intensity}$$

• Angle:ration of subtended arc length on circle to radius

• $\theta = \frac{l}{r}$
• Circle has $2\pi$ radians
• Solid angle:ration of subtended area on sphere to radius squared

• $\Omega = \frac{A}{r^2}$
• Sphere has $4\pi$ steradians

$$dA = (rd\theta)(rsin\theta d\phi) = r^2 sin\theta d\theta d\phi$$
$$d\omega = \frac{dA}{r^2} = sin\theta d\theta d\phi \tag{solid angles}$$

Irradiance is the power per(perpendicular/projected)unit area incident on a surface point.

$$E(x) = \frac{d\Phi(x)}{dA} [\frac{W}{m^2}][\frac{lm}{m^2} = lux] \tag{irradiance}$$

Do you still rememeber that in the Lighting-Shading-Texture article, we mentioned the empirical model Blinn phong model, and there’s diffuse component, when we calculate the diffuse component, we just assume the point’s worldposition,the point’s worldnormal etc.

Actually,we assume this point is not really a point but a very small surface which we call differential area, denoted as $dA$.Now,what we actually consider as well is the amount of light falling on the surface of this very small area around P.Light that falls at P is not reduced to a single light ray since we are not interested in singular point but the small region $dA$ around that point. Light that falls on this region is contained within a small volume perpendicular to P.

In the empirical model, the diffuse component is calculated like this:
$$c_{diff} = c_{light} \cdot m_{diff} \cdot max(0,n \cdot l) \tag{blinn-phong-diffuse}$$
as before, n is the surface normal and l is a unit vector that points towards the light source. The factor $m_{diff}$ is the material’s diffuse color, which is the value that most people think of when they think of the “color” of an object. The diffuse material color often comes from a texture map. The diffuse color of the light source is $c_{light}$.

The diffuse component obeys Lambert’s Cosine Law,here the Irradiance also obeys Lambert’s Cosine Law:

Radiance(luminance) is the power emitted,reflected, transmitted or received by a surface, per unit solid angle, per projected unit area.Radiance is the fundamental field quantity that describes the distribution of light in an environment

• Radiance is the quantity associated with a ray

• Rendering is all about computing radiance

$$L(p,\omega) = \frac{d^2\Phi(p,\omega)}{d\omega dAcos\theta} [\frac{W}{sr m^2}][\frac{cd}{m^2} = \frac{lm}{sr m^2} = nit] \tag{radiance}$$

• Incident radiance is the irradiance per unit solid angle arriving at the surface
• Exiting surface radiance is the intensity per unit projected area leaving the surface.

Those concepts are really hard to remember, but don’t feel frustrated. Keep going. Read more. If you cannot recall the concepts, turn back, read again.

### BRDF

Finally BRDF. Actualy the Radiometry step is for BRDF.
As mentioned above what Phong essentially used to simulate the appearance of shiny materials is a function. This function (which includes a specular and a diffuse compute). This function contains a certain number of parameters such as n that can be tweaked to change the appearance of the material, but more importantly, it actually depends on two variables, the incident light direction (which is used to compute both the diffuse and specular component) and the view direction (which is used to compute the specular component only). We could essential write this function as:

$$f_R(\omega_o,\omega_i)$$
Where $\omega_o$ and $\omega_i$ are the angle between the surface normal (N) and the view direction (V) and the surface normal and the light direction (I) respectively.The subscript o stands for outgoing.

In computer graphics, this function is given the fancy name of Bidirectional Reflectance Distribution Function or in short BRDF. A BRDF is nothing else than a function that returns the amount of light reflected in the view direction for a given incident light direction:

$$BRDF(\omega_o,\omega_i)$$

In the following pic,BRDF represents how much light is reflected into each outgoing direction $\omega_r$ from each incoming direction:

$$f_r(\omega_i \rightarrow \omega_r) = \frac{dL_r(\omega_r)}{dE_i(\omega_i)} = \frac{dL_r(\omega_r)}{L_i(\omega_i)cos\theta_i d\omega_i} \tag{BRDF}$$

One thing that I want to mention, why camera can see the objects? It’s because when one light beam hits the surface, it will reflect in all directions among the half hemisphere, but only the reflected light enter the viewer’s eye make sense. So we need to calculate the output reflected light.

So here come’s The Reflection Equation.

$$L_r(p,\omega_r) = \int_{H^2}f_r(p,\omega_i \rightarrow \omega_r)L_i(p,\omega_i)cos\theta_i d\omega_i \tag{The-Reflection-Equation}$$

Please make sure you understand the meaning of each symbol in the above formulas.

Here come’s The Rendering Equation.

$$L_o(p,\omega_o) = L_e(p,\omega_o) + \int_{\Omega^+}L_i(p,\omega_i)f_r(p,\omega_i,\omega_o)(n\cdot\omega_i)d\omega_i \tag{The-Rendering-Equation}$$

$L_e(p,\omega_o)$ means the object’point p emit the emissive light to the $\omega_o$ direction(remeber the $\omega_o$ direction should be the view direction), and $\int_{\Omega^+}L_i(p,\omega_i)f_r(p,\omega_i,\omega_o)(n\cdot\omega_i)d\omega_i$ means the reflected light from $L_i(p,\omega_i)$.

# Appendix

## Monte Carlo Methods

Before we start, one question, do u have the background in Probability and Statistics Theory? If the answer is no, please go to learn something and later come back.

### What is Monte Carlo?

Assume that we want to know the average height of all adults in one city, the most common method is to sample some adults and calculate the average height of them to approximate the true result. Here’s the formula:

$$Approximation(Average(X)) = \frac{1}{N}\sum_{n=1}^{N}x_{n}$$

We generally denote random variables with upper case letters,the height of a population would be called a random variable,so the letter X is used.The formula can be read as,the approximation of the average value of the random variable X(the height of the adult population of the given country),is equal to sum of the height of N adults randomly chosen from that population(the samples),divided by the number N(the sample size). This in essence, is called a Monte Carlo approximation. In statistics,the average of the random variable X is called an expectation and is written E(X).

So To summarize, Monte Carlo approximation (which is one of the MC methods) is a technique to approximate the expectation of random variables, using samples.

### Monte Carlo raytracing

References:

[1]GAMES

[3]3D Math Primer for Graphics and Game Development 2nd Edition.

[4]Fundamentals of Computer Graphics and 3rd Edition.

[8]基于物理着色：BRDF

[9]如何正确理解 BRDF

Your Majesty, coding and blogging is hard, but a cup of tea/coffee would be the best reward. Scan the QRcode below, I’ll swear my allegiance to you. :) memeda.

Keneyr

2020-04-06

2021-08-28