[计算机图形学]视图变换:MVP变换、视口变换

1、MVP变换

MVP变换是模型变换(M)、视角变换(V)、投影变换(P)的统称。MVP变换操做的是三维空间中的点,通过MVP变换后会被映射到标准二维平面上(实际上这个标准二维平面仍保留了z轴坐标)。ui

1. 模型变换

模型变换在三维空间中对物体进行的操做,对三维物体自己进行缩放、旋转、平移操做spa

注意,模型变换是相对于三维坐标系(亦称世界坐标系)的原点进行的!
特别地,当须要绕物体中心旋转时,须要将物体中心移动到原点,而后进行相应操做,操做完成后再移动回去(用矩阵描述为:\(M = M_t^{-1} \cdot M_s \cdot M_r \cdot M_t\),而后与点进行操做,\(Point_N = M \cdot Point\))。
上述M矩阵的点乘顺序是随机的,同时也不是都须要的。同时你们能够思考一下调换项的位置会发生什么效果?.net

这里,只给出缩放、旋转、平移矩阵,不作解释说明(若是想知道详细过程,简单)
下面列举的公式与前面的没有必然联系blog

1.1 缩放矩阵

\[ M_s = \begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \]

1.2 旋转矩阵

参考文章:https://blog.csdn.net/csxiaoshui/article/details/65446125get

\[ M_{rx} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\theta & -sin\theta & 0 \\ 0 & sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} M_{ry} = \begin{pmatrix} cos\theta & 0 & sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} M_{rz} = \begin{pmatrix} cos\theta & -sin\theta & 0 & 0 \\ sin\theta & cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \]

1.3 平移矩阵

\[ M_t = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \]

使用时,只须要带入要变换的点(为方便分析,这里没有给出放缩矩阵和平移矩阵)class

\[ \begin{pmatrix} x' \\ y' \\ z' \\ 1 \end{pmatrix} = M_r \cdot \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\theta & -sin\theta & 0 \\ 0 & sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} cos\theta & 0 & sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} cos\theta & -sin\theta & 0 & 0 \\ sin\theta & cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} \]

若是你须要绕物体中心进行相应的操做(为方便分析,这里没有给出放缩矩阵)
这里的平移矩阵须要计算物体中心的坐标,而后计算到三维空间坐标系原点的距离
一样,反平移矩阵计算过程相似mvp

\[ \begin{pmatrix} x' \\ y' \\ z' \\ 1 \end{pmatrix} = M_t^{-1} \cdot M_r \cdot M_t \cdot \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & -t_x \\ 0 & 1 & 0 & -t_y \\ 0 & 0 & 1 & -t_z \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\theta & -sin\theta & 0 \\ 0 & sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} cos\theta & 0 & sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} cos\theta & -sin\theta & 0 & 0 \\ sin\theta & cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \cdot \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} \]

若是须要引入实际的平移矩阵,则变成
其中\(M_{tr}\)为平移矩阵,\(M_t\)是经物体中心移动到坐标原点自动添加的viewport

\[ \begin{pmatrix} x' \\ y' \\ z' \\ 1 \end{pmatrix} = M_{tr} \cdot M_t^{-1} \cdot M_r \cdot M_t \cdot \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} \]

最后,若是须要再加上放缩矩阵,这个就不给出了(只须要知道将放缩矩阵放在不一样的位置会有的含义就能够了)移动

如下内容在单片机上基本上就不要奢求了,因此用到的不多,所以本文中再也不多作介绍,甚至也不会给出公式。
若是由兴趣,欢迎一块儿探讨(liangzongnan0214@163.com)di

2. 视角变换

将三维空间坐标系内的点映射到相机坐标系中

须要给出相机所处的三维空间的位置、相机观察点、相机向上方向(与相机位置和相机观察点构成的连线垂直)
而后根据右手法则来创建相机坐标系
在建系时,统一先归一化到单位向量,由于只须要方向信息

至于如何将三维空间坐标系映射到相机坐标系,这里给出一篇参考文章

参考文章:https://blog.csdn.net/silangquan/article/details/50987196

3. 投影变换

将相机坐标系映射到标准的二维平面上(注意,这里的z轴信息没有丢失,正因如此,才能够进行后续的着色、遮挡等工做)
只须要知道,投影变换包括正交投影和透视投影,透视投影的前期就是正交投影

参考文章:http://www.javashuo.com/article/p-vykizixx-pv.html

2、Viewport变换

Viewport变换只作了一件事:将投影变换获得的标准二维平面调整到显示屏幕上 所以,Viewport变换本质是一个放缩+平移的过程

相关文章
相关标签/搜索