University of British Columbia View Volumes Canonical View Volumes Why Canonical View Volumes? CPSC 314 Computer Graphics Jan-Apr 2016 • specifies field-of-view, used for clipping • standardized viewing volume representation • permits standardization • restricts domain of z stored for visibility test • clipping Tamara Munzner perspective orthographic • easier to determine if an arbitrary point is perspective view volume orthographic view volume orthogonal enclosed in volume with canonical view y=top parallel volume vs. clipping to six arbitrary planes y=top x=left x=left • rendering y y x or y x or y = +/- z x or y back • projection and rasterization algorithms can be Viewing 3 z plane z x=right back 1 reused VCS front plane front VCS y=bottom z=-near z=-far x -z x z=-far plane -z plane -1 x=right y=bottom z=-near http://www.ugrad.cs.ubc.ca/~cs314/Vjan2016 -1 2 3 4
Normalized Device Coordinates Normalized Device Coordinates Understanding Z Understanding Z
near, far always positive in GL calls • convention left/right x =+/- 1, top/bottom y =+/- 1, near/far z =+/- 1 • z axis flip changes coord system handedness THREE.OrthographicCamera(left,right,bot,top,near,far); • viewing frustum mapped to specific mat4.frustum(left,right,bot,top,near,far, projectionMatrix); • RHS before projection (eye/view coords) parallelepiped Camera coordinates NDC • LHS after projection (clip, norm device coords) • Normalized Device Coordinates (NDC) x x perspective view volume orthographic view volume • same as clipping coords x=1 VCS NDCS y=top • only objects inside the parallelepiped get right y=top y (1,1,1) y=top x=left x=left x=left rendered Frustum y -z z z y left y • which parallelepiped? z (-1,-1,-1) z x= -1 x=right x x=right VCS • depends on rendering system z= -1 z=1 x z=-far VCS y=bottom z=-near z=-far x x z=-far y=bottom y=bottom z=-n z=-f z=-near x=right z=-near 5 6 7 8
Understanding Z Orthographic Derivation Orthographic Derivation Orthographic Derivation
• why near and far plane? • scale, translate, reflect for new coord sys • scale, translate, reflect for new coord sys • scale, translate, reflect for new coord sys • near plane: y = top → y'= 1 y = top → y'= 1 1 = a ⋅top + b y'= a ⋅ y + b y'= a ⋅ y + b • avoid singularity (division by zero, or very y = bot → y' = −1 y = bot → y' = −1 −1 = a ⋅bot + b small numbers) VCS NDCS VCS NDCS 2 • far plane: 1 = top + b b = 1− a⋅top,b = −1− a ⋅bot top bot y=top y y=top y − • store depth in fixed-point representation x=left (1,1,1) x=left (1,1,1) 1 a top 1 a bot y y − ⋅ = − − ⋅ 2⋅top (integer), thus have to have fixed range of z z b = 1− z (-1,-1,-1) z (-1,-1,-1) 1− (−1) = −a⋅bot − (−a⋅top) top − bot values (0…1) x=right x x=right x 2 = a(−bot + top) (top − bot ) − 2⋅top x x b = • avoid/reduce numerical precision artifacts for z=-far z=-far 2 top − bot distant objects y=bottom z=-near y=bottom z=-near a = − top − bot top − bot b = top bot 9 10 11 − 12
Orthographic Derivation Orthographic Derivation Orthographic Derivation Orthographic Derivation
• scale, translate, reflect for new coord sys • scale, translate, reflect for new coord sys • scale, translate, reflect for new coord sys • scale, translate, reflect for new coord sys y = top → y'= 1 y'= a ⋅ y + b 2 right + left 2 right + left 2 right + left y = bot → y' = −1 0 0 − 0 0 − 0 0 − right − left right − left right − left right − left right − left right − left VCS 2 a = 2 top + bot 2 top + bot 2 top + bot 0 0 − 0 0 − 0 0 − y=top top − bot top − bot top − bot top − bot top − bot top − bot top − bot x=left P' = P P' = P P' = P y top + bot − 2 far + near − 2 far + near − 2 far + near 0 0 − 0 0 − 0 0 − z b = − far − near far − near far − near far − near far − near far − near x=right top − bot x z=-far 0 0 0 1 0 0 0 1 0 0 0 1 y=bottom z=-near same idea for right/left, far/near
13 14 15 16 Orthographic Derivation Projective Rendering Pipeline Projection Warp Perspective Warp
• scale, translate, reflect for new coord sys • warp perspective view volume to orthogonal • perspective viewing frustum transformed to object world viewing view volume cube O2W W2V V2C OCS WCS VCS 2 right + left 0 0 − modeling viewing projection • render all scenes with orthographic projection! • orthographic rendering of cube produces same right − left right − left transformation transformation transformation clipping • aka perspective warp image as perspective rendering of original 2 top + bot C2N frustum 0 0 − OCS - object/model coordinate system CCS top bot top bot perspective P' = − − P WCS - world coordinate system divide normalized x x − 2 far + near 0 0 − VCS - viewing/camera/eye coordinate N2D device far − near far − near system NDCS z=α z=d z=0 z=d viewport CCS - clipping coordinate system 0 0 0 1 transformation NDCS - normalized device coordinate device system Z Z DCS DCS - device/display/screen coordinate 17 system 18 19 20
Predistortion Projective Rendering Pipeline Separate Warp From Homogenization Perspective Divide Example
viewing clipping normalized • specific example V2C CCS C2N device object world viewing VCS O2W W2V V2C NDCS • assume image plane at z = -1 OCS WCS VCS projection perspective modeling viewing projection transformation division • a point [x,y,z,1]T projects to [-x/z,-y/z,-z/z,1]T ≡ transformation transformation transformation alter w / w clipping [x,y,z,-z]T C2N OCS - object/model coordinate system CCS • warp requires only standard matrix multiply perspective x x WCS - world coordinate system y divide normalized • distort such that orthographic projection of distorted y z VCS - viewing/camera/eye coordinate device objects is desired persp projection z N2D 1 system NDCS − z • w is changed viewport CCS - clipping coordinate system • clip after warp, before divide -z transformation NDCS - normalized device coordinate device • division by w: homogenization system DCS DCS - device/display/screen coordinate 21 system 22 23 24
Perspective Divide Example Perspective Normalization Perspective To NDCS Derivation Perspective Derivation • matrix formulation x' 1 0 0 0 x x 1 0 0 0 x x x / z 1 0 0 0 x x − x simple example earlier: y' 0 1 0 0 y y z / d = 0 1 0 0 x p y 0 1 0 0 y y − y / z y y z' 0 0 1 0 z T = ⋅ = ≡ d −α ⋅d (z − α )⋅d y 0 0 = p = w' 0 0 1/d 0 1 z 0 0 1 0 z z − 1 d − α d − α z d −α z / d z 2 VCS y=top 1 p d α NDCS 1 0 0 −1 0 1 − z 1 0 0 0 1 z 1− d z complete: shear, scale, projection-normalization d d −α x=left y (1,1,1) € • after homogenizing, once again w=1 y z • warp and homogenization both preserve x ′ E 0 A 0 x (-1,-1,-1) x z y ′ 0 F B 0 y projection z=-near perspective relative depth (z coordinate) y=bottom z=-far = transformation division x z ′ 0 0 C D z x=right alter w / w w ′ 0 0 −1 0 1
25 27 28
€
Perspective Derivation Perspective Derivation Perspective Derivation Perspective Derivation
x = left → x′ / w′= −1 x' 1 0 0 0 x x' E 0 A 0 x x'= Ex + Az • similarly for other 5 planes x' 1 0 0 0 x x = right → x′/ w′ = 1 earlier: y' 0 1 0 0 y earlier: y' 0 F B 0 y y'= Fy + Bz = y' 0 1 0 0 y = • 6 planes, 6 unknowns z' 0 0 1 0 z = z' 0 0 C D z y = top → y′ / w′= 1 z' 0 0 1 0 z z'= Cz + D w' 0 0 1/d 0 1 w' 0 0 −1 0 1 y = bottom → y′ / w′= −1 2n r + l w' 0 0 1/d 0 1 w'= −z z near z / w 1 0 0 = − → ′ ′ = − r − l r − l complete: shear, scale, projection-normalization complete: shear, scale, projection-normalization z = − far → z′ / w′ = 1 2n t + b € € € €y' Fy + Bz Fy + Bz Fy + Bz 0 0 y'= Fy + Bz, = , 1= , 1= , t − b t − b x ′ E 0 A 0 x x ′ E 0 A 0 x w' w' w' −z −( f + n) −2 fn y ′ 0 F B 0 y y ′ 0 F B 0 y y z y top = = 1 = F + B , 1 = F − B, 1= F − B, 0 0 z ′ 0 0 C D z z ′ 0 0 C D z −z −z −z −(−near) f − n f − n w ′ 0 0 −1 0 1 w ′ 0 0 −1 0 1 top 1 = F − B 0 0 −1 0 near 29 30 31 32 € € € € € Projective Rendering Pipeline NDC to Device Transformation Origin Location N2D Transformation
• map from NDC to pixel coordinates on display • yet more (possibly confusing) conventions • general formulation object world viewing • NDC range is x = -1...1, y = -1...1, z = -1...1 • GL origin: lower left • reflect in y for upper vs. lower left origin O2W W2V V2C OCS WCS VCS • typical display range: x = 0...500, y = 0...300 • most window systems origin: upper left • scale by width, height, depth modeling viewing projection transformation • maximum is size of actual screen transformation transformation clipping • then must reflect in y • translate by width/2, height/2, depth/2 C2N • z range max and default is (0, 1), use later for visibility • FCG includes additional translation for pixel centers at OCS - object/model coordinate system CCS • when interpreting mouse position, have to flip your y perspective gl.viewport(0,0,w,h); (.5, .5) instead of (0,0) gl.depthRange(0,1); // depth = 1 by default coordinates WCS - world coordinate system divide normalized
device 0 x 500 0 x 500 0 x 500 VCS - viewing/camera/eye coordinate N2D y y y system NDCS 0 y 0 y 0 y height viewport CCS - clipping coordinate system -1 -1 1 transformation NDCS - normalized device coordinate device x viewport x viewport x viewport system NDC NDC NDC DCS 1 1 -1 DCS - device/display/screen coordinate width system 33 -1 1 300 34 -1 1 300 35 -1 1 300 36
N2D Transformation Device vs. Screen Coordinates Projective Rendering Pipeline Coordinate Systems
width 1 w idth width(xN + 1) −1 1 0 0 − 0 0 0 viewing xD 2 2 2 1 0 0 0 x N 2 • viewport/window location wrt actual display not available glVertex3f(x,y,z) (4-space, W=1) height 1 height height(− yN +1) −1 yD 0 1 0 − 0 0 0 0 −1 0 0 yN = = within GL z 2 2 2 0 0 1 0 z 2 object world viewing D depth depth N depth(z +1) clipping 0 0 1 0 0 0 N • usually don’t care O2W W2V V2C alter w 1 2 2 0 0 0 1 1 2 OCS WCS VCS glFrustum(...) projection (4-space parallelepiped, projection 0 0 0 1 0 0 0 1 1 • use relative information when handling mouse events, not modeling viewing matrix with COP moved transformation Display z range is 0 to 1. absolute coordinates transformation transformation clipping backwards to infinity gl.depthRange(n,f) can • could get actual display height/width, window offsets from OS glTranslatef(x,y,z) gluLookAt(...) C2N / w CCS normalized device € glRotatef(a,x,y,z) reminder: constrain further, but depth = 1 • loose use of terms: device, display, window, screen... divide by w (3-space .... perspective NDC z range is -1 to 1 is both max and default 0 x 1024 parallelepiped) OCS - object coordinate system division normalized 0 x 500 0 0 y glutInitWindowSize(w,h) device y x 500 y offset N2D scale & device 0 y WCS - world coordinate system glViewport(x,y,a,b) NDCS height 0 y translate (3-space 1 x offset display VCS - viewing coordinate system viewport parallelipiped) viewport viewport height transformation viewport CCS - clipping coordinate system x 300 display device NDC framebuffer -1 NDCS - normalized device coordinate system DCS width 768 37 38 39 40 -1 1 300 display width DCS - device coordinate system
Perspective Example Perspective Example Perspective Example
view volume tracks in VCS: view volume 1 1 1 left = -1, right = 1 • left = -1, right = 1 left x=-1, y=-1 bot = -1, top = 1 −1 1 −1 right x=1, y=-1 • bot = -1, top = 1 = near = 1, far = 4 • near = 1, far = 4 − 5zVCS /3− 8/ 3 −5/ 3 −8/3 zVCS 2n r + l 0 0 1 0 0 0 −zVCS −1 1 x=-1 x=1 r − l r − l 1 ymax-1 2n t + b 0 0 0 1 0 0 z=-4 t − b t − b real xND CS = −1/ zVCS midpoint −( f + n) −2 fn z=-1 -1 0 0 0 0 −5/3 −8 /3 / w -1 1 0 xmax-1 x -1 0 f n f n € yND CS = 1/zVCS NDCS DCS − − 0 0 1 0 (z not shown) (z not shown) 0 0 −1 0 − 5 8 z z = + VCS ND CS 3 3z top view VCS 41 42 43 € € €