Projective Transformations • planar geometric projections • planar: onto a plane • geometric: using straight lines • projections: 3D -> 2D • aka projective mappings • counterexamples? 38
Projective Transformations • properties • lines mapped to lines and triangles to triangles • parallel lines do NOT remain parallel • e.g. rails vanishing at infinity • affine combinations are NOT preserved • e.g. center of a line does not map to center of projected line (perspective foreshortening) 39
Perspective Projection • project all geometry • through common center of projection (eye point) • onto an image plane x y -z z z x x 40
Perspective Projection projection plane center of projection (eye point) how tall should this bunny be? 41
Basic Perspective Projection similar triangles P(x,y,z) y P(x ’ ,y ’ ,z ’ ) z z ’ =d z ' = d but • nonuniform foreshortening • not affine 42
Perspective Projection • desired result for a point [x, y, z, 1] T projected onto the view plane: x ' x y ' y , = = d z d z x d x y d y � � x ' , y ' , z ' d = = = = = z z d z z d • what could a matrix look like to do this? 43
Simple Perspective Projection Matrix x � � � � z / d � � y � � � � z / d � � � � d � � � � 44
Simple Perspective Projection Matrix x x � � � � � � � � y z / d is homogenized version of � � � � z � � y � � � � � � z / d where w = z/d z / d � � � � � � d � � � � 45
Simple Perspective Projection Matrix x x � � � � � � � � y z / d is homogenized version of � � � � z � � y � � � � � � z / d where w = z/d z / d � � � � � � d � � x 1 0 0 0 x � � � � � � � � � � � � � � y 0 1 0 0 y � � � � � � = z 0 0 1 0 z � � � � � � � � � � � � z / d 0 0 1 d 0 1 � � � � � � 46
Perspective Projection • expressible with 4x4 homogeneous matrix • use previously untouched bottom row • perspective projection is irreversible • many 3D points can be mapped to same (x, y, d) on the projection plane • no way to retrieve the unique z values 47
Moving COP to Infinity • as COP moves away, lines approach parallel • when COP at infinity, orthographic view 48
Orthographic Camera Projection • camera’s back plane x x � � � � parallel to lens p � � � � y y = • infinite focal length � p � � � z 0 � � � � • no perspective � � � � p convergence x 1 0 0 0 x � � � � � � p � � � � � � • just throw away z values y 0 1 0 0 y p � � � � � � = z 0 0 0 0 z � � � � � � p � � � � � � 1 0 0 0 1 1 � � � � � � 49
Perspective to Orthographic • transformation of space • center of projection moves to infinity • view volume transformed • from frustum (truncated pyramid) to parallelepiped (box) x x Frustum Parallelepiped -z -z 50
View Volumes • specifies field-of-view, used for clipping • restricts domain of z stored for visibility test orthographic view volume perspective view volume y=top y=top x=left x=left y y z x=right z VCS z=-near x VCS y=bottom z=-far z=-far x y=bottom x=right z=-near 51
Canonical View Volumes • standardized viewing volume representation perspective orthographic orthogonal parallel x or y x or y = +/- z back x or y plane back 1 front plane front -z plane -z -1 plane -1 52
Why Canonical View Volumes? • permits standardization • clipping • easier to determine if an arbitrary point is enclosed in volume with canonical view volume vs. clipping to six arbitrary planes • rendering • projection and rasterization algorithms can be reused 53
Normalized Device Coordinates • convention • viewing frustum mapped to specific parallelepiped • Normalized Device Coordinates (NDC) • same as clipping coords • only objects inside the parallelepiped get rendered • which parallelepiped? • depends on rendering system 54
Normalized Device Coordinates left/right x =+/- 1, top/bottom y =+/- 1, near/far z =+/- 1 NDC Camera coordinates x x x=1 right Frustum -z z left x= -1 z=1 z= -1 z=-n z=-f 55
Understanding Z • z axis flip changes coord system handedness • RHS before projection (eye/view coords) • LHS after projection (clip, norm device coords) VCS NDCS y=top y (1,1,1) x=left y z (-1,-1,-1) z x x=right x z=-far y=bottom z=-near 56
Understanding Z near, far always positive in OpenGL calls glOrtho(left,right,bot,top,near,far); glFrustum(left,right,bot,top,near,far); glPerspective(fovy,aspect,near,far); orthographic view volume perspective view volume y=top y=top x=left x=left y y z x=right VCS z=-near x VCS y=bottom z=-far z=-far x y=bottom x=right z=-near 57
Understanding Z • why near and far plane? • near plane: • avoid singularity (division by zero, or very small numbers) • far plane: • store depth in fixed-point representation (integer), thus have to have fixed range of values (0…1) • avoid/reduce numerical precision artifacts for distant objects 58
Orthographic Derivation • scale, translate, reflect for new coord sys VCS NDCS y=top y (1,1,1) x=left y z (-1,-1,-1) z x x=right x z=-far y=bottom z=-near 59
Orthographic Derivation • scale, translate, reflect for new coord sys y top y ' 1 = � = y ' a y b = � + y bot y ' 1 = � = � VCS NDCS y=top y (1,1,1) x=left y z (-1,-1,-1) z x x=right x z=-far y=bottom z=-near 60
Orthographic Derivation • scale, translate, reflect for new coord sys y top y ' 1 1 a top b = � = = � + y ' a y b = � + y bot y ' 1 1 a bot b = � = � � = � + 2 1 top b b 1 a top , b 1 a bot = + = � � = � � � top bot � 1 a top 1 a bot � � = � � � 2 top � b 1 = � 1 ( 1 ) a bot ( a top ) top bot � � = � � � � � � ( top bot ) 2 top 2 a ( bot top ) � � � = � + b = top bot � 2 a = top bot � � top bot � b = top bot � 61
Orthographic Derivation • scale, translate, reflect for new coord sys y top y ' 1 = � = y ' a y b = � + y bot y ' 1 = � = � 2 VCS a = top bot � y=top x=left top bot y + b = � z top bot x=right � x z=-far y=bottom z=-near same idea for right/left, far/near 62
Orthographic Derivation • scale, translate, reflect for new coord sys 2 right left + � � 0 0 � � � right left right left � � � � 2 top bot � � + 0 0 � � � top bot top bot � � P ' P = � � � 2 far near � � + 0 0 � � � far near far near � � � � � � 0 0 0 1 � � � � 63
Orthographic Derivation • scale, translate, reflect for new coord sys 2 right left + � � 0 0 � � � right left right left � � � � 2 top bot � � + 0 0 � � � top bot top bot � � P ' P = � � � 2 far near � � + 0 0 � � � far near far near � � � � � � 0 0 0 1 � � � � 64
Orthographic Derivation • scale, translate, reflect for new coord sys 2 right left + � � 0 0 � � � right left right left � � � � 2 top bot � � + 0 0 � � � top bot top bot � � P ' P = � � � 2 far near � � + 0 0 � � � far near far near � � � � � � 0 0 0 1 � � � � 65
Orthographic Derivation • scale, translate, reflect for new coord sys 2 right left + � � 0 0 � � � right left right left � � � � 2 top bot � � + 0 0 � � � top bot top bot � � P ' P = � � � 2 far near � � + 0 0 � � � far near far near � � � � � � 0 0 0 1 � � � � 66
Orthographic OpenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(left,right,bot,top,near,far); 67
Demo • Brown applets: viewing techniques • parallel/orthographic cameras • projection cameras • http://www.cs.brown.edu/exploratories/freeSoftware/ catalogs/viewing_techniques.html 68
Projections II 69
Asymmetric Frusta • our formulation allows asymmetry • why bother? x x right right Frustum Frustum -z -z left left z=-n z=-f 70
Asymmetric Frusta • our formulation allows asymmetry • why bother? binocular stereo • view vector not perpendicular to view plane Left Eye Right Eye 71
Simpler Formulation • left, right, bottom, top, near, far • nonintuitive • often overkill • look through window center • symmetric frustum • constraints • left = -right, bottom = -top 72
Field-of-View Formulation • FOV in one direction + aspect ratio (w/h) • determines FOV in other direction • also set near, far (reasonably intuitive) x w h fovx/2 Frustum -z α fovy/2 z=-n z=-f 73
Perspective OpenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(left,right,bot,top,near,far); or glPerspective(fovy,aspect,near,far); 74
Demo: Frustum vs. FOV • Nate Robins tutorial (take 2): • http://www.xmission.com/~nate/tutors.html 75
Projective Rendering Pipeline object world viewing O2W W2V V2C VCS WCS OCS projection modeling viewing transformation transformation transformation clipping C2N CCS OCS - object/model coordinate system perspective WCS - world coordinate system normalized divide device VCS - viewing/camera/eye coordinate N2D system NDCS viewport CCS - clipping coordinate system transformation NDCS - normalized device coordinate device system DCS DCS - device/display/screen coordinate system 76
Projection Warp • warp perspective view volume to orthogonal view volume • render all scenes with orthographic projection! • aka perspective warp x x z=d z= α z=d z=0 Z Z 77
Perspective Warp • perspective viewing frustum transformed to cube • orthographic rendering of cube produces same image as perspective rendering of original frustum 78
Predistortion 79
Projective Rendering Pipeline object world viewing O2W W2V V2C VCS WCS OCS projection modeling viewing transformation transformation transformation clipping C2N CCS OCS - object/model coordinate system perspective WCS - world coordinate system normalized divide device VCS - viewing/camera/eye coordinate N2D system NDCS viewport CCS - clipping coordinate system transformation NDCS - normalized device coordinate device system DCS DCS - device/display/screen coordinate system 80
Separate Warp From Homogenization normalized clipping viewing device V2C C2N CCS VCS NDCS projection perspective transformation division alter w / w • warp requires only standard matrix multiply • distort such that orthographic projection of distorted objects is desired persp projection • w is changed • clip after warp, before divide • division by w: homogenization 81
Perspective Divide Example • specific example • assume image plane at z = -1 • a point [ x,y,z,1 ] T projects to [- x/z,-y/z,-z/z,1 ] T ≡ [ x,y,z,-z ] T x � � x � � � � y � � y � � � � z � � z � � � � 1 � � � � � z � � -z 82
Perspective Divide Example x 1 0 0 0 x x x / z � � � � � � � � � � � � � � � � � � � � � � � � � y 0 1 0 0 y y y / z � � � � � � � � � � � � � T = � = � � � z 0 0 1 0 z z 1 � � � � � � � � � � � � � � � � � � � � � � � � � 1 0 0 1 0 1 z 1 � � � � � � � � � � � � � � • after homogenizing, once again w=1 projection perspective transformation division alter w / w 83
Perspective Normalization • matrix formulation x x � � 1 0 0 0 � � � � x � � � � � � � � y z / d x � � 0 1 0 0 � � p � � � � � � y y � � d d ( z ) d � � � � � � � � � � y � � � � = � 0 0 � = � � p � � z / d � � z d d � � � d � � � � � � � � � � � � z � 2 � d � 1 � � � � � � � � � � p � � z 1 1 0 0 0 � � � � � � � � � � d z � � d � � � � � � d � � • warp and homogenization both preserve relative depth (z coordinate)
Demo • Brown applets: viewing techniques • parallel/orthographic cameras • projection cameras • http://www.cs.brown.edu/exploratories/freeSoftware/ catalogs/viewing_techniques.html 85
Perspective To NDCS Derivation VCS NDCS y=top x=left y (1,1,1) y z (-1,-1,-1) x z z=-near y=bottom z=-far x x=right 86
Perspective Derivation x ' 1 0 0 0 x � � � � � � � � � � � � simple example earlier: y ' 0 1 0 0 y � � � � � � = z ' 0 0 1 0 z � � � � � � � � � � � � w ' 0 0 1/ d 0 1 � � � � � � complete: shear, scale, projection-normalization x E 0 A 0 x � � � � � � � � � � � � � y 0 F B 0 y � � � � � � � = z 0 0 C D z � � � � � � � � � � � � � w 0 0 � 1 0 1 � � � � � � � 87
Perspective Derivation x ' 1 0 0 0 x � � � � � � � � � � � � y ' 0 1 0 0 y earlier: � � � � � � = z ' 0 0 1 0 z � � � � � � � � � � � � w ' 0 0 1/ d 0 1 � � � � � � complete: shear, scale, projection-normalization x E 0 A 0 x � � � � � � � � � � � � � y 0 F B 0 y � � � � � � � = z 0 0 C D z � � � � � � � � � � � � � w 0 0 � 1 0 1 � � � � � � � 88
Perspective Derivation x ' 1 0 0 0 x � � � � � � earlier: � � � � � � y ' 0 1 0 0 y � � � � � � = z ' 0 0 1 0 z � � � � � � � � � � � � w ' 0 0 1/ d 0 1 � � � � � � complete: shear, scale, projection-normalization x E 0 A 0 x � � � � � � � � � � � � � y 0 F B 0 y � � � � � � � = z 0 0 C D z � � � � � � � � � � � � � w 0 0 � 1 0 1 � � � � � � � 89
Recorrection: Perspective Derivation L/R sign error x = left � � x / w = � 1 � x ' E 0 A 0 x � � � � � � x ' = Ex + Az � � � � � � x = right � � x / w = 1 � y ' 0 F B 0 y y ' = Fy + Bz � � � � � � = y = top � � y / w = 1 � z ' 0 0 C D z � � � � � � z ' = Cz + D � � � � � � y = bottom � � y / w = � 1 � w ' 0 0 � 1 0 1 w ' = � z � � � � � � z = � near � � z / w = � 1 � z = � far � � z / w = 1 � z axis flip! y ' = Fy + Bz , y ' = Fy + Bz , 1 = Fy + Bz , 1 = Fy + Bz , w ' w ' w ' � z 1 = F y + B z , 1 = F y top � B , 1 = F � B , � z � z � z � ( � near ) 1 = F top � B near 90
Perspective Derivation • similarly for other 5 planes • 6 planes, 6 unknowns 2 n r + l � � 0 0 � � r � l r � l � � 2 n t + b 0 0 � � t � b t � b � � � ( f + n ) � 2 fn � � 0 0 f � n f � n � � 0 0 � 1 0 � � � � 91
Projective Rendering Pipeline object world viewing O2W W2V V2C VCS WCS OCS projection modeling viewing transformation transformation transformation clipping C2N CCS OCS - object/model coordinate system perspective WCS - world coordinate system normalized divide device VCS - viewing/camera/eye coordinate N2D system NDCS viewport CCS - clipping coordinate system transformation NDCS - normalized device coordinate device system DCS DCS - device/display/screen coordinate system 92
NDC to Device Transformation • map from NDC to pixel coordinates on display • NDC range is x = -1...1, y = -1...1, z = -1...1 • typical display range: x = 0...500, y = 0...300 • maximum is size of actual screen • z range max and default is (0, 1), use later for visibility glViewport(0,0,w,h); glDepthRange(0,1); // depth = 1 by default 0 500 x y 0 y -1 viewport x NDC 1 1 -1 93 300
Origin Location • yet more (possibly confusing) conventions • OpenGL origin: lower left • most window systems origin: upper left • then must reflect in y • when interpreting mouse position, have to flip your y coordinates 0 500 x y 0 y -1 viewport x NDC 1 1 -1 300 94
N2D Transformation • general formulation • reflect in y for upper vs. lower left origin • scale by width, height, depth • translate by width/2, height/2, depth/2 • FCG includes additional translation for pixel centers at (.5, .5) instead of (0,0) 0 500 x y 0 y height 1 viewport x NDC -1 width 1 95 -1 300
N2D Transformation width � 1 width width ( x N + 1) � 1 � � � � � � 1 0 0 0 0 0 � � � � � � x D 1 0 0 0 x N � � � � � � 2 2 2 2 � � � � � � height � 1 height height ( � y N + 1) � 1 � � � � � � y D 0 � 1 0 0 y N 0 1 0 0 0 0 � � � � � � � � � � � � = = 2 2 2 2 � � � � � � z D 0 0 1 0 z N � � � � � � depth depth depth ( z N + 1) � � � � � � 0 0 1 0 0 0 � � � � � � 1 0 0 0 1 1 2 2 2 � � � � � � � � � � � � 0 0 0 1 0 0 0 1 1 � � � � � � � � � � � � Display z range is 0 to 1. glDepthRange(n,f) can constrain reminder: further, but depth = 1 is both NDC z range is -1 to 1 max and default 0 500 x y 0 y height 1 viewport x NDC -1 width 1 96 -1 300
Device vs. Screen Coordinates • viewport/window location wrt actual display not available within OpenGL • usually don’t care • use relative information when handling mouse events, not absolute coordinates • could get actual display height/width, window offsets from OS • loose use of terms: device, display, window, screen... 0 1024 x 0 y 0 500 x y offset 0 y display x offset viewport viewport height display 300 768 97 display width
Projective Rendering Pipeline glVertex3f(x,y,z) object world viewing O2W W2V V2C alter w VCS WCS OCS glFrustum(...) projection modeling viewing transformation transformation transformation clipping glTranslatef(x,y,z) gluLookAt(...) C2N / w CCS glRotatef(a,x,y,z) perspective .... normalized division OCS - object coordinate system device glutInitWindowSize(w,h) N2D WCS - world coordinate system glViewport(x,y,a,b) NDCS viewport VCS - viewing coordinate system transformation CCS - clipping coordinate system device DCS NDCS - normalized device coordinate system DCS - device coordinate system 98
Coordinate Systems viewing (4-space, W=1) clipping (4-space parallelepiped, projection with COP moved matrix backwards to infinity normalized device divide by w (3-space parallelepiped) device scale & (3-space translate parallelipiped) framebuffer 99
Perspective Example view volume tracks in VCS: left = -1, right = 1 left x=-1, y=-1 bot = -1, top = 1 right x=1, y=-1 near = 1, far = 4 x=1 x=-1 1 ymax-1 z=-4 real midpoint -1 z=-1 1 -1 xmax-1 0 -1 0 x NDCS DCS (z not shown) (z not shown) z VCS top view 100
Recommend
More recommend