3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats...

32
Visualization and Computer Graphics Lab Jacobs University 3.3 Polygon filling

Transcript of 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats...

Page 1: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

Visualization and Computer Graphics LabJacobs University

3.3 Polygon filling

Page 2: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 254

Visualization and Computer Graphics LabJacobs University

Polygon filling

• Polygon filling treats the drawing of polygonal facesafter being projected to the screen space.

• The input is a (set of) polygon(s).

• The output is a raster image stored in theframebuffer that represents a discrete version of the projected polygons.

Page 3: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 255

Visualization and Computer Graphics LabJacobs University

Seed fill algorithm

• The seed fill (also: flood fill) algorithm starts with a seed point inside the polygon and keeps on filling in all directions until the boundary of the polygon is hit.

1. Scan convert all edges of the polygon.2. Choose a seed point inside the polygon and fill the

respective pixel.3. Recursively fill all pixels of the 4-point neighborhood,

if they are not already filled. Stop recursion at already filled pixels.

Page 4: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 256

Visualization and Computer Graphics LabJacobs University

Seed fill algorithm

polygonscan conversionseedingneighborsseed fill iterations

Page 5: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 257

Visualization and Computer Graphics LabJacobs University

Seed fill algorithm

• How to pick appropriate seed point?• It must lie inside the polygon

Strategy:1. Use heuristic (e.g., barycenter of polygon) to pick

any point.2. Check inside-property using a ray-intersection test.

Page 6: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 258

Visualization and Computer Graphics LabJacobs University

Ray-intersection test

• Let x be the chosen seed point.• Shoot a ray to infinity (typically along a coordinate axis)• Compute intersection of ray with all edges of the polygon• Iff number of intersections is odd, x lies inside the polygon

Page 7: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 259

Visualization and Computer Graphics LabJacobs University

Scan line algorithm

• The scan line algorithm is an alternative to the seedfill algorithm.

• It does not require scan conversion of the edgesbefore filling the polygons

• It can be applied simultaneously to a set of polygonsrather than filling each polygon individually.

Page 8: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 260

Visualization and Computer Graphics LabJacobs University

Scan line algorithm

• Use a horizontal scan line that traverses the scenetop-down.

• Stop at each pixel row• For each pixel row

– Compute the intersections of the polygon edges with thescan line.

– Sort the intersections from left to right.– Start filling at first intersection.– Stop filling at second intersection– Start filling again at third intersection.…

Page 9: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 261

Visualization and Computer Graphics LabJacobs University

Scan line algorithn

• Horizontal scan lines scan scene top-down and stop at each row:

(xk+1, yk+1)

(xk , yk) Scan Line yk

Scan Line yk +1

Page 10: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 262

Visualization and Computer Graphics LabJacobs University

Scan line algorithm

• Filling pixels between intersections in an alternatingfashion:

Page 11: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 263

Visualization and Computer Graphics LabJacobs University

Scan line algorithm

Example:

Page 12: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 264

Visualization and Computer Graphics LabJacobs University

Scan line algorithm

Example:

Page 13: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 265

Visualization and Computer Graphics LabJacobs University

Data structure for scan line algorithm

• The scan line algorithm is supported by a linked list data structure that stores all the edges that areintersected by the current position of the scan line in the correct order.

• Linked list exploits spatial coherence betweensuccessive scan line positions.

• The order of the edges stored in the linked list onlychanges at distinct points (event points).

Page 14: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 266

Visualization and Computer Graphics LabJacobs University

Data structure for scan line algorithm

• Even points are:– start/end of edge: insert/remove edge into sorted list.– edge crossings: flip order of successive edges in sorted list.

(Only in case of multiple polygons!)• Start/end points are known from the beginning.• Edge crossings are checked between neighboring

edges in the sorted list whenever a new neighborhoodemerges, i.e., at event points.

Page 15: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 267

Visualization and Computer Graphics LabJacobs University

Caveats• Scan line crossing vertex of polygon

– Distinguish two cases– Case 1: 2 edges start -> remove intersection points– Case 2: 1 edge starts & 1 ends -> treat as 1 intersection

• Horizontal edges– Eliminate horizontal edge and treat neighboring vertices as above.

1

1 2 1

2 1 1

Scan Line y1

Scan Line y

Page 16: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 268

Visualization and Computer Graphics LabJacobs University

3.4 Anti-aliasing

Page 17: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 269

Visualization and Computer Graphics LabJacobs University

Aliasing

Observation:• Scan converted objects exhibit discretization

artifacts such as jagged edges (staircase effect).• These artifacts are called aliasing.

Page 18: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 270

Visualization and Computer Graphics LabJacobs University

Anti-aliasing

• Anti-aliasing is a technique to alleviate the aliasingproblem.

• Idea: Replace binary fill/no-fill decision with saturation-based filling.

Page 19: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 271

Visualization and Computer Graphics LabJacobs University

Anti-aliasing

for each fragment of the framebuffer• Compute the percentage, by which the fragment is

covered by the edge.• Fill the fragment with a color, whose saturation

reflects the coverage percentage.

Remark:• In order to compute the coverage, one must assign a

certain thickness to the edge.

Page 20: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 272

Visualization and Computer Graphics LabJacobs University

Anti-aliasing

Example:• When scan converting a black edge in

front of a white background, the blackedge will be drawn using grayscalecolors.

• When scan converting a blue edge in front of a red background, the greenedge will be drawn using a color palettethat successively mixes red and blue.

Page 21: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 273

Visualization and Computer Graphics LabJacobs University

Aliasing and anti-aliasing

Page 22: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 274

Visualization and Computer Graphics LabJacobs University

Anti-aliasing of polygonal meshes

Page 23: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 275

Visualization and Computer Graphics LabJacobs University

Anti-aliasing of entire scenes

Page 24: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 276

Visualization and Computer Graphics LabJacobs University

Moiré effect and anti-aliasing

Page 25: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 277

Visualization and Computer Graphics LabJacobs University

Coverage computation

• How do we compute the coverage?• Geometrically:

– precise– expensive

• Speed is more important than accuracy-> compute an approximate solution!

Page 26: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 278

Visualization and Computer Graphics LabJacobs University

Supersampling anti-aliasing

Idea: • Sample each fragment multiple times.• Determine at each sample position, whether the

sample lies inside or outside the object.• The percentage of samples lying inside the object

approximates the coverage of that fragment by theobject.

Page 27: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 279

Visualization and Computer Graphics LabJacobs University

Subpixel supersampling

• Divide each fragment into a number of subpixels.• Apply standard scan conversion to subpixel image.• Determine coverage as percentage of colored

subpixels of each fragment.• Fill the fragment with the edge color using the

respective percentage as saturation.

fragment subpixels scanconversion

fragmentwith 75% saturation

Page 28: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 280

Visualization and Computer Graphics LabJacobs University

Subpixel supersampling

Remarks:• 2x2 or 4x4 subpixel supersampling are common.• Sometimes a rotated subpixel grid is used (rotation

by 45°) to eliminate artifacts occurring for regularpatterns (cf. Moiré effect).

Page 29: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 281

Visualization and Computer Graphics LabJacobs University

Stochastic supersampling• Select a number a sample locations (pseudo-)randomly

within each fragment.• Perform an inside-outside test for each sample.• Determine coverage as percentage of samples

detected as lying inside the object.• Fill the fragment with the edge color using the

respective percentage as saturation.

fragment stochasticsampling

scanconversion

fragmentwith 60% saturation

Page 30: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 282

Visualization and Computer Graphics LabJacobs University

Stochastic supersampling

Remarks:• Stochastic supersampling further decreases the risk

of artifacts due to regular pattern.• Its performance depends on the computation time for

the inside/outside test.• It is suitable for implicit surfaces.

– Example: For spheres one would only have to compare thedistance of the sample to the sphere‘s center with its radius.

Page 31: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 283

Visualization and Computer Graphics LabJacobs University

Supersampling on shaded objects

• Average of inside/outside property at samples to obtain coverage percentage (as before).

• Average color property at samples to obtain color forfragment filling.

Page 32: 3.3 Polygon filling - faculty.jacobs-university.de · Polygon filling • Polygon filling treats the drawing of polygonal faces after being projected to the screen space. • The

320322: Graphics and Visualization 284

Visualization and Computer Graphics LabJacobs University

Supersampling hardware implementations• Subpixel supersampling (GeForce 2):

– Increased memory requirements.• Multisampling anti-aliasing (GeForce 3):

– Use a mask with 5 or 9 samples to compute coverage.– The mask determines the sampling pattern.– Use only 2 or 4 of these samples to compute shaded color.

• Coverage sampling anti-aliasing (GeForce 8):– Introducing coverage samples to compute coverage.– Decouple shaded color samples and coverage samples.