透视矫正插值
实际操作中计算重心坐标用的是经过透视投影矩阵得到的屏幕坐标求得的,此插值与用原世界坐标得到的插值有误差,所以要对其进行矫正,这里首先用了线性插值举例然后再进一步推广到重心坐标,推导如下(参考虎书11.2.4)
假设有世界坐标里的点q,点Q和屏幕坐标上的点r,点R,如图所示
图中 p s = r + s ( R − r ) , p t = q + t ( Q − q ) p_s=r+s\left( R-r \right) ,p_t=q+t\left( Q-q \right) ps=r+s(R−r),pt=q+t(Q−q)
s和t是不同的,一般我们要根据s求t,即要求出s与t的关系式。
我们先设透视投影矩阵为 M \boldsymbol{M} M,则点 p s p_s ps可由点 p t p_t pt经过透视变换矩阵可得,即
p s = M p t p_s=\boldsymbol{M}p_t ps=Mpt
而 p t = q + t ( Q − q ) p_t=q+t\left( Q-q \right) pt=q+t(Q−q)
故
p s = M ( q + t ( Q − q ) ) = M q + t ( M Q − M q ) ( 1 ) p_s=\boldsymbol{M}\left( q+t\left( Q-q \right) \right) =\boldsymbol{M}q+t\left( \boldsymbol{M}Q-\boldsymbol{M}q \right) \,\, \left( 1 \right) ps=M(q+t(Q−q))=Mq+t(MQ−Mq)(1)
而 p s p_s ps同样可由r和R插值得
p s = r + s ( R − r ) p_s=r+s\left( R-r \right) ps=r+s(R−r)
同时有 r = M q , R = M Q r=\boldsymbol{M}q,R=\boldsymbol{M}Q r=Mq,R=MQ,故有
p s = M q w 1 + s ( M Q w 2 − M q w 1 ) ( 2 ) p_s=\frac{\boldsymbol{M}q}{w_1}+s\left( \frac{\boldsymbol{M}Q}{w_2}-\frac{\boldsymbol{M}q}{w_1} \right) \,\, \left( 2 \right) ps=w1Mq+s(w2MQ−w1Mq)(2)
这里的 w 1 = ( M q ) . w ( ) , w 2 = ( M Q ) . w ( ) w_1=\left( \boldsymbol{M}q \right) .w\left( \right) ,w_2=\left(\boldsymbol{M}Q \right) .w\left( \right) w1=(Mq).w(),w2=(MQ).w()
我们知道引用四维坐标(x,y,z,w)表示三维空间的点需w值为1,而一个点经过 M \boldsymbol{M} M变换后w有所改变, M ( x y z 1 ) = ( x m y m z m w m ) ( 3 ) \boldsymbol{M}\left( \begin{array}{c} x\\ y\\ z\\ 1\\ \end{array} \right) =\left( \begin{array}{c} x_m\\ y_m\\ z_m\\ w_m\\ \end{array} \right) \,\, \left( 3 \right) M⎝⎜⎜⎛xyz1⎠⎟⎟⎞=⎝⎜⎜⎛xmymzmwm⎠⎟⎟⎞(3)
所以我们要把它标准化,同理(1)式中也应改写为 p s = M q + t ( M Q − M q ) w 1 + t ( w 2 − w 1 ) ( 4 ) p_s=\frac{\boldsymbol{M}q+t\left( \boldsymbol{M}Q-\boldsymbol{M}q \right)}{w_1+t\left( w_2-w_1 \right)}\,\,\left( 4 \right) ps=w1+t(w2−w1)Mq+t(MQ−Mq)(4)
故有(1)式与(2)式应对应项相等,即有 M q w 1 + s ( M Q w 2 − M q w 1 ) = M q + t ( M Q − M q ) w 1 + t ( w 2 − w 1 ) , ⋯ + s w 2 M Q = ⋯ + t w 1 + t ( w 2 − w 1 ) M Q \frac{\boldsymbol{M}q}{w_1}+s\left( \frac{\boldsymbol{M}Q}{w_2}-\frac{\boldsymbol{M}q}{w_1} \right) =\frac{\boldsymbol{M}q+t\left( \boldsymbol{M}Q-\boldsymbol{M}q \right)}{w_1+t\left( w_2-w_1 \right)}, \\ \cdots +\frac{s}{w_2}\boldsymbol{M}Q=\cdots +\frac{t}{w_1+t\left( w_2-w_1 \right)}\boldsymbol{M}Q w1Mq+s(w2MQ−w1Mq)=w1+t(w2−w1)Mq+t(MQ−Mq),⋯+w2sMQ=⋯+w1+t(w2−w1)tMQ
故有 s = w 2 t w 1 + t ( w 2 − w 1 ) ( 5 ) s=\frac{w_2t}{w_1+t\left( w_2-w_1 \right)}\,\,\left( 5 \right) s=w1+t(w2−w1)w2t(5)
此式为由t求出s,我们需要的是由s求出t,我们可以直接从上式求出t关于s的表达式 t = w 1 s w 2 + s ( w 1 − w 2 ) ( 6 ) t=\frac{w_1s}{w_2+s\left( w_1-w_2 \right)}\,\,\left( 6 \right) t=w2+s(w1−w2)w1s(6)
同样我们也可以运用 p t = M − 1 p s p_t=\boldsymbol{M}^{-1}p_s pt=M−1ps进行相同的运算,得到
t = w 2 ′ s w 1 ′ + s ( w 2 ′ − w 1 ′ ) t=\frac{w_{2}^{\prime}s}{w_{1}^{\prime}+s\left( w_{2}^{\prime}-w_{1}^{\prime} \right)} t=w1′+s(w2′−w1′)w2′s
其中 w 1 ′ = ( M − 1 r ) . w ( ) , w 2 ′ = ( M − 1 R ) . w ( ) w_{1}^{\prime}=\left( \boldsymbol{M}^{-1}r \right) .w\left( \right) ,w_{2}^{\prime}=\left( \boldsymbol{M}^{-1}R \right) .w\left( \right) w1′=(M−1r).w(),w2′=(M−1R).w()
而由(3)式可知 w 1 ′ = 1 w 1 , w 2 ′ = 1 w 2 w_{1}^{\prime}=\frac{1}{w_1},w_{2}^{\prime}=\frac{1}{w_2} w1′=w11,w2′=w21
代入(5)式得 t = 1 w 2 s 1 w 1 + s ( 1 w 2 − 1 w 1 ) → 上下同乘 w 1 w 2 w 1 s w 2 + s ( w 1 − w 2 ) t=\frac{\frac{1}{w_2}s}{\frac{1}{w_1}+s\left( \frac{1}{w_2}-\frac{1}{w_1} \right)}\xrightarrow{\text{上下同乘}w_1w_2}\frac{w_1s}{w_2+s\left( w_1-w_2 \right)} t=w11+s(w21−w11)w21s上下同乘w1w2w2+s(w1−w2)w1s
结果与(6)式相同,现在我们来用这个式子来对顶点属性求矫正插值,现用color来举例, c o l t = c o l q + w 1 s w 2 + s ( w 1 − w 2 ) ( c o l Q − c o l q ) = ( 1 − s ) w 2 c o l q + s w 1 c o l Q s w 1 + ( 1 − s ) w 2 = ( 1 − s w 1 c o l q + s w 2 c o l Q ) ∗ 1 1 − s w 1 + s w 2 col_t=col_q+\frac{w_1s}{w_2+s\left( w_1-w_2 \right)}\left( col_Q-col_q \right) \\ =\frac{\left( 1-s \right) w_2col_q+sw_1col_Q}{sw_1+\left( 1-s \right) w_2} \\ =\left( \frac{1-s}{w_1}col_q+\frac{s}{w_2}col_Q \right) *\frac{1}{\frac{1-s}{w_1}+\frac{s}{w_2}} colt=colq+w2+s(w1−w2)w1s(colQ−colq)=sw1+(1−s)w2(1−s)w2colq+sw1colQ=(w11−scolq+w2scolQ)∗w11−s+w2s1
同理可得对于重心坐标
α M A w 1 + β M B w 2 + γ M C w 3 = M ( α ′ A + β ′ B + γ ′ C ) α ′ w 1 + β ′ w 2 + γ ′ w 3 \alpha \frac{\boldsymbol{M}A}{w_1}+\beta \frac{\boldsymbol{M}B}{w_2}+\gamma \frac{\boldsymbol{M}C}{w_3}=\frac{\boldsymbol{M}\left( \alpha ^{\prime}A+\beta ^{\prime}B+\gamma ^{\prime}C \right)}{\alpha ^{\prime}w_1+\beta ^{\prime}w_2+\gamma ^{\prime}w_3} αw1MA+βw2MB+γw3MC=α′w1+β′w2+γ′w3M(α′A+β′B+γ′C)
α = α ′ w 1 α ′ w 1 + β ′ w 2 + γ ′ w 3 \alpha =\frac{\alpha ^{\prime}w_1}{\alpha ^{\prime}w_1+\beta ^{\prime}w_2+\gamma ^{\prime}w_3} α=α′w1+β′w2+γ′w3α′w1
α ′ = α w 1 α w 1 + β w 2 + γ w 3 \alpha \prime=\frac{\frac{\alpha}{w_1}}{\frac{\alpha}{w_1}+\frac{\beta}{w_2}+\frac{\gamma}{w_3}} α′=w1α+w2β+w3γw1α
c o l = ( α w 1 c o l A + β w 2 c o l B + γ w 3 c o l C ) 1 α w 1 + β w 2 + γ w 3 col=\left( \frac{\alpha}{w_1}col_A+\frac{\beta}{w_2}col_B+\frac{\gamma}{w_3}col_C \right) \frac{1}{\frac{\alpha}{w_1}+\frac{\beta}{w_2}+\frac{\gamma}{w_3}} col=(w1αcolA+w2βcolB+w3γcolC)w1α+w2β+w3γ1
其它顶点属性同理,可由下图中的伪代码求得
以上是我个人的见解,如有错误还请指出,详情参考fundamentals of computer graphics(fourth edition)11.2.4,P255-258