1 / 33

CS 445 / 645: Introductory Computer Graphics

CS 445 / 645: Introductory Computer Graphics. Polygon Rasterization and Clipping. Assignment 1. Grader has released a version of the code that you can review Go to class web page Click on Assignment 1 Find link to sample code. Triangle Rasterization Issues.

ervin
Download Presentation

CS 445 / 645: Introductory Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 445 / 645: Introductory Computer Graphics Polygon Rasterization and Clipping

  2. Assignment 1 • Grader has released a version of the code that you can review • Go to class web page • Click on Assignment 1 • Find link to sample code

  3. 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 • Example: draw pixels on left or top edge, but not on right or bottom edge

  4. Triangle Rasterization Issues • Sliver

  5. Triangle Rasterization Issues • Moving Slivers

  6. Triangle Rasterization Issues • Shared Edge Ordering

  7. General Polygon Rasterization • Now that we can rasterize triangles, what about general polygons? • We’ll take an edge-walking approach

  8. 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

  9. Polygon Rasterization • Inside-Outside Points

  10. Polygon Rasterization • Inside-Outside Points

  11. 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);

  12. General Polygon Rasterization • Count your vertices carefully • Details… G F I H E C J D A B

  13. 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)

  14. Active Edge Table • Idea: • Edges intersecting a given scanline are likely to intersect the next scanline • The order of edge intersections doesn’t change much from scanline to scanline

  15. Active Edge Table • Algorithm: scanline from bottom to top… • Go over example… • 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

  16. Next Topic: Clipping • We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport • In general, this assumption will not hold:

  17. Clipping • Analytically calculating the portions of primitives within the viewport

  18. Why Clip? • Bad idea to rasterize outside of framebuffer bounds • Also, don’t waste time scan converting pixels outside window

  19. Clipping • The naïve approach to clipping lines: for each line segment for each edge of viewport find intersection points pick “nearest” point if anything is left, draw it • What do we mean by “nearest”? • How can we optimize this? B D C A

  20. Trivial Accepts • Big optimization: trivial accept/rejects • How can we quickly determine whether a line segment is entirely inside the viewport? • A: test both endpoints.

  21. Trivial Rejects • How can we know a line is outside viewport? • A: if both endpoints on wrong side of same edge, can trivially reject line

  22. Clipping Lines To Viewport • Discard segments of lines outside viewport • Trivially accept lines with both endpoints inside all edges of the viewport • Trivially reject lines with both endpoints outside the same edge of the viewport • Otherwise, reduce to trivial casesby splitting into two segments

  23. Cohen-Sutherland Line Clipping • Divide viewplane into regions defined by viewport edges • Assign each region a 4-bit outcode: 1001 1000 1010 0001 0000 0010 0101 0100 0110

  24. Cohen-Sutherland Line Clipping • Assign an outcode to each vertex of line • If both outcodes = 0, trivial accept • bitwise AND vertex outcodes together • If result  0, trivial reject

  25. Cohen-Sutherland Line Clipping • If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded • Pick an edge that the line crosses (how?) • Intersect line with edge (how?) • Discard portion on wrong side of edge and assign outcode to new vertex • Apply trivial accept/reject tests; repeat if necessary

  26. Cohen-Sutherland Line Clipping • If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded • Pick an edge that the line crosses • Check against edges in same order each time • For example: top, bottom, right, left E D C B A

  27. E D C B A Cohen-Sutherland Line Clipping • Intersect line with edge (how?)

  28. Cohen-Sutherland Line Clipping • Discard portion on wrong side of edge and assign outcode to new vertex • Apply trivial accept/reject tests and repeat if necessary D C B A

  29. Solving Simultaneous Equations • Equation of a line • Slope-intercept (explicit equation): y = mx + b • Implicit Equation: Ax + By + C = 0 • Parametric Equation: Line defined by two points, P0 and P1 • P(t) = P0 + (P1 - P0) t, where P is a vector [x, y]T • x(t) = x0 + (x1 - x0) t • y(t) = x0 + (y1 - y0) t

  30. Parametric Line Equation • Describes a finite line • Works with vertical lines (like the viewport) • 0 <=t <= 1 • Defines line between P0 and P1 • t < 0 • Defines line before P0 • t > 1 • Defines line after P1

  31. Parametric Lines and Clipping • Define each line in parametric form: • P0(t)…Pn-1(t) • Define each edge of viewport in parametric form: • PL(t), PR(t), PT(t), PB(t) • When using Cohen-Sutherland perform intersection calculation for appropriate viewport edge and line

  32. Computing Intersections • Line 0: • x0 = x00 + (x01 - x00) t0 • y0 = y00 + (y01 - y00) t0 • Viewport Edge L: • xL = xL0 + (xL1 - xL0) tL • yL = yL0 + (yL1 - yL0) tL • x00 + (x01 - x00) t0 = xL0 + (xL1 - xL0) tL • y00 + (y01 - y00) t0 = yL0 + (yL1 - yL0) tL • Solve for t0 and tL • Truth in advertising… Cohen-Sutherland doesn’t exactly do it this way. You don’t need to solve for t0 and tL

  33. Cohen-Sutherland Line Clipping • Outcode tests and line-edge intersects are quite fast • But some lines require multiple iterations • Fundamentally more efficient algorithms: • Cyrus-Beck uses parametric lines to clip against arbitrary polygons (not just rectangles) • Liang-Barsky optimizes this for upright volumes

More Related