38
CMSC 435 Introductory Computer Graphics Rasterization Penny Rheingans UMBC

CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

CMSC 435

Introductory Computer Graphics

Rasterization Penny Rheingans

UMBC

Page 2: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scan conversion

• Problem – How to generate filled polygons (by determining which

pixel positions are inside the polygon)

– Conversion from continuous to discrete domain

• Concepts – Spatial coherence

– Span coherence

– Edge coherence

Page 3: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Rectangles

for ( y from y0 to yn )

for ( x from x0 to xn )

Write Pixel (x, y, val)

Page 4: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Rectangles (2)

for ( y from y0 to yn )

for ( x from x0 to xn )

Write Pixel (x, y, val)

Page 5: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Rectangles (3)

for ( y from y0 to yn )

for ( x from x0 to xn )

Write Pixel (x, y, val)

Page 6: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Arbitrary Polygons • vertices:

(4, 1) , (7, 13) , (11 , 2)

Page 7: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Arbitrary Polygons (2) • vertices:

(4, 1) , (7, 13) , (11 , 2)

• Intersect scanline w/pgon edges => span extrema

Page 8: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Arbitrary Polygons (3) • vertices:

(4, 1) , (7, 13) , (11 , 2)

• Intersect scanline w/pgon edges => span extrema

• Fill between pairs of span extrema

Page 9: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanning Arbitrary Polygons (4) • vertices:

(4, 1) , (7, 13) , (11 , 2)

For each nonempty scanline

Intersect scanline w/pgon edges => span extrema

Fill between pairs of span extrema

Page 10: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example Cases (2)

4 intersections w/ scanline 6 at x = 1, 6, 6, 12 1/7

Page 11: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example Cases (3)

• 3 intersections w/scanline 5 at x = 1, 1, 11 5/7

Page 12: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example Cases (4)

3 intersections w/scanline 5 at x = 1, 1, 11 5/7

==>

Count continuing edges once (shorten lower edge) now x=1, 11 5/7

Page 13: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example Cases (5)

4 intersections w/ scanline 1at x = 5, 5, 10, 10

Page 14: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example Cases (6)

4 intersections w/ scanline 1 at x = 5, 5, 10, 10

=>

Don't count vertices of horizontal edges.

Now x = 5, 10

Page 15: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanline Data Structures

Sorted edge table:

all edges

sorted by min y

holds:

max y

init x

inverse slope

Active edge table:

edges intersecting

current scanline

holds:

max y

current x

inverse slope

Page 16: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Scanline Algorithm 1. Bucket sort edges into sorted edge table

2. Initialize y & active edge table

y = first non- empty scanline

AET = SET [y]

3. Repeat until AET and SET are empty

Fill pixels between pairs of x intercepts in AET

Remove exhausted edges

Y++

Update x intercepts

Resort table (AET)

Add entering edges

Page 17: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

Page 18: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

sort on minY: 1

store:

max Y: 11

min X: 4

1/m : (Xmax - Xmin) / (Ymax - Ymin) = (1 - 4) / (11 - 1) = -3 / 10

Page 19: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

Page 20: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

Page 21: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x=4,12)

Page 22: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x=4,12)

remove exhausted edges

update intersection points

resort table

add entering edges

Page 23: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x=3 1/10,12)

remove exhausted edges

update intersection points

Page 24: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x=3 1/10,12)

remove exhausted edges

update intersection points

resort table

add entering edges

Page 25: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x = 2 8/10, 9; 9,12)

remove exhausted edges

update intersection points

resort table

add entering edges

Page 26: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Example: vertices (4,1), (1,11), (9,5), (12,8), (12,1)

bucket sort edges into sorted edge table

initialize active edge list to first non empty scanline

for each non empty scanline

fill between pairs (x=2 5/10, 7 2/3; 10,12)

remove exhausted edges

update intersection points

resort table

add entering edges

Page 27: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Fill Variants

Fill between pairs:

for ( x = x1; x < x2; x++ )

framebuffer [ x, y ] = c

Page 28: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Fill Variants (2)

• Pattern Fill

Fill between pairs:

for ( x = x1; x < x2; x++ )

if ( ( x + y ) % 2 )

framebuffer [ x, y ] = c1

else

framebuffer [ x, y ] = c1

Page 29: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Fill Variants (3)

• Colorwash

Red to blue

Fill between pairs:

for ( x = x1; x < x2; x++ ) framebuffer [ x, y ] = C0 + dC * ( x1 - x )

For efficiency carry C and dC in AETand calculate color incrementally

Page 30: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Fill Variants (4)

• Vertex colors

Red, green, blue

Fill between pairs:

for ( x = x1; x < x2; x++ ) framebuffer [ x, y ] =

Cy1x1 + [(x - x1)/(x2 - x1)*(Cy1x2 - Cy1x1)]/dCx

For efficiency carry Cy and dCy in AET calculate dCx at beginning of scanline

Page 31: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Coordinates

• Use non-orthogonal coordinates to describe

position relative to vertices

– Coordinates correspond to scaled signed

distance from lines through pairs of vertices

p = a+ b a( ) + c a( ) p , ,( ) = a+ b+ c

Page 32: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Example

Page 33: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Coordinates

• Computing coordinates

=ya yb( )x+ xb xa( )y+ xayb xbyaya yb( )xc + xb xa( )yc + xayb xbya

=ya yc( )x+ xc xa( )y+ xayc xbyaya yc( )xb + xc xa( )yb + xayc xcya

=1

Page 34: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Alternative Computation

Page 35: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Rasterization For all x do

For all y do

Compute ( , , ) for (x,y)

If ( [0,1] and [0,1] and [0,1] then

c = c0 + c1 + c2Draw pixel (x,y) with color c

Page 36: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Rasterization xmin = floor(xi)

xmax = ceiling(xi)

ymin = floor(yi)

ymax = ceiling(xi)

for y = ymin to ymax do

for x = xmin to xmax do

= f12(x,y)/f12(x0,y0)

= f20(x,y)/f20(x1,y1)

= f01(x,y)/f01(x2,y2)

If ( [0,1] and [0,1] and [0,1] then

c = c0 + c1 + c2Draw pixel (x,y) with color c

Page 37: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices:

Barycentric Rasterization

• Computing coordinates

=f01(x, y)

f01(x2, y2 )=

y0 y1( )x+ x1 x0( )y+ x0y1 x1y0y0 y1( )x2 + x1 x0( )y2 + x0y1 x1y0

=f20 (x, y)

f20 (x1, y1)=

y2 y0( )x+ x0 x2( )y+ x2y0 x0y2y2 y0( )x1 + x0 x2( )y1 + x2y0 x0y2

=f12 (x, y)

f12 (x0, y0 )=

y1 y2( )x+ x2 x1( )y+ x1y2 x2y1y1 y2( )x0 + x2 x1( )y0 + x1y2 x2y1

Page 38: CMSC 435 Introductory Computer Graphics Rasterizationolano/class/435-08-2/... · 2015. 8. 25. · • Fill between pairs of span extrema . Scanning Arbitrary Polygons (4) • vertices: