03 - 2D Raster Graphics
03 - 2D Raster Graphics
Christoph Garth
Scientific Visualization Lab
Most computer graphics are presented on raster displays:
Examples:
• Flat-panel screens in computer monitors, TVs, and cell phones consist are
rectangular arrays of light-emitting cells (e.g. diodes).
• Printers form an image by depositing ink or toner at selected points on a
grid, usually line-by-line.
• Digital cameras use a grid of light-sensitive sensors that record light color
and intensity.
• Scanners read intensity on a grid, line-by-line.
• A raster image is simply a 2D array that stores the pixel value for each pixel.
• Each pixel stores color/intensity, represented in some color space (e.g. RGB).
RGB RGB RGB RGB RGB (0, 3) (1, 3) (2, 3) (3, 3) (4, 3)
RGB RGB RGB RGB RGB (0, 1) (1, 1) (2, 1) (3, 1) (4, 1)
RGB RGB RGB RGB RGB (0, 0) (1, 0) (2, 0) (3, 0) (4, 0)
Functional form:
y = f (x) = mx + b p1
Determine via
y ∆y
∆y y1 − y0
m = =
∆x x1 − x0
p0 ∆x
b = y0 − mx0
Parametric form:
Specifically:
p0 ∆x
x(u) = x0 + u(x1 − x0 ) = (1 − u)x0 + ux1
y(u) = y0 + u(y1 − y0 ) = (1 − u)y0 + uy1
x
This is also often called linear interpolation.
l(p) = hn, pi − d = 0
Geometrically: y
p0 ∆y
n p hn, pi = 0
• n is orthogonal to (p1 − p0 ), i.e. ∆x
y = mx + c
where m = ∆y
∆x is the slope and c = y0 − mx0 is the offset.
∆y
∆x
Iterate i = 0, . . . , L − 1:
• plot the pixel at (x, y), rounded towards nearest integer
• step along the line using x = x + ∆x/L, y = y + ∆y/L
y
i=3
i=2
i=1 ∆y/L
i=0 ∆x/L
i=3 i x y plot
i=2 0 1 1 (1, 1)
(4, 3)
i=1 1 2 5/3 (2, 2)
i=0 2 3 7/3 (3, 2)
3 4 3 (4, 3)
(1, 1)
)
F (E
a = −∆y = y0 − y1
b = ∆x = x1 − x0 P E
c = ∆x · b.
M = (x + 1, y + 1/2)
NE d>0
d ← d − ∆y + ∆x d ← d − ∆y
= d + ∆NE = d + ∆E
M1
NE
M
M M2
P P E
p0 is on the line, hence, F(x0 , y0 ) = 0, and for the first midpoint M we find
// initialize variables
Observe that Bresenham’s int x = x0, y = y0, d = dx - 2*dy;
algorithm uses only integer plot(x, y);
dx = 5, dy = 4 ⇒ ∆NE = −2, ∆E = 8
x y d setPixel
p1
1 1 3 (1, 1)
2 2 1 (2, 2)
3 3 −1 (3, 3)
4 3 7 (4, 3)
5 4 5 (5, 4)
p0 6 5 3 (6, 5)
p1
p0
r cos θ
F(x, y) = (x − xc )2 + (y − yc )2 − r2 = 0
or explicitly
r
Both representations require complex θ
d ← d + 2x + 3 = d + ∆E d ← d + 2x − 2y + 5 = d + ∆SE
P E P
M1 M
M
M2
SE
while( y > x )
d = F(1, r − 1/2) = 5/4 − r {
if( d >= 0 ) {
Termination: when x = y. // SE
d += 8*(x-y) + 20;
p6 p3
self-intersection
edge
p5 p4
bounding box
p7 p2
vertex
p8 p1
p9 p0
Notation: P = (p0 , . . . , pn−1 ) with vertices pi = (xi , yi ) and edges ei = (pi , p(i+1) mod n ).
1
(1 − a)p + aq ∈ P ∀p, q ∈ P, a ∈ [0, 1]
Computer Graphics – 2D Rasterization – Polygon Rasterization 3–31
Simplest polygon rasterization or simply polygon scan conversion:
• Compute polygon bounding box by finding min/max of xi and yi .
• Iterate over all pixels of polygon’s bounding box [xmin , xmax] × [ymin , ymax ].
• Plot pixel if it is inside() the polygon.
The inside() function is complex in the general case, leading to a slow algorithm.
(Do note however that this is embarrasingly parallel.)
Example:
• y = 2: intersections at x ∈ {1, 8}.
Pixels for x ∈ [1, 8] are inside, all
y=4
others outside.
• y = 4: intersections at x ∈ {1, 4, 6, 8}. y=2
1. Find the intersections of the scan line with all edges of the polygon.
2. Sort the intersections by increasing x-coordinate.
3. Fill all pixels between pairs of intersections.
But, we can use coherence between scan lines: neighboring scan lines are likely
to intersect the same edges.
d
b
a
c
Edge (a, b) will be placed in the active list before edge (c, d) because it has smaller ymax ; hence, the
right intersection point would be found before the one.
Let’s look at
The vectors p1 − p0 and p2 − p0 are linearly independent and form a basis of the
plane. Any point p in the plane can be uniquely written in terms of this basis as
p = p0 + β1 (p1 − p0 ) + β2 (p2 − p0 )
p = (1 − β1 − β2 )p0 + β1 p1 + β2 p2 := β0 p0 + β1 p1 + β2 p2
where β0 + β1 + β2 = 1.
p2
Ai (p)
βi (p) =
A
2
an affine combination is a linear combination in which the coefficients sum up to one
Computer Graphics – 2D Rasterization – Polygon Rasterization 3–41
Math recap: If p and q are two arbitrary vectors, then
xp xq
A = p × q = det(p, q) = = kpkkqk sin θ
yp yq
q p
θ θ
p q
(This can be obtained by treating p and q as 3D vectors and computing 3D cross product. If p and q form a
right-hand system, then it points along the z-axis, giving positive area; otherwise the area is negative.)
l2 (p) = hn2 , pi − d
A2 is computed as
A2 = hn2 , pi − d
p2
A2 and h are positive if the p is on
the same side that n2 points to.
p
A2 does not change as p moves on
the dashed line, since the distance h h n2
to l2 stays the same.
h>0 A2
h<0
p0
l2
The maximum distance inside the p1
triangle is obtained for p2 , which
gives A2 = A. If p is on l2 , then A2 = 0.
For a triangle T = (p0 , p1 , p2 ), we can formulate three barycentric coordinates β0,1,2 (p)
such that
Furthermore:
Hence, the βi provide a “perfect” coordinate system for T. They are often called edge
functions (e.g. β0 = F12 ) in rasterization.
β2 (x + 1, y) = βi (x, y) + (y1 − y0 )
Assume we have three colors at the three corners C(p0 ), C(p1 ), C(p2 ); how can we
smoothly blend them across a triangle?
This can also be used for other per-vertex quanities ( Pipelines + Mapping chapters).
window defines the viewing window in the scene; or, which part of a scene
should be represented,
viewport defines the area of the target image or screen in which the window
contents should be drawn.
Typically, but not always, both window and viewport are axis-aligned rectangles.
viewport
viewport
window
window
viewport
viewport
window
window
window
window
It may appear at first glance that we can simply limit plot() to the bounds of the
viewport, but that is not ideal:
For an axis-aligned viewport (xmin , ymin , xmax , ymax ), there are three cases:
The plane is divided in nine regions by the lines corresponding to the viewport edges.
Each region receives a unique 4-bit code.
1001 1000
0000
0000 0001
0001
case 1 case 2 case 3
Simply clipping the polygon edges gives an incorrect result (right image).
Consecutive intersections (exit → entry) can be safely connected in this case. However,
the intermediate results – partially clipped polygons – must be stored.