美文网首页
MVP transformation

MVP transformation

作者: ospacer | 来源:发表于2020-10-24 11:22 被阅读0次

(本文为三无产品。不含详细推导过程,不含示意图,不知所云。仅供围观)


M = M_{vp}*M_{proj}*M_{view}*M_{model}

  1. model transformation M_{model}

    模型在世界坐标系下的表示

  2. view/camera transformation M_{view}

    将相机调整到世界坐标系上,使得模型在相机坐标系下表示

  3. projection transformation M_{proj}

设模型位于给定frustum/cube中,将其映射至[-1,1]^3的标准立方体中

  1. viewport transformation M_{vp}

    将标准立方体映射至显示屏幕上

1. Model Transformation (M)

from model to world transform: M_{model}

2. View/Camera Transformation (V)

from world coordinate to camera

  • \bf{e} -- camera/eye position
  • \bf{g} -- gaze direction (look at)
  • \bf{t} -- view-up vector

其中\bf{t}\bf{g}不一定垂直

世界坐标系:\{\bf{x},\bf{y},\bf{z}\}

camera的base在世界坐标系的表示为:\{\bf{u},\bf{v},\bf{w}\},最后归一化
\begin{align} \bf{w} &= -\bf{g} \\ \bf{u} &= \bf{t} \times \bf{w} \\ \bf{v} &= \bf{w} \times \bf{u} \\ \end{align}
R_{world \to cam}表示为:
R_{world \to cam} = \begin{pmatrix} \bf{u} & \bf{v} & \bf{w} \end{pmatrix}
M_{view}表示为:
\begin{align} M_{view} &= \begin{pmatrix} R_{world \to cam}^{-1} & -\bf{e} \\ \bf{0} & 1 \\ \end{pmatrix} \\ &= \begin{pmatrix} \bf{u}_x & \bf{u}_y & \bf{u}_z & -\bf{e}_x \\ \bf{v}_x & \bf{v}_y & \bf{v}_z & -\bf{e}_y \\ \bf{w}_x & \bf{w}_y & \bf{w}_z & -\bf{e}_z \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \end{align}

3. Projection Transformation (P)

M_{proj} = \begin{cases} M_{orth} & \text {Sec 3.1} \\ M_{persp} & \text {Sec 3.2} \\ \end{cases}

3.1 Orthographic Projection Transformation (正交投影变换)

from cube to canonical cube

define cube plane:

  • l -- left plane
  • r -- right plane
  • b -- bottom plane
  • t -- top plane
  • n -- near plane
  • f -- far plane

变换步骤:先平移至原点,再缩放
M_{orth} = \begin{pmatrix} \cfrac{2}{r-l} & 0 & 0 & -\cfrac{r+l}{r-l} \\ 0 & \cfrac{2}{t-b} & 0 & -\cfrac{t+b}{t-b} \\ 0 & 0 & \cfrac{2}{n-f} & -\cfrac{n+f}{n-f} \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}

3.2 Perspective Projection Transformation (透视投影变换)

M_{persp \to orth}: from frustum to cube: 保持近平面不变,远平面形状压缩为近平面(z值不变)

define frustum plane distance:

  • n -- near plane
  • f -- far plane

以下公式基于虎书(此处假设n,f<0
M_{persp \to orth} = \begin{pmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \\ \end{pmatrix}

而在一些图形API中,假设n,f>0,此时:
M_{persp \to orth} = \begin{pmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & -1 & 0 \\ \end{pmatrix}

最终透视投影变换可以表示为:
M_{persp} = M_{orth} * M_{persp \to orth}

如果给定以下参数,define frustum para:

  • fov -- field of view (视场角,竖直方向)
  • aspect\_ratio -- 宽高比

so:

ratio = \frac{w}{h} \\ h = 2 |n| \tan{\frac{fov}{2}} \\ w = ratio \cdot 2 |n| \tan{\frac{fov}{2}} \\

考虑frustum参数,并且认为frustum设置于camera朝向轴线上,M_{orth}可以表示为:
M_{orth} = \begin{pmatrix} \cfrac{-n/|n|}{ratio \cdot \tan{\frac{fov}{2}}} & 0 & 0 & 0 \\ 0 & \cfrac{-n/|n|}{\tan{\frac{fov}{2}}} & 0 & 0 \\ 0 & 0 & \cfrac{2}{n-f} & -\cfrac{n+f}{n-f} \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}

so:
M_{persp} = \begin{pmatrix} \cfrac{-n/|n|}{ratio \cdot \tan{\frac{fov}{2}}} & 0 & 0 & 0 \\ 0 & \cfrac{-n/|n|}{\tan{\frac{fov}{2}}} & 0 & 0 \\ 0 & 0 & \cfrac{n+f}{n-f} & -\cfrac{2nf}{n-f} \\ 0 & 0 & n/|n| & 0 \\ \end{pmatrix}

4. Viewport Transformation (视口变换)

from canonical cube to screen

define M_{vp}:can \to scr

scale: [-1, 1]^2 \to [0, width]\times[0,height],z方向不变
M_{vp} = \begin{pmatrix} \cfrac{w}{2} & 0 & 0 & \cfrac{w}{2} \\ 0 & \cfrac{h}{2} & 0 & \cfrac{h}{2} \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}

Ref

  1. games101
  2. Fundamentals of Computer Graphics, Fourth Edition, 4th Edition
  3. 计算机图形学二:视图变换(坐标系转化,正交投影,透视投影,视口变换)

相关文章

网友评论

      本文标题:MVP transformation

      本文链接:https://www.haomeiwen.com/subject/wduimktx.html