光线追踪


标量三重积(Scalar Triple Product)

a⋅(b×c)\mathbf a\cdot(\mathbf b\times\mathbf c)

它的结果是一个标量,表示由三个向量/(a,b,c)/(\mathbf a,\mathbf b,\mathbf c)构成的平行六面体体积。

a⋅(b×c)=∣axayazbxbybzcxcycz∣\mathbf a\cdot(\mathbf b\times\mathbf c) = \begin{vmatrix} a_x&a_y&a_z\\ b_x&b_y&b_z\\ c_x&c_y&c_z \end{vmatrix}

循环交换不改变结果:

a⋅(b×c)=b⋅(c×a)=c⋅(a×b)\mathbf a\cdot(\mathbf b\times\mathbf c) = \mathbf b\cdot(\mathbf c\times\mathbf a) = \mathbf c\cdot(\mathbf a\times\mathbf b)

但是交换任意两个向量会变号:

a⋅(c×b)=−a⋅(b×c)\mathbf a\cdot(\mathbf c\times\mathbf b) = -\mathbf a\cdot(\mathbf b\times\mathbf c)

Snell 折射定律

设入射方向II,法线NN,通常约定II指向表面。 定义:

cos⁡θi​=−I⋅N\cosθ_{i}​=−I⋅N

根据 Snell 定律:

n1​sin⁡θi​=n2sin⁡θtn_{1}​\sin\theta_{i}​=n_{2}\sin\theta_{t}

令η=n2n1​​η=\frac{n_{2}}{n_{1}}​​​,用三角变换,则:

k=cos⁡2θt​=1−η2(1−cos⁡2θi​)​k=\cos^2θ_{t}​=1−η^2(1−\cos^2θ_{i}​)​

判别:若k<0k<0则无实数解,发生全反射。否则正常折射,方向:

T=ηI+(ηcos⁡θi​​−k​)NT=ηI+(η\cosθ_{i}​​−\sqrt{k }​)N

Möller–Trumbore 光线-三角形求交算法

射线:

P=O+tDP=O+tD

三角形:

P=P0​+uE1​+vE2P=P_{0}​+uE_{1}​+vE_{2}

所以:

O+tD=P0+uE1​+vE2O+tD=P_{0}+uE_{1}​+vE_{2}

整理得非齐次方程组:

tD−uE1​−vE2=P0​−OtD−uE_{1}​−vE_{2}=P_{0}​−O

写成矩阵形式:

[−D, E1, E2][tuv]​​​=P0​−O=S​\begin{bmatrix} −D, E_{1}, E_{2} \end{bmatrix}\begin{bmatrix} t \\ u \\ v \\ \end{bmatrix}​​​=P_{0}​−O=S​

根据 Cramer 法则,方程组 Ax=cAx=c 有解当且仅当 det⁡A≠0\det A\ne 0,且 xi=det⁡Aidet⁡Ax_{i}=\frac{\det A_{i}}{\det A},AiA_{i} 为第 ii 列被 cc 替代的系数矩阵。 因此有

t=det⁡(S,E1,E2)/det⁡(−D,E1,E2)t=\det (S,E_1,E_2)/\det(-D,E_1,E_2)

根据向量三重积性质

t=S×E1⋅E2E1⋅D×E2t=\frac{S \times E_{1} \cdot E_{2}}{E_{1}\cdot D \times E_{2}}

同理:

u=S⋅D×E2E1⋅D×E2u=\frac{S\cdot D \times E_{2}}{E_{1}\cdot D \times E_{2}} v=S×E1⋅DE1⋅D×E2v = \frac{S \times E_{1} \cdot D}{E_{1}\cdot D\times E_{2}}

即有

[tuv]=1E1S2[S1E1S2SS1D]\begin{bmatrix} t \\ u \\ v \end{bmatrix}= \frac{1}{E_{1}S_{2}}\begin{bmatrix} S_{1}E_{1} \\ S_{2}S \\ S_{1}D \\ \end{bmatrix}

其中

S1=S×E1S2=D×E2S=P0−O\begin{align} S_{1}&=S\times E_{1}\\ S_{2}&=D\times E_{2}\\ S&=P_{0}-O \end{align}

且

det⁡A=E1S2≠0\det A=E_{1}S_{2}\ne 0

一次性解出三个几何参数。

BRDF

立体角

立体角 ω\omega 具有如下微分形式:

dω=dAr2d \omega = \frac{\mathrm{d} A}{r^2}

面积微元在球面坐标系下可以写成:

dA=(rdθ)(rsin⁡θdϕ)=r2sin⁡θdθdϕdA = (r\mathrm{d}\theta)(r \sin \theta \mathrm{d}\phi)=r^2\sin\theta \mathrm{d}\theta \mathrm{d}\phi

因此:

dω=dAr2=sin⁡θdθdϕ\mathrm{d}\omega = \frac{\mathrm{d}A}{r^2}=\sin \theta \mathrm{d}\theta \mathrm{d}\phi

Radiant Intensity:辐射强度

光源向某个方向,每单位立体角辐射多少功率。

I(ω)=dΦdωI(ω)=\frac{\mathrm{d}Φ}{\mathrm{d}ω}

其中:

  • ΦΦ:光通量Radiant Flux
  • ωω:立体角
  • dΦ\mathrm{d}\Phi:微小立体角

Irradiance:辐照度/辉度

单位面积表面接收到多少辐射功率。公式:

E=dΦdAE=\frac{\mathrm{d}\Phi}{\mathrm{d}A}

Radiance:辐亮度

从某个表面位置、沿某个方向传播的单位面积、单位立体角上的辐射功率。

定义:

L=d2ΦdA⊥​dωL=\frac{\mathrm{d}^2Φ}{\mathrm{d}A_{⊥}​\mathrm{d}ω}

其中:

dA⊥​=dAcos⁡θ\mathrm{d}A_{\perp}​=\mathrm{d}A\cos\theta

因此:

L=d2Φ​dAcos⁡θdωL=\frac{\mathrm{d}^2Φ​}{\mathrm{d}A\cosθ\mathrm{d}ω}

单位:

W/(m2⋅sr)​W/(m^2⋅sr)​

Lambert’s Cosine Law

如果光从方向ω\omega入射到表面:

dE=L(ω)cos⁡θdω\mathrm{d}E=L(\omega)\cos\theta\mathrm{d}\omega

其中:

cos⁡θ=n⋅ω\cos\theta=n⋅\omega

因此:

E=∫Ω​L(ω)(n⋅ω)dωE=\int_{\Omega}​L(\omega)(n⋅\omega)\mathrm{d}\omega

BRDF(Bidirectional Reflectance Distribution Function,双向反射分布函数)

物理定义:

fr​(x,ωi​,ωo​)=dLo​(x,ωo​)dEi​(x,ωi​)​f_{r}​(x,ω_{i}​,ω_{o}​)=\frac{\mathrm{d}L_{o}​(x,ω_{o}​)}{\mathrm{d}E_{i}​(x,ω_{i}​)}​

​其中:

  • xx:表面上的位置
  • ωiω_{i}:入射光方向(向外)
  • ωoω_{o}:出射光方向
  • dLo\mathrm{d}L_{o}​:沿 ωo​ 方向产生的出射 Radiance
  • dEi\mathrm{d}E_{i}​:来自 ωi​ 方向的微分 Irradiance

即

fr​(ωi​,ωo​)=dLo​Licos⁡θidωi​f_{r}​(ω_{i}​,ω_{o}​)=\frac{\mathrm{d}L_{o}​}{L_i\cos\theta_i\mathrm{d}\omega_i}​

移项积分:

Lo​(ωo​)=∫Ω​fr​(ωi​,ωo​)Li​(ωi​)cos⁡θidωi​​L_{o}​(ω_{o}​)=\int_{\Omega}​f_{r}​(ω_{i}​,ω_{o}​)L_{i}​(ω_{i}​)\cos\theta_{i} \mathrm{d} ω_{i}​​

如果考虑自发光,即有渲染方程(rendering equation):

Lo​(ωo​)=Le(ωo)+∫Ω​fr​(ωi​,ωo​)Li​(ωi​)(n⋅ωi)dωiL_{o}​(ω_{o}​)=L_{e}(\omega_{o})+\int_{\Omega}​f_{r}​(ω_{i}​,ω_{o}​)L_{i}​(ω_{i}​)(n\cdot\omega_{i}) \mathrm{d} ω_{i}

光源推广到多次反射,简写为

L=E+KLL=E+KL

其中LL为递归定义的。 Monte Carlo 积分:

I≈1N∑i=1N​f(xi)p(xi)​I≈ \frac{1}{N}\sum_{i=1}^N \frac{​f(x_{i})}{p(x_{i})​}

代入得Monte Carlo光线追踪方程:

Lo​≈Le​+1N∑i=1N​fr​(ωi​,ωo​)Li​(ωi​)cos⁡θip(ωi)​​​​L_{o}​≈L_{e}​+\frac{1}{N}\sum_{i=1}^N \frac{​f_{r}​(ω_{i}​,ω_{o}​)L_{i}​(ω_{i}​)\cos\theta_{i}}{p(\omega_{i})​}​​​

为了提高效率,在光源dA\mathrm{d}A上采样(直接光照),立体角和光源面积微分的关系如下:

dω=dAcos⁡θ′∣∣x′−x∣∣2\mathrm{d}\omega=\frac{\mathrm{d}A \cos \theta '}{||x'-x||^2}

重写渲染方程

Lo​(ωo​)=∫Ω​fr​(ωi​,ωo​)Li​(ωi​)cos⁡θcos⁡θ′∣∣x′−x∣∣2dA​​L_{o}​(ω_{o}​)=\int_{\Omega}​f_{r}​(ω_{i}​,ω_{o}​)L_{i}​(ω_{i}​)\frac{\cos\theta \cos \theta '}{||x'-x||^2}\mathrm{d}A​​

其他间接光照,采取俄罗斯轮盘赌(RR)算法。