Triangles Rasterization
Final pixels
Triangles Rasterization
Computer Graphics
Scan-line Algorithm
Basic Idea:
1. Sort edges on y
2. For every row
from ymin to ymax1. Find the first fragment A inside
2. Find the first fragment B outside
3. Output all fragments from A included to B
Triangles Rasterization
Scan-line Algorithm
Basic Idea:
1. Sort edges on y
2. For every row
from ymin to ymax1. Find the first fragment A inside
2. Find the first fragment B outside
Bresenham
• For every row:
– Finding hte first fragment inside and the first one outside is similar to Bresenham, but:
• Only one case (iterate on rows)
• Rounding to the smallest integer larger
Terminology in triangle rasterization
• "Scan-Conversion" = Rasterization
• "to scan-convert" = to Rasterize
• "Span" = interval from the first-in to the first-out
• "Line-scan" = processed line
• "Active edges" = edges taken into consideration
• "Edge tables" = edges (precomputed)
3
2
In the case of triangles!
Problems with the scan-line algorithm
• Fragments are produced one at the time
• Not suited for a parallel implementation
Solution
• Produce the fragments in groups
– E.g., groups of 2x2 or 4x4 or 4x1 or 8x1...
• Not all members of a group of fragments will belong to the triangle
• Test each fragment:
Inclusion test for a triangle
• Inclusion test for an half plane:
YES
NO
p n xk
n
x
n
p
x
)
0
(
n
p
k
where TEST:
Line defined by a point p
Inclusion test for a triangle
• The halfplane is defined by:
NO
v0=(x0, y0 )
n
v1=(x1, y1 )
q
The
so-called
EDGE
FUNCTIO
N
n = ( - ( y1 -y0 ) , x1 -x0)
p = (y0 , x0)
f(q) = n‧q - n‧p
Inclusion test for a triangle
• Triangle=intersection of 3 half-planes
Inclusion test for a triangle
• Triangle=intersection of 3 half-planes
NO
v0 YES
NO YES YES YES NO v1 v2 YES YES YES YES NO NO YES NO NO NO YES NO YES
Inclusion test for a triangle
• Swap
v
1 andv
2v2
v1
v0
Back-facing triangles (counterclockwise)
Inclusion test for a triangle
Three Edge Functions: (one per edge)
• All YES:
– Fragment inside a front-facing triangle
• All NO:
– Fragment inside a back-facing triangle
• Mixed results:
Example, using a bounding box
1. Compute the bounding box of the triangle
How to compute the bounding box
Example, using a bounding box
Example, using a bounding box
1. Compute the bounding box of the triangle
2. Process each block (e.g. 2x2) 1. Test each fragment in the
block
Triangles Rasterization
SETUP: compute the bounding box
test a group of fragments (in parallel)
Final pixels
Clipping
• Clipping!
Outside: CULLED !
no prob.
Screen
Inside: Rasterize (simple case)
Partly
overlappin g
Clipping: old approach
• Clipping!
Screen
1. Compute intersections 2. Connect the intersections
A
B
3. Split the polygon into triangles 4. Rasterize each triangle
Clipping: old approach
• Clipping!
Screen
1. Compute intersections 2. Connect the intersections
3. Split the polygon into triangles 4. Rasterize each triangle
B
Clipping: old approach
• Clipping!
Screen
B
C
A
D
Worst case, very complex
Very bad for HW implementations
We cannot ignore this case
1. Compute intersections 2. Connect the intersections
Clipping: modern implementation
• Clipping!
Screen
1. Compute the bounding box 2. Intersect the bounding box
with the screen
3. Rasterize wrt the bounding box, as usual
Watch out!
• What about clipping against the far and near planes?
bottom plane right plane
left plane
near plane
view frustum
top plane
Triangles Rasterization
• The method based on computing the bounding box and the inclusion test (edge functions):
– Pros
• easily parallelizable
– (groups 4x4)
• clipping is easy
– For the planes UP, DOWN, LEFT e RIGHT
– Cons
• Large Overheads
A terrible situation
Screen
Triangles:
• Long and thin
• Along the diagonal
Very unpleasant situation in computer graphics:
(not just for rasterization)
Long and thin= bad
Many algorithms perform poorly
Samples
•
most things in the real world are
continuous
•
everything in a computer is
discrete
•
the process of mapping a continuous function to a
discrete one is called
sampling
•
the process of mapping a discrete function to a
continuous one is called
reconstruction
•
the process of mapping a continuous variable to a
discrete one is called
quantization
•
rendering an image requires sampling and
quantization
Jaggy Line Segments
• we tried to sample a line segment so it would map to a 2D raster display
• we quantized the pixel values to 0 or 1
Less Jaggy Line Segments
• better if quantize to many shades
– image is less visibly jaggy
• find color for area, not just single point at center of pixel
30
Supersample and Average
• supersample: create image at higher resolution
– e.g. 768x768 instead of 256x256
– shade pixels wrt area covered by thick line/rectangle
• average across many pixels
– e.g. 3x3 small pixel block to find value for 1 big pixel
– rough approximation divides each pixel into a finer grid of pixels
6/9 9/9
5/9 9/9
31
Supersample and Average
•
supersample: jaggies less obvious, but still there
– small pixel center check still misses information – unweighted area sampling
• equal areas cause equal intensity, regardless of distance from pixel center to area
• aka box filter
6/9 9/9
5/9 9/9
0/9 4/9
x Intensity
Supersampling Example: Image
no supersampling 3x3 supersampling with
Weighted Area Sampling
•
intuitively, pixel cut through the center should be
more heavily weighted than one cut along corner
•
weighting function, W(x,y)
– specifies the contribution of primitive passing
through the point (x, y) from pixel center
– Gaussian filter (or approximation) commonly used
x Intensity
•
some objects missed entirely, others poorly sampled
– could try unweighted or weighted area sampling
– but how can we be sure we show everything?
•
need to think about entire class of solutions!
– brief taste of signal processing
Image As Signal
• image as spatial signal
• 2D raster image
– discrete sampling of 2D spatial signal
• 1D slice of raster image
– discrete sampling of 1D spatial signal
Examples from Foley, van Dam, Feiner, and Hughes
Pixel position across scanline
In
te
ns
Sampling Frequency
• if don’t sample often enough, resulting signal misinterpreted as lower-frequency one
– we call this aliasing
Sampling Theorem
continuous signal can be completely recovered from its samples
iff
sampling rate greater than twice maximum frequency present in signal
Nyquist Rate
• lower bound on sampling rate
Aliasing
• incorrect appearance of high frequencies as low frequencies
• to avoid: antialiasing
– supersample
• sample at higher frequency
– low pass filtering
Low-Pass Filtering
Low-Pass Filtering
Filtering
• low pass
– blur
• high pass
Texture Antialiasing
Temporal Antialiasing
• subtle point: collision detection about algorithms for finding collisions in time as much as space
• temporal sampling
– aliasing: can miss collision completely with point samples!
• temporal antialiasing