CS559: Computer Graphics Lecture 9: Rasterization Li Zhang Spring 2008 Today • • • • Finish Projection Rasterization Antialising Hidden Surface Removal • Reading: – Shirley ch 7,4,8 3D Geometry Pipeline Model Space (Object Space) Eye Space (View Space) World Space Rotation Translation Resizing Rotation Translation 1 0 0 0 0 0 1 0 0 1 0 1/ d Projective, Scale Translation 0 x ( d / z ) x 0 y (d / z ) y 0 z d 0 1 1 Clipping and the viewing frustum The center of projection and the portion of the projection plane that map to the final image form an infinite pyramid. The sides of the pyramid are clipping planes. Frequently, additional clipping planes are inserted to restrict the range of depths. These clipping planes are called the near and far or the hither and yon clipping planes. 1 0 0 0 0 0 1 0 0 1 0 1/ d 0 x ( d / z ) x 0 y (d / z ) y 0 z d 0 1 1 All of the clipping planes bound the viewing frustum. View Volume Transformation • We want a matrix that will take points in our perspective view volume and transform them into the orthographic view volume – This matrix will go in our pipeline before an orthographic projection matrix (r,t,f) (r,t,n) (l,b,n) (r,t,n) (l,b,n) (r,t,f) Mapping Lines y -z • We want to map all the lines through the center of projection to parallel lines – This converts the perspective case to the orthographic case, we can use all our existing methods • The relative intersection points of lines with the near clip plane should not change • The matrix that does this looks like the matrix for our simple perspective case n f How to get this mapping? y 1 0 0 0 -z 0 x ( n / z ) x 1 0 0 y (n / z ) y 0 1 0 z n 0 1 / n 0 1 1 0 1 0 M 0 0 0 0 0 1 0 0 A 0 1/ n x x y y M n ( An B) 1 1 n n 0 0 B 0 x (n / f ) x y (n / f ) y M f (n / f )( Af B) 1 1 f f f A 1 n Bf Properties of this mapping y 1 0 0 0 0 1 0 0 1 0 M 0 0 0 x ( n / z ) x 0 0 y (n / z ) y 1 0 z n 1 / n 0 1 1 0 0 0 1 0 0 0 (n f ) / n f 0 1/ n 0 -z 0 nx x' x z y ' y ny M z z ' z f n fn 1 1 z 1 n f If z = n, M(x,y,z,1) = [x,y,z,1] near plane is unchanged If x/z = c1, y/z = c2, then x’=n*c1, y’=n*c2 bending converging rays to parallel rays If z1 < z2, z1’ < z2’ z ordering is preserved In pyramid In cube General Perspective 1 0 MViewOrthoView 0 0 0 n 1 0 0 0 0 n f n f 0 0 1n 0 0 0 0 0 0 n 0 0 n f 0 1 0 0 nf 0 To Canonical view volume • Perspective projection transforms a pyramid volume to an orthographic view volume (r,t,f) (r,t,f) (r,t,n) (r,t,n) (l,b,n) (l,b,n) MViewOrthoView 2 r l 0 0 0 2 t b 0 0 2 n f 0 0 0 0 0 1 0 0 0 0 1 0 r l 2 1 0 t b 2 0 1 n f 2 0 0 1 Scale, translate 0 0 (-1,1,-1) (1,-1,1) Canonical view volume [-1,1]^3 Orthographic View to Canonical Matrix xcanonical 2 y canonical zcanonical 1 r l 2 r l 0 0 0 2 t b 0 0 2 n f 0 0 0 0 0 0 0 2 t b 0 0 2 n f 0 0 0 0 0 1 0 0 0 0 1 0 r l 2 xorthoView 1 0 t b 2 yorthoView 0 1 n f 2 zorthoView 0 0 1 1 0 0 r l r l xorthoView t b t b yorthoView n f n f zorthoView 1 1 xcanonical M orthoViewcanonicalxorthoView Complete Perspective Projection • After applying the perspective matrix, we map the orthographic view volume to the canonical view volume: M view canonical M orthoViewCanonicalM vieworthoView 2 r l 0 0 0 0 2 t b 0 0 0 0 2 n f 0 r l r l n t b 0 t b 0 n f n f 0 1 0 0 n 0 0 n f 0 1 0 0 nf 0 3D Geometry Pipeline Rotation Translation Rotation Translation Resizing Model Space (Object Space) M world view Eye Space (View Space) World Space M viewcanonical : Projective, Scale, Translation Canonical View Space Screen Space (2D) Image Space (pixels) Raster Space 3D Geometry Pipeline Rotation Translation Rotation Translation Resizing Model Space (Object Space) M world view World Space Eye Space (View Space) M world viewM viewcanonical Canonical View Space Screen Space (2D) Image Space (pixels) Raster Space 3D Geometry Pipeline Rotation Translation Rotation Translation Resizing Model Space (Object Space) M world view World Space Eye Space (View Space) M viewcanonicalM world view Order matters!!! Canonical View Space Screen Space (2D) Image Space (pixels) Raster Space Canonical Window Transform (xmax, ymax)=(nx-0.5,ny-0.5) ny-1 (1,1) (-1,-1) nx-1 (xmin,ymin)=(-0.5,-0.5) x pixel xmax y pixel z pixel 1 xmin 2 0 0 xmax xmin 2 xcanonical ymax ymin 2 ycanonical 0 ymax ymin 2 0 0 1 0 0 0 0 1 0 M canonical pixel zcanonical 1 3D Geometry Pipeline Rotation Translation Resizing Model Space (Object Space) Rotation Translation M world view World Space Eye Space (View Space) M viewcanonical M canonical pixel Canonical View Space Screen Space (2D) Image Space (pixels) Raster Space Polygons are basic shape primitives • Any 3D shape can be approximated by a polygon using a locally linear (planar) approximation. To improve the quality of fit, we need only increase the number of edges Line Drawing (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y Line Drawing for x = x0 to x1 do y=y0+(∆y/∆x)(x-x0) draw(x,round(y)) (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y Line Drawing y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y Implicit Line Drawing y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then f(x+1,y+0.5)<0 y=y+1 (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) f ( x, y ) 0 on the line f ( x, y ) 0 above the line f ( x, y ) 0 below the line Implicit Line Drawing y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then f(x+1,y+0.5)<0 y=y+1 (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) Incremental Evaluation f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing d=f(x+1,y+0.5) y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then d<0 (x1, y1) (x0, y0) D=d-∆y+∆x y=y+1 else D=d-∆y y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) Incremental Evaluation f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing d=2f(x+1,y+0.5) y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then d<0 (x1, y1) (x0, y0) D=d-2∆y+2∆x y=y+1 else D=d-2∆y y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) Incremental Evaluation Integer operation f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing d=2f(x+1,y+0.5) y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then d<0 (x1, y1) (x0, y0) D=d-2∆y+2∆x y=y+1 else D=d-2∆y y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing d=2f(x+1,y+0.5) y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then d<0 D=d-2∆y+2∆x y=y+1 else D=d-2∆y y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 (x1, y1) (x0, y0) y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then y=y+1 y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Implicit Line Drawing d=2f(x+1,y+0.5) y = y0 for x = x0 to x1 do draw(x,y) if (some condition) then d<0 (x1, y1) (x0, y0) D=d-2∆y+2∆x y=y+1 else D=d-2∆y y ( x x0 ) y0 x x x1 x0 , y y1 y0 y f ( x, y) x ( y y0 ) y ( x x0 ) If ∆y>∆x, switch x and y f ( x 1, y ) f ( x, y ) y f ( x 1, y 1) f ( x, y ) y x Drawing Triangle Drawing Triangle A2 for all y do for all x do if (in triangle) then draw(x,y) with color c P A1 A3 x3 xP x1 x2 y t1 y t2 y t3 y P 1 2 3 t1 t2 t3 1 t1 area ( PA2 A3 ) area ( A1 A2 A3 ) t2 area ( PA3 A1 ) area ( PA1 A2 ) t3 area ( A1 A2 A3 ) area ( A1 A2 A3 ) A1 : ( x1 , y1 ), c1 A2 : ( x2 , y 2 ), c2 A3 : ( x3 , y3 ), c3 area ( PA2 A3 ) area ( PA2 A3 ) 1 PA2 PA3 2 1 PA2 PA3 sin( A2 PA3 ) 2 Drawing Triangle for all y do for all x do compute t1,t2,t3 if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3 x3 xP x1 x2 y t1 y t2 y t3 y P 1 2 3 t1 t2 t3 1 t1 area ( PA2 A3 ) area ( A1 A2 A3 ) t2 area ( PA3 A1 ) area ( A1 A2 A3 ) t3 area ( PA1 A2 ) area ( A1 A2 A3 ) A2 P A1 A3 A1 : ( x1 , y1 ), c1 A2 : ( x2 , y 2 ), c2 A3 : ( x3 , y3 ), c3 area ( PA2 A3 ) area ( PA2 A3 ) 1 PA2 PA3 2 1 PA2 PA3 sin( A2 PA3 ) 2 Common Edge for all y do for all x do compute t1,t2,t3 if t1,t2,t3 in [0,1] then draw(x,y) with color t1c1+t2c2+t3c3 x3 xP x1 x2 y t1 y t2 y t3 y P 1 2 3 t1 t2 t3 1 t1 area ( PA2 A3 ) area ( A1 A2 A3 ) t2 area ( PA3 A1 ) area ( A1 A2 A3 ) t3 area ( PA1 A2 ) area ( A1 A2 A3 ) A1 : ( x1 , y1 ), c1 A2 : ( x2 , y 2 ), c2 A3 : ( x3 , y3 ), c3 area ( PA2 A3 ) area ( PA2 A3 ) 1 PA2 PA3 2 1 PA2 PA3 sin( A2 PA3 ) 2