计算机图形学-MVP变换

2D变换

缩放

可知,变换后边长原来的s倍

x=sxxy=syy\begin{align} x’=s_{x}x \\ y’=s_{y}y \end{align}

化为矩阵形式

(xy)=(sx00sy)(xy)\begin{pmatrix} x’ \\ y’ \end{pmatrix} = \begin{pmatrix} s_{x} & 0 \\ 0 & s_{y} \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix}

切变

如图,在X方向的移动水平距离均为a/1,可得

(xy)=(1a01)(xy)\begin{pmatrix} x’ \\ y’ \end{pmatrix} = \begin{pmatrix} 1 & a \\ 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix}

旋转

默认逆时针旋转,可用待定系数法求得变换矩阵

(xy)=(cosθsinθsinθcosθ)(xy)\begin{pmatrix} x’ \\ y’ \end{pmatrix} = \begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix}

移动

移动不是线性变换,不能直接用乘法表示。

(xy)=(xy)+(txty)\begin{pmatrix} x’ \\ y’ \end{pmatrix} = \begin{pmatrix} x \\ y \end{pmatrix} + \begin{pmatrix} t_{x} \\ t_{y} \end{pmatrix}

为了统一使用乘法,引入齐次坐标。

2D点:(x,y,1)(x,y,1)

2D坐标:(x,y,0)(x,y,0)

移动矩阵可以改写为:

(xy1)=(10tx01ty001)(xy1)\begin{pmatrix} x’ \\ y’ \\ 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}

总结

缩放

S(sx,sy)=(sx000sy0001)S(s_{x}, s_{y}) = \begin{pmatrix} s_{x} & 0 & 0 \\ 0 & s_{y} & 0 \\ 0 & 0 & 1 \end{pmatrix}

旋转

R(θ)=(cosθsinθ0sinθcosθ0001)R(\theta) = \begin{pmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{pmatrix}

移动

T(tx,ty)=(10tx01ty001)T(t_{x}, t_{y}) = \begin{pmatrix} 1 & 0 & t_{x} \\ 0 & 1 & t_{y} \\ 0 & 0 & 1 \end{pmatrix}

矩阵乘法从右到左的顺序计算。

3D变换

同样的,为了统一线性与非线性变换,引入齐次坐标。

3D点:(x,y,z,1)(x, y, z, 1)

3D向量:(x,y,z,0)(x, y, z, 0)

缩放

S(sx,sy,sz)=(sx0000sy0000sz00001)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}

旋转:

Rx(θ)=(10000cosθsinθ00sinθcosθ00001)R_{x}(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta & 0 \\ 0 & \sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}
Ry(θ)=(cosθ0sinθ00100sinθ0cosθ00001)R_{y}(\theta) = \begin{pmatrix} \cos \theta & 0 & \sin \theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin \theta & 0 & \cos \theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}
Rz(θ)=(cosθsinθ00sinθcosθ0000100001)R_{z}(\theta) = \begin{pmatrix} \cos \theta & -\sin \theta & 0 & 0 \\ \sin \theta & \cos \theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}

移动

T(tx,ty,tz)=(100tx010ty001tz0001)T(t_{x}, t_{y}, t_{z}) = \begin{pmatrix} 1 & 0 & 0 & t_{x} \\ 0 & 1 & 0 & t_{y} \\ 0 & 0 & 1 & t_{z} \\ 0 & 0 & 0 & 1 \end{pmatrix}

视图变换

上面的变换方法都属于模型变换,在世界中对模型进行变换。

视图变换,即将视口摄像机移动到原点,上方向为+Y,朝向-Z,以便后续计算。同时,其他模型也需要进行变换以保持与摄像机的相对位置。所以,视图变换即使求摄像机移动到原点,并且旋转到上方向为+Y,朝向-Z的矩阵,并且对于其他模型也应用这样的矩阵。

Tview=(100x010y001z0001)T_{view} = \begin{pmatrix} 1 & 0 & 0 & -x \\ 0 & 1 & 0 & -y \\ 0 & 0 & 1 & -z \\ 0 & 0 & 0 & 1 \end{pmatrix}

设相机朝向为g^\hat{g},上方向为t^\hat{t}。需要将g^\hat{g}旋转到-Z,t^\hat{t}旋转到+Y,g^×t^\hat{g} \times \hat{t}旋转到+X。可得,

Rview1=(xg^×t^xtxg0yg^×t^ytyg0zg^×t^ztzg00001)R_{view}^{-1} = \begin{pmatrix} x_{ \hat{g} \times \hat{t} } & x_{t} & x_{-g} & 0 \\ y_{ \hat{g} \times \hat{t} } & y_{t} & y_{-g} & 0 \\ z_{ \hat{g} \times \hat{t} } & z_{t} & z_{-g} & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}

旋转矩阵为正交矩阵,所以其逆矩阵等于转置矩阵

Rview=(xg^×t^yg^×t^zg^×t^0xtytzt0xgygzg00001)R_{view} = \begin{pmatrix} 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{pmatrix}

投影变换

正交投影

将一个空间映射到正则立方体[1,1]2[-1, 1]^{2}上。

Mortho=(2rl00002tb00002nf00001)(100r+l2010t+b2001n+f20001)M_{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} \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}

透视投影

首先,可以将台体挤压为一个正方体,再对正方体作正交变换。

设挤压的变换矩阵M,即求

M(xyz1)=(xyz1)M \begin{pmatrix} x\\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} x’\\ y’ \\ z’ \\ 1 \end{pmatrix}

由相似三角形,有

M(xyz1)=(xyz1)=(nzxnzy?1)=(nxny?z)M \begin{pmatrix} x\\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} x’\\ y’ \\ z’ \\ 1 \end{pmatrix} = \begin{pmatrix} \frac{n}{z}x\\ \frac{n}{z}y \\ ? \\ 1 \end{pmatrix} = \begin{pmatrix} nx\\ ny \\ ? \\ z \end{pmatrix}

可解得

M=(n0000n00????0010)M = \begin{pmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ ? & ? & ? & ? \\ 0 & 0 & 1 & 0 \end{pmatrix}

在近平面处,n不变,将n代入

(xyn1)=(nxnyn2n)\begin{pmatrix} x \\ y \\ n \\ 1 \end{pmatrix} = \begin{pmatrix} nx \\ ny \\ n^{2} \\ n \end{pmatrix}

可得第3行一定是(0,0,A,B)(0, 0, A, B)的形式

(0,0,A,B)(xyn1)=(nxnyn2n)(0, 0, A, B) \begin{pmatrix} x \\ y \\ n \\ 1 \end{pmatrix} = \begin{pmatrix} nx \\ ny \\ n^{2} \\ n \end{pmatrix}

An+B=n2An+B=n^{2}

远平面中心点不变

(0,0,A,B)(00f1)=(00f2f)(0, 0, A, B) \begin{pmatrix} 0 \\ 0 \\ f \\ 1 \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ f^{2} \\ f \end{pmatrix}

Af+B=f2Af+B=f^{2}

解得

M=(n0000n0000n+fnf0010)M = \begin{pmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \end{pmatrix}

因为进行完MVP变换后,得到的矩阵(Xclip,yclip,Zclip,Wclip)(X_{clip}, y_{clip},Z_{clip},W_{clip}),其中Wclip=ZclipW_{clip} \equal Z_{clip}(根据左右手系的不同也可能等于相反数)。WclipW_{clip}储存了深度信息。只有将矩阵归一化,也就是除以WclipW_{clip}才能实现真正的近大远小的效果。这一步被称为齐次除法或者透视除法。

进行完MVP变化之后得到的空间被称作裁剪空间。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇