<<

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) 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. • Screen space coordinates: – z “is in world units from the camera” This property is used by Unity's water rendering to – (0,0) is bottom-left setup an matrix. – (Camera.pixelWidth,Camera.pixelHeight) is top-right Using custom projections requires good knowledge of transformation and projection matrices.”

31 http://msdn.microsoft.com/en-us/library/bb205351(VS.85).aspx 32

8 5/21/14

Viewport transformation Backface culling • Determine “facing direction” • Triangle order matters • How to compute a normal vector for 2 given vectors? – Using cross product of 2 given vectors

2 Vectors

V1 = (b1− a1)i + (b2 − a2)j + (b3− a3)k V 2 = (c1− a1)i + (c2 − a2)j + (c3− a3)k (a1, a2, a3)

Cross product

(c1, c2, c3) V1 = x1i + x2 j + x3k • The actual 2D projection to the viewer (b1, b2, b3) V 2 y i y j y k • Copy to your back buffer (frame buffer) = 1 + 2 + 3 V1×V 2 = (x y − x y )i + (x y − x y )j + (x y − x y )k • Can be programmed, scaled, ... 2 3 3 2 3 1 1 3 1 2 2 1

33 34

Compute the surface normal for a triangle Backface culling method (1) • Clockwise normals, LHS • Check if the normal is facing the camera y • How to determine that? – Use Dot Product (3, 3, 0) z v1 v1 = 3i+3j+0k Surface vectors (0, 0, 0) (4, 0, 0) x v2= 4i+0j+0k v2

(3, 3, 0) z Eye vectors v2 v1 = 4i+0j+0k (0, 0, 0) (4, 0, 0) x v2= 3i+3j+0k v1

35 36

9 5/21/14

Backface culling method (2) Dot product method (1)

• Check if the normal is facing the camera → • How to determine that? A A• B = A B cosθ – Use Dot Product θ → Surface vectors B π π A• B > 0 ⇒ − <θ < 2 2

Eye vectors

37 38

Dot product method (2) Dot product method (3)

→ A A• B = A B cosθ

θ → π π B π π A• B > 0 ⇒ − <θ < A• B > 0 ⇒ − <θ < 2 2 2 2

39 40

10 5/21/14

Caution! When to perform backface culling?

π π A• B > 0 ⇒ − <θ < Clipping

2 2 Rasterization Vertex Lighting Vertex View Transform Transform View World Transform Transform World Perspective Divide Backface Culling Viewport Transform Transform Viewport Projection Transform Transform Projection Clipping Rasterization Vertex Lighting Vertex View Transform Transform View World Transform Transform World Backface Culling Perspective Divide Viewport Transform Transform Viewport Projection Transform Transform Projection

Make sure camera is in correct coordinates!

41 42

How about before you even start? Or how about at the very end? Clipping Rasterization Clipping Vertex Lighting Vertex View Transform Transform View World Transform Transform World Backface Culling Rasterization Perspective Divide Viewport Transform Transform Viewport Projection Transform Transform Projection Vertex Lighting Vertex View Transform Transform View World Transform Transform World Backface Culling Perspective Divide Viewport Transform Transform Viewport Projection Transform Transform Projection • Now you can just check “winding order” in 2D Clipping Clipping Rasterization Vertex Lighting Vertex Rasterization View Transform Transform View World Transform Transform World Vertex Lighting Vertex Perspective Divide Backface Culling View Transform Transform View Viewport Transform Transform Viewport World Transform Transform World Backface Culling Projection Transform Transform Projection Perspective Divide Projection Transform Transform Projection Viewport Transform Transform Viewport Transform camera vectors into object spaces • As “officially” done by OpenGL

43 44

11 5/21/14

3D clipping

Appendix

• Test 6 planes if a triangle is inside, outside, or partially inside the view frustum • Clipping creates new triangles (triangulation) – Interpolate new vertices info

45 46

Clipping against a plane Specifying a plane • Test each vertex of a triangle V=(5, 6, 7) – Outside Plane equation

– Inside 5*(x-1)+6*(y-2)+7*(z-3)=0 – Partially inside K=(x, y, z) P=(1, 2, 3) • Incurred computation overhead • Save unnecessary computation (and bandwidth) later • You need two things to specify a plane • Need to know how to determine a plane – A point on the plane (p0, p1, p2) – A vector (normal) perpendicular to the plane (a, b, c) • Need to know how to determine a vertex is – Plane à a*(x – p0) + b*(y – p1) + c*(z - p2) = 0 inside or outside a plane

47 48

12 5/21/14

Distance calculation from a plane (1) Distance calculation from a plane (2)

→ → υ υ Normal R d d P 180-θ θ θ P d υ •(R − P) υ •(R − P) R d = | R − P | ⋅cosθ = | R − P | ⋅ = υ ⋅ R − P υ d = | R − P |⋅cos(180 −θ) • Given a point R, calculate the distance – Distance > 0 inside the plane – Distance = 0 on the plane – Distance < 0 outside the plane

49 50

Triangulation using interpolation

(a2, b2, c2) (x, y, z) d2 d1

(a1, b1, c1) d1 s = d1+ d2 x = a1+ s ⋅(a2 − a1) y = b1+ s ⋅(b2 − b1) z = c1+ s ⋅(c2 − c1)

51

13