lecture09-1

lecture09-1 - Introduction to Computer Graphics CS 445 /...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Graphics CS 445 / 645 Lecture 9 Chapter 4: Rasterization L. McMillan MIT 6.837 notes What made line drawing faster? Symmetry and Differential Analyzer Symmetry f ( x ) = mx + b f ( x + 1) = m( x + 1) + b = mx + m + b = f (x ) + ¢ ¢ = f ( x + 1) ¡ f ( x ) = mx ¡ mx + m + b ¡ b =m f ( x + 1) = f ( x ) + m The Rendering Pipeline Transform Illuminate ModelWorld WorldCamera Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display Switch to MIT Slides Course slides from Leonard McMillan’s MIT Course Course (6.837) from Fall ‘98 Course Rasterizing Polygons In interactive graphics, polygons rule the world Two main reasons: • Lowest common denominator for surfaces – Can represent any surface with arbitrary accuracy Can with – Splines, mathematical functions, volumetric isosurfaces… • Mathematical simplicity lends itself to simple, regular rendering Mathematical algorithms algorithms – Like those we’re about to discuss… Like – Such algorithms embed well in hardware Rasterizing Polygons Triangle is the minimal unit Triangle minimal of a polygon of • All polygons can be broken up into All triangles triangles • Triangles are guaranteed to be: – Planar – Convex Triangulation Convex polygons easily Convex triangulated (Delaunay) triangulated Concave polygons present Concave a challenge challenge Triangle Rasterization Issues Sliver Triangle Rasterization Issues Moving Slivers Moving Triangle Rasterization Issues Shared Edge Ordering Rasterizing Triangles Interactive graphics hardware commonly uses Interactive edge walking or edge equation techniques for edge edge rasterizing triangles rasterizing Edge Walking Basic idea: Basic • Draw edges vertically – Interpolate colors down edges • Fill in horizontal spans for each Fill scanline scanline – At each scanline, interpolate At edge colors across span edge Edge Walking: Notes Order three triangle vertices in x and y • Find middle point in y dimension and compute if it is to the left or right Find of polygon. Also could be flat top or flat bottom triangle of We know where left and right edges are. • Proceed from top scanline downwards • Fill each span • Until breakpoint or bottom vertex is reached Advantage: can be made very fast Disadvantages: Disadvantages: • Lots of finicky special cases Edge Walking: Disadvantages Fractional offsets: Be careful when interpolating color values! Beware of gaps between adjacent edges Beware of duplicating shared edges Triangle Rasterization Issues Color Interpolation Triangle Rasterization Issues Shared Edge Ordering Edge Equations An edge equation is simply the equation of the line An defining that edge defining • Q: What is the implicit equation of a line? Q: What • A: Ax + By + C = 0 A: Ax • Q: Given a point (x,y), what does plugging x & y into this equation tell Q: Given ), us? us? • A: Whether the point is: – On the line: Ax + By + C = 0 On – “Above” the line: Ax + By + C > 0 Above” – “Below” the line: Ax + By + C < 0 Below” Edge Equations Edge equations thus define two half-spaces: Edge half-spaces Edge Equations Ax Ax 2 2 +B +B 2 2 +B 3 Ax +B 3 Ax y+ y+ 3 y+ C< 3 0 3y + C3 > 0 And a triangle can be defined as the intersection And of three positive half-spaces: of 0 + C1 > By A 1x + 1 <0 y + C1 B A 1x + 1 C< 2 0 C 2> 0 Edge Equations So…simply turn on those pixels for which all edge So…simply equations evaluate to > 0: equations -+ + +- Using Edge Equations Which pixels: compute min,max bounding box Edge equations: compute from vertices Computing Edge Equations Want to calculate A, B, C for each edge from (x1, y1) and and (x2, y2) Treat it as a linear system: Ax1 + By1 + C = 0 Ax2 + By2 + C = 0 Notice: two equations, three unknowns What can we solve? Goal: solve for A & B in terms of C Computing Edge Equations Set up the linear system: x 0 y 0 A 1 x1 y1 B = −C 1 Multiply both sides A y1 − y 0 −C by matrix inverse: B = x 0 y1 − x1 y 0 x1 − x 0 Let C = x0 y1 - x1 y0 for convenience Let • Then A = y0 - y1 and B = x0 – x1 Then Edge Equations So…we can find edge equation from two verts. So…we Given three corners P0, P1, P2 of a triangle, what are our three edges? three How do we make sure the half-spaces defined by the How edge equations all share the same sign on the interior of the triangle? interior A: Be consistent (Ex: [P0 P1], [P1 P2], [P2 P0]) A: [P How do we make sure that sign is positive? A: Test, and flip if needed (A= -A, B= -B, C= -C) Edge Equations: Code Basic structure of code: • Setup: compute edge equations, bounding box • (Outer loop) For each scanline in bounding box... (Outer • (Inner loop) …check each pixel on scanline, (Inner evaluating edge equations and drawing the pixel if all three are positive three Optimize This! findBoundingBox(&xmin, &xmax, &ymin, &ymax); setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2); /* Optimize this: */ for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { float e0 = a0*x + b0*y + c0; float e1 = a1*x + b1*y + c1; float e2 = a2*x + b2*y + c2; if (e0 > 0 && e1 > 0 && e2 > 0) setPixel(x,y); }} Edge Equations: Speed Hacks Some speed hacks for the inner loop: int xflag = 0; for (int x = xMin; x <= xMax; x++) { if (e0|e1|e2 > 0) { setPixel(x,y); xflag++; } else if (xflag != 0) break; e0 += a0; e1 += a1; e2 += a2; } • Incremental update of edge equation values (think DDA) (think • Early termination (why does this work?) • Faster test of equation values Triangle Rasterization Issues Exactly which pixels should be lit? A: Those pixels inside the triangle edges What about pixels exactly on the edge? • Draw them: order of triangles matters (it shouldn’t) • Don’t draw them: gaps possible between triangles We need a consistent (if arbitrary) rule We • Example: draw pixels on left or top edge, but not on Example: right or bottom edge right General Polygon Rasterization Now that we can rasterize triangles, what about Now general polygons? general We’ll take an edge-walking approach General Polygon Rasterization Consider the following polygon: D B C A E F How do we know whether a given pixel on the How scanline is inside or outside the polygon? scanline Polygon Rasterization Inside-Outside Points Polygon Rasterization Inside-Outside Points General Polygon Rasterization Basic idea: use a parity test Basic parity for each scanline edgeCnt = 0; edgeCnt for each pixel on scanline (l to r) for if (oldpixel->newpixel crosses edge) edgeCnt ++; edgeCnt // draw the pixel if edgeCnt odd // if (edgeCnt % 2) if setPixel(pixel); setPixel(pixel); General Polygon Rasterization Count your vertices carefully • If exactly on pixel boundary? • Shared vertices? • Vertices defining horizontal edge? – Consider A-B versus I-H G I F H E C J A D B Faster Polygon Rasterization How can we optimize the code? for each scanline edgeCnt = 0; edgeCnt for each pixel on scanline (l to r) for if (oldpixel->newpixel crosses edge) edgeCnt ++; edgeCnt // draw the pixel if edgeCnt odd // if (edgeCnt % 2) if setPixel(pixel); setPixel(pixel); Big cost: testing pixels against each edge Solution: active edge table (AET) Solution: active Active Edge Table Idea: Idea: • Edges intersecting a given scanline are likely to intersect Edges the next scanline the • The order of edge intersections doesn’t change much from The scanline to scanline scanline Active Edge Table Algorithm: scanline from bottom to top… Algorithm: scanline • Sort all edges by their minimum y coord • Starting at bottom, add edges with Ymin= 0 to AET • For each scanline: – Sort edges in AET by x intersection – Walk from left to right, setting pixels by parity rule – Increment scanline – Retire edges with Ymax < Y – Add edges with Ymin < Y – Recalculate edge intersections (how?) • Stop when Y > Ymax for last edges Review: Polygon Rasterization For scanline, determine all polygon edges that intersect current scanline Sort edge intersections with scanline in least to greatest order Use parity count to determine when pixels are drawn Horizontal lines do not contribute to parity count Ymin endpoints do contribute to parity count Ymax endpoints do not contribute to parity count Not drawn because H is max of AH And HG does not contribute H F Not drawn because D is min of ED And increments counter to 2. DC doesn’t contribute G E D A C B Bottom edge drawn because A is min of AH. AB does not contribute ...
View Full Document

This note was uploaded on 01/23/2012 for the course CS 445 taught by Professor Bloomfield,a during the Spring '08 term at UVA.

Ask a homework question - tutors are online