C. O. P. d y! "#"" (-1, -1) (1, 1) x z CSC 305 The Graphics Pipeline-1 by Brian Wyvill The University of Victoria Graphics Group
Perspective Viewing Transformation l l l Tools for creating and manipulating a camera that produces pictures of a 3D scene Viewing transformations and projections Perform culling or back-face elimination University of Victoria Graphics Lab. CSC 305 2017 page 2
The Illusion of Depth Perception is an active organising process Many cues to depth! University of Victoria Graphics Lab. CSC 305 2017 page 3
Window to Viewport Transformation University of Victoria Graphics Lab. CSC 305 2017 page 4
University of Victoria Graphics Lab. CSC 305 2017 page 5
Viewing Transformations Sequence that takes a model from object space to the viewport (window) Shirley: Chapter 7. University of Victoria Graphics Lab. CSC 305 2017 page 6
Pixel Conventions Rectangular domain of the image: R = [ 0.5,n x 0.5]x[ 0.5,n y 0.5] University of Victoria Graphics Lab. CSC 305 2017 page 7
Canonical View Volume (chapter 7 Shirley see also chapter 5 on matrices) y z x Object to map lines in the canonical view volume to the screen. (x,y,z) in [-1, 1] 3 (as in interval [a,b]) For now assume that all line segments are in the view volume (clipping later!) The camera views the canonical view volume looking along the z axis University of Victoria Graphics Lab. CSC 305 2017 page 8
Mapping to the screen (ignore z distance along projection axis not affect x,y) Pixel Geometry (-½, ½) (½, ½) (-½, -½) y (0,0) (½, -½) -x z (-1, -1) Map lines to Screen along the Z axis in (+)ve direction. 2 Screen n x by n y pixels x = -1 à left side of screen x = +1 à right side of screen y = -1 à bottom of screen y = +1 à top of screen Maps square [-1, +1] 2 to non-square scales S x and S y will be defined. (1, 1) 2 (-½, n y -½) (-½, -½) Screen Geometry n x (n x -½, n y -½) n y (n x -½, -½) University of Victoria Graphics Lab. CSC 305 2017 page 9
As Before a Window to Viewport Transform (-1, -1) Canonical Canonical Map lines to Screen along the Z axis in (+)ve direction. 2 (1, 1) 2 (-½, n y -½) (-½, -½) Pixels Screen Geometry n x (n x -½, n y -½) n y (n x -½, -½) [ 1 x pixel ] y pixel = [ nx/2 0 (nx-1)/2 0 n y /2 (n y -1)/2 0 0 1 ] [ x caonical ] y canonical 1 For now simply ignore The z-values distorts by aspect ratio - Note that M vp is the above plus a line for z University of Victoria Graphics Lab. CSC 305 2017 page 10
Mvp Window to Viewport Matrix 4x4 to preserve z [ 1 x pixel ] y pixel = [ nx/2 0 (nx-1)/2 0 n y /2 (n y -1)/2 0 0 1 ] [ x caonical ] y canonical 1 " M vp = # n x n 0 0 x 1 2 2 0 n y n 0 y 1 2 2 0 0 1 0 0 0 0 1 % & University of Victoria Graphics Lab. CSC 305 2017 page 11
Orthographic View Volume (fixed axis view) Right Handed System Gaze (or camera view) along Z direction ( note n > f less negative) x to the right and y up The transform we want is just a scale and translate. It takes: transforms to transforms to y = b à y = -1, y = t à y = +1 x = l à x = -1, x = r à x = +1 z = n à z = 1, z = f à z = -1 University of Victoria Graphics Lab. CSC 305 2017 page 12
Orthographic Projection What happens if we don t have a canonical view volume? (l,b,n) a b (r,t,f) (l,b,n) = (lower, bottom, near) (r,t,f)= (right, top, front) Left plane à xà Right Plane Bottom plane à yà Top Plane Near plane à zà Far Plane Find matrix M s.t. M.a and M.b are in canonical view volume. e.g lines in Orthographic view volume above Bounded by axis aligned planes M orth M orth transforms from Orthographic View volume to canonical " M orth = # 2 r l 0 0 0 0 0 r + l r l 2 0 t + b t b t b 2 n + f n f n f 0 0 0 1 % & University of Victoria Graphics Lab. CSC 305 2017 page 13
Window to Viewport Transform Revisited: -1 + + 0 +, 0 0 1 0 u v min min 1 * ( ( ( ) - + + +, λ 0 0 x 0 λ 0 y 0* ( 0 ( 1( ) -1 + + 0 +, 0 0 1 0 x y 1 min min * ( ( ( ) -x* + y ( + ( +, 1( ) where M 0 = u x n x /2 0 0 (n λ x -1)/2 % max 0 n = x y /2 0 (n % y -1)/2 & 0 0 1 max 0, u x 2/(r-l) 0 0 v 0 min " max 0, 2/(t-b) = " λ x 0 0 y min # 0 0 2/(n-f) max 0 v y 1 0 0 -(l+r)/2 min 0 1 0 -(b+t)/2 min 0 0 1 -(n+f)/2 0 0 0 1 0 0 0 1 0 0 0 1 M 0 =M vp M orth is just Another Window to Viewport Transform University of Victoria Graphics Lab. CSC 305 2017 page 14
Scale then translate The transform from the orthographic view volume to the canonical view volume. n is less (-)ve than f x canonical 2/(r-l) 0 0 0 1 0 0 -(l+r)/2 x y canonical z canonical 1 = 0 2/(t-b) 0 0 0 0 2/(n-f) 0 0 0 0 1 0 1 0 -(b+t)/2 0 0 1 -(n+f)/2 0 0 0 1 y z 1 The earlier transform took canonical coords and put them on the Screen. Combining that with the above: 2/(r-l) 0 0 -(l+r)/2 0 2/(t-b) 0 -(b+t)/2 M orth = 0 0 2/(n-f) -(n+f)/2 0 0 0 1 University of Victoria Graphics Lab. CSC 305 2017 page 15
Orthographic Projection: Now we can find pixel coordinates from some user defined view volume coordinates: projection transform 2/(r-l) 0 0 -(l+r)/2 0 2/(t-b) 0 -(b+t)/2 M orth = 0 0 2/(n-f) -(n+f)/2 0 0 0 1 = M vp viewport transform n x /2 0 0 (n x -1)/2 0 n y /2 0 (n y -1)/2 0 0 1 0 0 0 0 1 x pixel y pixel z canonical 1 = M 0 M vp M orth x y z 1 Note that z will be in [-1,1] useful later for z-buffer University of Victoria Graphics Lab. CSC 305 2017 page 16
Orthographic Projection Algorithm compute M vp compute M orth M=M 0 =M vp M orth for each line segment in 3D (a i, b i ) do { p = Ma i q = Mb i drawline(x p, y p, x q, y q ) } University of Victoria Graphics Lab. CSC 305 2017 page 17
Arbitrary View Positions e = Eye Position g = gaze direction t = view up vector W V t Derive a coordinate system with origin e and uvw basis W= U= - g g t x W t x W U e g V= W x U View up vector points to the sky! Bi-sects the viewers head as in photography. University of Victoria Graphics Lab. CSC 305 2017 page 18
Arbitrary View Positions View volume coordinates: origin o and xyz axes. need to convert these to origin e and uvw axes. We can use:! # M vrot = # # # " u x u y u z v x v y v z w x w y w z & & & & % " M v = M vrot # 1 0 0 x e 0 1 0 y e 0 0 1 z e 0 0 0 1 % & University of Victoria Graphics Lab. CSC 305 2017 page 19
Why is Mv computed this way?! # M vrot = # # # " u x u y u z v x v y v z w x w y w z & & & & % Orthogonal Matrix Properties upper 3x3 rotates row vectors into the major axes applies to any rotation matrix or rotation translation (+normalization). cosθ -sinθ 0 sinθ cosθ 0 0 0 1 cosθ -sin 0 = 1 0 0 University of Victoria Graphics Lab. CSC 305 2017 page 20
University of Victoria Graphics Lab. CSC 305 2017 page 21
University of Victoria Graphics Lab. CSC 305 2017 page 22
University of Victoria Graphics Lab. CSC 305 2017 page 23
Rodrigues Forumula https://en.wikipedia.org/wiki/rodrigues%27_rotation_formula If v is a vector in R 3 and k is a unit vector describing an axis of rotation about which v rotates by an angle θ according to the right hand rule, the Rodrigues formula is : v rot = vcosθ + (k v)sinθ + k(k v)(1 cosθ) University of Victoria Graphics Lab. CSC 305 2017 page 24
Example v y w u z x eye (6, 7.5, 8 ) vup t(0, 1, 0) atpt (0, 0, 0) gaze g= (0,0,0) (6,7.5,8) sqrt(6*6+7.5*7.5+8*8) = 12.5 w = -g/ g = (6/12.5, 7.5/12.5, 8/12.5 )=(0.48, 0.6, 0.64) u=(t x w)/ t x W v=(w x u) University of Victoria Graphics Lab. CSC 305 2017 page 25
Calculate U-vector w= 1/12.5 (6, 7.5, 8) t =(0, 1, 0) u=(t x w)/ t x w = (1/12.5)det i j k 0 1 0 6 7.5 8 t x w = 1/12.5 ( 8, -0.0, -6.0)= (0.6, 0.0, -0.48) t x w t x w 1/12.5 ( 8, -0.0, -6.0) = = ( 0.8, 0.0, -0.6) 1/12.5 * sqrt(64+36) University of Victoria Graphics Lab. CSC 305 2017 page 26
Calculate V-vector v = w x u w = 1/12.5 (6, 7.5, 8) u = ( 0.8, 0.0, -0.6) (1/12.5* 1/12.5) det i j k 6 7.5 8 10 0-7.5 v = 1/(12.5 *12.5)( -56.25, 35, -75) v = 1/12.5(-4.5, 10, -6) v = (-0.36, 0.8, -0.48) University of Victoria Graphics Lab. CSC 305 2017 page 27
Calculate M vrot " M vrot = #! # M vrot = # # # " 0.8 0.0 0.6 0.0 0.36 0.8 0.48 0.0 0.48 0.6 0.64 0.0 0.0 0.0 0.0 1.0 M % & u x u y u z v x v y v z w x w y w z & & & & % for eye at ( 6, 7.5, 8 ) Add last column and row For homogeneous coords check the matrix: 0.8 0.0-0.6-0.36 0.8-0.48 0.48 0.6 0.64 0.8 0.0-0.6 = 1 0 0 similarly 2 nd row into y-axis 3 rd row into z-axis University of Victoria Graphics Lab. CSC 305 2017 page 28
Orthographic Projection Algorithm Arbitrary View Point compute M v compute M 0 " M v = M vrot # M = M 0 M v for each line segment in 3D (a i, b i ) do { p = Ma i q = Mb i drawline(x p, y p, x q, y q ) } 1 0 0 x e 0 1 0 y e 0 0 1 z e 0 0 0 1 % & University of Victoria Graphics Lab. CSC 305 2017 page 29
3 Point Perspective Objective is to simulate this method University of Victoria Graphics Lab. CSC 305 2017 page 30
v u -w University of Victoria Graphics Lab. CSC 305 2017 page 31
Geometry for the View Volume v (0.5,0.5) up u w ( n + 0.5, n + 0.5) x y University of Victoria Graphics Lab. CSC 305 2017 page 32
True Perspective in 2D size of an object proportional to 1/z y s = (d/z) y y (x,y) g!!#!!! " d! #! " y s View Plane x University of Victoria Graphics Lab. CSC 305 2017 page 33
University of Victoria Graphics Lab. CSC 305 2017 page 34
Perspective View Transformation n f n f The view frustrum is an inconvenient shape. Clipping Z depth calculation Hidden surface calculation University of Victoria Graphics Lab. CSC 305 2017 page 35
Perspective View Transformation We want to find a matrix that 1. does not change points on z=n plane n f 2. maps large rectangle at z=f to small rectangle at z=n 3. achieve division by z by using homogeneous to cartesian conversion i.e. homogenize University of Victoria Graphics Lab. CSC 305 2017 page 36
Perspective Matrix M p = M p 1 0 0 0 0 1 0 0 0 0 n+f n -f 0 0 1 n 0 for homogenous point (x,y,z,1) = (x/h, y/h, z/h, h) " x% y z # 1& = " x y z n + f n z # n f % & Transforms lines to lines and planes to planes. Line segments in the view volume are transformed to line segments in the canonical view volume. hom ogenize(/ w) )))) z w = n " # nx z ny z n + f nf z 1 % & University of Victoria Graphics Lab. CSC 305 2017 page 37
Perspective Projection Algorithm We can now use the mechanism we used before for orthographic projection Again Arbitrary View Point compute M v compute M 0 compute M p M=M 0 M v M p for each line segment in 3D (a i, b i ) do { p = Ma i } q = Mb i x p, y p drawline(, x q, y q h h h h ) University of Victoria Graphics Lab. CSC 305 2017 page 38
Z-behavior as a result of Perspective Projection Matrix z s = n + f fn/z n=10 f=100 Points (lines and planes) outside viewing volume should be clipped. z s Fewer bits of resolution are available to compare z values far from the eye. Z-buffer does linear interpolation in Z. z University of Victoria Graphics Lab. CSC 305 2017 page 39