本文主要是介绍OpenGL Transformation 几何变换的顺序概要(MVP,NDC,Window坐标变换过程),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
OpenGL Transformation 几何变换的顺序概要(MVP,NDC,Window坐标变换过程)
Geometry transforming or culling sequence:
- application passing the vertex pos(object space pos) to the shader program。
- transforming of clip space pos from object space pos by mulipling the MVP matrix。
- to world space pos from object space pos by multipling the model/world matrix。
( x w o r l d y w o r l d z w o r l d ) = M m o d e l / w o r l d ( x o b j y o b j z o b j ) \left( \begin{matrix} x_{\color{#aaaaaa}world}\\ y_{\color{#aaaaaa}world}\\ z_{\color{#aaaaaa}world} \end{matrix} \right)= M_{\color{#aaaaaa}model/world} \left( \begin{matrix} x_{\color{#aaaaaa}obj}\\ y_{\color{#aaaaaa}obj}\\ z_{\color{#aaaaaa}obj} \end{matrix} \right) xworldyworldzworld =Mmodel/world xobjyobjzobj - to view space pos from world space pos by multipling the view/camera/eye matrix。
- right-hand coordinates
( x e y e y e y e z e y e ) = M m o d e l V i e w ( x o b j y o b j z o b j ) = M v i e w ⋅ M m o d e l / w o r l d ( x o b j y o b j z o b j ) \left( \begin{matrix} x_{\color{#aaaaaa}eye}\\ y_{\color{#aaaaaa}eye}\\ z_{\color{#aaaaaa}eye} \end{matrix} \right)= M_{\color{#aaaaaa}modelView} \left( \begin{matrix} x_{\color{#aaaaaa}obj}\\ y_{\color{#aaaaaa}obj}\\ z_{\color{#aaaaaa}obj} \end{matrix} \right)= M_{\color{#aaaaaa}view} \cdot M_{\color{#aaaaaa}model/world} \left( \begin{matrix} x_{\color{#aaaaaa}obj}\\ y_{\color{#aaaaaa}obj}\\ z_{\color{#aaaaaa}obj} \end{matrix} \right) xeyeyeyezeye =MmodelView xobjyobjzobj =Mview⋅Mmodel/world xobjyobjzobj
- right-hand coordinates
- to projection/clip space pos from view space pos by multipling the projective/clip matrix。
( x c l i p y c l i p z c l i p ) = M p r o j e c t ( x e y e y e y e z e y e ) \left( \begin{matrix} x_{\color{#aaaaaa}clip}\\ y_{\color{#aaaaaa}clip}\\ z_{\color{#aaaaaa}clip} \end{matrix} \right)= M_{\color{#aaaaaa}project} \left( \begin{matrix} x_{\color{#aaaaaa}eye}\\ y_{\color{#aaaaaa}eye}\\ z_{\color{#aaaaaa}eye} \end{matrix} \right) xclipyclipzclip =Mproject xeyeyeyezeye - culling the vertex which − w c l i p > x y z c l i p > w c l i p -w_{\color{#aaaaaa}clip}>xyz_{\color{#aaaaaa}clip}>w_{\color{#aaaaaa}clip} −wclip>xyzclip>wclip。
- add edge/vertex when culling occurs。
- only reserve the vertex which − w c l i p < x y z c l i p < w c l i p -w_{\color{#aaaaaa}clip}<xyz_{\color{#aaaaaa}clip}<w_{\color{#aaaaaa}clip} −wclip<xyzclip<wclip。
- projection/clip space pos still are Homogeneouse coordinates。
- to perspective effect, transforming to NDC space (Euclidean/Cartesian space) from projection/clip space pos, then dividing by the w c l i p w_{\color{#aaaaaa}clip} wclip, x y z n d c = x y z c l i p / w c l i p xyz_{\color{#aaaaaa}ndc}=xyz_{\color{#aaaaaa}clip}/w_{\color{#aaaaaa}clip} xyzndc=xyzclip/wclip。
- culling the vertex which − w c l i p > x y z c l i p > w c l i p -w_{\color{#aaaaaa}clip}>xyz_{\color{#aaaaaa}clip}>w_{\color{#aaaaaa}clip} −wclip>xyzclip>wclip。
- to world space pos from object space pos by multipling the model/world matrix。
- to NDC(Normalized Device Coordinates) space, just using clip space pos divide by w c l i p w_{\color{#aaaaaa}clip} wclip。
∵ e y e s p a c e i s r i g h t − h a n d c o o r d i n a t e s ∴ w c l i p = − z c l i p ∴ ( x c l i p y c l i p z c l i p w c l i p → ( − z c l i p ) ) = [ ⋅ ⋯ ⋯ ⋅ ⋮ ⋱ ⋱ ⋮ ⋅ ⋯ ⋯ ⋅ 0 0 − 1 0 ] ( x e y e y e y e z e y e w e y e ) ( x n d c y n d c z n d c ) = ( x c l i p / w c l i p y c l i p / w c l i p z c l i p / w c l i p ) { − w c l i p < x y z n d c < w c l i p x y z n d c = x y z c l i p / w c l i p − 1 < x y z n d c < 1 \because eye \space space \space is \space right-hand \space coordinates\\ \therefore w_{\color{#aaaaaa}clip}=-z_{\color{#aaaaaa}clip}\\ \therefore\left( \begin{matrix} x_{\color{#aaaaaa}clip}\\ y_{\color{#aaaaaa}clip}\\ z_{\color{#aaaaaa}clip}\\ w_{\color{#aaaaaa}clip} \rightarrow ({\color{#ff0000}-z_{\color{#aaaaaa}clip}})\\ \end{matrix} \right)= \begin{bmatrix} \cdot & \cdots & \cdots & \cdot \\ \vdots & \ddots & \ddots & \vdots \\ \cdot & \cdots & \cdots & \cdot \\ 0 & 0 & \color{#ff0000}-1 & 0 \end{bmatrix} \left( \begin{matrix} x_{\color{#aaaaaa}eye}\\ y_{\color{#aaaaaa}eye}\\ \color{#ff0000}z_{\color{#aaaaaa}eye}\\ \color{#ff0000}w_{\color{#aaaaaa}eye} \end{matrix} \right)\\ \left( \begin{matrix} x_{\color{#aaaaaa}ndc}\\ y_{\color{#aaaaaa}ndc}\\ z_{\color{#aaaaaa}ndc} \end{matrix} \right)= \left( \begin{matrix} x_{\color{#aaaaaa}clip}/w_{\color{#aaaaaa}clip}\\ y_{\color{#aaaaaa}clip}/w_{\color{#aaaaaa}clip}\\ z_{\color{#aaaaaa}clip}/w_{\color{#aaaaaa}clip} \end{matrix} \right) \begin{cases} -w_{\color{#aaaaaa}clip}<xyz_{\color{#aaaaaa}ndc}<w_{\color{#aaaaaa}clip}\\ xyz_{\color{#aaaaaa}ndc}=xyz_{\color{#aaaaaa}clip}/w_{\color{#aaaaaa}clip}\\ -1<xyz_{\color{#aaaaaa}ndc}<1 \end{cases} ∵eye space is right−hand coordinates∴wclip=−zclip∴ xclipyclipzclipwclip→(−zclip) = ⋅⋮⋅0⋯⋱⋯0⋯⋱⋯−1⋅⋮⋅0 xeyeyeyezeyeweye xndcyndczndc = xclip/wclipyclip/wclipzclip/wclip ⎩ ⎨ ⎧−wclip<xyzndc<wclipxyzndc=xyzclip/wclip−1<xyzndc<1- w c l i p = − z c l i p w_{\color{#aaaaaa}clip}=-z_{\color{#aaaaaa}clip} wclip=−zclip see OpenGL Projection Matrix。
- NDC like a cube, [l,r] range [-1,1], [b,t] range [-1,1], [-n,-f] range [-1,1],see Perspective Projection。
- to mapping Window Coordinates (Screen Coordinates), using glViewport(x,y,w,h), glDepthRange(n,f) functions set the parameters of mapping, see following formula:
( x w i n d o w / s c r e e n y w i n d o w / s c r e e n z w i n d o w / s c r e e n ) = ( w 2 x n d c + ( x + w 2 ) h 2 y n d c + ( y + h 2 ) f − n 2 z n d c + ( f + n ) 2 ) { x w i n d o w = { − 1 ⇒ x 1 ⇒ x + w y w i n d o w = { − 1 ⇒ y 1 ⇒ y + h z w i n d o w = { − 1 ⇒ n 1 ⇒ f \left( \begin{matrix} x_{\color{#aaaaaa}window/screen}\\ y_{\color{#aaaaaa}window/screen}\\ z_{\color{#aaaaaa}window/screen} \end{matrix} \right)= \left( \begin{matrix} \frac{w}{2}x_{\color{#aaaaaa}ndc}+\left(x+\frac{w}{2}\right) \\ \frac{h}{2}y_{\color{#aaaaaa}ndc}+\left(y+\frac{h}{2}\right) \\ \frac{f-n}{2}z_{\color{#aaaaaa}ndc}+\frac{\left(f+n\right)}{2} \end{matrix} \right) \begin{cases} x_{\color{#aaaaaa}window}= \begin{cases} -1 \Rightarrow x \\ 1 \Rightarrow x+w \\ \end{cases}\\ y_{\color{#aaaaaa}window}= \begin{cases} -1 \Rightarrow y \\ 1 \Rightarrow y+h \\ \end{cases}\\ z_{\color{#aaaaaa}window}= \begin{cases} -1 \Rightarrow n \\ 1 \Rightarrow f \\ \end{cases}\\ \end{cases} xwindow/screenywindow/screenzwindow/screen = 2wxndc+(x+2w)2hyndc+(y+2h)2f−nzndc+2(f+n) ⎩ ⎨ ⎧xwindow={−1⇒x1⇒x+wywindow={−1⇒y1⇒y+hzwindow={−1⇒n1⇒f
参考前辈一文:纹理投影,里面有 GPU-GEMS 一图
References
- LaTeX 各种命令,符号
- OpenGL Transformation
- Object Space Coordinates
- Eye Space Coordinates
- Clip Space Coordinates
- NDC(Normalized Device Coordinates)
- Perspective Projection
- Window Coordinates (Screen Coordinates)
- OpenGL Projection Matrix
- Homogeneous Coordinates
这篇关于OpenGL Transformation 几何变换的顺序概要(MVP,NDC,Window坐标变换过程)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!