https://www.cnblogs.com/straywriter/articles/15889273.html
https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf
https://8.136.115.103/perspective-correct-interpolation%E9%80%8F%E8%A7%86%E6%A0%A1%E6%AD%A3%E6%8F%92%E5%80%BC/
透视矫正插值
屏幕空间插值得到的Z值,根据比例矫正。
// https://comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf//Z1
float recipVPZ = 1. / viewPosition.z;// Zt 矫正后, 利用公式(12)
float viewReflectRayZ = 1. / (recipVPZ + s * (1. / d1viewPosition.z - recipVPZ));
一、插值 z
由相似三角形,有:
X 1 Z 1 = u 1 d ⇒ X 1 = u 1 Z 1 d (1) \frac{X_{1}}{Z_{1}}=\frac{u_{1}}{d} \Rightarrow X_{1}=\frac{u_{1} Z_{1}}{d} \tag{1} Z1X1=du1⇒X1=du1Z1(1)
X 2 Z 2 = u 2 d ⇒ X 2 = u 2 Z 2 d (2) \frac{X_{2}}{Z_{2}}=\frac{u_{2}}{d} \Rightarrow X_{2}=\frac{u_{2} Z_{2}}{d}\tag{2} Z2X2=du2⇒X2=du2Z2(2)
X t Z t = u s d ⇒ Z t = d X t u s (3) \frac{X_{t}}{Z_{t}}=\frac{u_{s}}{d} \Rightarrow Z_{t}=\frac{d X_{t}}{u_{s}} \tag{3} ZtXt=dus⇒Zt=usdXt(3)
通过在图像平面(或屏幕空间)中进行线性插值,有:
u s = u 1 + s ( u 2 − u 1 ) (4) u_{s}=u_{1}+s\left(u_{2}-u_{1}\right) \tag{4} us=u1+s(u2−u1)(4)
通过在相机坐标系中对图元进行线性插值,有:
X t = X 1 + t ( X 2 − X 1 ) (5) X_{t}=X_{1}+t\left(X_{2}-X_{1}\right) \tag{5} Xt=X1+t(X2−X1)(5)
Z t = Z 1 + t ( Z 2 − Z 1 ) (6) Z_{t}=Z_{1}+t\left(Z_{2}-Z_{1}\right) \tag{6} Zt=Z1+t(Z2−Z1)(6)
将(4)和(5)代入(3):
Z t = d ( X 1 + t ( X 2 − X 1 ) ) u 1 + s ( u 2 − u 1 ) (7) Z_{t}=\frac{d\left(X_{1}+t\left(X_{2}-X_{1}\right)\right)}{u_{1}+s\left(u_{2}-u_{1}\right)} \tag{7} Zt=u1+s(u2−u1)d(X1+t(X2−X1))(7)
将(1)和(2)代入(7):
Z t = d ( u 1 Z 1 d + t ( u 2 Z 2 d − u 1 Z 1 d ) ) u 1 + s ( u 2 − u 1 ) = u 1 Z 1 + t ( u 2 Z 2 − u 1 Z 1 ) u 1 + s ( u 2 − u 1 ) (8) \begin{aligned} Z_{t} &=\frac{d\left(\frac{u_{1} Z_{1}}{d}+t\left(\frac{u_{2} Z_{2}}{d}-\frac{u_{1} Z_{1}}{d}\right)\right)}{u_{1}+s\left(u_{2}-u_{1}\right)} \\ &=\frac{u_{1} Z_{1}+t\left(u_{2} Z_{2}-u_{1} Z_{1}\right)}{u_{1}+s\left(u_{2}-u_{1}\right)} \end{aligned} \tag{8} Zt=u1+s(u2−u1)d(du1Z1+t(du2Z2−du1Z1))=u1+s(u2−u1)u1Z1+t(u2Z2−u1Z1)(8)
将(6)代入(8):
Z 1 + t ( Z 2 − Z 1 ) = u 1 Z 1 + t ( u 2 Z 2 − u 1 Z 1 ) u 1 + s ( u 2 − u 1 ) (9) Z_{1}+t\left(Z_{2}-Z_{1}\right)=\frac{u_{1} Z_{1}+t\left(u_{2} Z_{2}-u_{1} Z_{1}\right)}{u_{1}+s\left(u_{2}-u_{1}\right)} \tag{9} Z1+t(Z2−Z1)=u1+s(u2−u1)u1Z1+t(u2Z2−u1Z1)(9)
可以简化为:
t = s Z 1 s Z 1 + ( 1 − s ) Z 2 (10) t=\frac{s Z_{1}}{s Z_{1}+(1-s) Z_{2}} \tag{10} t=sZ1+(1−s)Z2sZ1(10)
将(10)代入(6),有:
Z t = Z 1 + s Z 1 s Z 1 + ( 1 − s ) Z 2 ( Z 2 − Z 1 ) (11) Z_{t}=Z_{1}+\frac{s Z_{1}}{s Z_{1}+(1-s) Z_{2}}\left(Z_{2}-Z_{1}\right) \tag{11} Zt=Z1+sZ1+(1−s)Z2sZ1(Z2−Z1)(11)
可以简化为:
Z t = 1 1 Z 1 + s ( 1 Z 2 − 1 Z 1 ) (12) Z_{t}=\frac{1}{\frac{1}{Z_{1}}+s\left(\frac{1}{Z_{2}}-\frac{1}{Z_{1}}\right)} \tag{12} Zt=Z11+s(Z21−Z11)1(12)
二、插值属性
I t = I 1 + t ( I 2 − I 1 ) (13) I_{t}=I_{1}+t\left(I_{2}-I_{1}\right) \tag{13} It=I1+t(I2−I1)(13)
将(10)代入(13),有:
I t = I 1 + s Z 1 s Z 1 + ( 1 − s ) Z 2 ( I 2 − I 1 ) (14) I_{t}=I_{1}+\frac{s Z_{1}}{s Z_{1}+(1-s) Z_{2}}\left(I_{2}-I_{1}\right) \tag{14} It=I1+sZ1+(1−s)Z2sZ1(I2−I1)(14)
可以重新排列成:
I t = ( I 1 Z 1 + s ( I 2 Z 2 − I 1 Z 1 ) ) / ( 1 Z 1 + s ( 1 Z 2 − 1 Z 1 ) ) (15) I_{t}=\left(\frac{I_{1}}{Z_{1}}+s\left(\frac{I_{2}}{Z_{2}}-\frac{I_{1}}{Z_{1}}\right)\right) /\left(\frac{1}{Z_{1}}+s\left(\frac{1}{Z_{2}}-\frac{1}{Z_{1}}\right)\right) \tag{15} It=(Z1I1+s(Z2I2−Z1I1))/(Z11+s(Z21−Z11))(15)
从(12),我们可以看到(15)中的分母只是 1 / Z i 1/Z_{i} 1/Zi, 所以
I t = ( I 1 Z 1 + s ( I 2 Z 2 − I 1 Z 1 ) ) / 1 Z t (16) I_{t}=\left(\frac{I_{1}}{Z_{1}}+s\left(\frac{I_{2}}{Z_{2}}-\frac{I_{1}}{Z_{1}}\right)\right) / \frac{1}{Z_{t}} \tag{16} It=(Z1I1+s(Z2I2−Z1I1))/Zt1(16)