35
Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From Rich Riesenfeld Fall 2015 More Raster Line Issues Fat lines with multiple pixel width Symmetric lines End point geometry – how should it look? Generating curves, e.g., circles, etc. Jaggies, staircase effect, aliasing...

Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 1

Rasterization: Bresenham Circles

CS4600 Intro to Computer GraphicsFrom Rich Riesenfeld

Fall 2015

More Raster Line Issues

• Fat lines with multiple pixel width

• Symmetric lines

• End point geometry – how should it look?

• Generating curves, e.g., circles, etc.

• Jaggies, staircase effect, aliasing...

Page 2: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 2

Generating Circles

Exploit 8-Point Symmetry

),( yx),( yx

),( yx ),( yx

),( xy),( xy

),( xy),( xy

Page 3: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 3

Only 1 Octant Needed We will generate 2nd Octant

Generating pt (x,y) gives

the following 8 pts by symmetry:

{(x,y), (-x,y), (-x,-y), (x,-y),

(y,x), (-y,x), (-y,-x), (y,-x)}

Page 4: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 4

2nd Octant Is a Good Arc

• It is a function in this domain

– single-valued

– no vertical tangents: |slope| 1

• Lends itself to Bresenham

– only need consider ?

– E or SE

Implicit Eq’s for Circle

• Let F(x,y) = x2 + y2 – r 2

• For (x,y) on the circle, F(x,y) = 0

• So, F(x,y) > 0 (x,y) Outside

• And, F(x,y) < 0 (x,y) Inside

Page 5: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 5

Choose E or SE

• Function is x2 + y2 – r 2 = 0

• So, F(M) 0 SE

E

SE

Mideal curve

F(M) 0 SE

Page 6: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 6

Choose E or SE

• Function is x2 + y2 – r 2 = 0

• So, F(M) 0 SE

• And, F(M) < 0 E

E

SE

M

ideal curve

F(M) < 0 E

Page 7: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 7

Decision Variable d

Again, we let,

d = F(M )

E

SE

M

ideal curve

Look at Case 1: E

Page 8: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 8

1( 1, )222 1 2( 1) ( )2

old p p

p

F yx

yx rp

d

dold < 0 E

dold < 0 E

1( 2, )222 1 2( 2) ( )2

new p p

p p

F yx

yx r

d

Page 9: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 9

dold < 0 E

(2 3)pnew old xd d

Since,

32

)12()44()1( 2)2( 2 22

x p

x px px px px px p

oldnew dd

dold < 0 E

,

2 3

Enew old

E px

d d

where,

Page 10: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 10

E

SE

Mideal curve

Look at Case 2: SE

dold 0 SE

Because,…, straightforward manipulation

3( 2, )222 3 2( 2) ( )2

(2 2 5)

new

new old p p

p p

p p

F yx

yx r

yd d x

d

Page 11: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 11

dold 0 SE

222222 )

2

1()1()

2

3()2( ry px pry px p

dd oldnew

4

1

4

93)32(

22 y py py py px p=

dold 0 SE

222222 )

2

1()1()

2

3()2( ry px pry px p

dd oldnew

4

1

4

93)32(

22 y py py py px p=

x x

Page 12: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 12

dold 0 SE

222222 )

2

1()1()

2

3()2( ry px pry px p

dd oldnew

4

1

4

93)32(

22 y py py py px p=

x x

x x

dold 0 SE

)44

9 1()3()32( y py px p

From calculation

From newy-coordinate

dd oldnew

E From oldy-coordinate

Page 13: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 13

dold 0 SE

)44

9 1()3()32( y py px p

From calculation

From newy-coordinate

dd oldnew

E From oldy-coordinate

dold 0 SE

)44

9 1()3()32( y py px p

From calculation

From newy-coordinate

dd oldnew

E From oldy-coordinate

Page 14: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 14

dold 0 SE

)44

9 1()3()32( y py px p

From calculation

From newy-coordinate

dd oldnew

E From oldy-coordinate

2 2 5SE p pyx

dold 0 SE

(2 2 5)new old p p

SEold

yd d x

d

I.e.,

2 2 5SE p pyx

Page 15: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 15

Note: Δ΄s Not Constant

depend on values of xp and yp

andE SE

Summary

• Δ΄s are no longer constant over entire line

• Algorithm structure is exactly the same

• Major difference from the line algorithm

– Δ is re-evaluated at each step

– Requires real arithmetic

Page 16: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 16

Initial Condition

• Let r be an integer. Start at

• Next midpoint M lies at

• So,

),0( r

),1(2

1r

rrrrF 2)2(1),1(4

1

2

1

r4

5

Ellipses

• Evaluation is analogous

• Structure is same

• Have to work out the Δ΄s

Page 17: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 17

Getting to Integers

• Note the previous algorithm

involves real arithmetic

• Can we modify the algorithm to use

integer arithmetic?

Integer Circle Algorithm

• Define a shift decision variable

• In the code, plug in

4

1 dh

4

1 hd

Page 18: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 18

Integer Circle Algorithm

• Now, the initialization is h = 1 - r

• So the initial value becomes

r

rrF

1

4

1)

4

5(

4

1

2

1),1(

Integer Circle Algorithm

• Then,

• Since h an integer

10 becomes

4d h

1 0

4hh

Page 19: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 19

Integer Circle Algorithm

• But,h begins as an integer

• And, h gets incremented by integer

• Hence, we have an integer circle

algorithm

• Note: Sufficient to test for h < 0

End of Bresenham Circles

Page 20: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 20

Another Digital Line Issue

• Clipping Bresenham lines

• The integer slope is not the true slope

• Have to be careful

• More issues to follow

Line Clipping Problem

minyy

minxx

Clipping Rectangle

)0,0( yx

)1,1( yx

maxxx

Page 21: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 21

Clipped Line

minyy

minxx

Clipping Rectangle)0,0( yx

)1,1( yx

maxxx

maxyy

Drawing Clipped Lines

)0,0( yx

)1,1( yx

Page 22: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 22

Clipped Line Has Different Slope !

34m

12m

Pick Right Slope to Reproduce Original Line Segment

Zoom of previous situation

Page 23: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 23

Pick Right Slope to Reproduce Original Line Segment

Zoom of previous situation

Clipping Against x = xmin

E

NE

minyy

minxx

))( min(,min Bxmx

Clip Rectangle

))( min(,min Bxmx Round

midpointM

Page 24: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 24

Clipping Against y = ymin

minyy

minxx

1min yy

21

min yy

Line getting clipped

B A

Clipping Against y = ymin

• Situation is complicated

• Multiple pixels involved at (y = ymin )

• Want all of those pixels as “in”

• Analytic ∩ , rounding x gives A

• We want point B

Page 25: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 25

Clipping Against y = ymin

• Use Line ∩ y = ymin - ½

• Round up to nearest integer x

• This yields point B, the desired result

Jaggies-Manifestation of Aliasing

Added resolution helps, but does not directly address underlying issue of aliasing

Page 26: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 26

Jaggies and Aliasing

• To represent a line with discrete pixel values is to sample finitely a continuous function

• Jaggies are visual manifestation, artifacts, resulting from information loss

• The term aliasing is a complicated, unintuitive phenomenon which will be defined later

Jaggies and Aliasing

• Doubling resolution in x and y reduces the effect of the problem, but does not fix it

• Doubling resolution costs 4 times memory, memory bandwidth and scan conversion time!

Page 27: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 27

Anti-aliasing

5

4

3

2

1

00 1 2 3 4 5 6 7 8 9 10 11

Pixel intensity (darkness, in this case) is proportional to area covered by line

Pixel Space

Anti-aliasing

Pixel intensity (darkness, in this case)

is proportional to area covered by line

Pixel Space

Page 28: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 28

Anti-aliasing

• Set each pixel’s intensity value proportional to its area of overlap (i.e. sub-area) covered by primitive

• Not more than 1 pixel/column for lines with

0 < slope < 1

Gupta-Sproull Algorithm -1

• Standard Bresenham chooses E or NE

• Incrementally compute distance D from

chosen pixel to center of line

• Vary pixel intensity by value of D

• Do this for line above and below

Page 29: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 29

Gupta-Sproull Algorithm -2

• Use coarse (4-bit, say) lookup table for

intensity : Filter (D, t )

• Note, Filter value depends only on D

and t, not the slope of line! (Very clever)

• For line_width t = 1 geometry and

associated calculations greatly simplify

Cone Filter for Weighted Area Sampling

1r

1t

D

Unit thickness line intersects no more than 3 pixels

Page 30: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 30

Observations

• Lines are complicated

• Many aspects to consider

• We omitted many

• What about intensity of

y = x vs y = 0 ?

Rasterization: Triangles

CS4600 Intro to Computer GraphicsFrom Rich Riesenfeld

Fall 2014

Page 31: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 31

Rasterize This!(Rasterization intuition)

• When we render a triangle we want to determine if a pixel is within a triangle. (barycentric coords)

• Calculate the color of the pixel (use barycentric coors).

• Draw the pixel.• Repeat until the triangle is appropriately filled.

Rasterization Pseudo Code

Page 32: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 32

Rasterization

Rasterization

Page 33: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 33

Rasterization

Ymax, Xmax

Ymin, X???

Y???, Xmin

Bounding Box

Ymax, Xmax

Ymin, X???

Y???, Xmin

Page 34: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 34

Barycentric Coordinates

• weighted combination of vertices

321 PPPP

1P

3P

2P

P

(1,0,0)

(0,1,0)

(0,0,1)

5.0

1

01,,0

1

“convex combinationof points”

Barycentric Coordinates for Interpolation

• how to compute ? – use bilinear interpolation or plane equations

– once computed, use to interpolate any # of parameters from their vertex values

,,

interpolate ,,

...

dzcybxa

321 xxxx 321 rrrr

321 gggg

etc.

Page 35: Rasterization: Bresenham Circlescs4600/lectures/Wk2_Rasterization2.pdf · 2015-09-02 · Fall 2015 CS4600 1 Rasterization: Bresenham Circles CS4600 Intro to Computer Graphics From

Fall 2015

CS4600 35

Interpolatation: Gouraud Shading

• need linear function over triangle that yields original vertex colors at vertices

• use barycentric coordinates for this– every pixel in interior gets colors resulting from mixing colors of

vertices with weights corresponding to barycentric coordinates

– color at pixels is affine combination of colors at vertices

)()()(

:)(

321

321

xxx

xxx

ColorColorColor

Color

Gouraud Shading Scanline Alg

• algorithm

– modify scanline algorithm for polygon scan-conversion :

• linearly interpolate colors along edges of triangle to obtain colors for endpoints of span of pixels

• linearly interpolate colors from these endpoints within the scanline

maxminmax

maxmin

minmax

max *1* CXX

XXC

XX

XX curcur

Xmin XmaxXcur