Rasterization, Clipping, Visibility, Antialiasing
Algorithms
Kalpathi Subramanian
Computer Science
The University of North Carolina at Charlotte
Last Modified: November, 2024
Rasterization (Drawing Algorithms, Clipping, Visible
Surfaces, Aliasing)
Learning Objectives
Explain the principles underlying drawing algorithms for as lines,
circles and polygons that contribute to their efficient implementation.
Compare and contrast the Cohen-Sutherland and Liang-Barsky line
clipping algorithms, in terms of their efficiency and tradeoffs.
Distinguish between line clipping and polygon clipping algorithms.
Explain and distinguish between image space and object space
visible surface detection.
Explain the popularity of the Z-Buffer visible surface detection
algorithm, how its derives its efficiencies and tradeoffs in comparison
to other competing algorithms
Explain how the BSP tree algorithms compute the visible surface,
its applications, and its advantages over the Z-buffer algorithm.
Rasterization (Drawing Algorithms, Visible Surfaces,
Aliasing)
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
“The process of converting geometric primitives into their discrete
approximations”
B
A
Analytic Scan−converted
Scan Conversion: Approximating analytically defined geometric
primitives by a set of pixels, to be stored in the frame-buffer memory.
Clipping: Only sections of primitives determined to be within a
clipping region is drawn.
Speed: Algorithms must be very efficient.
Drawing Lines
Given: Line end points (x0 , y0 ) and (x1 , y1 )
Line Equation:
y = mx + b
where m is the slope and b is the Y intercept.
Goal: To determine the sequence of points between (x0 , y0 ) and
(x1 , y1 ) on the raster grid (end points are in screen coordinates).
Drawing Lines
Brute Force Algorithm
m = (y1 − y0 )/(x1 − x0 )
for i = x0 to i = x1 {
yi = mxi + b
PLOT (xi , ROUND ( yi ))
}
Inefficient: Involves division, multiplications and rounding.
DDA Algorithm
Line equation is linear, can exploit incremental computation for a
more efficient algorithm
yi+1 = mxi+1 + b = m(xi + ∆x) + b
= (mxi + b) + m∆x
= yi + m(1) = yi + m
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 ) }
Still inefficient, involves division to compute slope, rounding.
MidPoint Line Algorithm
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
Idea: Determine relationship of mid points, Mi in the pixel grid with
respect to the line segment, and select the closer pixels to be rendered. In
the above example, A and C will be picked.
MidPoint Line Algorithm
Characteristic Function, F(x,y)
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
The sign of d = F (x, y ), the decision variable, will determine whether A
or B is chosen as the next pixel
MidPoint Line Alg.: Incremental Calculation of d
Case 1: A is chosen
M
i+1
A C
M M
i i+1
x ,y B D
p p
F (Mi ) = F (xp + 1, yp + 1/2)
F (Mi+1 ) = F (xp + 2, yp + 3/2)
F (Mi+1 ) − F (Mi ) = F (xp + 2, yp + 3/2) − F (xp + 1, yp + 1/2)
= {a(xp + 2) + b(yp + 3/2) + c} −
{a(xp + 1) + b(yp + 1/2) + c}
= (a + b)
F (Mi+1 ) = F (Mi ) + (a + b) = F (Mi ) + dy − dx
MidPoint Line Alg.: Incremental Calculation of d
Case 2: B is chosen
M
i+1
A C
M M
i i+1
x ,y B D
p p
F (Mi+1 ) = F (xp + 2, yp + 1/2)
F (Mi+1 ) − F (Mi ) = F (xp + 2, yp + 1/2) − F (xp + 1, yp + 1/2)
= a
F (Mi+1 ) = F (Mi ) + (a)
= F (Mi ) + dy
MidPoint Line Algorithm
Initializing the Algorithm (assumes line is oriented at less than 45◦ )
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
d = 2a + b (only sign of d is important)
Hence
d1 = 2(a + b) = 2(dy − dx)
d2 = 2a = 2(dy )
MidPoint Line Algorithm
Final Algorithm (Quadrant 1 only, slope < 45 deg.)
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
(−x, −y) (x, −y)
MidPoint Circle Algorithm
F (x, y ) = x 2 + y 2 − R 2 = 0 (circle centered at the origin)
d = F (x, y ) = 0, x, y on the circle
> 0, x, y outside the circle
< 0, x, y inside the circle
A similar analysis can be performed to find midpoint and decision variable
updates.
The Polygon Filling Algorithms
Old Approaches:
E
C
I I1 I2
I3
0
j
A
B
Scaneline based, exploiting the geometry of the polygon.
Use scanline and edge coherence for efficient filling.
Require computing intersections between scanline and polygons - but
can be done efficiently
Smooth shading using these algorithms caused artifacts such as
Mach bands, due to a lateral inhibition property of the human visual
system.
Steps in shading tende to overshoot and undershoot on either side of
a step change in intensity
More Modern Approaches
Graphics drivers primarily render triangles, not general polygons,
and thus, can optimize.
Triangles tend to be small (in very large models), and hence
advantages of scanline algorithms decline.
Use of barycentric coordinates, more accurate
interpolants/smoothing functions that minimize shading artifacts.
Ray Casting Based Approach
In screen space, cast a ray from the camera (at infinity) to center of
each pixel and find intersection with object model
Raycasting and Ray Tracing can use spatial data structures to
improve performance, but are still expensive
Ray tracing support provided in modern GPUs
Can instead use a combination of depth buffer and polygon projection
to improve efficiency (by discarding those polygons away from the
pixel center).
Rendering Triangles Using Barycentric Coordinates
A means towards accurate interpolation of polygonal meshes
Interpolant that is consistent across boundaries of meshes and avoids
artifacts such Mach Banding.
Can be computationally more expensive.
Rendering Triangles Using Barycentric Coordinates
What are barycentric coordinates?
Consider the parameterization of the points within a triangle:
R = (1 − s)Q + sC , Q = (1 − t)A + tB // s, t ∈ (0, 1)
Thus, R = (1 − s)(1 − t)A + (1 − s)tB + sC
Define a function
F : [0, 1] × [0, 1] → R2 : (s, t) → (1 − s)(1 − t)A + (1 − s)tB + sC
which maps to the triangle ABC .
Barycentric coordinates: Properties
The coefficients add upto 1!
(1 − s)(1 − t) + (1 − s)tB + sC = 1
These coefficients serve as weights to enumerate the points of ABC ,
αA + βB + γC , where α + β + γ = 1
α = 0 represent points on the edge BC, β = 0 on AC and γ = 0 on
AB.
Lines of constant α, β, γ are parallel to each other.
Area Interpretation. The coefficients are a measure of the
subtriangles they connect to, and a means to computing them.
Computing the barycentric coordinate of a point P
Given a point P (x, y, z), we can form the following system of
equations
αAx + βBx + γCx = Px
αAy + βBy + γCy = Py
αAz + βBz + γCz = Pz
α+β+γ =1
Or, in matrix form,
Ax Bx Cx Px
Ay By Cy α Py
β =
Az Bz Cz Pz
γ
1 1 1 1
Computing the barycentric coordinate of a point P
Solving the system - Issues
Rectangular system to solve
Can use the Normal N as the fourth point - estimates error
Ignore the 4th row and solve 3x3 system.
Better techniques exist that are more expensive and accurate.
Rendering Triangles Using Barycentric Coordinates
When rendering a pixel, its barycentric coordinates need to be
computed.
For perspectively correct interpolation, would require inverse
projecting to camera space and computing rendering attributes.
Bounding boxes can be used to improve performance, processing only
pixels internal to the triangle.
Clipping Lines
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
Simplification: Clip region (window) is an upright rectangle.
Clipping Lines
Strategy
if end points are inside clip region
entire line segment is visible
else
calculate intersections between line segment and clip region
output segments interior to clip region
end
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
Idea: Consider the clipping region to be the intersection of
half-spaces
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
1000 0000 0100
1010 0010 0110
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
Calculate codes of both end points(code0, code1).
done = FALSE
while !done {
if (code0 == 0 && code2 == 0)
“Accept line segment”, done = true // trivial accept
else if ( (code0 & code1) ̸= 0 ) // bitwise logical AND
“Reject line segment”, done = true // trivial reject
else {
choose end point P outside clip window // possible points swap
Intersect line segment with clip window
Discard line segment from P to intersection point
compute codes of the new (clipped) line segment
}
}
Cohen-Sutherland Clipping Algorithm
An Example
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
Code (I1 ) = 0000 , Code (B) = 0001
Cannot trivially accept or reject, choose end point B
Intersect with top boundary, y = ymax
t ymax −y1 ymax −y1
1.0 = y0 −y1 , t = y0 −y1
−y1
x = x1 + t(x0 − x1 ) = x1 + ymax
y0 −y1 (x0 − x1 )
Intersection I2 = (x, ymax )
Calculate codes of I1 and I2 , both zero
Trivially accept the segment (I1 , I2 )
Clipping Polygons
Sutherland-Hodgman Clipping Algorithm
Outside Inside Outside
Inside P
I
P S
Output P Output I
Inside Outside Inside Outside
S
P I S P
Output I and P No Output
Clipping Polygons
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
Object Space Algorithms: Solve the problem in affine space
for each object in the environment {
determine parts of the object unobstructed by itself or
any other object.
Scanconversion to render polygons from a viewing specification.
}
Complexity: O(n2 ), n: number of objects;
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 Ray Casting Algorithm
The ray tracing algorithm, by definition traces rays to the closest
intersection, and by definition, a computation of visibility.
Only the primary ray is cast into the world; shadow rays may be cast
to compute shadows.
Algorithm is significantly more expensive; must use space subdivision
structures to minimize computation.
Octrees, BSP trees, uniform grids are typically used.
Visible Surface Algorithms
Back Face Culling
Back facing polygons can be removed with a simple test by testing
the angle between the view vector and the polygon normal.
Face is visible if cos(θ) = v • n ≥ 0.
Need to only test the sign of the cos(θ)
Visible Surface Algorithms
Z (Depth) Buffer Algorithm
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
Z (Depth) Buffer Algorithm
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
Calculating Z: Scanline Coherence
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
Solve for z’:
A
+B
z′ = z − m
C
which involves an increment to update z
Z Buffer Algorithm
Efficiency and Tradeoffs
Additional memory needed for Z buffer, typically part of the graphics
card.
Z values are usually 32 bits wide or more.
Z buffer is implemented easily in hardware.
Every polygon, whether visible or not must be rendered; a significant
fraction of the geometry might be occluded, resulting in wasted
computation.
Advantage decreases as the polygons reduce in size (or have fewer
and fewer pixels in their projection)
Visible Surface Algorithms
Binary Space Partitioning Trees
A piecewise continuous space representation.
A space subdivision based structure to represent polyhedra.
Represents geometry with dividing hyperplanes.
Convex decomposition of space by recursive subdivision.
Internal nodes contain discontinuities, leaf nodes represent convex
regions.
Visible Surface Algorithms
Binary Space Partitioning Trees: Example
Partitioner Order: [3,6], 2a, 5, 4, 2b, 1
(From Slater, Steed, Chrysanthou, CGVE)
Visible Surface Algorithms
Binary Space Partitioning Trees: Example
a, b, c, d, e, f, g are the convex cells of the decomposition
(From Slater, Steed, Chrysanthou, CGVE)
Binary Space Partitioning Trees
Generating Visibility Order
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
Techniques to control the effects of aliasing
1 Prefiltering (unweighted/weighted area sampling)
2 Postfiltering (supersampling, jittering)
Area Sampling Techniques
Area Sampling Techniques
Area Sampling Techniques
Area Sampling Techniques
Area Sampling Techniques
Unweighted Area Sampling
Pixel intensity is varied in proportion to the area of the pixel
intercepted by the primitive.
Unweighted – equivalent to a box filter of unit height over pixel.
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
Equal areas can contribute unequally in terms of pixel intensity.
Areas closer to the pixel center contribute more.
Essentially results in filtering with a mask that is centered over the
pixel with decreasing radial influence.
Cone filters are a compromise between computational expense and
optimality.
Postfiltering Techniques
Postfiltering Techniques
Supersampling
Postfiltering Techniques
Regular vs. Jittered Sampling
Postfiltering Techniques
Filtering
Postfiltering Techniques
Filtering
Postfiltering Techniques
Filtering Example
Postfiltering Techniques
Filtering Example
Postfiltering Techniques
Filtering Example
Postfiltering Techniques
Filtering Example
Postfiltering Techniques
Filtering Example
Aliasing from a Sampling Theory Viewpoint
Sampling (Spatial Domain)
Sampling (Spatial Domain)
Image is a spatial signal
Sampling (Frequency Domain)
X axis (position): frequency
Y axis (height): strength of each frequency
Examples: sine wave: impulse, square wave: infinite train of impulses
Sampling (Frequency Domain)
How do we get to the Frequency Domain?
By applying the Fourier Transform to the signal
Let ϕ(x) be a continuous function of a real variable x. Then
Z ∞
ℑ{ϕ(x)} = ϕ(ω) = ϕ(x)e −j2πωx dx
−∞
√
is the Fourier Transform of ϕ(x), with j = −1 and,
Z ∞
ℑ−1 {ϕ(ω)} = ϕ(x) = ϕ(ω)e j2πωx dω
−∞
is the Inverse Fourier Transform.
ϕ(x) is continuous and integrable
ϕ(ω) is integrable
x (spatial domain), ω (frequency domain)
Sampling (Frequency Domain)
How does this relate to Computer Graphics?
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)
Sampling Theorem and the Nyquist Rate
Continuous-time signal can be completely recovered from its samples
iff the sampling rate is greater than twice the maximum frequency
present in the signal.”
— Claude Shannon
Also known as the Nyquist rate
Sampling (Frequency Domain)
Nyquist Rate
Sampling (Frequency Domain)
Nyquist Rate: Undersampling
The lower signal is undersampled and results in an aliased wave
(dotted curve).
Reducing Aliasing (Frequency Domain)
Frequency Domain Filtering
Can use different low pass filters to attenuate high frequencies
Examples: Box, Triangle, Gaussian filters