Games101笔记 投影

Games101 投影

Orthographic projection

simple way

  • center cuboid by translating

  • a cuboid \([l,r]\times[b,t]\times[f,n]\) to the canonical cube \([-1, 1]^3\)

  • Transformation matrix

    Translate (center to origin)first, then scale (\(\frac{lenght/width/height}{2}\)

    \[M_{ortho} = \left[ \begin{matrix} \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{matrix} \right] \left[ \begin{matrix} 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{matrix} \right] \]

\(\blacktriangleright\) OpenGL 投影后的坐标系是左手的,即屏幕右 \(X\) 正向, 上 \(Y\) 正向, 但是屏幕朝里面 \(Z\) 正向

Perspective projective

How to do perspective projection

  • squish the frustum into a cuboid \((n \rightarrow n, f\rightarrow f)~ (M_{persp\rightarrow ortho})\)

    • 一些规定

      • 近平面不变
      • 远平面 \(Z\) 值不变
      • 远平面的中心点不变
    • relationship between transformed points \((x', y', z')\) and original points \((x, y, z)\)

      \(y' = \frac{n}{z}y\) \(x' = \frac{n}{z}x ~ (similar~to~y')\)

    • In homogeneous coordinates

      \[\begin{pmatrix} x\\ y\\ z\\ 1 \end{pmatrix} \Rightarrow \begin{pmatrix} nx/z\\ ny/z\\ unknown\\ 1 \end{pmatrix} \times z == \begin{pmatrix} nx\\ ny\\ still~nuknown\\ z \end{pmatrix} \]

      \(\triangleright\) so squish \((persp \rightarrow ortho)\) does this

      \[\begin{pmatrix} nx\\ ny\\ unknown\\ z \end{pmatrix} == M_{persp \rightarrow ortho}^{4\times 4} \begin{pmatrix} x\\ y\\ z\\ 1 \end{pmatrix} \]

\[M_{persp \rightarrow ortho} = \begin{pmatrix} n & 0 & 0 & 0\\ 0 & n & 0 & 0\\ ? & ? & ? & ?\\ 0 & 0 & 1 & 0 \end{pmatrix} \]

\(\triangleright\) the third row is responsible to \(z'\)

​ - 在近平面上的任何点都不会变

\[\begin{pmatrix} x\\ y\\ n\\ 1 \end{pmatrix} \Rightarrow \begin{pmatrix} x\\ y\\ n\\ 1 \end{pmatrix} == \begin{pmatrix} nx\\ ny\\ n^2\\ n \end{pmatrix} \]

\(\triangleright\) 由此可以得到第三行的形式必为 \((0~0~A~B)\)

\[(0~0~A~B) \begin{pmatrix} x\\ y\\ n\\ 1 \end{pmatrix} = n^2 \Rightarrow An+B=n^2 \]

​ - 在远平面上的点的 \(z\) 值不变

\[\begin{pmatrix} 0\\ 0\\ f\\ 1 \end{pmatrix} \Rightarrow \begin{pmatrix} 0\\ 0\\ f\\ 1 \end{pmatrix} == \begin{pmatrix} 0\\ 0\\ f^2\\ f \end{pmatrix} \Rightarrow Af+B=f^2 \]

\(\triangleright\) 综上可以解出 \(A\)\(B\)

\[\begin{matrix} An + B = n^2\\ Af + B = f^2 \end{matrix} ~~ \Rightarrow ~~ \begin{matrix} A = n + f\\ B = -nf \end{matrix} \]

  • Do orthographic projection \((M_{ortho})\)

\(\blacktriangleright\) 最终于的结果也就是 \(M_{persp} = M_{ortho}M_{persp\rightarrow ortho}\)

posted @ 2021-12-02 18:19  blahhhh  阅读(59)  评论(0)    收藏  举报