Scan Conversion & Shading
Total Page:16
File Type:pdf, Size:1020Kb
3D Rendering Pipeline (for direct illumination) 3D Primitives 3D Modeling Coordinates Modeling TransformationTransformation 3D World Coordinates Scan Conversion LightingLighting 3D World Coordinates ViewingViewing P1 & Shading TransformationTransformation 3D Camera Coordinates ProjectionProjection TransformationTransformation Adam Finkelstein 2D Screen Coordinates P2 Princeton University Clipping 2D Screen Coordinates ViewportViewport COS 426, Spring 2003 TransformationTransformation P3 2D Image Coordinates Scan Conversion ScanScan Conversion & Shading 2D Image Coordinates Image Overview Scan Conversion • Scan conversion • Render an image of a geometric primitive o Figure out which pixels to fill by setting pixel colors • Shading void SetPixel(int x, int y, Color rgba) o Determine a color for each filled pixel • Example: Filling the inside of a triangle P1 P2 P3 Scan Conversion Triangle Scan Conversion • Render an image of a geometric primitive • Properties of a good algorithm by setting pixel colors o Symmetric o Straight edges void SetPixel(int x, int y, Color rgba) o Antialiased edges o No cracks between adjacent primitives • Example: Filling the inside of a triangle o MUST BE FAST! P1 P1 P2 P2 P4 P3 P3 1 Triangle Scan Conversion Simple Algorithm • Properties of a good algorithm • Color all pixels inside triangle Symmetric o void ScanTriangle(Triangle T, Color rgba){ o Straight edges for each pixel P at (x,y){ Antialiased edges if (Inside(T, P)) o SetPixel(x, y, rgba); o No cracks between adjacent primitives } } o MUST BE FAST! P P1 1 P2 P2 P4 P3 P3 Line defines two halfspaces Inside Triangle Test • Implicit equation for a line • A point is inside a triangle if it is in the o On line: ax + by + c = 0 positive halfspace of all three boundary lines o On right: ax + by + c < 0 o Triangle vertices are ordered counter-clockwise o On left: ax + by + c > 0 o Point must be on the left side of every boundary line P1 L L1 P P2 L3 L2 Inside Triangle Test Simple Algorithm Boolean Inside(Triangle T, Point P) • What is bad about this algorithm? { for each boundary line L of T { void ScanTriangle(Triangle T, Color rgba){ Scalar d = L.a*P.x + L.b*P.y + L.c; for each pixel P at (x,y){ if (d < 0.0) return FALSE; if (Inside(T, P)) } SetPixel(x, y, rgba); return TRUE; } } } L1 P1 L3 P2 L2 P3 2 Triangle Sweep-Line Algorithm Triangle Sweep-Line Algorithm • Take advantage of spatial coherence void ScanTriangle(Triangle T, Color rgba){ for each edge pair { o Compute which pixels are inside using horizontal spans initialize xL,xR; Process horizontal spans in scan-line order compute dxL/dyL and dxR/dyR; o for each scanline at y for (int x = xL; x <= xR; x++) • Take advantage of edge linearity SetPixel(x, y, rgba); x += dx /dy ; o Use edge slopes to update coordinates incrementally L L L xR += dxR/dyR; } dx } L dxR dy dx L dyR dy Bresenham’s algorithm works the same way, but uses only integer xL xR operations! Polygon Scan Conversion Polygon Scan Conversion • Fill pixels inside a polygon • Need better test for points inside polygon o Triangle o Triangle method works only for convex polygons o Quadrilateral o Convex o Star-shaped L5 L5 L4 L4 o Concave o Self-intersecting L1 L1 o Holes L3B L3 L3A L2 L2 What problems do we encounter with arbitrary polygons? Convex Polygon Concave Polygon Inside Polygon Rule Inside Polygon Rule • What is a good rule for which pixels are inside? • Odd-parity rule o Any ray from P to infinity crosses odd number of edges Concave Self-Intersecting With Holes Concave Self-Intersecting With Holes 3 Polygon Sweep-Line Algorithm Polygon Sweep-Line Algorithm • Incremental algorithm to find spans, void ScanPolygon(Triangle T, Color rgba){ sort edges by maxy and determine insideness with odd parity rule make empty “active edge list” Takes advantage of scanline coherence for each scanline (top-to-bottom) { o 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(xi, xi+1, y, rgba); } } xL xR Triangle Polygon Hardware Scan Conversion Hardware Antialiasing • Convert everything into triangles • Supersample pixels o Scan convert the triangles o Multiple samples per pixel o Average subpixel intensities (box filter) o Trades intensity resolution for spatial resolution P1 P2 P3 Overview Shading • Scan conversion • How do we choose a color for each filled pixel? o Figure out which pixels to fill o Each illumination calculation for a ray from the eyepoint through the view plane provides a radiance sample • Shading » How do we choose where to place samples? o Determine a color for each filled pixel » How do we filter samples to reconstruct image? Emphasis on methods that can be implemented in hardware Angel Figure 6.34 4 Ray Casting Polygon Shading • Simplest shading approach is to perform • Can take advantage of spatial coherence independent lighting calculation for every pixel o Illumination calculations for pixels covered by same o When is this unnecessary? primitive are related to each other I = I + K I + (K (N • L )I + K (V • R )n I ) I = I + K I + (K (N • L )I + K (V • R )n I ) E A AL ∑i D i i S i i E A AL ∑i D i i S i i Polygon Shading Algorithms Polygon Shading Algorithms • Flat Shading • Flat Shading • Gouraud Shading • Gouraud Shading • Phong Shading • Phong Shading Flat Shading Flat Shading • What if a faceted object is illuminated only by • One illumination calculation per polygon directional light sources and is either diffuse or o Assign all pixels inside each polygon the same color viewed from infinitely far away N I = I + K I + (K (N • L )I + K (V • R )n I ) E A AL ∑i D i i S i i 5 Flat Shading Polygon Shading Algorithms • Objects look like they are composed of polygons • Flat Shading OK for polyhedral objects o • Gouraud Shading o Not so good for smooth surfaces • Phong Shading Gouraud Shading Gouraud Shading • What if smooth surface is represented by • Method 1: One lighting calculation per vertex polygonal mesh with a normal at each vertex? o Assign pixels inside polygon by interpolating colors computed at vertices Watt Plate 7 I = I + K I + (K (N • L )I + K (V • R )n I ) E A AL ∑i D i i S i i Gouraud Shading Gouraud Shading • Bilinearly interpolate colors at vertices • Smooth shading over adjacent polygons down and across scan lines o Curved surfaces o Illumination highlights o Soft shadows Mesh with shared normals at vertices Watt Plate 7 6 Gouraud Shading Polygon Shading Algorithms • Produces smoothly shaded polygonal mesh • Flat Shading Piecewise linear approximation o • Gouraud Shading o Need fine mesh to capture subtle lighting effects • Phong Shading Flat Shading Gouraud Shading Phong Shading Phong Shading • What if polygonal mesh is too coarse to capture • Method 2: One lighting calculation per pixel illumination effects in polygon interiors? o Approximate surface normals for points inside polygons by bilinear interpolation of normals from vertices I = I + K I + (K (N • L )I + K (V • R )n I ) E A AL ∑i D i i S i i Phong Shading Polygon Shading Algorithms • Bilinearly interpolate surface normals at vertices down and across scan lines Wireframe Flat Gouraud Phong Watt Plate 7 7 Shading Issues Summary • Problems with interpolated shading: • 2D polygon scan conversion o Polygonal silhouettes o Paint pixels inside primitive o Perspective distortion o Sweep-line algorithm for polygons Orientation dependence (due to bilinear interpolation) o • Polygon Shading Algorithms o Problems computing shared vertex normals o Flat Less expensive o Problems at T-vertices o Gouraud o Phong o Ray casting More accurate • Key ideas: o Sampling and reconstruction o Spatial coherence 8.