3.5
Line Segments
A line segment results from a line strip, a line loop, or a series of separate line segments. Line segment rasterization is controlled by several variables. Line width, which may be set by calling
void LineWidth(floatwidth);
with an appropriate positive floating-point width, controls the width of rasterized line segments. The default width is1.0. Values less than or equal to 0.0 generate the error INVALID VALUE. Antialiasing is controlled with Enable and Disable using the symbolic constantLINE SMOOTH.
3.5.1 Basic Line Segment Rasterization
Line segment rasterization begins by characterizing the segment as eitherx-major
ory-major. x-major line segments have slope in the closed interval [−1,1]; all other line segments arey-major (slope is determined by the segment’s endpoints). We shall specify rasterization only forx-major segments except in cases where the modifications fory-major segments are not self-evident.
Ideally, the GL uses a “diamond-exit” rule to determine those fragments that are produced by rasterizing a line segment. For each fragmentf with center at win- dow coordinatesxf andyf, define a diamond-shaped region that is the intersection
of four half planes:
Rf ={(x, y)| |x−xf|+|y−yf|<1/2.}
Essentially, a line segment starting atpaand ending atpbproduces those frag-
mentsf for which the segment intersectsRf, except ifpbis contained inRf. See
figure3.2.
To avoid difficulties when an endpoint lies on a boundary ofRf we (in princi-
ple) perturb the supplied endpoints by a tiny amount. Letpaandpb have window
coordinates(xa, ya)and(xb, yb), respectively. Obtain the perturbed endpointsp0a
given by(xa, ya)−(, 2)andp0b given by(xb, yb)−(, 2). Rasterizing the line
segment starting atpaand ending atpb produces those fragmentsf for which the
segment starting atp0aand ending onp0bintersectsRf, except ifp0bis contained in
Rf. is chosen to be so small that rasterizing the line segment produces the same
fragments whenδis substituted forfor any0< δ≤.
When pa and pb lie on fragment centers, this characterization of fragments
reduces to Bresenham’s algorithm with one modification: lines produced in this description are “half-open,” meaning that the final fragment (corresponding topb)
3.5. LINE SEGMENTS 93
Figure 3.2. Visualization of Bresenham’s algorithm. A portion of a line segment is shown. A diamond shaped region of height 1 is placed around each fragment center; those regions that the line segment exits cause rasterization to produce correspond- ing fragments.
is not drawn. This means that when rasterizing a series of connected line segments, shared endpoints will be produced only once rather than twice (as would occur with Bresenham’s algorithm).
Because the initial and final conditions of the diamond-exit rule may be difficult to implement, other line segment rasterization algorithms are allowed, subject to the following rules:
1. The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in eitherxorywindow coordinates from a corresponding fragment produced by the diamond-exit rule.
2. The total number of fragments produced by the algorithm may differ from that produced by the diamond-exit rule by no more than one.
3. For anx-major line, no two fragments may be produced that lie in the same window-coordinate column (for ay-major line, no two fragments may ap- pear in the same row).
4. If two line segments share a common endpoint, and both segments are either
x-major (both left-to-right or both right-to-left) ory-major (both bottom-to- top or both top-to-bottom), then rasterizing both segments may not produce
3.5. LINE SEGMENTS 94
duplicate fragments, nor may any fragments be omitted so as to interrupt continuity of the connected segments.
Next we must specify how the data associated with each rasterized fragment are obtained. Let the window coordinates of a produced fragment center be given bypr= (xd, yd)and letpa= (xa, ya)andpb = (xb, yb). Set
t= (pr−pa)·(pb−pa)
kpb−pak2
. (3.5)
(Note thatt= 0atpaandt= 1atpb.) The value of an associated datumf for the
fragment, whether it be a varying shader output or the clipwcoordinate, is found as
f = (1−t)fa/wa+tfb/wb (1−t)/wa+t/wb
(3.6) wherefa andfb are the data associated with the starting and ending endpoints of
the segment, respectively;waandwbare the clipwcoordinates of the starting and
ending endpoints of the segments, respectively. However, depth values for lines must be interpolated by
z= (1−t)za+tzb (3.7)
wherezaandzb are the depth values of the starting and ending endpoints of the
segment, respectively.
The noperspective andflat keywords used to declare varying shader
outputs affect how they are interpolated. When neither keyword is specified, inter- polation is performed as described in equation3.6. When thenoperspective
keyword is specified, interpolation is performed in the same fashion as for depth values, as described in equation 3.7. When the flat keyword is specified, no interpolation is performed, and varying outputs are taken from the corresponding generic attribute value of the last (highest numbered) vertex transferred to the GL corresponding to that primitive.
3.5.2 Other Line Segment Features
We have just described the rasterization of non-antialiased line segments of width one. We now describe the rasterization of line segments for general values of the line segment rasterization parameters.
3.5. LINE SEGMENTS 95
Figure 3.3. The region used in rasterizing and finding corresponding coverage val- ues for an antialiased line segment (an x-major line segment is shown).
Antialiasing
Rasterized antialiased line segments produce fragments whose fragment squares intersect a rectangle centered on the line segment. Two of the edges are parallel to the specified line segment; each is at a distance of one-half the current width from that segment: one above the segment and one below it. The other two edges pass through the line endpoints and are perpendicular to the direction of the specified line segment. Coverage values are computed for each fragment by computing the area of the intersection of the rectangle with the fragment square (see figure3.3; see also section3.3). Equation3.6is used to compute associated data values just as with non-antialiased lines; equation3.5is used to find the value oftfor each frag- ment whose square is intersected by the line segment’s rectangle. Not all widths need be supported for line segment antialiasing, but width1.0antialiased segments must be provided. As with the point width, a GL implementation may be queried for the range and number of gradations of available antialiased line widths.
3.5.3 Line Rasterization State
The state required for line rasterization consists of the floating-point line width and a bit indicating whether line antialiasing is on or off. The initial value of the line width is1.0. The initial state of line segment antialiasing is disabled.