1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.

Post on 19-Dec-2015

228 views 1 download

Tags:

Transcript of 1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.

1

CSCE 441: Computer GraphicsScan Conversion of Polygons

Jinxiang Chai

2/95

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

3/95

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

4/95

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

5/95

Drawing General Polygons

How to draw every interior pixels?

?

6/95

Drawing Curved Objects

How to draw every interior pixels?

?

7/95

Outline

Methods for drawing polygons or curved objects

- Scanline conversion of polygons

- Boundary-fill

- Flood-fill

Required readings:

- HB 4-9 to 4-14

8/95

Drawing Rectangles

Which pixels should be filled?

9/95

Drawing Rectangles

Is this correct?

10/95

Drawing Rectangles

What if two rectangles overlap?

11/95

Drawing Rectangles

Is this correct?

12/95

Drawing Rectangles

Is this correct? Overlap!!!

13/95

Drawing Rectangles

Solution: Exclude pixels on top and right

14/95

Drawing Rectangles

Artifacts are possible

15/95

General Polygons – Basic Idea

How to draw every interior pixels?

16/95

General Polygons – Basic Idea

How to draw every interior pixels for a scan line?

17/95

General Polygons – Basic Idea

Intersect scan lines with edges Find ranges along x Fill interior of those

ranges

Don’t fill top/right

Edges may NOT match

with line drawing algo

18/95

General Polygons – Basic Idea

Intersect scan lines with edges Find ranges along x Fill interior of those

ranges

Don’t fill top/right

Edges may NOT match

with line drawing algo

Use coherence to

speed up

19/95

General Polygons – Basic Idea

Intersect scan lines with edges Find ranges along x Fill interior of those

ranges

Use coherence to

speed up

Edges intersecting one scan line are

mostly same as those intersecting

previous scan line

20/95

General Polygons – Basic Idea

Intersect scan lines with edges Find ranges along x Fill interior of those

ranges

Use coherence to

speed up

The x-value of an intersection with

one scan line is close to the

intersection with the previous one

21/95

How to store polygon edges?

22/95

General Polygons – Data Structures

Sorted Edge Table:

01234567

Sca

n L

ines

Edges

Store a linked-list per scan-line.

Insert edges into table at scan-

line associated with lowest end-

point.

23/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

Sorted Edge Table

24/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

Sorted Edge Table

25/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

Sorted Edge Table

26/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

Sorted Edge Table

Exclude horizontal edges!

27/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

Sorted Edge Table

28/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

Sorted Edge Table

29/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

30/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Which edges are intersecting with the current scan

line?

31/95

General Polygons – Data Structures

Active Edge List:

Edges

List of all edges intersecting

current scan-line sorted by their

x-values

32/95

General Polygons – Data Structures

Active Edge List:

Edges

List of all edges intersecting

current scan-line sorted by their

x-values

Implement active edge list with linked list

33/95

General Polygons – Data Structures

Active Edge List:

Edges

List of all edges intersecting

current scan-line sorted by their

x-values

Implement active edge list with linked list

So what kind of information should be stored in the linked list for our polygon drawing algorithm?

34/95

General Polygons – Data Structures

Edge:

maxY: highest y-value

currentX: x-value of end-point

with lowest y-value

xIncr: 1 / slope

mazY

currentX

xIncr

Edge

35/95

Scan line intersection

kk

kk

xx

yym

1

1

Scan line yk+1Scan line yk

mxx kk

11

),( 00 yx

m

kxxk 0

),( endend yx

),( 11 kk yx

),( kk yx

36/95

Scan line intersection

),( 11 kk yx

),( kk yx

kk

kk

xx

yym

1

1

Scan line yk+1Scan line yk

mxx kk

11

),( 00 yx

m

kxxk 0

),( endend yx

37/95

General Polygons – Data Structures

Edge:

maxY: highest y-value

currentX: x-value of end-point

with lowest y-value

xIncr: 1 / slope

mazY

currentX

xIncr

Edge

Horizontal edges will not be used!!!

38/95

General Polygons – Algorithm

line = 0

While (line < height )

Add edges to Active Edge List from Sorted Edge Table starting at line

Remove edges that end at line

Fill pixels

Increment x-values on edges in Active Edge List

Increment line

39/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List

40/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List

maxY

currentX

xIncr

AG

30

32

600

AB

41/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List?

42/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List

maxY

currentX

xIncr

ED

55

21

33

21

FG

43/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List

maxY

currentX

xIncr

ED

55

21

33

21

FG

Is it correct?

44/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Sorted Edge Table

Active Edge List

AG

33

2

32

600

AB

maxY

currentX

xIncr

ED

55

21

33

21

FG

45/95

General Polygons – Algorithm

line = 0

While (line < height )

Add edges to Active Edge List from Sorted Edge Table starting at line

Remove edges that end at line

Fill pixels

Increment x-values on edges in Active Edge List

Increment line

46/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

33

2

32

600

AB

maxY

currentX

xIncr

ED

55

21

33

21

FG

47/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

33

2

32

600

AB

maxY

currentX

xIncr

ED

55

21

33

21

FG

? ? ? ?

48/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

33

4

32

600

AB

maxY

currentX

xIncr

ED

52

15

21

32

12

21

FG

49/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

33

4

32

600

AB

maxY

currentX

xIncr

ED

52

15

21

32

12

21

FG

50/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

32

32

600

AB

maxY

currentX

xIncr

ED

56

21

32

21

FG

51/95

General Polygons – Algorithm

line = 0

While (line < height )

Add edges to Active Edge List from Active Edge Table starting at line

Remove edges that end at line

Fill pixels

Increment x-values on edges in Active Edge List

Increment scan line

52/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

AG

32

32

600

AB

maxY

currentX

xIncr

ED

56

21

32

21

FG

53/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

56

21

54/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

56

21

55/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

52

16

21

56/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

52

16

21

57/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

57

21

CD

77

2

58/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

ED

52

16

21

CD

772

59/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

CD

772

60/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

600

AB

maxY

currentX

xIncr

CD

772

61/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

703

BC

maxY

currentX

xIncr

CD

752

600

AB

62/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

703

BC

maxY

currentX

xIncr

CD

752

600

AB

63/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

703

BC

maxY

currentX

xIncr

CD

752

64/95

General Polygons – Example

0 1 2 3 4 5 6 70

1

2

3

4

5

6

7

A

B

C

D

EF

G

01234567

AB AG

FG ED

CD

BC

Active Edge Table

Active Edge List

733

BC

maxY

currentX

xIncr

CD

732

65/95

General Polygons – Algorithm

line = 0

While (line < height )

Add edges to Active Edge List from Sorted Edge Table starting at line

Remove edges that end at line

Fill pixels

Increment x-values on edges in Active Edge List

Increment scan line

66/95

Pixel Fill-in

Which intervals should we draw?

67/95

Pixel Fill-in

Which intervals should we draw? - Odd intervals. But need to pay attention to scan lines

passing through vertices.

68/95

General Polygons – Problems

Sliver polygons may not be drawn correctly

No simple solution Long, thin triangles

cause problems Want triangles with

good aspect ratio (close to equilateral)

69/95

Curved Boundaries

How to deal with curved boundaries?

70/95

Boundary Fill

Start with drawn boundaries and an interior point

Recursively recolor outward from that point If neighbor different, then recolor and

recur

Everything within the boundary is changed to that color

71/95

Boundary Fill

Start with drawn boundaries and an interior point

Recursively recolor outward from that point If neighbor different, then recolor and

recur

Everything within the boundary is changed to that color

72/95

Boundary Fill

Work for inner and outer boundaries

73/95

Boundary Fill

Start with drawn outline of a polygon and an interior point

Recursively recolor outward from that point If neighbor different, then recolor and

recur

Everything within the boundary is changed to that color

74/95

Boundary Fill

How to define a neigbhbor?

4-connected 8-connected

75/95

Boundary Fill – Example

Black pixels indicate boundary pixels

76/95

Boundary Fill – Example

77/95

Boundary Fill – Example

Continue checking neighboring pixels until reaching boundary pixels or previously visited pixels!

78/95

Boundary Fill – Example

Continue checking neighboring pixels until reaching boundary pixels or previously visited pixels!

79/95

Boundary Fill – Example

Continue checking neighboring pixels until reaching boundary pixels or previously visited pixels!

80/95

Boundary Fill – Example

Continue checking neighboring pixels until reaching boundary pixels or previously visited pixels!

81/95

Boundary Fill – Example

Continue checking neighboring pixels until reaching boundary pixels or previously visited pixels!

82/95

Boundary Fill

83/95

How to deal with this?

Multiple color boundaries?

How to paint this region to yellow?

84/95

How to deal with this?

Multiple color boundaries?

How to paint this region to yellow?

Three keys:- a start pixel

- a target color

- a paint color

85/95

Flood Fill

Start with a point Define color under that point as the interior

color Recursively recolor outward from that point

If neighbor is interior color, then recolor and recur

Contiguous regions are recolored

86/95

Flood Fill – Example

87/95

Flood Fill – Example

88/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

89/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

90/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

91/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

92/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

93/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

94/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

95/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

96/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

97/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!

98/95

Flood Fill – Example

Continue setting the colors for neighboring pixels if it is interior pixels!