
5/21/14 Specifying the view transformation • Most commonly parameterized by: – Position of camera – Position of point to look at – Vector indicating “up” direction of camera 3D to 2D Projection • In Direct3D: D3DXMatrixLookAtLH! – D3D uses a LHS, but also have D3DXMatrixLookAtRH • In XNA: Matrix.CreateLookAt (RHS) Prof. Aaron Lanterman • In OpenGL: gluLookAt (RHS) (Based on slides by Prof. Hsien-Hsin Sean Lee) • Can also build a rotation+translation matrix as if the School of Electrical and Computer Engineering camera was an object in scene, then take the Georgia Institute of Technology inverse of that matrix msdn.microsoft.com/en-us/library/bb205342(VS.85).aspx msdn.microsoft.com/en-us/library/bb205343(VS.85).aspx 2 Projection from 3D space Canonical view volume • Projection transforms your geometry into a canonical view volume in normalized device coordinates • Only X- and Y-coordinates will be mapped onto the screen y • Z will be almost useless, but used for depth test y (1, 1, 1) z z (0, 0, 0) x x (-1, -1, 0) Canonical view volume (LHS) Much discussion adapted from Joe Farrell’s article (http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__1/ ) (-1, -1, 0) to (1,1,1) used by Direct3D 3 4 1 5/21/14 Strange “conventions” Orthographic (or parallel) projection y y (1, 1, 1) (1, 1, 1) View plane z z (0, 0, 0) x (0, 0, 0) x Viewer’s (-1, -1, 0) (-1, -1,-1) position Canonical view volume (LHS) Canonical view volume (LHS) (-1, -1, 0) to (1,1,1) used by D3D/XNA (-1, -1, 1) to (1,1,1) used by OpenGL (remember eye-space coordinates in (remember eye-space coordinates in XNA are in a RHS!!!) OpenGL are in a RHS!!!) • Project from 3D space to the viewer’s 2D space 5 6 Style of orthographic projection Orthographic projection (r, t, f) y (1, 1, 1) • Same size in 2D and 3D • No sense of distance z (l, b, n) View Volume • Parallel lines remain parallel x • Good for tile-based games where camera is in fixed location (an axis-aligned box) (e.g., Mahjong or 3D Tetris) (-1, -1, 0) Canonical view volume (D3D & XNA) See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 7 8 2 5/21/14 Orthographic projection math (1) Orthographic projection math (2) • Derive x’ and y’ • Derive z’ (slightly different for the range in D3D) 2(x − l) x∈[l, r] x'∈[−1, 1] −1≤ −1≤1 z ∈[n, f ] z'∈[0, 1] z n r −l 0 ≤ − ≤1 f − n f − n l ≤ x ≤ r 2x − 2l − r + l −1≤ ≤1 n ≤ z ≤ f r −l z n 0 ≤ x − l ≤ r − l ∴ z'= − f − n f − n 2x r + l 0 ≤ z − n ≤ f − n x − l −1≤ − ≤1 0 ≤ ≤1 r −l r −l r − l z − n 2x r + l 2y t + b 0 ≤ ≤1 2(x − l) ∴ x'= − y'= − f − n 0 ≤ ≤ 2 r − l r − l t −b t −b r − l • OpenGL transform for z looks more like x & y transforms See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 9 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 10 Ortho projection matrix (LHS) Ortho proj (LHS) Microsoft style • Rearranging to look like Microsoft documentation • Put it all together # & 2 $ 2 ' % 0 0 0 ( 0 0 0 % r − l ( & r − l ) % ( & 2 ) 2 & 0 0 0) % 0 0 0 ( t − b t − b [x',y',z',1] = [x,y,z,1]⋅ P where P = & ) [x', y', z',1] = [x, y, z,1]⋅ P where P = % ( 1 % 1 ( & 0 0 0) 0 0 0 & f − n ) % f − n ( % ( & l + r t + b n ) & 1) % r + l t + b n ( % l − r b − t n − f ( % − − − 1 ( $ r − l t − b f − n ' • In Direct3D: D3DXMatrixOrthoOffCenterLH(*o,l,r,b,t,n,f) • LHS is default system in Direct3D € See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 11 http://msdn.microsoft.com/en-us/library/bb205347(VS.85).aspx 12 3 5/21/14 Orthographic projection (RHS) Simpler ortho projection (LHS) • Math the same, but z clipping plane inputs in most • In most orthographic projection setups API calls are negated so $ 2 ' – Z-axis passes through the center of your view volume 0 0 0 z input parameters & r − l ) – Field of view (FOV) extends equally far & 2 ) • To the left as to the right (i.e., r = -l) are positive & 0 0 0) $ 2 ' t − b • To the top as to the below (i.e., t=-b) & 0 0 0) [x',y',z',1] = [x,y,z,1]⋅ P where P = & 1 ) w & 0 0 0) & 2 ) & 0 0 0) & n − f ) h & l + r t + b n ) [x',y',z',1] = [x,y,z,1]⋅ P where P = & 1 ) & 1) & 0 0 0) % l − r b − t n − f ( & f − n ) • In Direct3D: D3DXMatrixOrthoOffCenterRH(*o,l,r,b,t,n,f) & n ) & 0 0 1) • In XNA: Matrix.CreateOrthographicOffCenter(l,r,b,t,n,f) % n − f ( € • In OpenGL: glOrtho(l,r,b,t,n,f) (matrix is different) • In Direct3D: D3DXMatrixOrthoLH(*o,w,h,n,f) • OpenGL maps z to [-1,1] & uses column vectors http://msdn.microsoft.com/en-us/library/bb205348(VS.85).aspx € http://www.cs.utk.edu/~vose/c-stuff/opengl/glOrtho.html 13 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 14 Simpler ortho projection (RHS) Perspective projection • Math the same, but z clipping plane inputs in most API calls are negated so z input parameters are View plane positive $ 2 ' 0 0 0 & w ) & 2 ) & 0 0 0) Viewer’s h position [x',y',z',1] = [x,y,z,1]⋅ P where P = & 1 ) & 0 0 0) & n − f ) & n ) & 0 0 1) % n − f ( • In Direct3D: D3DXMatrixOrthoRH(*o,w,h,n,f) • In XNA: Matrix.CreateOrthographic(w,h,n,f) € See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm 15 16 4 5/21/14 Viewing frustum Viewing frustum with furniture Far plane Far plane Near plane Near plane Think about looking through a window in a dark room Viewer’s Viewer’s position position 17 18 Perspective projection Perspective projection mapping • Given a point (x,y,z) within the view frustum, project it onto (r, t, f) the near plane z=n – x ∈ [l, r] and y ∈ [b, t] • We will map x from [l,r] to [-1,1] and y from [b,t] to [-1,1] y (r, t, f) (1, 1, 1) y (1, 1, 1) (l, b, n) z (l, b, n) View Frustum z x (a truncated pyramid) x View Frustum (-1, -1, 0) (-1, -1, 0) Canonical view volume (D3D & XNA) Canonical view volume 19 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 20 5 5/21/14 Perspective projection math (1) Perspective projection math (2) (x,y,z) (x,y,z) Far Plane Far Plane (x’,y’,n) (x’,y’,n) y y y y Near Plane Near Plane y’ y’ (0,0,0) x (0,0,0) x (0,0,n) x’ (0,0,n) x’ x n z x n z z z f f To calculate new coordinates of x’ and y’ 2 nx r + l 2n ny t + b x' n nx x' = ⋅ − y' = ⋅ − = ⇒ x'= r − l z r − l t − b z t − b x z z Next apply our orthographic projection formulas 2n r + l 2n t + b y' y yx' y nx ny x' ⋅ z = ⋅ x − ⋅ z y' ⋅ z = ⋅ y − ⋅ z = ⇒ y'= = ⋅ = r l r l t b t b x' x x x z z € − − − − Now let’s tackle the z’ component See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 21 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 22 Perspective projection math (3) Perspective projection math (4) 2n r + l z'⋅z = p⋅ z + q where p and q are constants x' ⋅ z = ⋅ x − ⋅ z r − l r − l 0 = p ⋅n + q f fn ⇒ ∴ p = and q = − 2n t + b f p f q f n f n y' ⋅ z = ⋅ y − ⋅ z = ⋅ + − − t − b t − b z'⋅z = p⋅ z + q where p and q are constants f fn z'⋅z = ⋅ z − f − n f − n • We know z (depth) transformation has nothing to € do with x and y € • We know (boxed equations above) – z’ = 0 when z=n (near plane) – z’ = 1 when z=f (far plane) See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 23 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 24 6 5/21/14 Perspective projection math (5) Simpler perspective projection 2n r + l • Similar to orthographic projection, if l=-r and t=-b, we can x' ⋅ z = ⋅ x − ⋅ z simplify to r − l r − l 2n t + b # & y' ⋅ z = ⋅ y − ⋅ z 2n t b t b % 0 0 0 ( − − % w ( f fn % 2n ( z'⋅z = ⋅ z − 0 0 0 f − n f − n ⎡ 2n ⎤ % ( ⎢ 0 0 0⎥ % h ( w' z z r − l [x'z, y'z, z'z, w'z] = [x, y, z,1]⋅ P where P = ⋅ = ⎢ 2n ⎥ % f ( 0 0 0 0 0 1 ⎢ ⎥ % f − n ( ⎢ t − b ⎥ % ( [x' z, y' z, z' z, w' z] = [x, y, z,1]⋅ P where P = r + l t + b f ⎢− − 1⎥ % fn ( ⎢ r l t b f n ⎥ 0 0 − 0 € − − − % f − n ( ⎢ fn ⎥ $ ' ⎢ 0 0 − 0⎥ ⎢ f − n ⎥ ⎣ ⎦ • In any case, we will have to divide by z to obtain [x’,y’, z’, w’] – Implemented by dividing by the fourth (w'z) coordinate See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 25 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 26 Define viewing frustum Reparameterized matrix ⎡2n ⎤ +y ⎢ 0 0 0⎥ w +y Width ⎢ 2n ⎥ Far (f) ⎢ 0 0 0⎥ ⎢ h ⎥ P = f ⎢ 0 0 1⎥ h/2 Near (n) ⎢ ⎥ +z f − n a n ⎢ fn ⎥ ⎢ 0 0 − 0⎥ h/2 fov/2 ⎣⎢ f − n ⎦⎥ +z a 2n cot( ) = Viewer’s 2 h Height position w Need to replace w and h with FOV and r = aspect ratio Parameters: h 2n 2n 2n 1 a FOV: Field of View = = = ⋅cot( ) Aspect ratio = Width/Height w rh 2n r 2 r ⋅ Near z a Far z XNA: Matrix.CreatePerspectiveFieldOfView cot( ) 2 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 27 See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm 28 7 5/21/14 Final matrix of perspective proj (LHS) Final matrix of perspective proj (RHS) $ 1 a ' ⎡1 a ⎤ ⋅cot( ) 0 0 0 ⋅cot( ) 0 0 0 & r 2 ) ⎢r 2 ⎥ & a ) ⎢ ⎥ & 0 cot( ) 0 0 ) a 2 ⎢ 0 cot( ) 0 0⎥ [x'⋅z,y'⋅z,z'⋅z,w'⋅z] = [x,y,z,1]⋅ P where P = & ) ⎢ 2 ⎥ f [x'⋅z, y'⋅z, z'⋅z, w'⋅z] = [x, y, z,1]⋅ P where P = f & 0 0 −1) ⎢ 0 0 1⎥ & n − f ) ⎢ f − n ⎥ & fn ) ⎢ fn ⎥ & 0 0 0 ) ⎢ 0 0 − 0⎥ % n − f ( ⎣⎢ f − n ⎦⎥ width a : Field of View (FOV) r : aspect ratio = n : near plan f : far plane width height a : Field of View (FOV) r : aspect ratio = n : near plan f : far plane height • In Direct3D: D3DXMatrixPerspectiveFovRH(*o,a,r,n,f) • In Direct3D: D3DXMatrixPerspectiveFovLH(*o,a,r,n,f) • In XNA: Matrix.CreatePerspectiveFieldOfView(a,r,n,f) € • LHS is default system in Direct3D • In OpenGL: gluPerspective(a,r,n,f) http://msdn.microsoft.com/en-us/library/bb205350(VS.85).aspx 29 http://msdn.microsoft.com/en-us/library/bb205351(VS.85).aspx 30 Unity’s coordinate systems Custom projections in Unity • Viewport space: • From Camera.projectionMatrix documentation: – (0,0) is bottom-left – (1,1) is top-right “Use a custom projection only if you really need a non- standard projection.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages13 Page
-
File Size-