3D Rendering Pipeline (for direct illumination) 3D Primitives 3D Modeling Coordinates Modeling Transformation 3D World Coordinates Scan Conversion Lighting 3D World Coordinates & Shading P 1 Viewing Transformation 3D Camera Coordinates Projection Transformation Taken from Thomas Funkhouser 2D Screen Coordinates P 2 Clipping 2D Screen Coordinates P 3 Viewport Transformation Scan Conversion 2D Image Coordinates Scan & Shading Conversion 2D Image Coordinates Image Overview Scan Conversion • Scan conversion • Render an image of a geometric primitive by setting pixel colors � Figure out which pixels to fill • Shading void SetPixel(int x, int y, Color rgba) � Determine a color for each filled pixel • Example: Filling the inside of a triangle P 1 P 2 P 3 Scan Conversion Triangle Scan Conversion • Render an image of a geometric primitive • Properties of a good algorithm by setting pixel colors � Symmetric � Straight edges void SetPixel(int x, int y, Color rgba) � Antialiased edges � No cracks between adjacent primitives � MUST BE FAST! • Example: Filling the inside of a triangle P 1 P 1 P 2 P 2 P 4 P 3 P 3 1
Triangle Scan Conversion Simple Algorithm • Properties of a good algorithm • Color all pixels inside triangle � Symmetric void ScanTriangle(Triangle T, Color rgba){ � Straight edges for each pixel P at (x,y){ if (Inside(T, P)) � Antialiased edges SetPixel(x, y, rgba); � No cracks between adjacent primitives } } � MUST BE FAST! P 1 P 1 P 2 P 2 P 4 P 3 P 3 Inside Triangle Test Inside Triangle Test • A point is inside a triangle if it is in the Boolean Inside(Triangle T, Point P) { positive halfspace of all three boundary lines for each boundary line L of T { � Triangle vertices are ordered counter-clockwise Scalar d = L.a*P.x + L.b*P.y + L.c; if (d < 0.0) return FALSE; � Point must be on the left side of every boundary line } return TRUE; } L 1 L 1 P L 3 L 3 L 2 L 2 Simple Algorithm Triangle Sweep-Line Algorithm • What is bad about this algorithm? • Take advantage of spatial coherence � Compute which pixels are inside using horizontal spans void ScanTriangle(Triangle T, Color rgba){ � Process horizontal spans in scan-line order for each pixel P at (x,y){ if (Inside(T, P)) SetPixel(x, y, rgba); • Take advantage of edge linearity } � Use edge slopes to update coordinates incrementally } P 1 dx dy P 2 P 3 2
Triangle Sweep-Line Algorithm Polygon Scan Conversion void ScanTriangle(Triangle T, Color rgba){ • Fill pixels inside a polygon for each edge pair { � Triangle initialize x L , x R ; compute dx L /dy L and dx R /dy R ; � Quadrilateral for each scanline at y � Convex for (int x = x L ; x <= x R ; x++) SetPixel(x, y, rgba); � Star-shaped x L += dx L /dy L ; � Concave x R += dx R /dy R ; } dx L dx R � Self-intersecting } dy L � Holes dy R x L x R What problems do we encounter with arbitrary polygons? Polygon Scan Conversion Inside Polygon Rule • Need better test for points inside polygon • What is a good rule for which pixels are inside? � Triangle method works only for convex polygons L 5 L 5 L 4 L 4 L 1 L 1 L 3B L 3 L 3A L 2 L 2 Concave Self-Intersecting With Holes Concave Polygon Convex Polygon Inside Polygon Rule Polygon Sweep-Line Algorithm • Odd-parity rule • Incremental algorithm to find spans, � Any ray from P to infinity crosses odd number of edges and determine insideness with odd parity rule � Takes advantage of scanline coherence x L x R Concave Self-Intersecting With Holes Triangle Polygon 3
Polygon Sweep-Line Algorithm Hardware Scan Conversion void ScanPolygon(Triangle T, Color rgba){ • Convert everything into triangles sort edges by maxy � Scan convert the triangles make empty “active edge list” for each scanline (top-to-bottom) { insert/remove edges from “active edge list” update x coordinate of every active edge sort active edges by x coordinate for each pair of active edges (left-to-right) SetPixels(x i , x i+1 , y, rgba); } } Hardware Antialiasing • Supersample pixels � Multiple samples per pixel � Average subpixel intensities (box filter) � Trades intensity resolution for spatial resolution P 1 P 2 P 3 4
Recommend
More recommend