Today - PowerPoint PPT Presentation

About This Presentation
Title:

Today

Description:

Today s class Drawing lines Bresenham s algorithm Compositing Polygon filling Drawing lines Plot line from (x1, y1) to (x2, y2) y = mx + b m = (y2 - y1) / (x2 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 28
Provided by: CaryL7
Category:

less

Transcript and Presenter's Notes

Title: Today


1
Todays class
  • Drawing lines
  • Bresenhams algorithm
  • Compositing
  • Polygon filling

2
Drawing lines
  • Plot line from (x1, y1) to (x2, y2)
  • y mx b
  • m (y2 - y1) / (x2 - x1)
  • b y1 - mx1
  • Straight-forward approach
  • for x x1 to x2 do
  • y m x b
  • WritePixel (x, round(y))

3
Digital differential analyzer
  • Straight-forward approach involves one
    multiplication and one addition per iteration of
    the loop
  • Recognize ?y m?x
  • Choose ?x to be 1 ?y m yi1 yi m
  • Digital differential analyzer (DDA)
  • y y1
  • for x x1 to x2 do
  • WritePixel (x, round(y))
  • y y m
  • Have removed the multiplication from the loop

4
Effect of slope
  • The DDA algorithm shown assumes left-to-right
    processing (i.e., x1 lt x2) and slope lt 1
  • For slope gt 1 reverse x and y roles
  • step along y
  • change x by 1 / m

5
Remaining problems
  • There are still two problems that remain to be
    removed
  • one real addition per iteration of the loop
  • one rounding per iteration of the loop
  • Would like to get the algorithm down to only
    integer operations
  • Bresenhams algorithm will do this

6
Bresenhams algorithm
  • An algorithm for drawing lines with only integer
    operations
  • Consider the case where 0 ? slope ? 1
  • Suppose (xi, yi) is plotted
  • Which y value do you plot at xi1? Is it yi or
    yi1?
  • The true value is y

7
Error terms
  • Let e1 yi1 - y yi1 - (m(xi1) b)
  • Let e2 y - yi m(xi1) b - yi
  • e1-e2 2yi - 2m(xi1) - 2b 1
  • 2yi - 2(?y/?x)(xi1) - 2b 1
  • If this difference is negative, e2 gt e1 so yi 1
    is closer to true value than yi

8
Definition of pi
  • Multiply through by ?x to get rid of fraction
  • pi ?x(e1-e2)
  • 2yi?x - 2?y(xi1) - 2b?x ?x
  • 2?xyi - 2?yxi - 2?y - ?x(2b-1)
  • pi1 2?xyi1 - 2?yxi1 - 2?y - ?x(2b-1)

9
Updating equation for pi
  • pi1-pi
  • 2?xyi1 - 2?xyi - 2?yxi1 2?yxi
  • 2?x(yi1-yi) - 2?y(xi1-xi)
  • 2?x(yi1-yi) - 2?y(xi1-xi)
  • 2?x(yi1-yi) - 2?y
  • pi1 pi 2?x(yi1-yi) - 2?y

10
Handling the yi1-yi term
  • yi1-yi 0 or 1, depending on whether we move up
    a scan line or not
  • If pi is positive (i.e., we dont move up, so
    yi1yi and yi1-yi 0), then pi1 pi - 2?y
  • If pi is negative (i.e., we move up, so yi1yi1
    and yi1-yi 1), then pi1 pi 2?x - 2?y

11
Only integer operations
  • Note that ?x and ?y are both integers, are
    constant for a line segment, and can be computed
    before entering the loop
  • The drawing loop now only contains integer
    addition operations

12
p1
  • Need a value for p1 to get started with
  • Use (x1, y1) in the equation for pi
  • p12?xy1 - 2?yx1 - 2?y - ?x(2b-1)
  • 2?xy1 - 2?yx1 - 2?y - ?x(2y1-2(?y/?x)x1-1)
  • 2?xy1 - 2?yx1 - 2?y - 2?xy1 2?yx1 ?x
  • ?x - 2?y

13
Other cases
  • How would you handle each of the following cases
    with Bresenhams algorithm?
  • -1 ? slope ? 0
  • slope gt 1
  • slope lt -1

14
Circles
  • Cartesian coordinates
  • Polar coordinates

15
A Bresenham algorithm for circles
  • Assume center of circle is (0, 0)
  • Recognize symmetry of circle (only need to
    consider 1/8 of circle)
  • Choose region to consider as (0, r) to (r?2, r?2)
    along circumference
  • slope of circle is between 0 and -1
  • can do work stepping along x-axis

16
The error terms for circles
  • Suppose (xi, yi) is plotted
  • Next point is either (xi1, yi) or (xi1, yi-1)
  • Compute two error terms, using y2 instead of y
  • let e1 be error for point above circle
  • let e2 be error for point below circle

17
pi for circles
  • Define pi as e1 - e2
  • If pi lt 0 then (xi1, yi) is plotted, otherwise
    xi1, yi-1) is plotted
  • pi 2(xi1)2 yi2 (yi-1)2 - 2r2
  • pi1 pi 4xi 6 2(yi12 - yi2) - 2(yi1 -
    yi)
  • If pilt0 then pi1 pi 4xi 6 else pi1 pi
    4(xi - yi) 10
  • p1 3 - 2r (at (0, r))

18
Compositing
  • Blending together of values from different
    objects at the same pixel
  • This is the use of the alpha channel (the A in
    RGBA)
  • Like R, G, and B, A is defined over 0.0, 1.0
  • A 1.0 implies a completely opaque surface,
    while A 0.0 implies a completely transparent
    surface

19
How to do compositing
  • When an object is rendered into the frame buffer,
    both the objects pixel and the current pixel
    value in the frame buffer are scaled by
    appropriate constants and the products added
    together
  • There are 4 scale factors for the objects pixel,
    1 each for RGBA, and 4 for the buffers pixel

20
Blending in OpenGL
  • glBlendFunc (sfactor, dfactor)
  • The sfactor tells how to scale the object being
    processed
  • The dfactor tells how to scale whats currently
    in the buffer
  • The table in the OpenGL Reference Manual for
    glBlendFunc gives the symbolic constants you can
    use for sfactor and dfactor
  • Note that you need to glEnable (GL_BLEND)

21
Example program
  • alpha.c is an example using blending
  • Can you derive the colors that should be
    displayed in each quadrant?

22
Scan line approach to filling a polygon
  • For each scan line in range of polygon
  • find intersections of scan line with all edges of
    polygon
  • sort intersections in increasing x
  • fill in all pixels between pairs of intersections
    that lie interior to polygon, using the
    odd-parity rule to determine that a point is
    inside
  • parity is initially even
  • each intersection encountered inverts the parity
  • draw when parity is odd

23
Edge coherence
  • To find intersections of each scan line with the
    edges of the polygon we take advantage of edge
    coherence - many of the edges that intersect scan
    line i will also intersect scan line i1
  • Will also take advantage of the incremental
    changes in x and y so we dont have to solve
    equations to find intersections

24
Global edge table
  • Contains each edge of the polygon, sorted by
    their smaller y coordinate
  • Use buckets for each scan line, and store edges
    in increasing x coordinate order
  • Each edge contains ymax, xmin, dy, dx, and
    x-increment in going from one scan line to the
    next

25
Active edge table
  • For each scan line we construct the active edge
    table, where the set of edges a scan line
    intersects is kept sorted on the x intersection
    values
  • When we move to the next scan line, the active
    edge table is updated

26
The complete algorithm
  • Set y to the smallest y coordinate that has an
    entry in the ET
  • Initialize the AET to be empty
  • Repeat until AET and ET are empty
  • Move from ET bucket y to the AET those edges
    whose ymin y (entering edges), then sort the
    AET on x (made easier because the ET is
    presorted)
  • Fill in desired pixel values on scan line y by
    using pairs of x coordinates from AET
  • Remove from AET those entries for which y ymax
    (edges not involved in next scan line)
  • Increment y by 1 (go to next scan line)
  • For each edge remaining in AET, update x for the
    new y

27
Patterns
  • Define a pattern in a small matrix
  • Fix a reference point (usually lower left corner)
  • As you fill an area, pick the corresponding pixel
    from the pattern matrix
  • The pattern usually repeats, so use a mod
    operation to get the repeating effect
Write a Comment
User Comments (0)
About PowerShow.com