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;
}
行列式就是行列式中的行或列向量所构成的超平行多面体的有向面积或有向体积
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∣∣∣∣∣∣
代数余子式 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=1∑nMikCik(M)det(M)=j=1∑nMkjCkj(M)
一个线性方程组可以用矩阵(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列的列向量后得到的矩阵。
其中后一个等号矩阵的转置矩阵的行列式等于这个矩阵的行列式
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∣∣∣∣∣∣
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)
P ( t ) = R o + t ∗ R d \mathbf{P(t) = R_o + t * R_d} P(t)=Ro+t∗Rd
平面表示:
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=n⋅P+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+t∗Rdn⋅P(t)+D=0t=−(n⋅Rd)D+n⋅Ro
第一步:判断三角形所在的面和线段所在的直线是否平行或者是否是从三角形面的背面进入。如果是就提前退出,否则进入第二步。(使用面的法向量 和 线段的向量 的 点积)
第二步:判断线段和三角形所在的面的交点是否在线段上。
第三步:判断是否交点在三角形内部。
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+t∗Rd=(1−β−γ)P0+βP1+γP2(Rd,P0−P1,P0−P2)⎝⎛tβγ⎠⎞=P0−Ro
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=P0−P1,E2=P0−P2,S=P0−Ro⎝⎛tβγ⎠⎞=det(Rd,E1,E2)1⎝⎛det(S,E1,E2)det(Rd,S,E2)det(Rd,E1,S)⎠⎞
从点引出一根“射线”,与多边形的任意若干条边相交,累计相交的边的数目,如果是奇数,那么点就在多边形内,否则点就在多边形外。
( 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}\\ (P−Pc)⋅(P−Pc)=R2(Ro+t∗Rd−Pc)⋅(Ro+t∗Rd−Pc)=R2Rd⋅Rd∗t2+2Rd⋅(Ro−Pc)∗t+(Ro−Pc)⋅(Ro−Pc)−R2=0
Is ray origin inside/outside 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=Pc−Rotp=l⋅RdD=l2−tp2
If D>R , no hit
t ′ 2 = R 2 − D 2 \mathbf{{t'}^2 = R^2 - D^2} t′2=R2−D2
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)