从渲染对象(objects)到屏幕(Raster)需要MVP变换,即Model Translation(渲染对象自身变换)、View Translation(相机变换)、Projection Translation(投影变换)。

在上一篇GAMES101:投影变换中,说到投影变换中的包围区域(Near、Far、Left、Right、Top、Bottom)。那么如何计算呢?

对于相机,除了它是一个3D object,拥有空间属性外,还定义了远近范围(Near-Far)、可视范围(Field-of-View也就是常见的FOV)和横纵比(Aspect Ratio)。

如图,可以推导出Near平面有 \(\tan\frac{fovY}{2}=\frac{t}{|n|} \\ aspect = \frac{r}{t}\) 得到点 \((r,t,n)\)。同理可以求得Far平面。

视口变换(Viewport Translation)

通过 \(M_{ortho}M_{presp \rightarrow ortho}\) 得到标准Cube \([-1,1]^3\)。

接下来只需要把xy投影到大小为width x height像素的屏幕上:\([-1,1]^2 \rightarrow [0,width]\times[0,height] \Rightarrow M_{viewport}=\begin{pmatrix} \frac{width}{2} & 0 & 0 & \frac{width}{2} \\ 0 & \frac{height}{2} & 0 & \frac{height}{2} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\)

小结

终于,通过 \(\begin{pmatrix}x’ \\ y’ \\ z’ \\ 1\end{pmatrix} = M_{viewport}M_{ortho}M_{presp \rightarrow ortho}\begin{pmatrix}x \\ y \\ z \\ 1\end{pmatrix}\) 把三维空间的点投影到了二维的屏幕空间~

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注