59
Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffe COMP30019 Graphics and Interaction Scan Converting Polygons and Lines Adrian Pearce Department of Computer Science and Software Engineering University of Melbourne The University of Melbourne Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionScan Converting Polygons and Lines

COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Adrian Pearce

Department of Computer Science and Software EngineeringUniversity of Melbourne

The University of Melbourne

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 2: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Lecture outline

Introduction

Scan conversion

Scan-line algorithm

Edge coherence

Bresenham’s algorithm

Edge coherence (revisited)

Z buffering

High Performance Visible Surface Detection

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 3: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Scan conversion

How are polygons and lines drawn onto a digital image?

Aim: understanding polygonal filling and line drawingalgorithms.

Reading:I Foley Sections 3.2 Scan converting lines and Section 3.5

Filling polygons.Additional reading:

I 3.3 Scan converting circles.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 4: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Inside or outside a polygon?

Filling polygons requires a way of determining whether a pointis inside or outside a polygon.

See if you can think up a technique for determining whetheryou are inside or outside a two-dimensional polygon from acompletely arbitrary point (imagine you have no way of knowingwhether you are inside or out initially)?

Hint: imagine you are standing at a point q and watchinganother point p that can reside at any vertex of polygon P.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 5: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Filling polygons using winding numbers

Filling polygons requires a way of determining whether a pointis inside or outside a polygon.

One technique is known as winding numbers.

Imagine you are standing at a point q while watching anotherpoint p traverse a polygon P counterclockwise (the point movesfrom vertex to vertex around the perimeter of the polygon).

If q is inside, you would turn a full circle, 2π radians, if q isoutside the sum of your total angular turn will be zero 0.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 6: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The winding number of q with respect to P is the number ofrevolutions point p makes around polygon P: the total signedangular turn divided by 2π.

P

q

For polygon P, exterior points q have winding number 0: a totalangular turn of 0.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 7: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Angle θ can be determined from dot or cross project

vi · vi+1 = |vi ||vi+1|cosθi

andvi × vi+1 = |vi ||vi+1|sinθi

e

vi

Vi+1

P[i+1]

i

P[i]

q

Angle θi is the angle subtended by e from q

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 8: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The odd-even test

The odd-even test—follow an arbitrary ray or along scan lineand count the number of times a boundary is crossed, either anodd or even number of times.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 9: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I Describe each of the two coloured lines and whichdirection they are going in and what happens to their parity.

I notice for the top most line starting outside and skimmingthe top of the polygon, it stays outside because the singlepixel is a special case of a horizontal line (more in nextslides).

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 10: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Selection of inside model

Let’s now compare the winding-number model to the odd-eventest model according to our desidera for issues in numericalmethods (numerical precision and computational complexity).

I Winding numbers work in case when q lies on theperimeter of P, but the odd-even case needs to treat theseas special case (e.g. when horizontal scan line intersects ahorizontal polygonal edge).

I However, winding numbers require floating point numbersthat is the winding angle will not always be 0 or 2πbecause of round-off error.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 11: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

In terms of computational complexity, both algorithms are oforder O(n) in the worst case, where n is the number of polygonvertices for winding numbers of pixels for the odd-even test.

I However, the winding-number algorithm depends criticallyon floating-point computations and trigonometriccomputations, which is means it is significantly slower onstandard hardware (up to 20 times slower according toresearch by Haines in 1992).

I For filling polygons in images, the odd-even test is superioras it only requires a parity bit and can be easilyimplemented by stepping along the scan lines of imagerows and columns.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 12: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Scan-line fillingThe scan-line method is an efficient way of filling-in polygons,based on the odd-even test by crossing segments.

Segments are sorted to allow for an orderly progression thatdetermines whether inside or outside.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 13: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Figure 1.14 (Rowe), using scan line to determine the interiorpoints of a polygon

scan line

A

B C

D

E

F

Question: does scan-line filling work for non-simple andmultiple-boundary polygons?

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 14: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I The first intersection occurs when the scan line crossesedge AB, so the parity becomes odd at that point.

I The second intersection occurs with edge AF , so the paritybecomes even.

I We can therefore say that points along the scan linebetween edges AB and AF are interior, points betweenedges AF and EF are exterior,

I and points between EF and DE are interior.I Scan-line filing does work for both non-simple and multiple

boundary polygons (provided you start scanning fromoutside.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 15: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Basic scan-line algorithm

1. Find intersections of scan lines with edges of polygon(s)2. Sort the intersections by increasing x coordinate3. fill in-between pixel extrema using odd-even parity rule

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 16: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Pixel extrema

(a) (b)

Span extrema Other pixels in the span

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 17: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Special cases

1. Given an intersection with an arbitrary, fractional x value,how do we determine which pixel on either side of theintersection is interior?

2. How do we deal with intersections at integer pixelcoordinates?

3. How do we deal with shared vertices?4. How do we deal with horizontal edges?

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 18: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Special case solutions1. If inside and approaching fraction intersection to the right

round x coordinate down; else if outside round up.2. If leftmost pixel (has integer coordinate) define as interior;

else if right most coordinate (has integer coordinate) defineto be exterior.

3. Count the ymin vertex of an edge in parity but not the ymaxvertex (therefore vertex ymax is only drawn if it is the yminvertex for the adjacent edge)

For example vertex A in 3.14, is counted one in the paritycalculation because it is the ymin vertex for edge FA but theymax vertex for edge AB (Thus, both edges and spans aretreated as intervals that are closed at their minimum value andopen at their maximum value).

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 19: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Horizontal edges

J

A B

C D

E

FG

HI

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 20: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The bucket-sorted edge table right for the polygon on the left(Foley Figures 3.14 and 3.18).

12

10

8

6

4

2

2 4 6 8 10 12 14

Scanline

A B

C

D

E

F

a b dc

64

52

0

x min

FA

9 2 0

11 13 0

CD

9 7

EF DE

11 7

1

2

3

4

5

6

7

8

9

10

11

y co

ordi

nate

3 7

AB BC

5 7

52

− 64

1 m

l

l

l

l

l

l

l

l

l

l

l

l

y max

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 21: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Active edge table for scan line 9 (above) and 10 (below) (FoleyFigures 3.14 and 3.19).

12

10

8

6

4

2

2 4 6 8 10 12 14

Scanline

A B

C

D

E

F

a b dc

AETpointer FA

9 2 0 52

−9 2 11 10 11 1364 0 l

DE CDEF

(a)

(b)

AETpointer

11 12 64 11 13

DE

0 l

CD

ymax1m

x

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 22: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I Need to fill discreet pixel locations, but don’t want to haveto check all edges from all polygons for large scenes -would take too much computationally!

I Therefore sort edge locations based on x and ycoordinates and use a hash-table like approach ofretrieving them, relative to what location you are presentlyscanning (or filling).

I Foley’s Figure 3.18 is shown along side figure 3.14 toindicate the associated bucket-sorted edge table generated(ahead of actual filling operation) for this polygon.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 23: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Scan-line polygon filling algorithm1 Create a data structure for edges, containing the minimum

and maximum y values (ymin and ymax , respectively) foreach edge, and the x value corresponding to ymin, calledxmin.

2 Create an edge table in which the edges are listed, sortedin ascending order by their ymin value. Omit any horizontaledges from the table.

3 Set scan line value yscan to the minimum ymin value for alledges (that is, to the lowest y value reached by thepolygon).

4 Create an active edge table, containing all the edgescurrently intersected by the scan line.

5 Add to the active edge table (and delete from the masteredge table) any edges whose ymin value is equal to thecurrent yscan.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 24: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Scan-line polygon filling algorithm

6 Sort the active edge table in ascending order on x .7 Traverse active edge table, taking pairs of edges and

drawing all pixels between x value for the first edge in apair and the x value in the second edge in the same pair.

8 Move to the next scan line by incrementing yscan by 1.9 Delete any edges from the active edge table if their ymax

value is equal to yscan.10 Determine where scan line intersects edge (see edge

coherence slide in next lecture).11 While either master edge table or the active edge table has

edges, goto step 5.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 25: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I Define a data structure for representing an edge. It mustbe remembered that xmin is not necessarily the minimum xvalue on the edge—it is the x value that corresponds to thepoint with minimum y value.

I The edge table lists edges, as represented by the datastructures defined in step 1. In practice, an array or linkedlist is used for the edge table. (Omitting horizontal edges,like in previous diagram, treat as a special outside case).

I This is easily done by taking the ymin value from the firstentry in the edge table, since the table was sorted on ymin.We begin scanning the polygon at this point (only startscanning at top of polygon).

I Like the original edge table, this second active edge tableis usually implemented as an array or linked list (only needto do this once).

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 26: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I Edges are therefore distributed between active edge tableand master edge table (loops from step 11, so do this atbeginning of each scan line).

I Sorting the edges in the active edge table means they arelisted in the order that they are intersected by the scan line(as it travels left to right).

I Traversing active edge table—For example, with the scanline positioned as shown in Fig 1.14, pixels along this linebetween edges AB and AF would be drawn from the firstpair of edges in the active edge table, then pixels betweenEF and ED. (Note that the active edge table will alwayscontain an even number of edges).

I Move to next scan line.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 27: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I Delete edges from active edge table—Note that we do notdraw the pixel corresponding to the ymax value beforedeleting the edge—this is consistent with the rule ed indetermining the parity of a pixel.

I Determine edge coherenceI Repeat algorithm by going back to step 5 and repeating,

while either master edge table or the active edge table hasedges.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 28: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Edge coherence

I Many edges that intersect scan line i also intersect scanline i + 1

I Can incrementally calculate intersection xi+1 = xi + am ,

where m is the slope of the edgeI Can avoid fractional arithmetic by computing an integer

decision variable and checking only its sign to decide whento increment x (as we will see later in lecture)

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 29: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Scan converting lines

Involves turning on the correct pixels, either in display or in an

image. Assume line segment is described by its start point[x0y0

]and end point

[x1y1

].

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 30: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

First attemptChoose function y = mx + b, and use the following basiciterative algorithm that steps through the range of x values[x0..x1]:

I For each x compute corresponding (real) y value from theline equation.

I Round that y value to the nearest pixel (grid) position and

switch on, or set pixel at that[xy

]position.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 31: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Example: Drawing y = 7x + 2 (Rowe fig 1.4).

(0,40) Increasing x

Increasin

g y

First, the problem that floating point arithmetic is computationallycomplex therefore slow to compute, particularly since thisneeds to be done very frequently, and

Second, the problem of discretisation in determining which pixel toturn on, one or both?

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 32: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The problem: a line with slope |m| > 1 will have gaps in it.

Solution:|m| > 1 draw x as function of y|m| ≤ 1 draw y as function of x

(as above)

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 33: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Refinement

Reduce line generation to one very special case, like 0 ≤ m ≤ 1Handle all other cases as symmetries of the special case, by

I parameterised subroutinesI explicit transformationsI duplicating code

Leads to kind of line generation algorithm where step is always0 or 1.Further refinement: Bresenham’s algorithm and variants—onlyinteger arithmetic, only addition/ subtraction.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 34: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Bresenham’s algorithm

Bresenham’s insight was to step horizontally, accumulatingerror until error gets big enough to force a step upwards.

I For integer coordinates all quantities are ratios with 2 or∆x = x1 − x0 in the denominator, multiplying everything by2∆x , makes everything integer.

Essential insight:I For integer coordinates, all quantities are ratios with 2 or

∆x = x1 − x0 in the denominator.I Multiplying everything by 2∆x , makes everything integer.

Step horizontally, accumulating error, until error gets bigenough to force a step upwards.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 35: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

I At each step, choose between two pixels based on the signof the decision variable calculated in the previous iteration:

I then it updates the decision variable by adding the error tothe old value, depending on the choice of pixel.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 36: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The recursive problem

Assuming starting point (x0, y0) is provided, the recursiveproblem is given (xk , yk ) to find y value for subscript positionxk + 1,

y = m(xk + 1) + b

y is either yk or yk + 1, assuming that algorithm is drawing linefrom left to right as in first case (positive slope < 1).

I Initially, y = mxk + b, next row we getI y = m(xk + 1) + b andI either y = y + k or y = yk + 1.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 37: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

d2

d1

yk + 1

yk

(xk, yk)

d1 = y − yk = m(xk + 1) + b − yk

d2 = yk + 1− y = yk + 1−m(xk + 1)− bd1 − d2 = 2m(xk + 1) + 2b − 2yk − 1

The sign of the difference d1 − d2 determines which pixel tochoose: if difference is negative then yk is closer to actual line(y ), if it is positive yk + 1 is closer.How are non-integer calculations eliminated?

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 38: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Bresenham requires that the endpoints of line segments(x0, y0) and (xn, yn) are provided, therefore the slope m can beexpressed as a ratio of integers, m = ∆y

∆x , substitution gives

d1 − d2 = 2m(xk + 1) + 2b − 2yk − 1∆x(d1 − d2) = 2∆yxk − 2∆xyk + [2∆y + ∆x(2b − 1)]

As ∆x is always 1 and [2∆y + ∆x(2b− 1)] does not depend onpixel index k , we can write

C = 2∆y + ∆x(2b − 1)

. The pixel choice parameter pk can now be defined as

pk = ∆x(d1 − d2) = 2∆yxk − 2∆xyk + C

If pk is positive, we choose yk + 1; if negative, yk .Now how do we get rid of C?

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 39: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The answer is to use recursion for calculating a series of pkvalues, the pixel choice at the next location is determined bypk+1, and C can be eliminated by subtraction, as follows

pk = ∆x(d1 − d2) = 2∆yxk − 2∆xyk + Cpk+1 = 2∆yxk+1 − 2∆xyk+1 + C

pk+1 − pk = 2∆y(xk+1 − xk )− 2∆x(yk+1 − yk )

and since algorithm steps along x axis, xk+1 = x + k + 1, canrecursively define a relationship for choosing pixels as

pk+1 = pk + 2∆y − 2∆x(yk+1 − yk )

Note that all quantities in this equation are integers.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 40: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

For initialisation, a starting value for the first pixel, p0, isrequired, using equation (see earlier)

∆x(d1 − d2) = 2∆yxk − 2∆xyk + [2∆y + ∆x(2b − 1)]

and using starting point of the line (x0, yo) and substituting thefollowing for b

b = y0 −mx0 = y0 − (∆y/∆x)x0

we obtainp0 = 2∆y −∆x

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 41: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Bresenham’s algorithm

1. Provide endpoints (x0, y0) and (xn, yn) of line.2. Calculate constants 2∆y and 2∆y − 2∆x , where

∆y = yn − y0 and ∆x = xn − x0.3. Plot the origin (x0, y0).4. Calculate the pixel choice parameter pk , and

I if pk < 0, plot (xk + 1, yk ) and

pk+1 = pk + 2∆y

I otherwise plot (xk + 1, yk + 1) and

pk+1 = pk + 2∆y − 2∆x

5. Repeat step 4, until xn is reached.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 42: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Implementation of Bresenham’s algorithm

Implementation of Bresenham’s algorithm for drawing linesbased on mathematical equation y = mx + b for either

(i) positive slope < 1 (only this case covered here)(ii) negative slope > −1, or(iii) positive slope ≥ 1.For the discrete case, subscript k is added to pixel xk , theequation for a line then becomes

y = mxk + b

however, in general y will not be an integer.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 43: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

/ / Bresenham ’ s a lgo r i t hm f o r l i n e drawingvoid drawBresenhamLine ( Graphics g ,

Po in t s t a r t , Po in t end ) {/ / Implements case f o r s lope < 1i f ( m_slopeLT1 ) {

x = m_x0 + 1; y = m_y0 ;while ( x < m_x1) {

i f (m_param < 0) {m_param += m_twoDeltaY ;

} else {m_param += m_twoDYDX;y += m_slopeSign ;

}drawPixel ( g , x , y ) ;x++;

}Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 44: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Edge coherence (revisited)

In principle, finding the intersection of the point between a scanline and a polygon edge just involves substituting y = yscan intoequation for a line and solving for s. However, it suffers fromsame problem as drawing lines, in that it involves floating pointarithmetic.

It turns out that there is a more efficient algorithm which allowsus to calculate the x value on a scan line directly from the xvalue on the previous scan line (like Bresenham’s algorithm),based on the edge coherence property of a polygon.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 45: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The idea behind coherence is that most geometric figures haveregions where properties are relatively consistent.

I In edge coherence, this means that non-horizontal edge ina polygon will be intersected by more than one scan lineand therefore should be able to generate intersectionpoints iteratively, rather than using the line equation ateach step.

I Suggests a recursive algorithm closely related toBresenham’s line drawing algorithm.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 46: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Edge coherence algorithm1. When a scan line is added to the active edge table,

initialise x to xmin and the increment to 0. Calculate andstore ∆x and ∆y .

2. When a scan line is increased by 1, add ∆x to theincrement.

3. If the magnitude of the slope is greater than or equal to 1,compare the magnitude of the increment with ∆y . If it isless than ∆y , leave x as it is. Otherwise, increase x by 1,and subtract (add) ∆y to the increment if the increment ispositive (negative).

4. If magnitude of slope is less than 1, determine number ofpixels to increase x by dividing magnitude of increment by∆y (ignoring remainder). The remainder (positive ifincrement is positive, and negative if it is negative)becomes new value of the increment.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 47: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Z Buffer

As well as pixel frame buffer, keep a parallel Z buffer, which ateach point records the depth of corresponding pixel.

The Z buffer is initialised to some representation of “infinite”depth.

The frame buffer initialised to “background” colour then pixelwrites take pixel value, x , y , and Z . Updates only take place at(x , y) only if new depth is closer.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 48: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Z Buffer

Depth must be in camera coordinates, any order-preservingfunction of true depth will do (pseudo-depth is OK).

Z buffer must have sufficient bits to resolve depth finely enoughand may use some bits of frame buffer, if enough bits areavailable, and display lookup tables can be manipulated.

Can use buffer with higher spatial resolution, and block-averagedown for antialiasing.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 49: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

The z-buffer drawing (a) first and (b) second polygons

+ =

0

0

0

0

0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

50

0

0

0

0

0 0 0 0 0 0 00

0 0 0 0 0 0 00

0 0 0 0 0 0 00

0

0 0

0 0 0

0 0 0 0

0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0 0

0 0 0 0 0 0 00

5 5 5 5 5 5

5 5 5 5 5 5

5 5 5 5 5

5 5 5 5

5 5 5

5 5

5

+ =

0

0 0

0 0 0

0 0 0 0

0 0 0 0 0

3

4

5

6

7

8 7 6 5 4 3

6 5 4 3

5 4 3

4 3

3

5

5

5

5

5

0 0 0 0 0 05

0 0 0 0 0 0 05

0 0 0 0 0 0 00

0

0 0

0 0 0

0 0 0 0

0 0 0 0

0 0 0

0 0

0 0 0 0 0 0 00

5 5 5 5 5 5

5 5 5 5 5

5 5 5 5

5 5 5

5 5

5

5

6

7

8 7 6 5 4 3

6 5 4 3

5 35

5 5 5

5

5

5

5

5

5

5 5

5 5

5 5 5 5 5

5 5

5

5 5 5 5 5 5 5

5 5 5 5 5 5

5 5 5 5 5

5 5 5 5

5 5 5

5 5

5

(a)

(b)

I Assume looking down the z axis.I Depends on spatial resolution (must have sufficient bits to

resolve depth finely enough).Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 50: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Interpolation of z values along edges for scan lines

y1

y2

y3

ys

y

za

z2

z1

z3

zb Scan linezp

za = z1 − (z1 − z2) ––––––y1 − ysy1 − y2

zb = z1 − (z1 − z3) ––––––

zp = zb − (zb − za) ––––––

y1 − ysy1 − y3

xb − xpxb − xa

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 51: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Pros and cons of the z buffer

+ simple to implement.+ implementable in hardware (generally is).+ works for all kinds of objects (not just polyhedra).- extra storage needed for Z buffer.- many accesses to Z buffer and frame buffer—bad for

display through a communications link.- limited to space and depth resolution of buffers.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 52: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Properties of Z-buffers

I for each polygon or object in scene (piecewise Z buffering).I (z-buffer) for each scan line (single-scan-line Z bufferingI If shading computation is time-consuming, a rough front-to

back depth sort of objects to display closest one firstimproves efficiency (can use radix sort for this, atresolution of z-buffer).

I Don’t calculate shading for hidden surfaces — important ifusing Phong illumination model as we will see later.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 53: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Rendering pipeline for z-buffer (revisited)

db traversal

DisplayRasterizationClipping

Modelingtransformation

Trivialaccept/reject Lighting Viewing

transformation

Divide by W,map to

3D viewport

(Foley Figure 14.41)

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 54: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Rendering pipeline for z-buffer - the stepsSee page 521 of Foley:

I db traversal & modelling transformation transform all(relatively defined) polygons or polygonal meshes to theircorrect location.

I Trivial accept/reject: Entirely outside & back face cullingI Lighting: calculate intensity (for vertices only: need to do

in 3D before perspective projection)I Viewing transformation: perspective transformation (e.g.

perspective foreshortening).I Clipping: clip to viewport (involves creation of new

vertices on border).I Map to viewport: Change of coordinate systems (Divide

by W: based on homogeneous coordinate system).I Rasterisation: scan-line drawing polygons (including

z-buffering & shading interpolation)Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 55: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Rendering pipeline for z-buffer and Phong shading

db traversal

Rasterization(includinglighting)

Display

ClippingViewingtransformation

Trivialaccept/reject

Modelingtransformation

Divide by W,map to

3D viewport

Using the Phong illumination model, lighting cannot becalculated early in the pipeline why?(Foley Figure 14.42),

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 56: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Rendering pipeline for z-buffer and Phong shading

See pages 522-523 of Foley:I Viewing transformation: Need to calculate lighting after

viewing transformation, why? because Phong illuminationis dependent on viewer orientation.

I Clipping: because Phong shading interpolates surfacenormals, rather than intensities, the vertices cannot be litearly in the pipeline

I Rasterisation: lighting equation must be calculated forevery pixel.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 57: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

High performance visible surface determination

Many methods and variations of visible surface determinationare possible, which performs best depends on hardwareavailable, and nature of graphics drawn, E.g.

I for a scene made of many small polygons, Z bufferingmight be best,

I for a scene made of a few large polygons, binary spacepartition techniques (BSPs) might be better.

The Z-buffer idea can also be used for a variety of tasks, suchas

I piecewise Z buffering (for example polygon-by polygon), orI single-scan-line Z buffering (pixel-by-pixel).

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 58: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Summary

I Polygons can be filled using a scan-line approach basedon the odd-even to determine whether inside or outside.

I Efficient scan-line algorithms rely on bucket (hash) sortingedges and maintaining active-edge tables. This allows onlyedges of relevant polygons to be checked.

I Efficient line drawing algorithms typically utilise the edgecoherence property to calculate the value of the pixel onthe current scan line from the value on previous scan line.

I Bresenham’s insight was how to rely only on integeroperations, thus avoiding floating point arithmetic.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines

Page 59: COMP30019 Graphics and Interaction Scan Converting ... · Filling polygons requires a way of determining whether a point is inside or outside a polygon. One technique is known as

Introduction Scan conversion Scan-line algorithm Edge coherence Bresenham’s algorithm Edge coherence (revisited) Z buffering High Performance Visible Surface Detection

Summary

I Edge-coherence properties can be utilised in polygonfilling algorithms, to decide whether x values in theactive-edge table need incrementing (Can use sameapproach for drawing circles and ellipses)

I The Rendering pipeline, based on z-buffering, is the basisof Hidden surface removal OpenGL.

I Various techniques exist for high performance visiblesurface detection, including binary space partition (BSP)and piecewise (polygon-by-polygon) Z buffering andsingle-scane-line (pixel-by-pixel) buffering.

Adrian Pearce University of Melbourne

COMP30019 Graphics and InteractionScan Converting Polygons and Lines