170 likes | 312 Views
Color. CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003. Admin. Drop deadline imminent!. Recap: Rasterizing Triangles. Edge walking: Use DDA approach to “walk” down edges of triangle At each scanline, walk the span from one edge to another Edge equations:
E N D
Color CS 445/645Introduction to Computer Graphics David Luebke, Spring 2003 David Luebke 18/20/2014
Admin • Drop deadline imminent! David Luebke 28/20/2014
Recap: Rasterizing Triangles • Edge walking: • Use DDA approach to “walk” down edges of triangle • At each scanline, walk the span from one edge to another • Edge equations: • Compute bounding box • Edge equations: compute from vertices (numerical issues!) • Orientation: ensure area is positive David Luebke 38/20/2014
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); } } David Luebke 48/20/2014
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) • Early termination (why does this work?) • Faster test of equation values David Luebke 58/20/2014
Edge Equations: Interpolating Color • Given colors (and later, other parameters) at the vertices, how to interpolate across? • Idea: triangles are planar in any space: • This is the “redness” parameter space • Note:plane follows formz = Ax + By + C • Look familiar? David Luebke 68/20/2014
Edge Equations: Interpolating Color • Given redness at the 3 vertices, set up the linear system of equations: • The solution works out to: David Luebke 78/20/2014
Edge Equations:Interpolating Color • Notice that the columns in the matrix are exactly the coefficients of the edge equations! • So the setup cost per parameter is basically a matrix multiply • Per-pixel cost (the inner loop) cost equates to tracking another edge equation value (which is?) • A: 1 add David Luebke 88/20/2014
Triangle Rasterization Issues • Exactly which pixels should be lit? • A: Those pixels inside the triangle edges • What about pixels exactly on the edge?(Ex.) • Draw them: order of triangles matters (it shouldn’t) • Don’t draw them: gaps possible between triangles • We need a consistent (if arbitrary) rule • Example: draw pixels on left or top edge, but not on right or bottom edge David Luebke 98/20/2014
General Polygon Rasterization • Now that we can rasterize triangles, what about general polygons? • We’ll not take an edge-equations approach (why?) David Luebke 108/20/2014
General Polygon Rasterization • Consider the following polygon: • How do we know whether a given pixel on the scanline is inside or outside the polygon? D B C A E F David Luebke 118/20/2014
General Polygon Rasterization • Does it still work? D B H C A G I E F David Luebke 128/20/2014
General Polygon Rasterization • Basic 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); • Why does this work? • What assumptions are we making? David Luebke 138/20/2014
Faster Polygon Rasterization • How 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 edge • Solution: active edge table (AET) David Luebke 148/20/2014
Active Edge Table • Idea: • Edges intersecting a given scanline are likely to intersect the next scanline • Within a scanline, the order of edge intersections doesn’t change much from scanline to scanline David Luebke 158/20/2014
Active Edge Table • Algorithm: • 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 and resort (how?) • Stop when Y > Ymax for last edges David Luebke 168/20/2014
Color • Next topic: Color To understand how to make realistic images, we need a basic understanding of the physics and physiology of vision. Here we step away from the code and math for a bit to talk about basic principles. David Luebke 178/20/2014