Raster Algorithms - CAIG Lab - National Chiao Tung...

Preview:

Citation preview

Raster AlgorithmsRaster Algorithms

I-Chen Lin’s CG slides, Doug James’s CG slides

Shirley, Fundamentals of Computer Graphics, Chap 3.5, 3.7, 4.4

Hearn and Baker, Computer Graphics, Chap 3.14, 3.15

林文杰, Wen-Chieh (Steve) Lin

Department of Computer Science &

Institute of Multimedia Engineering

DCP4516 Introduction to Computer Graphics 2

Advertisement: CMU ETC MS ProgramAdvertisement: CMU ETC MS Program

•Carnegie Mellon’s Entertainment TechnologyCenter will hold an information session on itsmaster program at 計中國際會議廳 at1:30PM on Thursday

http://www.etc.cmu.edu/

DCP4516 Introduction to Computer Graphics 3

Rasterization (Scan Conversion)Rasterization (Scan Conversion)

•Final step in pipeline

•From screen coordinates (float) to pixels (int)

•Writing pixels into frame buffer

DCP4516 Introduction to Computer Graphics 4

OutlineOutline

•2D graphics primitives

–Line drawing

–Circle drawing

–………

•Area filling

–Polygons

–………

•Antialiasing

DCP4516 Introduction to Computer Graphics 5

Line-Drawing AlgorithmsLine-Drawing Algorithms

•Start with line segment in windowcoordinates with integer values for endpoints

xy

m

y = mx + h

DCP4516 Introduction to Computer Graphics 6

Discrete LinesDiscrete Lines

•Lines vs. Line Segments

•What is a discrete line segment?

•How to generate a discrete line?

DCP4516 Introduction to Computer Graphics 7

“Good”Discrete Lines“Good”Discrete Lines

•No gaps in adjacent pixels

•Pixels close to ideal line

•Consistent choices; same pixels in samesituations

•Smooth looking

•Even brightness in all orientations

•Same line for P0 P1 as for P1 P0

DCP4516 Introduction to Computer Graphics 8

DDA AlgorithmDDA Algorithm

•Digital Differential Analyzer

–Line y = mx + h satisfies differential equation

•Along scan line Δx = 1For(x=x1; x<=x2,ix++) {

y+=m;

write_pixel(x, round(y), line_color)

}

DCP4516 Introduction to Computer Graphics 9

ProblemProblem

•DDA = for each x plot pixel at closest y.

–Problems for steep lines

DCP4516 Introduction to Computer Graphics 10

Using SymmetryUsing Symmetry

•Use for 1 ≥m ≥0

–For m > 1, swap roles of x and y

–For each y, plot closest x

DCP4516 Introduction to Computer Graphics 11

Bresenham’s AlgorithmBresenham’s Algorithm

•DDA requires one floating point addition per step.

•Bresenham’s algorithm eliminates all floating pointoperations

•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 areonly two candidates for the next pixel

DCP4516 Introduction to Computer Graphics 12

Key to Bresenham AlgorithmKey to Bresenham Algorithm

•“Reasonable assumptions”have reduced theproblem to making a binary choice at eachpixel:

(Previous)

NE (next)

E (next)

DCP4516 Introduction to Computer Graphics 13

Candidate PixelsCandidate Pixels

•1 ≥m ≥0

Last pixel

candidates

DCP4516 Introduction to Computer Graphics 14

Go NE if M is below the lineGo NE if M is below the line

ideal linemidpoint

E

NE

previous

Q

1xx

y

1y

M

),(211 yx

DCP4516 Introduction to Computer Graphics 15

Go E if M is above the lineGo E if M is above the line

ideal line

midpoint

E

NE

previous

Q

),( yx

),(211 yx

)1,1( yx

),( 1 yx

M

DCP4516 Introduction to Computer Graphics 16

Decision Variable dDecision Variable d

•Define a logical decision variable d

•linear in form

•incrementally updated (with addition)

•tells us whether to go E or NE

DCP4516 Introduction to Computer Graphics 17

Implicit Line EquationImplicit Line Equation

•Explicit: y = mx + h

•Implicit: f(x,y) = ax + by + c = 0

•Line passing (x0, y0) and (x1, y1):

f(x,y) = (y0–y1)x + (x1–x0)y + x0y1 –x1y0= 0

DCP4516 Introduction to Computer Graphics 18

Recall thatRecall that

•f(x,y) = (y0–y1)x + (x1–x0)y + x0y1 –x1y0= 0

•For 1 ≥m ≥0 and x1 > x0

Above line: consider f(x,+∞)

Below line

f(x,y) > 0

f(x,y) < 0

DCP4516 Introduction to Computer Graphics 19

Bresenham’s AlgorithmBresenham’s Algorithm

y = y0

For x=x0 to x1 do

draw(x,y)

If f(x+1, y+0.5) < 0 theny = y + 1

f(x+1,y+0.5) < 0

f(x+1,y+0.5) > 0

DCP4516 Introduction to Computer Graphics 20

Bresenham’s Algorithm (cont.)Bresenham’s Algorithm (cont.)

•Speed up by replacing function evaluationwith incremental update

•f(x,y) = (y0–y1)x + (x1–x0)y + x0y1 –x1y0= 0

•f(x+1, y+1) = f(x, y) + (y0–y1) + (x1–x0)

•f(x+1, y) = f(x, y) + (y0–y1)

DCP4516 Introduction to Computer Graphics 21

Bresenham’s Algorithm (cont.)Bresenham’s Algorithm (cont.)

y = y0

d = f(x0+1, y0+0.5)

For x=x0 to x1 do

draw(x,y)

If d< 0 theny = y + 1

d = d + (x1-x0) + (y0-y1)

else

d = d + (y0-y1)

f(x+1,y+0.5) < 0

f(x+1,y+0.5) > 0

DCP4516 Introduction to Computer Graphics 22

Bresenham’s Algorithm (cont.)Bresenham’s Algorithm (cont.)

y = y0

d = f(x0+1, y0+0.5)

For x=x0 to x1 do

draw(x,y)

If d< 0 theny = y + 1

d = d + (x1-x0) + (y0-y1)

else

d = d + (y0-y1)

Now, we want to remove thelast floating point operationin the code!

DCP4516 Introduction to Computer Graphics 23

Bresenham’s Algorithm (cont.)Bresenham’s Algorithm (cont.)

•2*d = 2*f(x0+1, y0+0.5)

•f(x0+1,y0+0.5) = (y0–y1) (x0+1) + (x1–x0)(y0+0.5)+ x0y1 –x1y0= 0

•2f(x0+1,y+0.5) = 2(y0–y1) (x0+1) + (x1–x0)(2y0+1)+ 2x0y1 –2x1y0= 0

DCP4516 Introduction to Computer Graphics 24

Code for Bresenham’s AlgorithmCode for Bresenham’s Algorithm

y = y0

d = 2*(y0–y1)(x0+1) + (x1–x0)(2*y0+1) + 2*(x0*y1–x1*y0)

For x=x0 to x1 do

draw(x,y)

If d< 0 theny = y + 1

d = d + 2*(x1-x0) + 2*(y0-y1)

else

d = d + 2*(y0-y1)

DCP4516 Introduction to Computer Graphics 25

Other cases for line drawingOther cases for line drawing

•m=0; m=1 trivial cases

•0 > m > -1 flip about x-axis

•m > 1 flip about x = y

DCP4516 Introduction to Computer Graphics 26

Flip about x-axis if 0>m>-1Flip about x-axis if 0>m>-1

), 00( yx

x

y

), 11( yx), 11( yx

), 00( yx

0 0 0 0

1 11 1

, ) ( ,

, ) , )

( );

( (

x y x y

x y x y

DCP4516 Introduction to Computer Graphics 27

How do slopes relate?How do slopes relate?

0

1 0

0

1 0

1

1

;

by definition

y y

y y

mx x

mx x

)0

1 0

(1i iSince ,

y ymyy

x x

DCP4516 Introduction to Computer Graphics 28

How do slopes relate?How do slopes relate?

1010 mm

i.e.,

)0

1 0

1( y ym

x x

m m

DCP4516 Introduction to Computer Graphics 29

Flip about line y=x if m>1Flip about line y=x if m>1

), 11( yx

), 00( yx

y

x

xy ), 11( yx

), 00( yx ), 00( yx

), 11( yx

DCP4516 Introduction to Computer Graphics 30

Flip about line y=x if m>1 (cont.)Flip about line y=x if m>1 (cont.)

,swap and prime them ,

,

y mx Bx yx my B

my x B

1,

1and,

1 0 1

y x Bm

mm

m m

DCP4516 Introduction to Computer Graphics 31

Circle-drawing AlgorithmCircle-drawing Algorithm

DCP4516 Introduction to Computer Graphics 32

Circle-drawing AlgorithmCircle-drawing Algorithm

for each x, yif | x2 + y2 –r2 | <= εSetPixel ( x, y )

for θ in [0~360 degree ]x = r cos(θ)y = r sin(θ)SetPixel ( x, y )

DCP4516 Introduction to Computer Graphics 33

Midpoint Circle AlgorithmMidpoint Circle Algorithm

•Can we utilize the similar idea inBresenham’s line-drawing algorithm ?

–Check only the next candidates

–Use symmetry and simple decision rules

Symmetry of a Circle

DCP4516 Introduction to Computer Graphics 34

Midpoint Circle Algorithm (cont.)Midpoint Circle Algorithm (cont.)

f(x,y) = x2 + y2 - R2f(x,y) > 0 => point outside circlef(x,y) < 0 => point inside circle

Pk = fcirc(xk + 1, yk –½)

DCP4516 Introduction to Computer Graphics 35

Midpoint Circle Algorithm (cont.)Midpoint Circle Algorithm (cont.)

•Given the starting point (0,r), the computation ismore efficient.P0 = 5/4 – r

At each x position,

if(pk < 0)

the next point is (xk+1, yk)

pk+1 = pk + 2xk+1 + 1

else

the next point is (xk+1, yk-1)

pk+1 = pk + 2xk+1 + 1 – 2yk+1

DCP4516 Introduction to Computer Graphics 36

2D Polygon Filling2D Polygon Filling

•In computer graphics, we usually usepolygons to approximate complex surfaces.

•Let’s focus on the polygon filling!

DCP4516 Introduction to Computer Graphics 37

General PolygonsGeneral Polygons

•Inside or Outside are not obvious

–It’s not obvious when the polygon intersects itself.

DCP4516 Introduction to Computer Graphics 38

Concave vs. ConvexConcave vs. Convex

•We prefer dealing with “simpler”polygons.

•Convex (easy to break into triangles)

convex concave

θ< 180o

θ > 180o

DCP4516 Introduction to Computer Graphics 39

Filling Convex PolygonsFilling Convex Polygons

•Find top and bottom vertices

•List edges along left and right sides

•For each scan line from top to bottom–Find left and right endpoints of span, xl and xr

–Fill pixels between xl and xr

–Can use Bresenham’s alg. to update xl and xr

xl xr

DCP4516 Introduction to Computer Graphics 40

Concave Polygons: Odd-Even TestConcave Polygons: Odd-Even Test

•Approach 1: odd-even test

•For each scan line

–Find all scan line/polygon intersections

–Sort them left to right

–Fill the interior spans between intersections

•Parity rule: inside after

an odd number of crossings

DCP4516 Introduction to Computer Graphics 41

Concave Polygons: Winding RuleConcave Polygons: Winding Rule

•Approach 2: winding rule

•Orient the lines in polygon

•For each test line (not passing a vertex)

–Winding number = right-hdd –left-hdd crossings

–Interior if winding number non-zero

211

1

1 1

A

BE

DC

0+1+0+1+0=2

0+0+0+1+0=1Starting from A:

Test line

DCP4516 Introduction to Computer Graphics 42

Even-odd Rule vs. Winding RuleEven-odd Rule vs. Winding Rule

•Different only for self-intersecting polygons

Even-odd rule

211

1

1 1

Winding rule

DCP4516 Introduction to Computer Graphics 43

AliasingAliasing

•Artifacts created during scan conversion

•Inevitable (going from continuous to discrete)

•Aliasing (name from digital signal processing):we sample a continuous image at grid points

•Effects

–Jagged edges

–Moiré patterns

DCP4516 Introduction to Computer Graphics 44

Sampling and ReconstructionSampling and Reconstruction

•An image is a 2D array of discrete samplesfrom real-world continuous signal

DCP4516 Introduction to Computer Graphics 45

Sampling and AliasingSampling and Aliasing

•Artifacts due to undersampling or poorreconstruction

•Formally, high frequencies masquerading as low

•e.g. high frequency line as low freq jaggies

DCP4516 Introduction to Computer Graphics 46

(Spatial) Aliasing(Spatial) Aliasing

DCP4516 Introduction to Computer Graphics 47

(Spatial) Aliasing(Spatial) Aliasing

•Jaggies probably biggest aliasing problem

DCP4516 Introduction to Computer Graphics 48

Moiré Patterns due to Bad DownsamplingMoiré Patterns due to Bad Downsampling

Original Image

Downsampled without filtering

Downsampled after filtering

DCP4516 Introduction to Computer Graphics 49

More AliasingMore Aliasing

DCP4516 Introduction to Computer Graphics 50

Antialiasing for Line SegmentsAntialiasing for Line Segments

•Use area averaging at boundary

•bottom is aliased, magnified

•top is antialiased, magnified

DCP4516 Introduction to Computer Graphics 51

Antialiasing by SupersamplingAntialiasing by Supersampling

•Traditionally for off-line rendering

•Render, say, 3x3 grid of mini-pixels

•Average results using a filter

•Can be done adaptively

–Stop if colors are similar

–Subdivide at discontinuities

DCP4516 Introduction to Computer Graphics 52

Supersampling ExampleSupersampling Example

•Other improvements

–Stochastic sampling (avoiding repetition)

–Jittering (perturb a regular grid)

Recommended