以上算法还是只考虑了光线只弹射一次的情况
由于点需要满足两个方程,所以
( o + t d − c ) 2 − R 2 = 0 (\mathbf{o}+t \mathbf{d}-\mathbf{c})^2-R^2=0 (o+td−c)2−R2=0
展开后也就有,
a t 2 + b t + c = 0 , where a = d ⋅ d b = 2 ( o − c ) ⋅ d c = ( o − c ) ⋅ ( o − c ) − R 2 t = − b ± b 2 − 4 a c 2 a \begin{aligned} &a t^2+b t+c=0, \text { where } \\ &a=\mathbf{d} \cdot \mathbf{d} \\ &b=2(\mathbf{o}-\mathbf{c}) \cdot \mathbf{d} \\ &c=(\mathbf{o}-\mathbf{c}) \cdot(\mathbf{o}-\mathbf{c})-R^2 \\ &t=\frac{-b \pm \sqrt{b^2-4 a c}}{2 a} \end{aligned} at2+bt+c=0, where a=d⋅db=2(o−c)⋅dc=(o−c)⋅(o−c)−R2t=2a−b±b2−4ac
注意,对于解出后的结果 t t t,需要满足 t ≥ 0 t\ge 0 t≥0
对于任意的隐式表面,有
解出 Substitute ray equation 即可
注意,解出的结果需要是实数并且为非负数
Applications
Compute
Ray equation: r ( t ) = o + t d , 0 ≤ t < ∞ \mathbf{r}(t)=\mathbf{o}+t \mathbf{d}, 0 \leq t<\infty r(t)=o+td,0≤t<∞
Plane equation: p : ( p − p ′ ) ⋅ N = 0 \mathbf{p}:\left(\mathbf{p}-\mathbf{p}^{\prime}\right) \cdot \mathbf{N}=0 p:(p−p′)⋅N=0
Solve for intersection
set p = r ( t ) and solve for t ( p − p ′ ) ⋅ N = ( o + t d − p ′ ) ⋅ N = 0 t = ( p ′ − o ) ⋅ N d ⋅ N \begin{aligned} & \text{set}\ \mathbf{p}=\mathbf{r}(t) \ \text{and solve for}\ t\\ &\left(\mathbf{p}-\mathbf{p}^{\prime}\right) \cdot \mathbf{N}=\left(\mathbf{o}+t \mathbf{d}-\mathbf{p}^{\prime}\right) \cdot \mathbf{N}=0 \\ &t=\frac{\left(\mathbf{p}^{\prime}-\mathbf{o}\right) \cdot \mathbf{N}}{\mathbf{d} \cdot \mathbf{N}} \end{aligned} set p=r(t) and solve for t(p−p′)⋅N=(o+td−p′)⋅N=0t=d⋅N(p′−o)⋅N
A faster approach, giving barycentric coordinate directly (以另外一个形式描述平面)
O → + t D → = ( 1 − b 1 − b 2 ) P → 0 + b 1 P → 1 + b 2 P → 2 \overrightarrow{\mathbf{O}}+t \overrightarrow{\mathbf{D}}=\left(1-b_1-b_2\right) \overrightarrow{\mathbf{P}}_0+b_1 \overrightarrow{\mathbf{P}}_1+b_2 \overrightarrow{\mathbf{P}}_2 O+tD=(1−b1−b2)P0+b1P1+b2P2
In summary, Ray and AABB intersect iff
t e n t e r < t e x i t and t e x i t ≥ 0 t_{enter} < t_{exit}\ \text{and}\ t_{exit}\ge0 tenter<texit and texit≥0
Grid Resolution?
Pros & Cons
基于 Grid 方法的优化,物体稀疏的地方不需要用很多格子
Examples
KD-Tree Pre-Processing
Data Structure for KD-Trees
Cons of KD-Tree
Pros of BVH
Cons of BVH
Summary
Methods to subdivide a node
Termination criteria
Data Structure for BVHs
fun Intersect(Ray ray, BVH node) {
if (ray misses node.bbox) return;
if (node is a leaf node)
test intersection with all objs;
return closest intersection;
hit1 = Intersect(ray, node.child1);
hit2 = Intersect(ray, node.child2);
return the closer of hit1, hit2;
}
在计算光强的时候,我们没有准确的物理定义
辐射度量学:定义了一系列方法和单位去描述光照
Accurately measure the spatial properties of light
Perform lighting calculations in a physically correct manner
Q [ J = Joule ] Q[\mathrm{~J}=\text { Joule }] Q[ J= Joule ]
Φ ≡ d Q d t [ W = W a t t ] [ lm = lumen ] ∗ \Phi \equiv \frac{\mathrm{d} Q}{\mathrm{~d} t}[\mathrm{~W}=\mathrm{Watt}][\operatorname{lm}=\text { lumen }]^* Φ≡ dtdQ[ W=Watt][lm= lumen ]∗
I ( ω ) ≡ d Φ d ω [ W s r ] [ lm s r = c d = candela ] \begin{gathered} I(\omega) \equiv \frac{\mathrm{d} \Phi}{\mathrm{d} \omega} \\ {\left[\frac{\mathrm{W}}{\mathrm{sr}}\right]\left[\frac{\operatorname{lm}}{\mathrm{sr}}=\mathrm{cd}=\text { candela }\right]} \end{gathered} I(ω)≡dωdΦ[srW][srlm=cd= candela ]
Angle: ratio of subtended arc length on circle to radius
Solid angle: ratio of subtended area on sphere to radius squared
对于一个均匀点光源,它的 Radiant Intensity 的相关计算是
对于所有方向上的 Intensity 积分起来可以得到 Flux
Φ = ∫ S 2 I d ω = 4 π I \begin{aligned} \Phi &=\int_{S^2} I \mathrm{~d} \omega \\ &=4 \pi I \end{aligned} Φ=∫S2I dω=4πI
对于任何一个方向上的 Intensity 有
I = Φ 4 π I=\frac{\Phi}{4 \pi} I=4πΦ
E ( x ) ≡ d Φ ( x ) d A [ W m 2 ] [ lm m 2 = lux ] \begin{gathered} E(\mathbf{x}) \equiv \frac{\mathrm{d} \Phi(\mathbf{x})}{\mathrm{d} A} \\ {\left[\frac{\mathrm{W}}{\mathrm{m}^2}\right]\left[\frac{\operatorname{lm}}{\mathrm{m}^2}=\operatorname{lux}\right]} \end{gathered} E(x)≡dAdΦ(x)[m2W][m2lm=lux]
Radiance is the fundamental field quantity that describes the distribution of light in an environment
L ( p , ω ) ≡ d 2 Φ ( p , ω ) d ω d A cos θ L(\mathrm{p}, \omega) \equiv \frac{\mathrm{d}^2 \Phi(\mathrm{p}, \omega)}{\mathrm{d} \omega \mathrm{d} A \cos \theta} L(p,ω)≡dωdAcosθd2Φ(p,ω)
Recall
So
d E ( p , ω ) = L i ( p , ω ) cos θ d ω E ( p ) = ∫ H 2 L i ( p , ω ) cos θ d ω \begin{aligned} d E(\mathrm{p}, \omega) &=L_i(\mathrm{p}, \omega) \cos \theta \mathrm{d} \omega \\ E(\mathrm{p}) &=\int_{H^2} L_i(\mathrm{p}, \omega) \cos \theta \mathrm{d} \omega \end{aligned} dE(p,ω)E(p)=Li(p,ω)cosθdω=∫H2Li(p,ω)cosθdω
Radiance from direction ωi turns into the power E that dA receives Then power E will become the radiance to any other direction ωo
The Bidirectional Reflectance Distribution Function (BRDF) represents how much light is reflected into each outgoing direction ω r \omega_r ωr from each incoming direction
BRDF 描述了光线和物体是如何作用的
f r ( ω i → ω r ) = d L r ( ω r ) d E i ( ω i ) = d L r ( ω r ) L i ( ω i ) cos θ i d ω i [ 1 s r ] f_r\left(\omega_i \rightarrow \omega_r\right)=\frac{\mathrm{d} L_r\left(\omega_r\right)}{\mathrm{d} E_i\left(\omega_i\right)}=\frac{\mathrm{d} L_r\left(\omega_r\right)}{L_i\left(\omega_i\right) \cos \theta_i \mathrm{~d} \omega_i} \quad\left[\frac{1}{\mathrm{sr}}\right] fr(ωi→ωr)=dEi(ωi)dLr(ωr)=Li(ωi)cosθi dωidLr(ωr)[sr1]
L r ( p , ω r ) = ∫ H 2 f r ( p , ω i → ω r ) L i ( p , ω i ) cos θ i d ω i L_r\left(\mathrm{p}, \omega_r\right)=\int_{H^2} f_r\left(\mathrm{p}, \omega_i \rightarrow \omega_r\right) L_i\left(\mathrm{p}, \omega_i\right) \cos \theta_i \mathrm{~d} \omega_i Lr(p,ωr)=∫H2fr(p,ωi→ωr)Li(p,ωi)cosθi dωi
Re-write the reflection equation:
L r ( p , ω r ) = ∫ H 2 f r ( p , ω i → ω r ) L i ( p , ω i ) cos θ i d ω i L_r\left(\mathrm{p}, \omega_r\right)=\int_{H^2} f_r\left(\mathrm{p}, \omega_i \rightarrow \omega_r\right) L_i\left(\mathrm{p}, \omega_i\right) \cos \theta_i \mathrm{~d} \omega_i Lr(p,ωr)=∫H2fr(p,ωi→ωr)Li(p,ωi)cosθi dωi
by adding an Emission term to make it general(考虑物体会发光的情况),这样就得到了 Rendering Equation
L o ( p , ω o ) = L e ( p , ω o ) + ∫ Ω + L i ( p , ω i ) f r ( p , ω i , ω o ) ( n ⋅ ω i ) d ω i L_o\left(p, \omega_o\right)=L_e\left(p, \omega_o\right)+\int_{\Omega^{+}} L_i\left(p, \omega_i\right) f_r\left(p, \omega_i, \omega_o\right)\left(n \cdot \omega_i\right) \mathrm{d} \omega_i Lo(p,ωo)=Le(p,ωo)+∫Ω+Li(p,ωi)fr(p,ωi,ωo)(n⋅ωi)dωi
Note: now, we assume that all directions are pointing outwards!
∫ a b f ( x ) d x \quad \int_a^b f(x) d x ∫abf(x)dx
X i ∼ p ( x ) \quad X_i \sim p(x) Xi∼p(x)
F N = ∫ f ( x ) d x = 1 N ∑ i = 1 N f ( X i ) p ( X i ) \quad F_N=\int f(x) \mathrm{d} x=\frac{1}{N} \sum_{i=1}^N \frac{f\left(X_i\right)}{p\left(X_i\right)} FN=∫f(x)dx=N1i=1∑Np(Xi)f(Xi)
如果随机变量均匀采样(均匀分布),Monte Carlo Integration 有以下形式
Definite integral
∫ a b f ( x ) d x \int_a^b f(x) d x ∫abf(x)dx
Uniform random variable
X i ∼ p ( x ) = 1 b − a X_i \sim p(x)=\frac{1}{b-a} Xi∼p(x)=b−a1
F N = b − a N ∑ i = 1 N f ( X i ) F_N=\frac{b-a}{N} \sum_{i=1}^N f\left(X_i\right) FN=Nb−ai=1∑Nf(Xi)
For Whitted-style ray tracing, it has some cons/ simplifications
Always perform specular reflections / refractions
Stop bouncing at diffuse surfaces
But the rendering equation is correct
L o ( p , ω o ) = L e ( p , ω o ) + ∫ Ω + L i ( p , ω i ) f r ( p , ω i , ω o ) ( n ⋅ ω i ) d ω i L_o\left(p, \omega_o\right)=L_e\left(p, \omega_o\right)+\int_{\Omega^{+}} L_i\left(p, \omega_i\right) f_r\left(p, \omega_i, \omega_o\right)\left(n \cdot \omega_i\right) \mathrm{d} \omega_i Lo(p,ωo)=Le(p,ωo)+∫Ω+Li(p,ωi)fr(p,ωi,ωo)(n⋅ωi)dωi
But it involves
因此,对于着色点 p p p 的 Monte Carlo Integration 为
L o ( p , ω o ) = ∫ Ω + L i ( p , ω i ) f r ( p , ω i , ω o ) ( n ⋅ ω i ) d ω i ≈ 1 N ∑ i = 1 N L i ( p , ω i ) f r ( p , ω i , ω o ) ( n ⋅ ω i ) p ( ω i ) \begin{aligned} L_o\left(p, \omega_o\right) &=\int_{\Omega^{+}} L_i\left(p, \omega_i\right) f_r\left(p, \omega_i, \omega_o\right)\left(n \cdot \omega_i\right) \mathrm{d} \omega_i \\ & \approx \frac{1}{N} \sum_{i=1}^N \frac{L_i\left(p, \omega_i\right) f_r\left(p, \omega_i, \omega_o\right)\left(n \cdot \omega_i\right)}{p\left(\omega_i\right)} \end{aligned} Lo(p,ωo)=∫Ω+Li(p,ωi)fr(p,ωi,ωo)(n⋅ωi)dωi≈N1i=1∑Np(ωi)Li(p,ωi)fr(p,ωi,ωo)(n⋅ωi)
shade(p, wo)
Randomly choose N directions wi~pdf
Lo = 0.0
For each wi
Trace a ray r(p, wi)
If ray r hit the light
Lo += (1 / N) * L_i * f_r * cosine / pdf(wi)
Return Lo
shade(p, wo)
Randomly choose N directions wi~pdf
Lo = 0.0
For each wi
Trace a ray r(p, wi)
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(p, wo)
Randomly choose ONE direction wi~pdf(w)
Trace a ray r(p, wi)
If ray r hit the light
Return L_i * f_r * cosine / pdf(wi)
Else If ray r hit an object at q
Return shade(q, -wi) * f_r * cosine / pdf(wi)
ray_generation(camPos, pixel)
Uniformly choose N sample positions within the pixel
pixel_radiance = 0.0
For each sample in the pixel
Shoot a ray r(camPos, cam_to_sample)
If ray r hit the scene at p
pixel_radiance += 1 / N * shade(p, sample_to_cam)
Return pixel_radiance
代码如下
shade(p, wo)
Manually specify a probability P_RR
Randomly select ksi in a uniform dist. in [0, 1]
If (ksi > P_RR) return 0.0;
Randomly choose ONE direction wi~pdf(w)
Trace a ray r(p, wi)
If ray r hit the light
Return L_i * f_r * cosine / pdf(wi) / P_RR
Else If ray r hit an object at q
Return shade(q, -wi) * f_r * cosine / pdf(wi) / P_RR
浪费现象产生的原因是我们均匀的在半球上采样,很大一部分采样没有进入光源,如果找到一个好的概率密度函数,可以提高效率 → \to → 直接在光源上进行采样
Assume uniformly sampling on the light: pdf = 1 / A \text{pdf} = 1 / A pdf=1/A (because ∫ pdf d A = 1 \int \text{pdf} \ \mathrm{d}A = 1 ∫pdf dA=1)
But the rendering equation integrates on the solid angle: L o = ∫ L i f r c o s d ω . L_o = \int L_i fr cos \ \mathrm{d} \omega. Lo=∫Lifrcos dω.
Recall Monte Carlo Integration: Sample on x x x & integrate on x x x
we sample on the light, so we must integrate on the light
我们只要知道 d ω \mathrm{d} \omega dω 和 d A \mathrm{d}A dA 的关系即可
d ω = d A cos θ ′ ∥ x ′ − x ∥ 2 \mathrm{d} \omega=\frac{\mathrm{d} A \cos \theta^{\prime}}{\left\|x^{\prime}-x\right\|^2} dω=∥x′−x∥2dAcosθ′
我们重写渲染方程即可(变量替换,改变积分域)
L o ( x , ω o ) = ∫ Ω + L i ( x , ω i ) f r ( x , ω i , ω o ) cos θ d ω i = ∫ A L i ( x , ω i ) f r ( x , ω i , ω o ) cos θ cos θ ′ ∥ x ′ − x ∥ 2 d A \begin{aligned} L_o\left(x, \omega_o\right) &=\int_{\Omega^{+}} L_i\left(x, \omega_i\right) f_r\left(x, \omega_i, \omega_o\right) \cos \theta \mathrm{d} \omega_i \\ &=\int_A L_i\left(x, \omega_i\right) f_r\left(x, \omega_i, \omega_o\right) \frac{\cos \theta \cos \theta^{\prime}}{\left\|x^{\prime}-x\right\|^2} \mathrm{~d} A \end{aligned} Lo(x,ωo)=∫Ω+Li(x,ωi)fr(x,ωi,ωo)cosθdωi=∫ALi(x,ωi)fr(x,ωi,ωo)∥x′−x∥2cosθcosθ′ dA
Previously, we assume the light is “accidentally” shot by uniform hemisphere sampling
Now we consider the radiance coming from two parts:
shade(p, wo)
# Contribution from the light source.
Uniformly sample the light at x’ (pdf_light = 1 / A)
L_dir = L_i * f_r * cos θ * cos θ’ / |x’ - p|^2 / pdf_light
# Contribution from other reflectors.
L_indir = 0.0
Test Russian Roulette with probability P_RR
Uniformly sample the hemisphere toward wi (pdf_hemi = 1 / 2pi)
Trace a ray r(p, wi)
If ray r hit a non-emitting object at q
L_indir = shade(q, -wi) * f_r * cos θ / pdf_hemi / P_RR
Return L_dir + L_indir
# Contribution from the light source.
L_dir = 0.0
Uniformly sample the light at x’ (pdf_light = 1 / A)
Shoot a ray from p to x’
If the ray is not blocked in the middle
L_dir = …
Previous
Modern (my own definition)
Uniformly sampling the hemisphere
Monte Carlo integration allows arbitrary pdfs
Do random numbers matter?
I can sample the hemisphere and the light
The radiance of a pixel is the average of radiance on all paths passing through it
Is the radiance of a pixel the color of a pixel?