Rasterization
Rasterization is the process of converting geometric primitives (points, lines, triangles) into discrete pixels (fragments) on the screen. It is the most common rendering approach used in real-time graphics.
Triangle Rasterization
Triangles are the fundamental primitive in graphics. Rasterizing triangles involves determining which pixels fall inside the triangle.
Bounding Box Method:
For triangle with vertices (x0,y0), (x1,y1), (x2,y2):
1. Compute bounding box:
min_x = min(x0, x1, x2)
max_x = max(x0, x1, x2)
min_y = min(y0, y1, y2)
max_y = max(y0, y1, y2)
2. For each pixel (px, py) in bounding box:
if inside_triangle(px, py):
rasterize(px, py)
Edge Functions
The edge function determines if a point is to the left or right of a directed edge. A point is inside the triangle if it is on the same side of all three edges.
Edge function for edge (V0, V1):
E(x,y) = (x - x0)(y1 - y0) - (y - y0)(x1 - x0)
E > 0 → point is to the left (inside)
E < 0 → point is to the right (outside)
E = 0 → point is on the edge
For triangle V0, V1, V2:
p is inside if:
E01(p) ≥ 0 AND
E12(p) ≥ 0 AND
E20(p) ≥ 0
Barycentric Coordinates
Barycentric coordinates express a point as a weighted combination of triangle vertices. They are used for interpolating attributes across the triangle.
Barycentric coordinates (λ0, λ1, λ2):
P = λ0*V0 + λ1*V1 + λ2*V2
λ0 + λ1 + λ2 = 1
λ0 = E12(P) / E12(V0)
λ1 = E20(P) / E20(V1)
λ2 = E01(P) / E01(V2)
Interpolation of attribute A:
A(P) = λ0*A(V0) + λ1*A(V1) + λ2*A(V2)
Can interpolate: color, normal, texcoord, depth
Z-Buffer (Depth Buffer)
The Z-buffer stores the depth of each rendered pixel. When rendering a new fragment, its depth is compared to the stored depth. Closer fragments replace farther ones.
Z-Buffer Algorithm:
Initialize depth_buffer[x][y] = 1.0 (far)
Initialize frame_buffer[x][y] = background
For each triangle:
For each pixel (x,y) in triangle:
z = interpolated depth at (x,y)
if z < depth_buffer[x][y]:
depth_buffer[x][y] = z
frame_buffer[x][y] = color(x,y)