Rasterization
Rasterization
Algorithms
Kalpathi Subramanian
Computer Science
The University of North Carolina at Charlotte
Learning Objectives
Explain and provide examples of aliasing artifacts in computer
graphics renderings.
Distinguish between pre and post filtering approaches to controlling
aliasing artifacts.
Distinguish between weighted and unweighted area sampling
based anti-aliasing techniques.
Explain the concepts of aliasing and anti-aliasing from a frequency
domain perspective.
Raster Algorithms
Topics
Drawing algorithms: Lines and Circles
Filling Algorithms: Boundaryfill, flood fill, polygon scanline
Clipping Algorithms:
▶ Line Clipping - Cohen-Sutherland, Liang-Barsky algorithms
▶ Polygon - Sutherland-Hodgman
Visible Surface Determination
▶ Classes of visible surface algorithms
▶ Ray Casting
▶ Backface Culling
▶ Z-Buffer (depth buffer) algorithm
▶ BSP Tree based algorithm
▶ Other algorithms (Depth Sort, Scanline, Warnock’s Area Subdivision)
Aliasing
Raster Algorithms
A
Analytic Scan−converted
Line Equation:
y = mx + b
m = (y1 − y0 )/(x1 − x0 )
for i = x0 to i = x1 {
yi = mxi + b
PLOT (xi , ROUND ( yi ))
}
Algorithm
m = (y1 − y0 )/(x1 − x0 )
for xi = x0 to xi = x1 {
plot ( xi , Round(yi ))
yi = yi + m
xi = xi + 1
PLOT (xi , yi ) }
Features
Uses only integer and shift operations.
Uses incremental calculations to determine successive pixels.
For lines and circles, same as Bresenham’s algorithm.
M
i+1
A C
M M
i i+1
x ,y B D
p p
M
i+1
A C
M M
i i+1
x ,y B D
p p
y = (dy /dx).x + B
dx.y = dy .x + B.dx, or,
F (x, y ) = dy .x − dx.y + B.dx = 0
= a.x + b.y + c = 0, (a = dy, b = -dx, c = B.dx)
d = F (x, y ) = 0, x, y on the line
> 0, x, y below the line
< 0, x, y above the line
M
i+1
A C
M M
i i+1
x ,y B D
p p
Case 2: B is chosen
M
i+1
A C
M M
i i+1
x ,y B D
p p
d = F (x0 + 1, y0 + 1/2)
= a(x0 + 1) + b(y0 + 1/2) + c
= (ax0 + by0 + c) + a + b/2
= a + b/2
or
Hence
d = 2 dy - dx
d1 = 2 dy
d2 = 2 dy - 2 dx
for x = x0 to x1 by 1 {
if (d ≤ 0)
d = d + d1
else{
d = d + d2
y=y+1
}
PLOT (x, y)
}
Very efficient, involves only additions and shifts by 2
Will need to switch roles of X and Y for orientations > 45◦ , and
similarly for the remaining 3 quadrants
Drawing Circles
Rotational symmetric: Only 1 octant needs to be scan converted
(0, R)
(x, y)
(−x, y)
(y, x) x ,y
p p A
(y,−x)
M M
i i+1
(R, 0) C
B
M’
i+1
(−y,x)
(−y,−x)
D
I3
0
j
A
B
Define a function
(1 − s)(1 − t) + (1 − s)tB + sC = 1
αA + βB + γC , where α + β + γ = 1
Given a clip region and a line segment, determine the sections of the
line interior to the region
Clip Window
E
Line Segment
C
B
But
We deal with line segments, not infinite lines.
Slope-Intercept form (y = mx + c) of line equation is not a
convenient representation.
Parametric representation preferred.
Approach is naive and inefficient.
Cohen-Sutherland Clipping Algorithm
Inside
Outside
Define In and Out half-spaces for each boundary segment of the clip
window.
clip region = Intersection of all In spaces.
Each In/Out region is represented by a 1 bit code.
Cohen-Sutherland Clipping Algorithm
Given: line end points: (x0 , y0 ), (x1 , y1 ), and clip Boundaries:
xmin , xmax , ymin , ymax
1001 0001 0101
L R B T
Encoding a point:
code0[0] = x0 < xmin
code0[1] = x0 > xmax
code0[2] = y0 < ymin
code0[3] = y0 > ymax
and similarly for (x1 , y1 )
Cohen-Sutherland Clipping Algorithm
Algorithm
Trivial Accept: No
Trivial Reject: ( 1000 & 0001 ) = 0000 No
Pick end point with non-zero code : A (can also pick B)
Intersect with left boundary (corresponding to the first non-zero bit)
t xmin −x0 xmin −x0
1.0 = x1 −x0 , t = x1 −x0
−x0
y = y0 + t(y1 − y0 ) = y0 + xxmin
1 −x0
(y1 − y0 )
Intersection I1 = (xmin , y ), x0 = xmin , y0 = y
Cohen-Sutherland Clipping Algorithm
An Example
Output P Output I
P I S P
We can use the algorithm to clip polygons agains the 3D view volume
in Clipping space.
Clipping against the front (z = −n) and back (z = −f ) is efficient as
it is axis aligned
Clipping agains the side (pyramidal) planes takes more operations per
vertex, so it would be more efficient to do so after the normalizing
transform, when the volume is the NDC cube and all planes are axis
aligned.
Visible Surface Algorithms
Algorithm Classification
Ray Casting: Image space algorithm, but visibility tests are at object
precision.
Z Buffer: Image space algorithm.
BSP Tree: Object space algorithm, depth ordering determined in
object space, followed by scan conversion.
Warnock’s Area Subdivision: Image space algorithm.
Depth Sort: Object space algorithm, followed by scan conversion
Scanline Algorithm: Image space algorithm
Visible Surface Algorithms
The most widely used algorithm for computing the visible surface and
implemented in modern graphics hardware.
A buffer of Z or depth values is maintained, one for each pixel, in
addition to pixel colors.
During scan conversion, if primitive’s depth is less (closer) than the
existing value in the Z buffer, replace it and corresponding color
(intensity) in the frame buffer.
Each primitive is scanconverted once (visible or not), independent
of any other primitive, and in any order.
Visible Surface Algorithms
f o r ( each p r i m i t i v e ) {
f o r ( each p i x e l i n p r o j e c t i o n ) {
Z = p r i m i t i v e ’ s depth v a l u e at p i x e l ( x , y ) ;
i f Z < Z {BUF } [ y ] [ x ] {
Z {BUF } [ y ] [ x ] = Z
Write Pixel (x , y , pixel \ color )
}
}
}
Z (Depth) Buffer Algorithm
Ax + By + Cz + D = 0
−Ax − By − D
z(x, y ) =
C
Let z(x, y ) = zc
−A(x + ∆x, y ) − By − D
z(x + ∆x, y ) =
C
A
= zc − (∆x)
C
A
= zc −
C
which involves just an increment from pixel to pixel
Z (Depth) Buffer Algorithm
Calculating Z: Can also exploit edge coherence along Y (from scanline
to scanline)
Note that from the scanline algorithm, x coordinate varies as
1
x′ = x − // assumes scanlines are top to bottom
m
Thus,
A(x) + By + Cz + D = 0
A(x − 1/m) + B(y + 1) + Cz ′ + D = 0
Algorithm traverses the tree and solves the visibility problem with
respect to the hyperplane stored at each tree node.
Example: Assume the viewer is in region b, looking towards
hyperplane (3-6):
▶ Start at root, (3,6).
▶ Process all objects on the far side of (3, 6) (right subtree of (3,6),
process (3, 6) and then the near side (left subtree of (3,6)
▶ Visit 4; will process far side of 4 (right subtree of 4), then 4, then left
subtree of 4
▶ Visit 2b, 1, process (3, 6), 2a, 5.
Binary Space Partitioning Trees
Advantages/Tradeoffs
The BSP tree creates a view independent order for polygons that
can be exploited by rendering algorithms to paint the faces back to
front.
Polygons are rendered accurately back to front, as faces in front
cannot obscure those that are behind.
Classification is performed recursively, resulting in generating a
priority order of polygons, for a given viewer position.
The BSP tree needs to be rebuilt, if objects move from frame to
frame.
Given a back to front order, transparency can be handled accurately,
unlike most other visibility algorithms.
Aliasing and Antialiasing
What is Aliasing?
“Errors and Artifacts arising during rendering, due to the conversion
from a continuously defined illumination field to a discrete raster grid
of pixels”
What is Aliasing?
What is Aliasing?
What is Aliasing?
Effects of Aliasing?
Effects of Aliasing?
Effects of Aliasing?
Effects of Aliasing?
Effects of Aliasing?
Anti-aliasing
Properties
Intensity of pixel decreases as the distance between the pixel center
and primitive increases.
A primitive cannot influence a pixel’s intensity if it does not intersect
it.
Equal areas (intersected) contribute equal intensity – not a desirable
property.
Weighted Area Sampling
Images are just a 2D signal and jagged edges are due to the pixel
sampling rate not being high enough to capture the “real” signal.
Sampling (Frequency Domain)