InkSoul/content/computergraphic/基本变换.md

244 lines
7.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: "3D基本变换和观测变换(viewing transform)"
date: 2022-05-10T09:10:25+08:00
---
## 3D基本变换
### 平移矩阵
通常处理三维中对模型进行平移的行为
<br>$T(t_x,t_y,t_z) = \begin{pmatrix} 1 &0 &0 &t_x \\\\ 0 &1 &0 &t_y \\\\ 0 &0 &0 &t_z \\\\ 0 &0 &0 &1 \end{pmatrix} $<br>
$t_x,t_y,t_z$通常表示对应轴上平移的距离
--------------------------
### 缩放矩阵
通常处理三维中对模型进行缩放的行为
$S(s_x,s_y,s_z) = \begin{pmatrix} s_x &0 &0 &0 \\\\0 &s_y &0 &0 \\\\ 0 &0 &s_z &0 \\\\ 0 &0 &0 &1 \end{pmatrix} $
$s_x,s_y,s_z$通常表示对应xyz轴的缩放比例
-------------------------------------------
### 旋转矩阵
这个矩阵通常处理三维中对模型绕坐标轴进行旋转的行为
-------------
绕x轴旋转的矩阵为
$\begin{pmatrix} 1 &0 &0 &0 \\\\ 0 &cos(r) &-sin(r) &0 \\\\ 0 &sin(r) &cos(r) &0 \\\\ 0 &0 &0 &1 \end{pmatrix} $
---------------------
绕y轴旋转的矩阵为
$\begin{pmatrix} cos(r) &0 &sin(r) &0 \\\\ 0 &1 &0 &0 \\\\ -sin(r) &0 &cos(r) &0 \\\\ 0 &0 &0 &1 \end{pmatrix} $
-----------------------
绕z轴旋转的矩阵为
$\begin{pmatrix} cos(r) &-sin(r) &0 &0 \\\\ sin(r) &cos(r) &0 &0 \\\\ 0 &0 &1 &0 \\\\ 0 &0 &0 &1 \end{pmatrix} $
----------------------
对于给定三个旋转角度的旋转,通常使用欧拉角
$R_{xyz}(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma)$
此时的三个旋转方向将被称为roll,pich,yaw
![flight_euler](../../images/flight_euler_angle.png)
-----------------------
对于围绕某一特定点进行旋转的行为,则将该点平移至原点处后视为绕特定轴旋转
![](../../images/rotate_around_point.png)
---------------------------
### 视角变换矩阵
这个矩阵通常用来定义相机对应的视角朝向,利用这个矩阵来将相机位置移动到原点,便于后续的模型进行平移旋转等变换
```c++
Eigen::Matrix4f view = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate << 1, 0, 0, -eye_pos[0],
0, 1, 0, -eye_pos[1],
0, 0, 1, -eye_pos[2],
0, 0, 0, 1;
view = translate * view;//移动相机位置到顶点
```
在上述代码中,eye_pos(x,y,z,1)往往为相机的位置
-----------------------------------------------------------
### 正交/投影矩阵
#### 正交矩阵
正交矩阵将使摄像头置于坐标系原点,看向-Z轴方向,可以在Y轴上平行移动。
最终结果将表现为在XY轴平面上的2D图像,让模型坐标归一化到[-1,1]之间
----------------------------------
总体流程
为了将一个$[l,r]\times[b,t]\times[f,n]$的长方体转换为符合canonical(正则、规范、标准)的正方体,我们需要进行两步操作
第一步
平移这个长方体到坐标系的原点
对应矩阵$M_{translate}=\begin{bmatrix} \frac{2}{r-l} &0 &0 &0 \\\\ 0 &\frac{2}{t-b} &0 &0 \\\\ 0 &0 &0 &\frac{2}{n-f} \\\\ 0 &0 &0 &1 \end{bmatrix}$
第二步
缩放这个长方体到符合正则、规范、标准的正方体
对应矩阵$M_{scale}=\begin{bmatrix} 1 &0 &0 &-\frac{r+l}{2} \\\\ 0 &1 &0 &-\frac{t+b}{2} \\\\ 0 &0 &1 &-\frac{n+f}{2} \\\\ 0 &0 &0 &1 \end{bmatrix}$
由第一二步可得出
正交矩阵为$M_{ortho}=\begin{bmatrix} \frac{2}{r-l} &0 &0 &0 \\\\ 0 &\frac{2}{t-b} &0 &0 \\\\ 0 &0 &0 &\frac{2}{n-f} \\\\ 0 &0 &0 &1 \end{bmatrix}\times\begin{bmatrix} 1 &0 &0 &-\frac{r+l}{2} \\\\ 0 &1 &0 &-\frac{t+b}{2} \\\\ 0 &0 &1 &-\frac{n+f}{2} \\\\ 0 &0 &0 &1 \end{bmatrix}$
图示
![正交矩阵](../../images/3I~K5PRW$Y5JLF3}RC@RE0P.png)
--------------------
#### 投影矩阵
投影矩阵将使模型满足自然界透视效果,如物体近大远小、所有的平行线变得不再平行,总会交于一点
------
推导过程
将远平面与近平面连线形成的梯形“挤压”到成为一个正方体
![](../../images/X({M{B)24R8DF[AB98E9]@1.png)
挤压的过程对梯形做横切面可知,计算挤压后的坐标点值实质上为计算相似三角形
如下图,可知挤压后的坐标与之前的坐标存在的数学关系为
![](../../images/3O_1NAGAMER%[EP6T91$LBO.png)
$$y^{'}=\frac{n}{z}y$$ $$x^{'}=\frac{n}{z}x$$
由此可知经过“挤压”后的坐标为
$$M_{persp->ortho}\times\begin{pmatrix}
x \\\\ y \\\\ z \\\\ 1
\end{pmatrix}=\begin{pmatrix}
nx \\\\ ny \\\\ {未知} \\\\ z
\end{pmatrix}$$
所以
$$M_{persp->ortho}=\begin{pmatrix}
n &0 &0 &0 \\\\ 0 &n &0 &0 \\\\ ? &? &? &? \\\\ 0 &0 &1 &0
\end{pmatrix}$$
又由远平面在被“挤压”后相当于近平面做正交投影得到的远平面,可知
1. 任何在近平面上的点的坐标在“挤压”的过程中不发生改变
2. 任何在远平面的点的坐标中的Z值不发生改变,即$$\begin{pmatrix}
0 \\\\ 0 \\\\ f \\\\ 1
\end{pmatrix}\rArr\begin{pmatrix}
0 \\\\ 0 \\\\ f \\\\ 1
\end{pmatrix}==\begin{pmatrix}
0 \\\\ 0 \\\\ f^2 \\\\ f
\end{pmatrix}$$
在将上述坐标公式中Z的值以n替换之后可得
$$\begin{pmatrix}
x \\\\ y \\\\ n \\\\ 1
\end{pmatrix}\rArr\begin{pmatrix}
x \\\\ y \\\\ n \\\\ 1
\end{pmatrix}==\begin{pmatrix}
nx \\\\ ny \\\\ n^2 \\\\ n
\end{pmatrix}$$
据此,可推测第三行未知坐标值符合以下关系
$$\begin{pmatrix}
0 &0 &A &B
\end{pmatrix}\begin{pmatrix}
x \\\\ y \\\\ n \\\\ 1
\end{pmatrix}=n^2$$
因此
$$An+B=n^2$$
联立性质2推导的
$$Af+B=f^2$$
可得出
$$A=n+f$$
$$B=-nf$$
所以
$$M_{persp}=M_{ortho}M_{persp->ortho}=$$
$$\begin{pmatrix} \frac{2}{r-l} &0 &0 &0 \\\\ 0 &\frac{2}{t-b} &0 &0 \\\\ 0 &0 &0 &\frac{2}{n-f} \\\\ 0 &0 &0 &1 \end{pmatrix}$$
$$\times$$
$$\begin{pmatrix} 1 &0 &0 &-\frac{r+l}{2} \\\\ 0 &1 &0 &-\frac{t+b}{2} \\\\ 0 &0 &1 &-\frac{n+f}{2} \\\\ 0 &0 &0 &1 \end{pmatrix}$$
$$\times$$
$$\begin{pmatrix} n &0 &0 &0 \\\\ 0 &n &0 &0 \\\\ 0 &0 &n+f &-nf \\\\ 0 &0 &1 &0 \end{pmatrix}$$
----------------------------------------------
#### 涉及FovY的投影矩阵
FovY表示视域即摄像机在固定时能看到的最大角度或最低角度的范围
Aspect ratio 表示纵横比,投影平面的长宽比
![](../../images/fovY.png)
对应的相似三角形关系不变,参数改变
![](../../images/triangle.png)
可得如下关系
$$\tan{\frac{fovY}{2}}=\frac{t}{|n|}$$
$$aspect=\frac{r}{t}$$
因此
$$t=near\times tan(\frac{fovY}{2})$$
$$r=aspect\times near\times tan(\frac{fovY}{2})$$
$$l=-aspect\times near \times tan(fovY/2)$$
带入上述由l,b,n,f构成的矩阵可得
$$M_{persp->ortho}$$
$$=$$
$$\begin{pmatrix} \frac{2}{r-l} &0 &0 &0 \\\\ 0 &\frac{2}{t-b} &0 &0 \\\\ 0 &0 &\frac{2}{n-f} &0 \\\\ 0 &0 &0 &1 \end{pmatrix}$$
$$\times$$
$$\begin{pmatrix} 1 &0 &0 &-\frac{r+l}{2} \\\\ 0 &1 &0 &-\frac{t+b}{2} \\\\ 0 &0 &1 &-\frac{n+f}{2} \\\\ 0 &0 &0 &1 \end{pmatrix}$$
$$=$$
$$\begin{pmatrix}
\frac{\frac{\cot{FovY}}{2}}{apsect*near} &0 &0 &0 \\\\ 0 & \frac{\frac{\cot{FovY}}{2}}{near} & 0 & 0 \\\\ 0 & 0 & \frac{2}{near-far} & 0 \\\\ 0 & 0 & 0 & 1
\end{pmatrix}$$
$$\times$$
$$\begin{pmatrix}
1 &0 &0 &0 \\\\ 0 &1 &0 &0 \\\\ 0 &0 &1 &-\frac{near+far}{2} \\\\ 0 &0 &0 &1
\end{pmatrix}$$
$$=$$
$$\begin{pmatrix}
\frac{\frac{\cot{FovY}}{2}}{apsect * near} &0 &0 &0 \\\\ 0 &\frac{\frac{\cot{FovY}}{2}}{near} &0 &0 \\\\ 0 &0 &\frac{2}{near-far} &-\frac{near+far}{near-far} \\\\ 0 &0 &0 &1
\end{pmatrix}$$
----------------
#### 视口变换
经过MVP矩阵计算后得到的一个正则的正方体需要将X轴和Y轴上的坐标映射到屏幕坐标[0,width]$\times$[0,height]
变换时需要先将[-1,1]缩放到屏幕大小[width,height],再进行平移使得原点坐标与屏幕原点对齐
变换矩阵
$$M_{viewport}=\begin{bmatrix}
\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{bmatrix}$$