cs-378: game technology lecture #1: scan conversion prof. okan arikan university of texas, austin...

45
CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica Hodgins for lecture notes V2005-02-1.3

Upload: josie-pixton

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

CS-378: Game Technology

Lecture #1: Scan Conversion

Prof. Okan ArikanUniversity of Texas, Austin

Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica Hodgins for lecture notes

V2005-02-1.3

Page 2: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

22

Today

2D Scan ConversionDrawing Lines

Drawing Curves

Filled Polygons

Filling Algorithms

Page 3: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

33

Drawing a Line

Basically, its easy... but for the details

Lines are a basic primitive that needs to be done well...

Page 4: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

44

Drawing a Line

Basically, its easy... but for the details

Lines are a basic primitive that needs to be done well...

From “A Procedural Approach to Style for NPR Line Drawing from 3D models,”by Grabli, Durand, Turquin, Sillion

Page 5: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

55

Drawing a Line

Page 6: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

66

Drawing a Line

Page 7: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

77

Drawing a Line

Some things to considerHow thick are lines?

How should they join up?

Which pixels are the right ones?

For example:

Page 8: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

88

Drawing a Line

InclusiveEndpoints

Page 9: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

99

Drawing a Line

Page 10: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1010

Drawing a Line

x=x1y=y1while(x<=x2) plot(x,y) x++ y+=Dy

Page 11: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1111

Drawing a Line

After rounding

Page 12: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1212

Drawing a Line

Accumulation ofroundoff errors

How slow is float-to-int conversion?

Page 13: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1313

Drawing a Line

Page 14: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1414

Drawing a Linevoid drawLine-Error1(int x1,x2, int y1,y2)

float m = float(y2-y1)/(x2-x1) int x = x1 float y = y1 while (x <= x2) setPixel(x,round(y),PIXEL_ON)

x += 1 y += m

Not exact math

Accumulates errors

Page 15: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1515

No more rounding

Drawing a Linevoid drawLine-Error2(int x1,x2, int y1,y2)

float m = float(y2-y1)/(x2-x1) int x = x1 int y = y1 float e = 0.0 while (x <= x2) setPixel(x,y,PIXEL_ON)

x += 1 e += m if (e >= 0.5) y+=1 e-=1.0

Page 16: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1616

Drawing a Linevoid drawLine-Error3(int x1,x2, int y1,y2)

int x = x1 int y = y1 float e = -0.5 while (x <= x2) setPixel(x,y,PIXEL_ON)

x += 1 e += float(y2-y1)/(x2-x1) if (e >= 0.0) y+=1 e-=1.0

Page 17: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1717

Drawing a Linevoid drawLine-Error4(int x1,x2, int y1,y2)

int x = x1 int y = y1 float e = -0.5*(x2-x1) // was -0.5 while (x <= x2) setPixel(x,y,PIXEL_ON)

x += 1 e += y2-y1 // was /(x2-x1) if (e >= 0.0) // no change y+=1 e-=(x2-x1) // was 0.0

Page 18: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1818

Drawing a Linevoid drawLine-Error5(int x1,x2, int y1,y2)

int x = x1 int y = y1 int e = -(x2-x1) // removed *0.5 while (x <= x2) setPixel(x,y,PIXEL_ON)

x += 1 e += 2*(y2-y1) // added 2* if (e >= 0.0) // no change y+=1 e-=2*(x2-x1) // added 2*

Page 19: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

1919

Drawing a Linevoid drawLine-Bresenham(int x1,x2, int y1,y2)

int x = x1 int y = y1 int e = -(x2-x1) while (x <= x2) setPixel(x,y,PIXEL_ON)

x += 1 e += 2*(y2-y1) if (e >= 0.0) y+=1 e-=2*(x2-x1)

FasterNot wrong

Page 20: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2020

Drawing a Line

How thick?

Ends?

Page 21: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2121

Drawing a Line

Joining?

Ugly Bevel Round Miter

Page 22: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2222

Drawing Curves

Only one value of y for each value of x...

Page 23: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2323

Drawing Curves

Parametric curvesBoth x and y are a function of some third parameter

Page 24: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2424

Drawing Curves

Page 25: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2525

Draw curves by drawing line segments

Must take care in computing end points for lines

How long should each line segment be?

Drawing Curves

Page 26: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2626

Draw curves by drawing line segments

Must take care in computing end points for lines

How long should each line segment be?

Variable spaced points

Drawing Curves

Page 27: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2727

Drawing Curves

Midpoint-test subdivision

Page 28: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2828

Drawing Curves

Midpoint-test subdivision

Page 29: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

2929

Drawing Curves

Midpoint-test subdivision

Page 30: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3030

Drawing Curves

Midpoint-test subdivisionNot perfect

We need more information for a guarantee...

Page 31: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3131

Filled Polygons

Page 32: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3232

Filled Polygons

Page 33: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3333

Filled Polygons

Page 34: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3434

Filled Polygons

Page 35: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3535

Filled Polygons

Page 36: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3636

Filled Polygons

Page 37: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3737

Filled Polygons

Page 38: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3838

Filled Polygons

Treat (scan y = vertex y) as (scan y > vertex y)

Page 39: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

3939

Filled Polygons

Horizontal edges

Page 40: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4040

Filled Polygons

Horizontal edges

Page 41: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4141

“Equality Removal” applies to all vertices

Both x and y coordinates

Filled Polygons

Page 42: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4242

Final result:

Filled Polygons

Page 43: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4343

Who does this pixel belong to?

Filled Polygons

1

2

34

5

6

Page 44: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4444

Flood Fill

Page 45: CS-378: Game Technology Lecture #1: Scan Conversion Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic,

4545

Suggested Reading

Fundamentals of Computer Graphics by Pete Shirley

Chapters 2.5-2.11, 3