+
CPCS 391 Computer Graphics 1 Lecture 5: Polygon Filling
Instructor: Dr. Sahar Shabanah
+
23-D Graphics Rendering Pipeline
Normalized view space Modeling
Transformation Viewing Transformation
Lighting & Shading
Clipping
Projection
Scan conversion, Hiding
Primitives
Image
Object space
World space Camera space
Image space,
Device coordinates Culling
(I added this step to the diagram)
Today
+ 2D Object Definition
Lines and Polylines: lines drawn between ordered points
Same first and last point make closed polyline or polygon
If it does not intersect itself, called simple polygon
+ 2D Object Definition
Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon.
Concave: Not convex: some two points in the polygon are joined by a line not fully contained in the polygon.
Special Polygons
triangle square
rectangle
+ 2D Object Definition
Circles
Consist of all points equidistant from one predetermined point (the center)
(radius) r = c, where c is a constant
On a Cartesian grid with center of circle at origin equation is r2 = x2 + y2
Circle as polygon
A circle can be approximated by a polygon with many sides (>15)
P
0P
1r
0 y
x
r
+
0 1 1
2 2
3 4 5 6 7 8 9 10
3 4 5 6
0 1 1
2 2
3 4 5 6 7 8 9 10
3 4 5 6
Example: height, on y-axis, remains 3, while length, on x-axis, changes from 3 to 6
2D Object Definition
(Aligned) Ellipses
A circle scaled along the x or y axis
+ 2D Shape Representation: Vertex-Edge Table
General purpose, simple overhead, reasonable efficiency
Each vertex listed once
Each edge is ordered pair of indices into vertex table
Sufficient information to draw shape and perform simple operations.
Order does not matter, convention is edges listed in counterclockwise order.
Vertexes 1 (0,0) 2 (1,0) 3 (0,1) 4 (1,1) 5 (0.5,1.5)
Edges 1 (1,2) 2 (2,4) 3 (4,5) 4 (5,3) 5 (3,1)
+ 2D Shape Representation: Splines
How they work: Parametric curves governed by control points
Mathematically: Several representations to choose from. More complicated than vertex lists.
Simple parametric representation:
Advantage: Smooth with just a few control points
Disadvantage: Can be hard to control
Uses:
representation of smooth shapes. Either as outlines in 2D or with Patches or Subdivision Surfaces in 3D
animation Paths for tweening
approximation of truncated Gaussian Filters
1 02 2 3
3
0 1
2 2 3
3
b t
b t
b t
b t
y
a t
a t
a t
a t
x
+ 2D to 3D Object Definition
Vertices in motion
(“Generative object description”)
Line
is drawn by tracing path of a point as it moves (one
dimensional entity)
Square
drawn by tracing vertices of a line as it moves perpendicularly to itself (two dimensional entity)
Cube
drawn by tracing paths of
vertices of a square as it moves perpendicularly to itself (three- dimensional entity)
Circle
drawn by swinging a point at a
fixed length around a center
point
+ Building 3D Primitives
Triangles and tri-meshes
Parametric polynomials, like the aforementioned splines used to
define surface patches.
+ Triangle Meshes
Most common representation of shape in three dimensions
All vertices of triangle are guaranteed to lie in one plane (unlike quadrilaterals or other
polygons)
Uniformity makes it easy to perform mesh operations: subdivision, simplification, etc.
Many different ways to represent triangular meshes:
http://en.wikipedia.org/wiki/Polygon_mesh
+ 3D Shape Representation Vertex-Triangle Tables
Each vertex gets listed once
Each triangle is ordered triple of indices into the vertex table
Edges between vertices inferred from triangles
Only need the triangular mesh representations to draw the shapes;
Counterclockwise ordering of vertices for normals
+ Polygon Fill Algorithms
A standard output primitive in general
graphics package is a solid color or patterned polygon area.
There are two basic approaches to filling on raster systems.
Scan line polygon fill: determine overlap Intervals for scan lines that cross that area, mostly used in general graphics packages
Seed fill: start from a given interior point and
paint outward from this point until we encounter the boundary, used in applications having
complex boundaries and interactive painting
systems
+ Seed Fill Algorithm
These algorithms assume that at least one pixel interior to a polygon or region is known
Regions maybe interior or boundary defined
Interior-defined region Interior-defined region
+
For each scan line crossing a polygon are then sorted from left to right, and the corresponding frame buffer positions between each intersection pair are set to the specified color.
These intersection points are then sorted from left to right , and the
corresponding frame buffer positions between each intersection pair are set to specified color
In the example, four pixel intersections define stretches from x=10 to x=14 and x=18 to x=24
Scan Line Polygon Fill Algorithm
Interior pixels along a scan line passing through a polygon area
10 14 18 24 Xk+1,yk+1
Xk , yk
Scan Line yk +1 Scan Line yk
+ Scan Line Polygon Fill Algorithm
Maintaining a data structure of all intersections of polygons with scan lines
Sort by scan line
Fill each span
For each scan line:
Find the intersections of the scan line with all edges of the polygon.
Sort the intersections by increasing x- coordinate.
Fill in all pixels between pairs of intersections.
Problem:
Calculating intersections is slow.
Solution:
Incremental computation / coherence
+ Scan Line Polygon Fill Algorithm
Edge Coherence
Observation:
Not all edges intersect each scanline.
Many edges intersected by scanline i will also be intersected by scanline i+1
Formula for scanline s is y = s,
for an edge is y = mx + b
Their intersection is
s = mx
s+ b x
s= (s-b)/m
For scanline s + 1,
x
s+1= (s+1 - b)/m= x
s+ 1/m
Incremental calculation: xs+1 = xs + 1/m
+ Scan Line Polygon Fill Algorithm
Problem of Scan-Line intersections at polygon vertices:
A scan Line passing through a vertex intersects two polygon edges at that position, adding two points to the list of intersections for the scan Line
In the example, scan Line y intersects 5 polygon edges and the scan Line y‘
intersects 4 edges although it also passes through a vertex
y‘ correctly identifies internal pixel spans, but y need some extra processing
1
1 2 1
2 1 1
Scan Line y1
Scan Line y
+ Scan Line Polygon Fill Algorithm
Solution 1:
shorten some polygon edges to split those vertices that should be counted as one intersection
a) When the end point y coordinates of the two edges are
increasing, the y value of the upper endpoint for the current edge is decreased by 1
b) When the endpoint y values are monotonically decreasing, we decrease the y coordinate of the upper endpoint of the edge following the current edge
(a) (b)
+ Scan Line Polygon Fill Algorithm
When a scan line intersects an edge endpoint (vertex), it intersects two edges.
Two cases:
Case A: edges are monotonically increasing or decreasing
Case B: edges reverse direction at endpoint
In Case A, we should consider this as only ONE edge intersection
In Case B, we should consider this as TWO edge intersections
Scan- line
Case A
Scan-line
Case B
+
Scan Line yk + 1
Scan Line yk
(Xk + 1, Yk + 1)
(Xk , Yk )
Scan Line Polygon Fill Algorithm
The Algorithm Steps:
Intersect each scanline with all edges using the iterative coherence calculations to obtain edge intersections quickly
Sort intersections in x
Calculate parity of intersections to determine in/out
Fill the “in” pixels
Special cases to be handled:
Horizontal edges should be excluded
For vertices lying on scanlines,
count twice for a change in slope.
Shorten edge by one scanline for no change in
slope
+ Scan Line Polygon Fill Algorithm
Need 2 data structures: Edge Table and Active Edge Table
Traverse Edges to construct an Edge Table
In this table, there is an entry for each scan-line.
Add only the non-horizontal edges into the table.
For each edge, we add it to the scan-line that it begins with (that is, the scan-line equal to its lowest y-value).
Each scan-line entry thus contains a sorted list of edges. The edges are sorted left to right. (To maintain sorted order, just use insertion based on their x value.)
Add edge to linked-list for the scan line corresponding to the lower vertex.
Store the following:
y_upper: last scanline to consider
x_lower: starting x coordinate for edge
1/m: for incrementing x; compute
+ Active Edge List
Process the scan lines from bottom to top to construct Active Edge Table during scan conversion.
Maintain an active edge list for the current scan-line.
When the current scan line reaches the lower / upper endpoint of an edge it becomes active.
When the current scan line moves above the upper / below the lower endpoint, the edge becomes inactive
Use iterative coherence calculations to obtain edge intersections quickly.
AEL is a linked list of active edges on the current scanline, y.
Each active edge line has the following information
y_upper: last scanline to consider
x_lower: edge’s intersection with current y
1/m: x increment
The active edges are kept sorted by x
+ Scan Line Polygon Fill Algorithm
1.
Set y to the smallest y coordinate that has an entry in the ET; i.e, y
for the first nonempty bucket.
2.
Initialize the AET to be empty.
3.
Repeat until the AET and ET are empty:
1. Move from ET bucket
y
to the AET those edges whosey_min = y
(entering edges).
2.
Remove from the AET those entries for which y = y_max (edges
not involved in the next scanline), the sort the AET on x (made easier because ET is presorted
3.
Fill in desired pixel values on scanline y by using pairs of x
coordinates from AET.
4.
Increment y by 1 (to the coordinate of the next scanline).
5.
For each nonvertical edge remaining in the AET, update x for the
new y .
+ Polygon fill example
1. Set y to smallest y with entry in ET, i.e., y for the first non-empty bucket
2. Init Active Edge Table (AET) to be empty
3. Repeat until AET and ET are empty:
1. Move form ET bucket y to the AET those edges whose ymin=y (entering edges)
2. Remove from AET those edges for which y=ymax (not involved in next scan line), then sort AET (remember: ET is presorted)
3. Fill desired pixel values on scan line y by using pairs of x-coords from AET Increment y by 1 (next scan line)
4. For each nonvertical edge remaining in AET, update x for new y