algorithm and scanline algorithm -...

16
BRESENHAM’S ALGORITHM AND SCANLINE ALGORITHM Lecture 5 – Supp Notes Comp3080 Computer Graphics HKBU

Upload: truonghuong

Post on 20-Jul-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

BRESENHAM’S

ALGORITHM AND

SCANLINE

ALGORITHM Lecture 5 – Supp Notes

Comp3080 Computer Graphics

HKBU

Page 2: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

BRESENHAM’S ALGORITHM

COMP3080 COMPUTER GRAPHICS , HKBU

DDA requires one floating

point addition per step

We can eliminate all fp through

Bresenham’s algorithm

Consider only 1 m 0

Other cases by symmetry

Assume pixel centers are at half

integers

If we start at a pixel that has

been written, there are only

two candidates for the next

pixel to be written into the

frame buffer

OCTOBER 24 , 2011 2

Page 3: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

CANDIDATE PIXELS

1 m 0

COMP3080 COMPUTER GRAPHICS , HKBU

last pixel

candidates

Note that line could have

passed through any

part of this pixel

OCTOBER 24 , 2011 3

Page 4: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

DECISION VARIABLE

COMP3080 COMPUTER GRAPHICS , HKBU

d = Δx(a - b)

d is an integer

d < 0 use upper pixel

d > 0 use lower pixel

OCTOBER 24 , 2011 4

where Δx and mΔx are integers. Notice Δy = mΔx

Page 5: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

INCREMENTAL FORM

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU 5

ak+1 = ak – m bk+1 = bk + m

ak+1 = ak + (1 – m) bk+1 = bk – (1 – m)

Page 6: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

INCREMENTAL FORM

More efficient if we look at dk, the value of the decision variable

at x = k

For each x, we need do only an integer addition and a test

Single instruction on graphics chips

COMP3080 COMPUTER GRAPHICS , HKBU

dk+1 = dk – 2 Δy, if dk < 0 dk+1 = dk – 2(Δy - Δx), otherwise

OCTOBER 24 , 2011 6

Page 7: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

BRESENHAM’S ALGORITHM

AN EXAMPLE

7

4 5 6 7 8 9

7

8

9

10

11

12

22

3

4

)11,9(),(

)8,5(),(

0

44

00

yxd

y

x

yx

yx

upper is choice first

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 8: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

8

BRESENHAM’S ALGORITHM

AN EXAMPLE

4 5 6 7 8 9

7

8

9

10

11

12

lower is choice second

0)1(22

)(201

xydd

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 9: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

9

BRESENHAM’S ALGORITHM

AN EXAMPLE

4 5 6 7 8 9

7

8

9

10

11

12

upper is choice third

660212

ydd

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 10: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

SCAN LINE POLYGON FILL:

THE ALGORITHM

10

Set y to smallest y coordinate that

has an entry in ET

Initialize the active edge table (AET)

to empty

Repeat until the ET and AET are

both empty:

Move from ET bucket at y to

AET those edges whose ymin

= y. (entering edges)

Remove from AET those edges

for which y = ymax.

Sort AET on x

Fill in desired pixels on scan

line using even-odd parity from

AET

Increment y by one (next scan

line)

For each edge in the AET,

update x by adding Δx/Δy

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 11: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

11

EXAMPLE

Given a sequence of ordered vertices

(10,10) (10,16) (16,20) (28,10) (28,16) (22,10)

(10,10)

(10,16)

(16,20)

(22,10) (28,10)

(28,16)

Edge 1

Edge 2

Edge 3

Edge 4

Edge 5

Edge 6

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 12: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

12

EDGE TABLE VS GLOBAL EDGE TABLE (GET)

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html

OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 13: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

13

GLOBAL EDGE TABLE VS ACTIVE EDGE TABLE

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 14: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

14

FILLING THE

POLYGON

Ymin = 10

Result

Sorted by x-val

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 15: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

15

FILLING THE POLYGON – 2ND SCANLINE TO SCANLINE 15

Y-max reaches the scanline

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU

Page 16: ALGORITHM AND SCANLINE ALGORITHM - comp.hkbu.edu.hkcomp3080/2011/wp-content/uploads/2011/10/Comp... · ALGORITHM AND SCANLINE ALGORITHM Lecture 5 ... Move from ET bucket at y to

16

FILLING THE POLYGON – CON’T

Combine

reorder

Final Result

Extracted from :http://www.cs.rit.edu/~icss571/filling/example.html OCTOBER 24 , 2011 COMP3080 COMPUTER GRAPHICS , HKBU