# CS 445 / 645 Introduction to Computer Graphics Lecture 8 Triangle Rasterization Lecture 8 Triangle Rasterization.

• Published on
29-Dec-2015

• View
213

• Download
0

Embed Size (px)

Transcript

• CS 445 / 645Introduction to Computer GraphicsLecture 8Triangle Rasterization

• Optimization TechniquesSymmetry

• Optimization TechniquesIncremental ComputationCompute DifferenceLine example

• Incremental EvaluationNoninteger additionround needed

• Line Midpoint EvaluationCredited to BresenhamOperate only on integers and avoid roundingCreate discriminator, d = d1 d2If d > 0 y increasesIf d
• Incremental EvaluationCircles Midpoint discriminatorTwo dimensional discriminator

Simple output

• Incremental EvaluationCirclesSimple comparisonsWe just want to know if incremental change in x requires an incremental change in y to stay in circleWe evaluate descriminator at f(x+1, y)Do it incrementally

• Incremental EvaluationCircle Discriminator

If

you must decrement y

• Incremental EvaluationCircleNote following correction from slides

Added inequalitiesJust like previous example with extra thrown in for rounding

• Assignment 1Due one week from today at 5:00 p.m.Use your directory on Blue.unix for turnin

• Rasterizing PolygonsIn interactive graphics, polygons rule the worldTwo main reasons:Lowest common denominator for surfacesCan represent any surface with arbitrary accuracySplines, mathematical functions, volumetric isosurfacesMathematical simplicity lends itself to simple, regular rendering algorithmsLike those were about to discuss Such algorithms embed well in hardware

• Rasterizing PolygonsTriangle is the minimal unit of a polygonAll polygons can be broken up into trianglesTriangles are guaranteed to be:PlanarConvex

• TriangularizationConvex polygons easily triangulated

Concave polygons present a challenge

• Rasterizing TrianglesInteractive graphics hardware commonly uses edge walking or edge equation techniques for rasterizing triangles

• Edge WalkingBasic idea: Draw edges verticallyInterpolate colors down edgesFill in horizontal spans for each scanlineAt each scanline, interpolate edge colors across span

• Edge Walking: NotesOrder three triangle vertices in x and yFind middle point in y dimension and compute if it is to the left or right of polygon. Also could be flat top or flat bottom triangleWe know where left and right edges are.Proceed from top scanline downwardsFill each spanUntil breakpoint or bottom vertex is reachedAdvantage: can be made very fastDisadvantages: Lots of finicky special cases

• Edge Walking: DisadvantagesFractional offsets:

Be careful when interpolating color values!Beware of gaps between adjacent edgesBeware of duplicating shared edges

• Edge EquationsAn edge equation is simply the equation of the line defining that edgeQ: What is the implicit equation of a line?A: Ax + By + C = 0Q: Given a point (x,y), what does plugging x & y into this equation tell us?A: Whether the point is:On the line: Ax + By + C = 0 Above the line: Ax + By + C > 0 Below the line: Ax + By + C < 0

• Edge EquationsEdge equations thus define two half-spaces:

• Edge EquationsAnd a triangle can be defined as the intersection of three positive half-spaces:

• Edge EquationsSosimply turn on those pixels for which all edge equations evaluate to > 0:

• Using Edge EquationsWhich pixels: compute min,max bounding box

Edge equations: compute from verticesOrientation: ensure area is positive (why?)

• Computing Edge EquationsWant to calculate A, B, C for each edge from (x1, y1) and (x2, y2)Treat it as a linear system:Ax1 + By1 + C = 0Ax2 + By2 + C = 0Notice: two equations, three unknownsWhat can we solve?Goal: solve for A & B in terms of C

• Computing Edge EquationsSet up the linear system:

Multiply both sides by matrix inverse:

Let C = x0 y1 - x1 y0 for convenienceThen A = y0 - y1 and B = x0 x1

• Edge EquationsSowe can find edge equation from two verts. Given three corners P0, P1, P2 of a triangle, what are our three edges?How do we make sure the half-spaces defined by the edge equations all share the same sign on the interior of the triangle?A: Be consistent (Ex: [P0 P1], [P1 P2], [P2 P0])How do we make sure that sign is positive?A: Test, and flip if needed (A= -A, B= -B, C= -C)

• Edge Equations: CodeBasic structure of code:Setup: compute edge equations, bounding box(Outer loop) For each scanline in bounding box... (Inner loop) check each pixel on scanline, evaluating edge equations and drawing the pixel if all three are positive

• Optimize This!findBoundingBox(&xmin, &xmax, &ymin, &ymax);setupEdges (&a0,&b0,&c0,&a1,&b1,&c1,&a2,&b2,&c2);

/* Optimize this: */for (int y = yMin; y 0 && e2 > 0)setPixel(x,y);}}

• Edge Equations: Speed HacksSome speed hacks for the inner loop:int xflag = 0;for (int x = xMin; x 0) {setPixel(x,y);xflag++;} else if (xflag != 0) break;e0 += a0; e1 += a1; e2 += a2;}Incremental update of edge equation values (think DDA)Early termination (why does this work?)Faster test of equation values

• Triangle Rasterization IssuesExactly which pixels should be lit?A: Those pixels inside the triangle edgesWhat about pixels exactly on the edge? Draw them: order of triangles matters (it shouldnt)Dont draw them: gaps possible between trianglesWe need a consistent (if arbitrary) rule Example: draw pixels on left or top edge, but not on right or bottom edge

• General Polygon RasterizationNow that we can rasterize triangles, what about general polygons?Well take an edge-walking approach

• Triangle Rasterization IssuesSliver

• Triangle Rasterization IssuesMoving Slivers

• Triangle Rasterization IssuesShared Edge Ordering

• General Polygon RasterizationConsider the following polygon:

How do we know whether a given pixel on the scanline is inside or outside the polygon?ABCDEF

• Polygon RasterizationInside-Outside Points

• Polygon RasterizationInside-Outside Points

• General Polygon RasterizationBasic idea: use a parity test

for each scanline edgeCnt = 0; for each pixel on scanline (l to r) if (oldpixel->newpixel crosses edge) edgeCnt ++; // draw the pixel if edgeCnt odd if (edgeCnt % 2) setPixel(pixel);

• General Polygon RasterizationCount your vertices carefullyIf exactly on pixel boundary?Shared vertices?Vertices defining horizontal edge?Consider A-B versus I-H

• Faster Polygon RasterizationHow can we optimize the code?for each scanline edgeCnt = 0; for each pixel on scanline (l to r) if (oldpixel->newpixel crosses edge) edgeCnt ++; // draw the pixel if edgeCnt odd if (edgeCnt % 2) setPixel(pixel);Big cost: testing pixels against each edgeSolution: active edge table (AET)

• Active Edge TableIdea: Edges intersecting a given scanline are likely to intersect the next scanlineThe order of edge intersections doesnt change much from scanline to scanline

• Active Edge TableAlgorithm: scanline from bottom to topSort all edges by their minimum y coordStarting at bottom, add edges with Ymin= 0 to AETFor each scanline:Sort edges in AET by x intersectionWalk from left to right, setting pixels by parity ruleIncrement scanlineRetire edges with Ymax < YAdd edges with Ymin < YRecalculate edge intersections (how?)Stop when Y > Ymax for last edges