3D To 2D Projection

Transcription

3D to 2D ProjectionProf. Aaron Lanterman(Based on slides by Prof. Hsien-Hsin Sean Lee)School of Electrical and Computer EngineeringGeorgia Institute of Technology

Specifying the view transformation Most commonly parameterized by:– Position of camera– Position of point to look at– Vector indicating “up” direction of camera In Direct3D: D3DXMatrixLookAtLH!– D3D uses a LHS, but also have D3DXMatrixLookAtRH In XNA: Matrix.CreateLookAt (RHS) In OpenGL: gluLookAt (RHS) Can also build a rotation translation matrix as if thecamera was an object in scene, then take theinverse of that (VS.85).aspx2

Projection from 3D space Projection transforms 3D geometry into a form that can berendered as a 2D imageyzxMuch discussion adapted from Joe Farrellʼs article e.php/c10123 1/ )3

Canonical view volume Projection transforms your geometry into a canonical viewvolume in normalized device coordinates Only X- and Y-coordinates will be mapped onto the screen Z will be almost useless, but used for depth testy(1, 1, 1)z(0, 0, 0)x(-1, -1, 0)Canonical view volume (LHS)(-1, -1, 0) to (1,1,1) used by Direct3D4

Strange “conventions”yy(1, 1, 1)(1, 1, 1)z(0, 0, 0)(-1, -1, 0)zx(0, 0, 0)x(-1, -1,-1)Canonical view volume (LHS)(-1, -1, 0) to (1,1,1) used by D3D/XNACanonical view volume (LHS)(-1, -1, 1) to (1,1,1) used by OpenGL(remember eye-space coordinates inXNA are in a RHS!!!)(remember eye-space coordinates inOpenGL are in a RHS!!!)5

Orthographic (or parallel) projectionView planeViewerʼsposition Project from 3D space to the viewer’s 2D space6

Style of orthographic projection Same size in 2D and 3DNo sense of distanceParallel lines remain parallelGood for tile-based games where camera is in fixed location(e.g., Mahjong or 3D Tetris)See .php/c10123 2/Deriving-Projection-Matrices.htm7

Orthographic projection(r, t, f)y(1, 1, 1)z(l, b, n)xView Volume(an axis-aligned box)(-1, -1, 0)Canonical view volume (D3D & XNA)8

Orthographic projection math (1) Derive x’ and yʼx [l , r ]x' [ 1, 1]l x r0 x l r lx l0 1r l2( x l )0 2r l2( x l ) 1 1 1r l2 x 2l r l 1 1r l 1 2x r l 1r l r l2x r l x' r l r l2y t by' t b t bSee .php/c10123 2/Deriving-Projection-Matrices.htm9

Orthographic projection math (2) Derive zʼ (slightly different for the range in D3D)z [n, f ]z ' [0, 1]n z f0 z n f nzn0 1f n f nzn z' f n f nz n0 1f n OpenGL transform for z looks more like x & y transformsSee .php/c10123 2/Deriving-Projection-Matrices.htm 10

Ortho projection matrix (LHS) Put all together 2& r#l&& 0[x', y',z',1] [x, y,z,1]" P where P && 0&& r l&# r # l%002t#b00t b#t#b1f #nn#f #n'0))0))0)))1)(!See .php/c10123 2/Deriving-Projection-Matrices.htm 11

Ortho proj (LHS) Microsoft style Rearranging to look like Microsoft documentation 2&r # l&& 0[x', y',z',1] [x, y,z,1]" P where P && 0&&l r&l # r%002t#b00t bb# t1f #nnn# f'0))0))0)))1)( In Direct3D: D3DXMatrixOrthoOffCenterLH(*o,l,r,b,t,n,f) LHS is default system in b205347(VS.85).aspx12

Orthographic projection (RHS) Math the same, but z clipping plane inputs in most 2'API calls are negated so000)&r#lz input parameters&)2are positive00)& 0[x', y',z',1] [x, y,z,1]" P where P && 0&&l r&l # r%t#b0t bb# t1n# fnn# f)0)))1)( In Direct3D: D3DXMatrixOrthoOffCenterRH(*o,l,r,b,t,n,f) In XNA: Matrix.CreateOrthographicOffCenter(l,r,b,t,n,f)! In OpenGL: glOrtho(l,r,b,t,n,f) (matrix is different) OpenGL maps z to [-1,1] & uses column 05348(VS.85).aspxhttp://www.cs.utk.edu/ vose/c-stuff/opengl/glOrtho.html13

Simpler ortho projection (LHS) In most orthographic projection setups– Z-axis passes through the center of your view volume– Field of view (FOV) extends equally far To the left as to the right (i.e., r -l) 2'000) To the top as to the below (i.e., t -b) &w&)200)&0h&)[x', y',z',1] [x, y,z,1]" P where P 1&0 00)f #n&)&)n& 0 0 n # f 1)%( In Direct3D: D3DXMatrixOrthoLH(*o,w,h,n,f)!See .php/c10123 2/Deriving-Projection-Matrices.htm 14

Simpler ortho projection (RHS) Math the same, but z clipping plane inputs in mostAPI calls are negated so z input parameters arepositive 2&w&&0[x', y',z',1] [x, y,z,1]" P where P &&0&&&0%002h0001n# fnn# f'0))0))0)))1)( In Direct3D: D3DXMatrixOrthoRH(*o,w,h,n,f) In XNA: Matrix.CreateOrthographic(w,h,n,f)! See .php/c10123 2/Deriving-Projection-Matrices.htm15

Perspective projectionView planeViewerʼsposition The farther the object is, the smaller it appears Some photo editing software allows you to perform“Perspective Correction”16

Viewing frustumFar planeNear planeViewer’spositionThink about looking through a window in a dark room17

Viewing frustum with furnitureFar planeNear planeViewerʼsposition18

Perspective projection(r, t, f)y(1, 1, 1)(l, b, n)zxView Frustum(a truncated pyramid)(-1, -1, 0)Canonical view volume (D3D & XNA)19

Perspective projection mapping Given a point (x,y,z) within the view frustum, project it ontothe 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](r, t, f)y(1, 1, 1)(l, b, n)zxView Frustum(-1, -1, 0)Canonical view volumeSee .php/c10123 3/Deriving-Projection-Matrices.htm20

Perspective projection math (1)(x,y,z)(xʼ,yʼ,n)yyyʼ(0,0,0)x(0,0,n) xʼnxzzfTo calculate new coordinates of xʼ and yʼx' nnx x' x zzy' yyx ' y nx ny y' x' xxx zzNext apply our orthographicprojection formulasSee .php/c10123 3/Deriving-Projection-Matrices.htm21

Perspective projection math (2)(x,y,z)(xʼ,yʼ,n)yyyʼ(0,0,0)(0,0,n) xʼxnxzzf2 nx r lx' # "r"l z r"lx' z !2nr l x zr lr l2n ny t by' # "t"b z t"by' z 2nt b y zt bt bNow let’s tackle the z’ componentSee .php/c10123 3/Deriving-Projection-Matrices.htm22

Perspective projection math (3)2nr lx' z x zr lr l2nt by' z y zt bt bz'"z p " z q!where p and q are constants We know z (depth) transformation has nothing todo with x and ySee .php/c10123 3/Deriving-Projection-Matrices.htm23

Perspective projection math (4)z ' z p z q where p and q are constants0 p n qf p f q ffn p and q f nf nffnz'"z "z#f #nf #n! We know (boxed equations above)– zʼ 0 when z n (near plane)– zʼ 1 when z f (far plane)See .php/c10123 3/Deriving-Projection-Matrices.htm24

!Perspective projection math (5)2nr lx' z x zr lr l2nt by' z y zt bt bffnz'"z "z#f #nf #n 2n r lw'"z z 0[ x' z , y ' z , z ' z , w' z ] [ x, y, z ,1] P where P r l r l 0 02nt bt b t b0See .php/c10123 3/Deriving-Projection-Matrices.htm00ff nfn f n 0 0 1 0 25

Simpler perspective projection Similar to orthographic projection, if l -r and t -b, we cansimplify to 2n w 0[ x' z , y ' z , z ' z , w' z ] [ x, y, z ,1] P where P 0 0 002nh000ff nfn f n 0 0 1 0 In any case, we will have to divide by z to obtain [x’,y’, z’, w’]– Implemented by dividing by the fourth (w'z) coordinateSee .php/c10123 3/Deriving-Projection-Matrices.htm26

Define viewing frustum yFar (f)Near (n)fov/2 zParameters:FOV: Field of ViewAspect ratio Width/HeightNear zXNA: Matrix.CreatePerspectiveFieldOfViewFar zSee .php/c10123 osition27

Reparameterized matrix 2n w 0P 0 0 002nh000ff nfn f n 0 0 1 0 yh/2an zh/2a2ncot( ) 2hr whNeed to replace w and h with FOV andaspect ratio2n 2n2n1a cot( )2nw rh r r2acot( )2See .php/c10123 3/Deriving-Projection-Matrices.htm28

Final matrix of perspective proj (LHS)a 100 r cot( 2 ) a 0cot( )02[ x' z , y ' z , z ' z , w' z ] [ x, y, z ,1] P where P f 00 f n fn 00 f n a : Field of View (FOV)r : aspect ratio widthheightn : near plan 0 0 1 0 f : far plane In Direct3D: D3DXMatrixPerspectiveFovLH(*o,a,r,n,f) LHS is default system in 205350(VS.85).aspx29

Final matrix of perspective proj (RHS)a 1"cot()0&r2&a0cot( )&2[x'"z, y'"z,z'"z,w'"z] [x, y,z,1]" P where P &&00&&00&%a : Field of View (FOV)r : aspect ratio widthheight00fn# ffnn# fn : near plan'0))0))#1)))0)(f : far plane In Direct3D: D3DXMatrixPerspectiveFovRH(*o,a,r,n,f) In XNA: Matrix.CreatePerspectiveFieldOfView(a,r,n,f) In OpenGL: n-us/library/bb205351(VS.85).aspx30

Viewport transformation The actual 2D projection to the viewer Copy to your back buffer (frame buffer) Can be programmed, scaled, .31

Backface culling Determine “facing direction” Triangle order matters How to compute a normal vector for 2 given vectors?– Using Cross product of 2 given vectors2 VectorsV 1 (b1 a1)i (b 2 a 2) j (b3 a3)k(a1, a2, a3)V 2 (c1 a1)i (c 2 a 2) j (c3 a3)kCross product(c1, c2, c3)(b1, b2, b3)V 1 x1i x2 j x3kV 2 y1i y2 j y3kV 1 V 2 ( x2 y3 x3 y2 )i ( x3 y1 x1 y3 ) j ( x1 y2 x2 y1 )k32

Compute the surface normal for a triangle Clockwise normals, LHSy(3, 3, 0)zv1 3i 3j 0k(0, 0, 0)v2x(4, 0, 0)(3, 3, 0)v2 4i 0j 0kzv1 4i 0j 0k(0, 0, 0)v1(4, 0, 0)xv2 3i 3j 0k33

Backface culling method (1) Check if the normal is facing the camera How to determine that?– Use Dot ProductSurface vectorsEye vectors34

Backface culling method (2) Check if the normal is facing the camera How to determine that?– Use Dot ProductSurface vectorsEye vectors35

Dot product method (1)A B A B cosθθ BA B 0 π2 θ π236

Dot product method (2)A B A B cosθθ BA B 0 π2 θ π237

Dot product method (3)A B 0 π2 θ π238

Caution!A B 0 π2 θ π239

World TransformView TransformBackface CullingVertex LightingProjection TransformClippingPerspective DivideViewport TransformRasterizationWorld TransformBackface CullingView TransformVertex LightingProjection TransformClippingPerspective DivideViewport TransformRasterizationWhen to perform backface culling?Make sure camera is in correct coordinates!40

World TransformBackface CullingView TransformVertex LightingProjection TransformClippingPerspective DivideViewport TransformRasterizationBackface CullingWorld TransformView TransformVertex LightingProjection TransformClippingPerspective DivideViewport TransformRasterizationHow about before you even start?Transform camera vectors into object spaces41

RasterizationRasterizationBackface CullingPerspective DivideClippingProjection TransformVertex LightingView TransformWorld TransformViewport Transform Now you can just check “winding order” in 2DBackface CullingViewport TransformPerspective DivideClippingProjection TransformVertex LightingView TransformWorld TransformOr how about at the very end? As “officially” done by OpenGL42

3D clipping Test 6 planes if a triangle is inside, outside, or partially inside the viewfrustum Clipping creates new triangles (triangulation)– Interpolate new vertices info43

Appendix44

Clipping against a plane Test each vertex of a triangle– Outside– Inside– Partially inside Incurred computation overhead Save unnecessary computation (andbandwidth) later Need to know how to determine a plane Need to know how to determine a vertex isinside or outside a plane45

Specifying a planeV (5, 6, 7)Plane equation5*(x-1) 6*(y-2) 7*(z-3) 0K (x, y, z)P (1, 2, 3) You need two things to specify a plane– A point on the plane (p0, p1, p2)– A vector (normal) perpendicular to the plane (a, b, c)– Plane à a*(x – p0) b*(y – p1) c*(z - p2) 046

Distance calculation from a plane (1) NormalυRdd θPd R P cosθ R P υ ( R P) υ ( R P)υ R P υ Given a point R, calculate the distance– Distance 0 inside the plane– Distance 0 on the plane– Distance 0 outside the plane47

Distance calculation from a plane (2) υP 180-θθdRd R P cos(180 θ )48

Triangulation using interpolation(x, y, z)(a2, b2, c2)d2d1(a1, b1, c1)d1s d1 d 2x a1 s (a 2 a1)y b1 s (b 2 b1)z c1 s (c 2 c1)49

Georgia Institute of Technology . 2 Specifying the view transformation Most commonly parameterized by: - Position of camera - Position of point to look at - Vector indicating "up" direction of camera In Direct3D: D3DXMatrixLookAtLH! - D3D uses a LHS, but also have D3DXMatrixLookAtRH In XNA: Matrix.CreateLookAt (RHS) In OpenGL: gluLookAt (RHS) Can also build a .