Rasterization MIT EECS 6.837 Frédo Durand and Barb Cutler MIT EECS 6.837, Cutler and Durand Final projects Rest of semester Weekly meetings with TAs Office hours on appointment This week, with TAs Refine timeline Define high-level architecture Project should be a whole, but subparts should be identified with regular merging of code MIT EECS 6.837, Cutler and Durand 2 The Graphics Pipeline 3D models defined in their own coordinate system (object space) transforms orient the models within a common coordinate frame (world space) Object space World space MIT EECS 6.837, Cutler and Durand 3 MIT EECS 6.837, Cutler and Durand 4 (Lighting) Vertices lit (shaded) according to material properties, surface properties (normal) and light sources Local lighting model (Diffuse, Ambient, Phong, etc.) Maps world space to eye space Viewing position is transformed to origin & direction is oriented along some ais (usually z) Eye space World space MIT EECS 6.837, Cutler and Durand 5 MIT EECS 6.837, Cutler and Durand 6
Transform to Normalized Device Coordinates (NDC) The objects are projected to the 2D image place (screen space) Eye space Portions of the object outside the view volume (view frustum) are removed NDC NDC Screen Space MIT EECS 6.837, Cutler and Durand 7 MIT EECS 6.837, Cutler and Durand 8 Rasterizes objects into piels Interpolate values as we go (color, depth, etc.) MIT EECS 6.837, Cutler and Durand 9 Each piel remembers the closest object (depth buffer) Almost every step in the graphics pipeline involves a change of coordinate system. are central to understanding 3D computer graphics. MIT EECS 6.837, Cutler and Durand Today Polygon scan conversion smart back to brute force 2D Geometric primitive 2D: point, line, polygon, circle... 3D: point, line, polyhedron, sphere... Primitives are continuous; screen is discrete Visibility MIT EECS 6.837, Cutler and Durand MIT EECS 6.837, Cutler and Durand 2 2
2D Solution: compute discrete approimation : algorithms for efficient generation of the samples comprising this approimation Brute force solution for triangles? MIT EECS 6.837, Cutler and Durand 3 MIT EECS 6.837, Cutler and Durand 4 Brute force solution for triangles For each piel Compute line equations at piel center clip against the triangle Problem? Brute force solution for triangles For each piel Compute line equations at piel center clip against the triangle Problem? If the triangle is small, a lot of useless computation MIT EECS 6.837, Cutler and Durand 5 MIT EECS 6.837, Cutler and Durand 6 Brute force solution for triangles Improvement: Compute only for the screen bounding bo of the triangle How do we get such a bounding bo? Brute force solution for triangles Improvement: Compute only for the screen bounding bo of the triangle Xmin, Xma, Ymin, Yma of the triangle vertices MIT EECS 6.837, Cutler and Durand 7 MIT EECS 6.837, Cutler and Durand 8 3
Can we do better? Kind of! We compute the line equation for many useless piels What could we do? Use line rasterization Compute the boundary piels Shirley page 55 MIT EECS 6.837, Cutler and Durand 9 MIT EECS 6.837, Cutler and Durand 2 Scan-line Rasterization Compute the boundary piels Fill the spans Scan-line Rasterization Requires some initial setup to prepare Shirley page 55 Shirley page 55 MIT EECS 6.837, Cutler and Durand 2 MIT EECS 6.837, Cutler and Durand 22 problem How do we clip parts outside window? problem How do we clip parts outside window? Create two triangles or more. Quite annoying. MIT EECS 6.837, Cutler and Durand 23 MIT EECS 6.837, Cutler and Durand 24 4
Old style graphics hardware Questions? Triangles were big Bresenham+interpolation is worth it Annoying clipping step + a couple of other things have changed Not that good to parallelize beyond triangle granularity MIT EECS 6.837, Cutler and Durand 25 MIT EECS 6.837, Cutler and Durand 26 Today Polygon scan conversion smart back to brute force For modern graphics cards Triangles are usually very small Setup cost are becoming more troublesome is annoying Brute force is tractable Visibility MIT EECS 6.837, Cutler and Durand 27 MIT EECS 6.837, Cutler and Durand 28 Modern rasterization Compute Compute line equations Framebuffer[,y]=triangleColor Modern rasterization Compute Compute line equations Framebuffer[,y]=triangleColor Note that Bbo clipping is trivial MIT EECS 6.837, Cutler and Durand 29 MIT EECS 6.837, Cutler and Durand 3 5
Can we do better? Compute Compute line equations Framebuffer[,y]=triangleColor Can we do better? Compute Compute line equations a+by+c Framebuffer[,y]=triangleColor We don t need to recompute line equation from scratch MIT EECS 6.837, Cutler and Durand 3 MIT EECS 6.837, Cutler and Durand 32 Can we do better? The Graphics Pipeline Compute Setup line eq compute a i d, b i dy for the 3 lines Initialize line eq, values for bbo corner L i =a i +b i y+c i For all scanline y in bbo For 3 lines, update Li For all in bbo Increment line equations: Li+=ad If all Li> //piel [,y] in triangle Framebuffer[,y]=triangleColor We save one multiplication per piel MIT EECS 6.837, Cutler and Durand 33 Modern hardware mostly avoids clipping Only with respect to plane z= MIT EECS 6.837, Cutler and Durand 34 Full One-plane clipping "clip" geometry to view frustum "clip" geometry to near plane (eye, eye y, eye z ) z ais (eye, eye y, eye z ) z ais image plane image plane MIT EECS 6.837, Cutler and Durand 35 MIT EECS 6.837, Cutler and Durand 36 6
Adding Gouraud shading Interpolate colors of the 3 vertices Linear interpolation Adding Gouraud shading Interpolate colors of the 3 vertices Linear interpolation, e.g. for R channel: R=a R +b R y+c R Such that R[,y]=R; R[, y]=r; R[2,y2]=R2 Same as a plane equation in (,y,r) MIT EECS 6.837, Cutler and Durand 37 MIT EECS 6.837, Cutler and Durand 38 Adding Gouraud shading Interpolate colors Compute Setup line eq Setup color equation Increment line equations Increment color equation If all Li> //piel [,y] in triangle Framebuffer[,y]=interpolatedColor In the modern hardware Edge eq. in homogeneous coordinates [, y, w] Tiles to add a mid-level granularity early rejection of tiles Memory access coherence MIT EECS 6.837, Cutler and Durand 39 MIT EECS 6.837, Cutler and Durand 4 Ref Henry Fuchs, Jack Goldfeather, Jeff Hultquist, Susan Spach, John Austin, Frederick Brooks, Jr., John Eyles and John Poulton, Fast Spheres, Shadows, Tetures, Transparencies, and Image Enhancements in Piel-Planes, Proceedings of SIGGRAPH 85 (San Francisco, CA, July 22 26, 985). In Computer Graphics, v9n3 (July 985), ACM SIGGRAPH, New York, NY, 985. Juan Pineda, A Parallel Algorithm for Polygon Rasterization, Proceedings of SIGGRAPH 88 (Atlanta, GA, August 5, 988). In Computer Graphics, v22n4 (August 988), ACM SIGGRAPH, New York, NY, 988. Figure 7: Image from the spinning teapot performance test. Triangle using 2D Homogeneous Coordinates, Marc Olano Trey Greer http://www.cs.unc.edu/~olano/papers/2dh-tri/2dh-tri.pdf Take-home message The appropriate algorithm depends on Balance between various resources (CPU, memory, bandwidth) The input (size of triangles, etc.) Smart algorithms often have initial preprocess Assess whether it is worth it To save time, identify redundant computation Put outside the loop and interpolate if needed MIT EECS 6.837, Cutler and Durand 4 MIT EECS 6.837, Cutler and Durand 42 7
Questions? Today Polygon scan conversion smart back to brute force Visibility MIT EECS 6.837, Cutler and Durand 43 MIT EECS 6.837, Cutler and Durand 44 Visibility How do we know which parts are visible/in front? Ray Casting Maintain intersection with closest object MIT EECS 6.837, Cutler and Durand 45 MIT EECS 6.837, Cutler and Durand 46 Painter s algorithm Painter s algorithm Draw back-to-front How do we sort objects? Can we always sort objects? Draw back-to-front How do we sort objects? Can we always sort objects? A B 3 5 7 No, there can be cycles Requires to split polygons 2 4 6 A B C MIT EECS 6.837, Cutler and Durand 47 MIT EECS 6.837, Cutler and Durand 48 8
Painter s algorithm Old solution for hidden-surface removal Good because ordering is useful for other operations (transparency, antialiasing) But Ordering is tough Cycles Must be done by CPU Hardly used now But some sort of partial ordering is sometimes useful Usuall front-to-back To make sure foreground is rendered first MIT EECS 6.837, Cutler and Durand 49 visibility In ray casting, use intersection with closest t Now we have swapped the loops (piel, object) How do we do? MIT EECS 6.837, Cutler and Durand 5 Z buffer In addition to frame buffer (R, G, B) Store distance to camera (z-buffer) Piel is updated only if new z is closer than z-buffer value Z-buffer pseudo code Compute, color at vertices Setup line equations Increment line equations Compute curentz Increment currentcolor If currentz<zbuffer[,y] //piel is visible Framebuffer[,y]=currentColor zbuffer[,y]=currentz MIT EECS 6.837, Cutler and Durand 5 MIT EECS 6.837, Cutler and Durand 52 Works for hard cases! What eactly do we store Floating point distance Can we interpolate z in screen space? i.e. does z vary linearly in screen space? image z A C B MIT EECS 6.837, Cutler and Durand 53 z MIT EECS 6.837, Cutler and Durand 54 9
Z interpolation X =/z Hyperbolic variation Z cannot be linearly interpolated z image Simple Perspective Project all points along the z ais to the z = d plane, eyepoint at the origin homogenize z MIT EECS 6.837, Cutler and Durand 55 * d / z y * d / z d = y z z / d = /d y z MIT EECS 6.837, Cutler and Durand 56 Yet another Perspective Change the z component Compute d/z Can be linearly interpolated Advantages of /z Can be interpolated linearly in screen space Puts more precision for close objects Useful when using integers more precision where perceptible homogenize * d / z y * d / z d/z = y z / d = /d y z MIT EECS 6.837, Cutler and Durand 57 MIT EECS 6.837, Cutler and Durand 58 Integer z-buffer Use /z to have more precision in the foreground Set a near and far plane /z values linearly encoded between /near and /far Careful, test direction is reversed far near Integer Z-buffer pseudo code Compute, color at vertices Setup line equations, depth equation Increment line equations Increment curent_ovz Increment currentcolor If current_ovz>ovzbuffer[,y]//piel is visible Framebuffer[,y]=currentColor ovzbuffer[,y]=currentovz MIT EECS 6.837, Cutler and Durand 59 MIT EECS 6.837, Cutler and Durand 6
Gouraud interpolation Gouraud: interpolate color linearly in screen space Is it correct? [R, z G, B] image Gouraud interpolation Gouraud: interpolate color linearly in screen space Not correct. We should use hyperbolic interpolation But quite costly (division) image [R, z G, B] [R, z G, B] [R, z G, B] MIT EECS 6.837, Cutler and Durand 6 MIT EECS 6.837, Cutler and Durand 62 Net time Segment intersection & scanline algorithms MIT EECS 6.837, Cutler and Durand 63