计算机图形学:光线追踪

1.Ray Tracing Algorithm

计算机图形学:光线追踪_第1张图片

IntersectColor( vBeginPoint, vDirection)
{
    Determine IntersectPoint;
    Color = ambient color;
    for each light
        Color += local shading term; 
    if(surface is reflective)
        color += reflect Coefficient * IntersectColor(IntersecPoint, ReflectRay);
    else if ( surface is refractive) 
        color += refract Coefficient * IntersectColor(IntersecPoint, Refract Ray);
    return color; 
}

行列式(determinant)

行列式就是行列式中的行或列向量所构成的超平行多面体的有向面积或有向体积

d e t ( M ) = ∣ M 11 M 12 M 13 M 21 M 22 M 23 M 31 M 32 M 33 ∣ \mathbf{det(M)=\begin{vmatrix} M_{11} & M_{12} &M_{13} \\ M_{21} & M_{22} & M_{23} \\ M_{31} & M_{32} & M_{33} \end{vmatrix}} det(M)=M11M21M31M12M22M32M13M23M33

  • 二阶行列式的几何意义就是由行列式的向量所张成的平行四边形的面积
  • 一个3×3阶的行列式是其行向量或列向量所张成的平行六面体的有向体积

计算机图形学:光线追踪_第2张图片

计算机图形学:光线追踪_第3张图片

代数余子式 C i j ( M ) = ( − 1 ) i + j d e t ( M { i , j } ) \bm{C_{ij}(M)=(-1)^{i+j}det(M^{\{i,j\}})} Cij(M)=(1)i+jdet(M{i,j}),其中 M { i , j } \bm{M^{\{i,j\}}} M{i,j}是去除i行和j列后的矩阵

矩阵M的行列式等于任意一行或者一列元素与其代数余子式乘积的和

d e t ( M ) = ∑ i = 1 n M i k C i k ( M ) d e t ( M ) = ∑ j = 1 n M k j C k j ( M ) \mathbf{det(M)=\sum_{i=1}^{n}M_{ik}C_{ik}(M)}\\ \mathbf{det(M)=\sum_{j=1}^{n}M_{kj}C_{kj}(M)} det(M)=i=1nMikCik(M)det(M)=j=1nMkjCkj(M)

克莱姆法则(Cramer’s Rule)

一个线性方程组可以用矩阵(M为n*n的方阵)与向量(X,C均长度为n的列向量)的方程来表示:

M X = C \mathbf{MX=C} MX=C

如果M是一个可逆矩阵,那么方程有解,为

X i = d e t ( M i ) d e t ( M ) \mathbf{X_i=\frac{det(M_i)}{det(M)}} Xi=det(M)det(Mi)

其中 M i \bm{M_i} Mi是被列向量C取代了M的第i列的列向量后得到的矩阵。

混合积的运算法则

1.混合积和三维矩阵行列式的关系

其中后一个等号矩阵的转置矩阵的行列式等于这个矩阵的行列式

a ⋅ ( b × c ) = ∣ a 1 a 2 a 3 b 1 b 2 b 3 c 1 c 2 c 3 ∣ = ∣ a 1 b 1 c 1 a 2 b 2 c 2 a 3 b 3 c 3 ∣ \mathbf{a\cdot(b\times c)=\begin{vmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \end{vmatrix}=\begin{vmatrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \end{vmatrix}} a(b×c)=a1b1c1a2b2c2a3b3c3=a1a2a3b1b2b3c1c2c3

2.混合积的交换律

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

2.Ray intersection(光线求交)

2.1 Ray Representation

P ( t ) = R o + t ∗ R d \mathbf{P(t) = R_o + t * R_d} P(t)=Ro+tRd

2.2 Plane intersection

平面表示:

H ( P ) = A x + B y + C z + D = n ⋅ P + D = 0 \mathbf{H(P) = Ax+By+Cz+D = n \cdot P + D = 0} H(P)=Ax+By+Cz+D=nP+D=0

n为法向量,D为距离原点的偏移

联立方程

P ( t ) = R o + t ∗ R d n ⋅ P ( t ) + D = 0 t = − D + n ⋅ R o ( n ⋅ R d ) \mathbf{P(t) = R_o + t * R_d}\\ \mathbf{n\cdot P(t)+D = 0}\\ \mathbf{t = -\frac{D+n\cdot Ro}{(n\cdot Rd)}} \\ P(t)=Ro+tRdnP(t)+D=0t=(nRd)D+nRo

2.3 Triangle intersection

  • 第一步:判断三角形所在的面和线段所在的直线是否平行或者是否是从三角形面的背面进入。如果是就提前退出,否则进入第二步。(使用面的法向量 和 线段的向量 的 点积)

  • 第二步:判断线段和三角形所在的面的交点是否在线段上。

  • 第三步:判断是否交点在三角形内部。

计算机图形学:光线追踪_第4张图片

重心坐标系(Barycentric coordinates):

Barycentric coordinates could be used for texture mapping, normal interpolation, color interpolation, etc.

P = α P 0 + β P 1 + γ P 2 α + β + γ = 1 0 ⩽ α , β , γ ⩽ 1 \mathbf{P = \alpha P_0 + \beta P_1 + \gamma P_2} \mathbf{\alpha + \beta + \gamma = 1} \mathbf{0\leqslant \alpha, \beta, \gamma \leqslant 1}\\ P=αP0+βP1+γP2α+β+γ=10α,β,γ1

相交检测:

P ( t ) = R o + t ∗ R d = ( 1 − β − γ ) P 0 + β P 1 + γ P 2 ( R d , P 0 − P 1 , P 0 − P 2 ) ( t β γ ) = P 0 − R o \mathbf{P(t) = R_o + t * R_d = (1-\beta-\gamma)P_0 + \beta P_1 + \gamma P_2}\\ \mathbf{(R_d, P_0-P_1, P_0-P_2) \begin{pmatrix} t\\ \beta\\ \gamma \end{pmatrix}=P_0 - R_o} P(t)=Ro+tRd=(1βγ)P0+βP1+γP2(Rd,P0P1,P0P2)tβγ=P0Ro

克莱姆法则:

E 1 = P 0 − P 1 , E 2 = P 0 − P 2 , S = P 0 − R o ( t β γ ) = 1 d e t ( R d , E 1 , E 2 ) ( d e t ( S , E 1 , E 2 ) d e t ( R d , S , E 2 ) d e t ( R d , E 1 , S ) ) \mathbf{E_1 = P_0 - P_1, E_2 = P_0 - P_2, S = P_0 - R_o}\\ \mathbf{\begin{pmatrix} t\\ \beta\\ \gamma \end{pmatrix}=\frac{1}{det(R_d,E_1,E_2)}\begin{pmatrix} det(S, E_1, E_2)\\ det(R_d, S, E_2)\\ det(R_d, E_1, S) \end{pmatrix}} E1=P0P1,E2=P0P2,S=P0Rotβγ=det(Rd,E1,E2)1det(S,E1,E2)det(Rd,S,E2)det(Rd,E1,S)

2.4 Polygon intersection

Crossing Test

从点引出一根“射线”,与多边形的任意若干条边相交,累计相交的边的数目,如果是奇数,那么点就在多边形内,否则点就在多边形外。

计算机图形学:光线追踪_第5张图片

2.5 Sphere intersection

Algebra Solution:

( P − P c ) ⋅ ( P − P c ) = R 2 ( R o + t ∗ R d − P c ) ⋅ ( R o + t ∗ R d − P c ) = R 2 R d ⋅ R d ∗ t 2 + 2 R d ⋅ ( R o − P c ) ∗ t + ( R o − P c ) ⋅ ( R o − P c ) − R 2 = 0 \mathbf{(P - P_c)\cdot(P - P_c)=R^2}\\ \mathbf{(R_o + t*R_d - P_c)\cdot(R_o+t*R_d - P_c)=R^2}\\ \mathbf{R_d\cdot R_d*t^2 + 2R_d\cdot(R_o - P_c)*t + (R_o - P_c)\cdot (R_o - P_c) - R^2 = 0}\\ (PPc)(PPc)=R2(Ro+tRdPc)(Ro+tRdPc)=R2RdRdt2+2Rd(RoPc)t+(RoPc)(RoPc)R2=0

Geometric Method

Is ray origin inside/outside the sphere?

  • l ⃗ 2 > R 2 \bm{{\vec{l}}^2>R^2} l 2>R2 Outside the sphere
  • l ⃗ 2 = R 2 \bm{{\vec{l}}^2=R^2} l 2=R2 On the sphere
  • l ⃗ 2 < R 2 \bm{{\vec{l}}^2l 2<R2 Inside the sphere

l ⃗ = P c − R o t p = l ⃗ ⋅ R d D = l ⃗ 2 − t p 2 \mathbf{\vec{l}=P_c - R_o}\\ \mathbf{t_p=\vec{l}\cdot R_d}\\ \mathbf{D = \sqrt{{\vec{l}}^2-{t_p}^2}}\\ l =PcRotp=l RdD=l 2tp2

If D>R , no hit

t ′ 2 = R 2 − D 2 \mathbf{{t'}^2 = R^2 - D^2} t2=R2D2

  • If origin outside sphere t = t p − t ′ \bm{t=t_p - t'} t=tpt
  • If origin inside sphere t = t p + t ′ \bm{t=t_p + t'} t=tp+t

计算机图形学:光线追踪_第6张图片

2.6 Box intersection

Foreachslab,intersecting with the ray,there is a minimum t value and maximum t value, these are called t i m i n \bm{t_{i}^{min}} timin and t i m a x \bm{t_{i}^{max}} timax ,(i=0, 1, 2)

The next step is to compute:
t m i n = m a x ( t 0 m i n , t 1 m i n , t 2 m i n ) t m a x = m i n ( t 0 m a x , t 1 m a x , t 2 m a x ) \mathbf{t_{}^{min}=max(t_{0}^{min},t_{1}^{min},t_{2}^{min})}\\ \mathbf{t_{}^{max}=min(t_{0}^{max},t_{1}^{max},t_{2}^{max})}\\ tmin=max(t0min,t1min,t2min)tmax=min(t0max,t1max,t2max)

  • if t m i n < t m a x \bm{t_{}^{min} < t_{}^{max}} tmin<tmax ,then the ray intersects the box
    t m i n \bm{t_{}^{min}} tmin is the enter-point, t m a x \bm{t_{}^{max}} tmax is the exit point
  • Otherwise, no intersections

计算机图形学:光线追踪_第7张图片

你可能感兴趣的:(计算机图形学,图形学,线性代数)