3.6 Polygons
3.6.1 Basic Polygon Rasterization
The first step of polygon rasterization is to determine if the polygon isback-facing
orfront-facing. This determination is made based on the sign of the (clipped or unclipped) polygon’s area computed in window coordinates. One way to compute this area is a= 1 2 n−1 X i=0 xiwyiw⊕1−xiw⊕1ywi (3.8) where xiw and ywi are the x and y window coordinates of theith vertex of then-vertex polygon (vertices are numbered starting at zero for purposes of this
3.6. POLYGONS 97
computation) andi⊕1is(i+1) modn. The interpretation of the sign of this value is controlled with
void FrontFace(enumdir);
SettingdirtoCCW(corresponding to counter-clockwise orientation of the pro- jected polygon in window coordinates) usesaas computed above. Settingdirto
CW(corresponding to clockwise orientation) indicates that the sign ofashould be reversed prior to use. Front face determination requires one bit of state, and is initially set toCCW.
If the sign ofa(including the possible reversal of this sign as determined by FrontFace) is positive, the polygon is front-facing; otherwise, it is back-facing. This determination is used in conjunction with theCullFaceenable bit and mode value to decide whether or not a particular polygon is rasterized. TheCullFace mode is set by calling
void CullFace(enummode);
modeis a symbolic constant: one ofFRONT,BACKorFRONT AND BACK. Culling is enabled or disabled with Enable or Disable using the symbolic constant
CULL FACE. Front-facing polygons are rasterized if either culling is disabled or theCullFacemode isBACKwhile back-facing polygons are rasterized only if ei- ther culling is disabled or theCullFacemode isFRONT. The initial setting of the CullFacemode isBACK. Initially, culling is disabled.
The rule for determining which fragments are produced by polygon rasteriza- tion is calledpoint sampling. The two-dimensional projection obtained by taking the x andy window coordinates of the polygon’s vertices is formed. Fragment centers that lie inside of this polygon are produced by rasterization. Special treat- ment is given to a fragment whose center lies on a polygon edge. In such a case we require that if two polygons lie on either side of a common edge (with identical endpoints) on which a fragment center lies, then exactly one of the polygons results in the production of the fragment during rasterization.
As for the data associated with each fragment produced by rasterizing a poly- gon, we begin by specifying how these values are produced for fragments in a triangle. Definebarycentric coordinatesfor a triangle. Barycentric coordinates are a set of three numbers,a,b, andc, each in the range[0,1], witha+b+c = 1. These coordinates uniquely specify any pointpwithin the triangle or on the trian- gle’s boundary as
3.6. POLYGONS 98
wherepa,pb, andpcare the vertices of the triangle.a,b, andccan be found as
a= A(ppbpc) A(papbpc) , b= A(ppapc) A(papbpc) , c= A(ppapb) A(papbpc) ,
whereA(lmn)denotes the area in window coordinates of the triangle with vertices
l,m, andn.
Denote an associated datum atpa,pb, orpcasfa,fb, orfc, respectively. Then
the valuef of a datum at a fragment produced by rasterizing a triangle is given by
f = afa/wa+bfb/wb+cfc/wc
a/wa+b/wb+c/wc
(3.9) wherewa, wb and wcare the clip w coordinates of pa, pb, and pc, respectively.
a, b, andcare the barycentric coordinates of the fragment for which the data are produced. a, b, and c must correspond precisely to the exact coordinates of the center of the fragment. Another way of saying this is that the data associated with a fragment must be sampled at the fragment’s center. However, depth values for polygons must be interpolated by
z=aza+bzb+czc (3.10)
whereza,zb, andzcare the depth values ofpa,pb, andpc, 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.9. When thenoperspective
keyword is specified, interpolation is performed in the same fashion as for depth values, as described in equation3.10. When theflat 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.
For a polygon with more than three edges, such as may be produced by clipping a triangle, we require only that a convex combination of the values of the datum at the polygon’s vertices can be used to obtain the value assigned to each fragment produced by the rasterization algorithm. That is, it must be the case that at every fragment f = n X i=1 aifi
wherenis the number of vertices in the polygon,fi is the value of thef at vertex
i; for each i0 ≤ai ≤1andPni=1ai = 1. The values of theai may differ from
3.6. POLYGONS 99
One algorithm that achieves the required behavior is to triangulate a polygon (without adding any vertices) and then treat each triangle individually as already discussed. A scan-line rasterizer that linearly interpolates data along each edge and then linearly interpolates data across each horizontal span from edge to edge also satisfies the restrictions (in this case, the numerator and denominator of equa- tion3.9should be iterated independently and a division performed for each frag- ment).
3.6.2 Antialiasing
Polygon antialiasing rasterizes a polygon by producing a fragment wherever the interior of the polygon intersects that fragment’s square. A coverage value is com- puted at each such fragment, and this value is saved to be applied as described in section3.10. An associated datum is assigned to a fragment by integrating the datum’s value over the region of the intersection of the fragment square with the polygon’s interior and dividing this integrated value by the area of the intersection. For a fragment square lying entirely within the polygon, the value of a datum at the fragment’s center may be used instead of integrating the value across the fragment.