Computer Graphics
Lecture 6
Rasterization
Taku Komura
1
Rasterization
• After projection, the polygons are still in the
continuous screen space
• We need to decide which pixels to lit how much
• This is called rasterization (or scan conversion)
• We have done this for lines already
(in lecture 4)
Let’s do it for polygons now
2
Overview
Rasterization
• Scanline algorithm
• Rasterizing triangles
• Interpolation by barycentric coordinates
• Mean value coordinates
• Dividing polygons into triangles
Scanline algorithm
A traditional approach of rasterization
Filling in the pixels within the polygon along
the scan line
Scanline algorithm
For each scan line:
1. Find the intersections of the scan
line with all edges of the polygon.
2. Sort the intersections in the
increasing order of the x
coordinate.
3. Fill in all pixels between pairs of
intersections.
Can also deal with concave polygons
Span extrema
Only turn on pixels whose centers are
interior to the polygon:
Otherwise will intrude other adjacent polygons
round up values on the left edge of a span,
round down on the right edge
midpoint algorithm interior
Computing the Interaction of Scan Lines
and Edges
• Computing the intersection of lines is
expensive
• Also requires floating point arithmatic
• Make use of edge coherence;
– Edges that intersected the previous scan line are
likely to intersect the next scan line
Edge Coherence
Use a method similar to the midpoint algorithm
𝑦−𝑏
𝑦 = 𝑚𝑥 + 𝑏, 𝑥=
𝑚
𝑠−𝑏
At 𝑦 = 𝑠, 𝑥𝑠 = 𝑚
𝑠+1−𝑏 1
At 𝑦 = 𝑠 + 1, 𝑥𝑠+1 = = 𝑥𝑠 + 𝑚
𝑚
1
Incremental calculation: 𝑥𝑠+1 = 𝑥𝑠 + 𝑚
How to avoid floating point
arithmatic?
1 𝑥𝑚𝑎𝑥 −𝑥𝑚𝑖𝑛
• 𝑥𝑠+1 = 𝑥𝑠 + = 𝑥𝑠 +
𝑚 𝑦𝑚𝑎𝑥 −𝑦𝑚𝑖𝑛
• We can keep track of the integer part of 𝑥𝑠
(defined as X) as well as the numerator part
(defined as N)
• Increase the X every time there is an overflow
(N > 𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛 )
Example
• (𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 ) = (0,0)
• (𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) = (2,5)
5
𝑦= 𝑥
2
As s increases, Xs will be
2 4 1 3
0, , , 1 , 1 , 2, . .
5 5 5 5
𝑥1 = 1, 𝑥2 = 1,
Pseudo code of computing the left
1 xmax xmin
m ymax ymin span extrema
int numerator xmax xmin
int denomenator ymax ymin
int N denomenator
for ( y ymin ; y ymax ; y ){
WritePixel ( x,y );
N numerator ;
if ( N denomenator ) {
/ * Overflow * /
X ;
N - denomenator;
}
}
Managing the Edges
• We need to manage the edges as they are used to
determine the area to be lit
• We prepare two lists of edges for this purpose
– Global Edge Table (all the egdes in the scene)
– Active Edge Table (the edges in the current scan
line)
Global Edge Table
– Edges are bucket sorted in the Global Edge Table
according to their minimum Y
Global Edge Table
• When the current scan line reaches the lower
endpoint of an edge (𝑦𝑚𝑖𝑛 ) it becomes active.
– Added into the Active Edge Table
• When the current scan line moves above the
upper endpoint (𝑦𝑚𝑎𝑥 ), the edge becomes
inactive.
– Removed from the Active Edge Table
Active Edge Table
•Active edges are sorted according to increasing X.
•Filling in pixels between left most edge intersection
and stops at the second.
•Restarts at the third intersection and stops at the fourth.
Polygon fill rules
(To ensure consistency)
Horizontal edges: Do not include in edge table
Vertices: If local max or min, then count
twice, else count once.
If pixel is on edge, only draw left / bottom
edges
Rasterizing Triangles
• Triangles are much easier to handle
• Always convex
• Always on a plane
• Never self-intersects
• Easy to interpolate data
• In many implementations, polygons with
more than three edges are divided into
triangles first
17
Triangle Rasterization by
Barycenteric Coordinates
Barycentric coordinates
• Can check whether a pixel is inside / outside
the triangle
• Can interpolate the attributes at the vertices
• Often used in modern graphics cards
• Can be easily parallelized
Triangle Rasterization
Consider a 2D triangle with vertices 𝒑𝟎 , 𝒑𝟏 , 𝒑𝟐 .
Let 𝒑 be any point in the plane, it can be expressed
by
𝒑 = 𝒑𝟎 + 𝛽 𝒑𝟏 − 𝒑𝟎 + 𝛾 𝒑𝟐 − 𝒑𝟎
= 1 − 𝛽 − 𝛾 𝒑𝟎 + 𝛽 𝒑𝟏 + 𝛾𝒑𝟐
= 𝛼𝒑𝟎 + 𝛽 𝒑𝟏 + 𝛾𝒑𝟐
𝛼 + 𝛽 +𝛾 = 1, 𝛼, 𝛽, 𝛾 ∈ ℛ
19
Barycentric Coordinates
We will have 𝛼, 𝛽, 𝛾 ∈ [0,1] if and only if p is inside
the triangle.
We call the 𝛼, 𝛽, 𝛾 the barycentric coordinates of
p.
20
Computing Barycentric Coordinates
The triangle is composed of 3 points
p0 (x0,y0), p1 (x1, y1), p2(x2,y2).
For point (x,y), its barycentric coordinates can be
computed by
where
Bounding Box of a Triangle
Calculate a tight bounding box for a triangle:
simply calculate pixel coordinates for each
vertex, and find the minimum/maximum for
each axis
min (x0,x1,x2), max (x0,x1,x2)
min (y0,y1,y2), max (y0,y1,y2)
22
Scanning inside the triangle
Once we've identified the bounding box, we
loop over each pixel in the box.
For each pixel, we first compute the
corresponding (x, y) coordinates in the
canonical view volume
Next we convert these into barycentric
coordinates for the triangle being drawn.
Only if the barycentric coordinates
are within the range of [0,1], we
plot it
23
Interpolation by Barycentric
Coordinates
We can use the barycentric coordinates to
interpolate attributes of the triangle
vertices
• color, depth, normal vectors, texture
coordinates c1 c2
(α,β,γ)
αc1+βc2 +γc3
c3
Interpolation of Color
• We can compute the color at the vertices
(computed using the lighting equation) and
interpolate the color on the surface of the
triangle
• This is called Gouraud shading
c1 c2
(α,β,γ)
αc1+βc2 +γc3
c3
Interpolation of Depth
• When triangles are overlapped, need to
compute the depth at each pixel
• Can be computed by barycentric coordinates
• Compare the depth of the pixel at different
triangles and only show the closest one
d1
• This is called Z-buffering d2’
d1’ (α,β,γ) d2
α'd1’+β’d2’+γ’d3’ α d1+β d2 +γ d3
d3’
d3
Exercise
1. What are the barycentric
coordinates at point A and B?
2. What is the depth of the triangle
surface at point B?
What about polygons with many
vertices?
• Can we compute barycentric coordinates for polygons
with more vertices?
• Can we compute barycentric coordinates for 3D
meshes?
-> Mean value coordinates
Harmonic coordinates
(generalized barycentric coordinates)
Mean Value Coordinates
• A good and smooth barycentric coordinates
that can
• smoothly interpolate the boundary values
• Also works with concave polygons
• There is also a 3D version
Mean Value Coordinates
• Can interpolate convex and concave polygons
• Smoothly interpolate the interior as well as
exterior
Mean Value Coordinates
• Can interpolate convex and concave polygons
• Smoothly interpolate the interior as well as
exterior
Mean Value Coordinates
• Can be computed in 3D
• Applicable for mesh editing
Polygon Decomposition
However, for polygons with more than four vertices,
we usually decompose them into triangles
P6 Simple for convex polygons.
P7 P5
Concave more difficult.
P4
P0
P1 P3
P2
Polygon Decomposition:
Algorithm
Start from the left and form the leftmost triangle:
• Find leftmost vertex (smallest x) – A
• Compose possible triangle out of A and the two
adjacent vertices B and C
• Check to ensure that no other polygon point P is
inside of triangle ABC
• If all other polygon points are outside of ABC
then cut it off from polygon and proceed with
next leftmost triangle
Polygon decomposition (2)
• The left most vertex A
• A triangle is formed by A and the two adjacent
B and C
• Check if all the other vertices are outside the
triangle
Polygon decomposition (3)
If a vertex is inside, form a new triangle with leftmost
inside vertex and point A, proceed as before.
Reading for rasterization
Scanline algorithm
Foley et al., Chapter 3.5, 3.6
Baricentric coordinates
www.cs.caltech.edu/courses/cs171/bar
ycentric.pdf
Mean value coordinates for closed triangular meshes,
SIGGRAPH 2005
Polygon decomposition
http://www.siggraph.org/education/mater
ials/HyperGraph/scanline/outprims/pol
ygon1.htm 37