06 Attributes of GR - Prim.
06 Attributes of GR - Prim.
10
Werner Purgathofer, TU Wien
Furthermore, anti-aliasing is an important topic for lines. More details follow in later sections.
Many applications also require creating a combination of the newly drawn pattern and the background.
There are several different ways which base upon logic operations like AND, OR, XOR. Generally the
blending of color is performed by a linear combination of the existing background color B and the given
foreground color F: P = t*F + (1-t)*B
Triangle Rasterization
For filling triangles, the use of barycentric coordinates is common.
Each point in the plane is represented as weighted average of the 3
triangle vertices: P=αP0 + βP1 + γP2 .
(α, β, γ) are then called the barycentric coordinates of the point P,
with α+β+γ=1.
All points with 0< α, β, γ<1 are inside the triangle.
When filling a triangle, the barycentric coordinates of each pixel of a
(preferably tight) bounding area are calculated, and all pixels with
0< α, β, γ<1 are rendered. The rendering color can easily be linearly interpolated from corner values using
the available weights (α, β, γ). If we denote the line through P1 and P2 with l12(x,y) = a12x+b12y+c12=0 then
α of the point P(x,y) is α = l12(x,y) / l12(x0,y0), β und γ analogous.
To avoid rendering the common border of 2 adjacent triangles twice, only pixels are rendered that lie inside
the triangle borders. Pixels that are exactly on a border need a special treatment.
When filling a more complex polygon it has to be defined which parts are “inside” and which areas are
“outside” (refer to “01 Graphics Primitives”). There are two categories of filling methods. The scan-line
method intersects each scan-line with the polygon (or the surface border), and parts which lie inside (spans)
are filled. The flood-fill method fills the surface originating from a seed point in all directions until the
whole polygon is recognized to be filled.
21
Scan-line Filling
All edges of the polygon are sorted by the
lower y-value of their endpoints (bucket sort).
For each edge, the following information is
stored:
[max. y-value, start x-value, slope]
Using this data structure, for each scan-line in
the list (bottom up) we create a list of “active”
edges, which are those that intersect that scan-
line. Then the scan-line is filled from the first
intersection point to the second one, from the
third to the fourth, from 5th to 6th and so on.
The list of active edges is generated incrementally. Starting with zero active edges, for each y-value we can
look up in the sorted-edge-list, whether there is a new edge beginning there. If so, this edge is added to the
current active-edge-list. At the same time, all edges whose maximum y-value has been exceeded are
removed (not active anymore). The active edges in this list are always sorted by their intersection points
(from the left to the right), so that drawing can always be performed instantaneously.
The intersection points can also be generated incrementally. Beginning from the (exact) intersection point
(xk,yk) of a scan-line with an edge, we get the next higher intersection point (xk+1,yk+1) by
xk+1= xk+1/m and yk+1= yk+1. Now it also becomes clear why we’ve stored the slope 1/m in each node of the
edge list.
Flood-fill Algorithm
Originating from some start point (reference point, seed point), this algorithm fills in all directions until
hitting the border. This border can either be defined explicitly, e.g. by a border of some certain color, or
implicitly, e.g. in the case that an already colored surface is filled with a new color. There are also variants
mixing implicit and explicit definitions. However, this definition is just used to formulate the stop criterion
for the algorithm. Without loss of generality, for the following code we want to assume that the surface to
be filled is already drawn in a defined color, and shall be filled with a new one.
The example to the right demonstrates the filling order, where one
of the pixels 1, 2 or 3 was chosen as start pixel. Recursion is first
performed upwards, then downwards. At each call all spans in this
direction have to be processed. After the span 4 to 11, the
recursion goes upwards and draws from left to right, then goes
downwards and draws from left to right as well. Already filled
parts are recognized and the recursion terminates (e.g. going
downwards from 4 to 11, 1 to 3 is already processed). Although
the total depth of recursion theoretically can be very high with this
method too, in practice it is proportional to the number of
scanlines of the polygon.
█ Attributes of Text
The attributes that can be assigned to text are common knowledge nowadays: Font (e.g. Courier, Arial,
Times, Broadway, …), style (normal, bold, italic, underlined, …), size, text direction, color, alignment
(left, right, centered, justified) and so on.
█ Attributes of 3D Polygons
Generally, 3D polygons are surface elements of objects in 3D space. Therefore their attributes mostly
describe properties of those objects’ surfaces: color, material parameters (roughness, absorption, …),
transparency, texture, geometrical microstructure, reflection behavior etc., which create scene dependent
effects under a given illumination. Additionally, normal vectors and texture coordinates for each vertex are
often included to allow for fast and adequate calculation of the polygon’s appearance. Later we will see how
this is used efficiently.
23
aliasing effects can be caused by too low resolution, too few colors available, too few images per second,
geometrical errors, and numerical errors.
The cause of aliasing is an insufficient sampling rate of the original image. The theoretical basis is
described by the Shannon Sampling Theorem. This theorem says
that information can only be correctly reconstructed if the used
sampling frequency (sampling rate) is at least double as high as
the highest information frequency within the picture. This limit is
called the Nyquist limit. The figure shows how a too coarse
sampling rate of a signal (curve) can lead to a completely false
reconstruction (polygon strip). Such errors can be reduced either
by prefiltering of the input signal or by post-processing of the
resulting image. In any case prefiltering leads to better results than
post-processing. The central strategy of prefiltering is super-
sampling (also called oversampling).
Anti-aliasing of Lines
Pixels which are more centrally intersected by some line
should get more of the line color than pixels which are just
touched slightly by the line. Therefore we subdivide each
pixel into a number of sub-pixels and choose a result intensity
for the pixel proportional to the number of sub pixels which
lie on the line. For broader lines we choose the intensity of
the line color according to the percentage of coverage of the
pixel by the line. Because a pixel’s center is more relevant
than its border, sometimes sub-pixels in the center get more
weight than those at the pixel’s border („weighted
oversampling“).