Docstoc

lecture07

Document Sample
lecture07 Powered By Docstoc
					         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
   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
Triangle Rasterization Issues
   Sliver
Triangle Rasterization Issues
   Moving Slivers
Triangle Rasterization Issues
   Shared Edge Ordering
General Polygon Rasterization
   Now that we can rasterize triangles, what
    about general polygons?
   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 scanline is inside or outside the polygon?
Polygon Rasterization
   Inside-Outside Points
Polygon Rasterization
   Inside-Outside Points
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);
General Polygon Rasterization
   Count your vertices carefully
    – Details…
                             G       F


                     I       H
                                             E
                             C
                     J                   D
                         A       B
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)
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
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
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:
Clipping
   Analytically calculating the portions of
    primitives within the viewport
Why Clip?
   Bad idea to rasterize outside of framebuffer
    bounds
   Also, don’t waste time scan converting pixels
    outside window
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”?
                                                B
   How can we optimize this?
                                            D
                                     C
                               A
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.
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
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 cases by splitting into two
      segments
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
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
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
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       D   E
                                                   C

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

                 B
         A
Cohen-Sutherland Line Clipping
   Discard portion on wrong side of edge and assign
    outcode to new vertex
                                             D
                                        C

                           B
                 A




   Apply trivial accept/reject tests and repeat if
    necessary
    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
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
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
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
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

				
DOCUMENT INFO