视频加载失败

Transformation Cont 视图变换

427 字
2 分钟
Transformation Cont 视图变换
Transformation Cont 视图变换

三维齐次坐标下变换的矩阵#

Scale#

S(sx,sy,sz)=[sx0000sy0000sz00001]S(s_x,s_y,s_z)=\begin{bmatrix} s_x&0&0&0\\0&s_y&0&0\\0&0&s_z&0\\0&0&0&1 \end{bmatrix}

Translation#

T(tx,ty,tz)=[100tx010ty001tz0001]T(t_x,t_y,t_z)=\begin{bmatrix} 1&0&0&t_x\\0&1&0&t_y\\0&0&1&t_z\\0&0&0&1 \end{bmatrix}

Rotation#

Rx(α)=[10000cosα−sinα00sinαcosα00001]R_x(\alpha) = \begin{bmatrix} 1&0&0&0\\0&cos\alpha&-sin\alpha&0\\0&sin\alpha&cos\alpha&0\\0&0&0&1 \end{bmatrix}Ry(α)=[cosα0sinα00100−sinα0cosα00001]R_y(\alpha) = \begin{bmatrix} cos\alpha&0&sin\alpha&0\\0&1&0&0\\-sin\alpha&0&cos\alpha&0\\0&0&0&1 \end{bmatrix}

由于指定了z轴方向为z=x×yz=x\times y,因此x×z=−yx\times z=-y

Rz(α)=[cosα−sinα00sinαcosα0000100001]R_z(\alpha) = \begin{bmatrix} cos\alpha&-sin\alpha&0&0\\ sin\alpha&cos\alpha&0&0\\0&0&1&0\\0&0&0&1 \end{bmatrix}
  • 任意的3D旋转:Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)R_{xyz}(\alpha,\beta,\gamma)=R_x(\alpha)R_y(\beta)R_z(\gamma)
  • 罗德里格斯旋转公式: R(n,α)=cos(α)I+(1−cos(α))n⊗nT+sin(α)[0−nznynz0−nx−nynx0]R(\bm{n},\alpha)=cos(\alpha)\bm{I}+(1-cos(\alpha))\bm{n}\otimes\bm{n^T}+sin(\alpha)\begin{bmatrix} 0&-n_z&n_y\\n_z&0&-n_x\\-n_y&n_x&0 \end{bmatrix}

View / Camera Transformation 模型视图变换#

将相机和模型一起移动,相机移动到世界坐标的原点

  • 相机需要的要素:
    1. Position e⃗\vec{e}
    2. Look-at/gaze direction g^\hat{g}
    3. Up direction t^\hat{t}
  • 相机规定:
    1. g^\hat{g}为-Z方向
    2. t^\hat{t}为Y方向
  • 相机变换:

    1.平移回原点2.调整角度(g→−Z,t→Y,g×t→Xg→-Z,t→Y,g\times t→X)

Mview=RviewTviewM_{view}=R_{view}T_{view}

Tview=[100−xe010−ye001−ze0001]T_{view}=\begin{bmatrix} 1&0&0&-x_e\\ 0&1&0&-y_e\\ 0&0&1&-z_e\\ 0&0&0&1 \end{bmatrix}Rview=[xg^×t^yg^×t^zg^×t^0xtytzt0x−gy−gz−g00001]R_{view}=\begin{bmatrix} x_{\hat{g}\times \hat{t}}&y_{\hat{g}\times \hat{t}}& z_{\hat{g}\times \hat{t}}&0\\x_t&y_t&z_t&0\\x_{-g}&y_{-g}&z_{-g}&0\\0&0&0&1 \end{bmatrix}

可以先求Rview−1R^{-1}_{view},再根据旋转矩阵为正交矩阵的性质求Rview=(Rview−1)TR_{view}=(R^{-1}_{view})^T

Projection Transformation 投影变换#

Orthographic projection 正交投影#

  • 丢掉Z方向元素
  • 归一化
Mortho=[2r−l00002t−b00002n−f00001][100−r+l2010−t+b2001−n+f20001]M_{ortho}=\begin{bmatrix} \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{bmatrix} \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}

Perspective Projection 透视投影#

将模型从空间坐标映射到摄像机近平面的坐标

  • 先将远平面挤压为近平面,再进行正交投影
  1. 规定挤压后的远平面z方向不变
  2. 规定近平面挤压后不变
  3. 规定挤压后远平面中心点位置不变
Mpersp→ortho=[n0000n0000n+f−nf0010]M_{persp→ortho} = \begin{bmatrix} n&0&0&0\\0&n&0&0\\0&0&n+f&-nf\\ 0&0&1&0 \end{bmatrix}Mpersp=MorthoMpersp→orthoM_{persp} =M_{ortho}M_{persp→ortho}

推导#

y′=nzyx′=nzxy'=\frac{n}{z}y\quad x'=\frac{n}{z}x

[xyz1]  ⟹  [nx/zny/zunknown1]=[nxnyunknownz]\begin{bmatrix} x\\y\\z\\1 \end{bmatrix}\implies \begin{bmatrix} nx/z\\ ny/z\\ {unknown} \\ 1 \end{bmatrix}= \begin{bmatrix} nx\\ ny\\ {unknown} \\ z \end{bmatrix}Mpersp→ortho[xyz1]=[nxbyunknownz]  ⟹  Mpersp→ortho=[n0000n00????0010]M_{persp→ortho}\begin{bmatrix} x\\y\\z\\1 \end{bmatrix}= \begin{bmatrix} nx\\ by\\ {unknown}\\ z \end{bmatrix}\implies M_{persp→ortho} = \begin{bmatrix} n&0&0&0\\0&n&0&0\\{?}&{?}&{?}&{?}\\ 0&0&1&0 \end{bmatrix}

又因为近平面的性质(近平面上任何一个点变换后坐标都不变):

Mpersp→ortho[xyn1]=[nxnyn2n]M_{persp→ortho}\begin{bmatrix} x\\ y\\ n\\ 1 \end{bmatrix}=\begin{bmatrix} nx \\ ny \\ n^2 \\ n \end{bmatrix}

故 Mpersp→orthoM_{persp→ortho} 第三行:

[00AB][xyn1]=n2  ⟹  An+B=n2\begin{bmatrix} 0&0&A&B \end{bmatrix}\begin{bmatrix} x\\ y\\ n\\ 1 \end{bmatrix}=n^2\implies An+B = n^2

又因为远平面中心点的性质(远平面的中心点的坐标值不变):

Mpersp→ortho[00f1]=[00f2f]  ⟹  Af+B=f2M_{persp→ortho}\begin{bmatrix} 0\\ 0\\ f\\ 1 \end{bmatrix}=\begin{bmatrix} 0\\0\\ f^2\\ f \end{bmatrix}\implies Af+B = f^2

故: A=n+fB=−nfA=n+f\quad B=-nf 即:

Mpersp→ortho=[n0000n0000n+f−nf0010]M_{persp→ortho} = \begin{bmatrix} n&0&0&0\\0&n&0&0\\0&0&n+f&-nf\\ 0&0&1&0 \end{bmatrix}

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Transformation Cont 视图变换
https://blog-zeta-seven-36.vercel.app/posts/graphics-study/games-101-study/l-04-transformation-cont/
作者
机巧叶片
发布于
2026-10-13
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
机巧叶片
给互联网留下一点微小的贡献
公告
欢迎来到我的博客!这是一则示例公告。
分类
标签
最新动态
站点统计
文章
26
分类
6
标签
22
总字数
27,977
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Cloudflare Pages
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0
文章目录