Whitted-Style Ray Tracing
Whitted-style ray tracing:
- Always perform specular reflections / refractions
- 如果光线打到 specular 光滑的物体上,会延镜面方向反射,折射方向折射。
- Stop bouncing at diffuse surfaces
- 如果光线打到漫反射物体,光线停止。
Are these simplifications reasonable?
High level: let’s progressively improve upon Whitted-Style Ray Tracing and lead to our path tracing algorithm!
Whitted-Style Ray Tracing: Problem 1
左边像镜子(specular),右边有磨砂感觉(有反射但又有点糊)。Whitted-Style Ray Tracing 对 Glossy 材质也是认为光线延镜面方向反射,这是不对的。
Whitted-Style Ray Tracing: Problem 2
Whitted-Style Ray Tracing 一根光线打到 Diffuse 漫反射物体上就停了,直接做 Shading,这是不对的。因为漫反射会将光线均匀反射到不同的方向上。
图中只有上面一盏灯,如果直接光照的话,天花板肯定是黑的,但是光线实际上能弹射很多次,弹到地板上也会反到天花板上。右图左边长方体的左边是红色的,光线打到左边红色墙壁再打到长方体上,最后打到我们颜色,所以应该是红色的。(Color Bleeding)
Whitted-Style Ray Tracing is Wrong
But the rendering equation is correct
But it involves
- Solving an integral over the hemisphere, and 解积分
- Recursive execution 递归值
How do you solve an integral numerically?
只要解定积分,就能用蒙特卡洛来解。
A Simple Monte Carlo Solution
Suppose we want to render one pixel (point) in the following scene for direct illumination only
先考虑一个点的直接光照,有一个相对大的面光源,左上角为摄像机,ωi 表示各个不同的入射方向。
Abuse the concept of Reflection Equation a little bit
首先假设这个点不发光
Fancy as it is, it’s still just an integration over directions
着色点 p,要算这个点反射到 camera 的 radiance 是多少,要算这个积分。
根据蒙特卡洛,要算一个积分,在积分域上进行采样,得到样本 X,算 f(x)/p(x) 然后求平均。
不断采样,不断地均匀地在半球方向上取 ωi 入射方向,分子就是 这个方向上来的光乘以 BRDF 加上 cos 的衰减,分母是 1/2π。
shade(p, wo) 点P往ωo方向出来 Randomly choose N directions wi~pdf 这个着色点往N个不同方向,按照某种PDF Lo = 0.0 For each wi 对于任何一个选中的方向 ωi Trace a ray r(p, wi) 从 P 点往 ωi 连个光线 If ray r hit the light 如果这个光线打到光源 Lo += (1 / N) * L_i * f_r * cosine / pdf(wi) Return Lo
L_i 光源辐射的 radiance
f_r BRDF 入射方向出射方向都知道,因此 BRDF 也知道了
目前只是直接光照的部分。
Introducing Global Illumination
引入间接光照
我们还要算 Q 点到 P 点反射的 radiance,这就好像是 P 点到 Q 点,算 Q 点的直接光照一样。
shade(p, wo) 点P往ωo方向出来 Randomly choose N directions wi~pdf 这个着色点往N个不同方向,按照某种PDF Lo = 0.0 For each wi 对于任何一个选中的方向 ωi Trace a ray r(p, wi) 从 P 点往 ωi 连个光线 If ray r hit the light 如果这个光线打到光源 Lo += (1 / N) * L_i * f_r * cosine / pdf(wi) Else If ray r hit an object at q 新增 Lo += (1 / N) * shade(q, -wi) * f_r * cosine / pdf(wi) Return Lo
shade(q, -wi) 是 Q 点直接光照的结果
但是有很多问题
光线的数量会爆炸。N=1的时候才不会爆炸。
没有了 for 循环,只采样一次噪声会大。
所有的路径都会穿过一个像素,像素最后求 radiance 的平均。
现实生活中光线也不会停,如果我们限制弹射次数是不对的。
Solution: Russian Roulette (RR)(俄罗斯轮盘赌)
我们用轮盘赌来判断何时停止。
Previously, we always shoot a ray at a shading point and get the shading result Lo
Suppose we manually set a probability P (0 < P < 1) 自己先定一个概率
With probability P, shoot a ray and return the shading result divided by P: Lo / P
P 概率打一条光线,返回结果除以 P,为什么除以P?因为这样期望算出来就是 Lo。
With probability 1-P, don’t shoot a ray and you’ll get 0
In this way, you can still expect to get Lo!:
期望: E = P * (Lo / P) + (1 - P) * 0 = Lo
期望结果可能有噪声,但结果肯定是对的。
目前已经是正确的 Path Tracing 的实现,但还是有个小问题。
采样率少:一个像素打出的 Path 少
Sampling the Light
there will be 1 ray hitting the light. So a lot of rays are “wasted” if we uniformly sample the hemisphere at the shading point.
打不打得到光源是看运气的。我们可以用别的 PDF 去不浪费光线来采样。
Sampling the Light (pure math)
Monte Carlo methods allows any sampling methods, so we can sample the light (therefore no rays are “wasted”) 从光源采样
Assume uniformly sampling on the light: pdf = 1 / A (because ∫pdf dA = 1)
But the rendering equation integrates on the solid angle: Lo = ∫Li fr cos dω.
Recall Monte Carlo Integration: Sample on x & integrate on x
在 X 上采样,就要在 X 上积分。
Need to make the rendering equation as an integral of dA
Need the relationship between dω and dA
Easy! Recall the alternative def. of solid angle:
Projected area on the unit sphere
可以将光源的面投影到球面上,作为立体角的面积。这样就把两个量联系起来了。
Then we can rewrite the rendering equation as
Now an integration on the light! 改变积分域,改成对光源的积分
Monte Carlo integration:
“f(x)”: everything inside
Pdf: 1 / A
Previously, we assume the light is “accidentally” shot by uniform hemisphere sampling
Now we consider the radiance coming from two parts:
- light source (direct, no need to have RR) 来自光源的贡献,对光源采样来解
- other reflectors (indirect, RR) 非光源,用之前的方案来解,俄罗斯轮盘赌
算不算其他的贡献,根据俄罗斯轮盘赌来测试。
One final thing: how do we know if the sample on the light is not blocked or not?
怎么判断中间有没有东西挡住?只用点 P 连线到 X',有挡到就是0。
对于路径追踪来说,点光源不好处理。要处理点光源采样概率等等,最好做成一个小的面积光源。
- Path tracing (PT) is indeed difficult
- Consider it the most challenging in undergrad CS
- Why: physics, probability, calculus, coding
- Learning PT will help you understand deeper in these
- Is it still “Introductory”?
- Not really, but it’s “modern" :)
- And so learning it will be rewarding also because …
Ray tracing: Previous vs. Modern Concepts
- Previous
- Ray tracing == Whitted-style ray tracing
- Modern (my own definition)
- The general solution of light transport, including
- (Unidirectional & bidirectional) path tracing
- Photon mapping
- Metropolis light transport
- VCM / UPBP…
Things we haven’t covered / won’t cover
- Uniformly sampling the hemisphere
- How? And in general, how to sample any function? (sampling)
- Monte Carlo integration allows arbitrary pdfs
- What's the best choice? (importance sampling)
- Do random numbers matter?
- Yes! (low discrepancy sequences)
- I can sample the hemisphere and the light
- Can I combine them? Yes! (multiple imp. sampling)
- The radiance of a pixel is the average of radiance on all paths passing through it
- Why? (pixel reconstruction filter)
- Is the radiance of a pixel the color of a pixel?
- No. (gamma correction, curves, color space)
- Asking again, is path tracing still “Introductory”?
- This time, yes. Fear the science, my friends.