Graphical Models 66 (2004) 127–132 www.elsevier.com/locate/gmod
Rasterizing complex polygons without tessellations A.J. Rueda,* R.J. Segura, F.R. Feito, and J. Ruiz de Miras Departamento de Informatica, Escuela Politecnica Superior, Avda. de Madrid, 35-23071 Jaen, Spain Received 21 January 2002; accepted 13 January 2004 Available online 5 March 2004
Abstract Rasterization of polygons is a basic operation in computer graphics systems, representing the last and most time-consuming step in the visualization process. Therefore, the development of simple, efficient, and general polygon rasterization techniques is of prime interest. In this paper, we describe a new algorithm for this purpose, inspired on the ideas of the geometric modeling based on simplicial chains [Computer and Graphics 5 (1998) 611]. This algorithm is valid for any kind of polygon, is robust and extremely simple, and can be easily implemented in hardware. A similar approach can be used for the problem of polihedra voxelization in 3D. Ó 2004 Elsevier Inc. All rights reserved. Keywords: Polygon rasterization; Tessellation
1. Introduction Many applications like GIS or font rasterization need to handle polygons with many different characteristics, such as having many edges, concavities, self-intersecting edges, and holes. Two basic approaches can be taken for this task: rasterize the polygon as it is, using a scanline algorithm, or decompose the polygon into triangles that can be rasterized very quickly in most current graphic systems. The scanline polygon fill algorithm described in its basic form in [7] is a popular technique for the rasterization of polygons. The idea beyond this approach is to *
Corresponding author. E-mail addresses:
[email protected] (A.J. Rueda),
[email protected] (R.J. Segura), ff
[email protected] (F.R. Feito),
[email protected] (J. Ruiz de Miras). 1524-0703/$ - see front matter Ó 2004 Elsevier Inc. All rights reserved. doi:10.1016/j.gmod.2004.01.001
128
A.J. Rueda et al. / Graphical Models 66 (2004) 127–132
perform a line sweep of the polygon, using a list of the active edges intersected by the current scanline to determine which pixels must be set in the framebuffer. The information in this list is properly updated, adding or deleting edges when the scanline reaches or leaves them. In order to make an efficient scan conversion of the algorithm, the edges of the algorithm must be initially ordered, and the active edge list must be kept ordered as well. The need for sorting and for some basic data structures has made its hardware implementation quite uncommon. By far the most popular approach is based on the previous tessellation of the polygon and the rasterization of the resulting triangles. The main advantage of this approach is the fact that most current graphics systems support hardware rasterization of triangles, so they can be displayed very efficiently. Nevertheless, general polygon triangulation is a very hard problem [2], and there are not simple, robust, and efficient algorithms for this task. For this reason, tessellators are not implemented in hardware. For instance, OpenGL is only able to display triangles or convex polygons, but includes an efficient software tessellator in the utility library that can be used to triangulate complex polygons [10]. Our approach is also based on a triangle decomposition of the polygon, but these triangles are not the result of a previous tessellation and no previous sorting or complex data structures are necessary. It works with any kind of polygon in linear time and has a simpler hardware implementation than previous approaches.
2. Theoretical foundations Feito et al. [4,6] have developed a deep and extensive work in the area of Solid Modeling, providing methods for the description of 2D and 3D objects using simplicial chains, that is combinations of triangles in 2D and tetrahedra in 3D. The use of simplicial chains in the description of objects is useful as several simple, efficient, and robust algorithms for the computation of Boolean operations [8] or point inclusion test [3,5] can be developed. In the 2D case, a polygon P determined by a sequence of n edges a1 a2 . . . an in a given orientation, with ai ¼ Qi Qiþ1 , can be built as additions and subtractions of triangles between an arbitrary common point O and every edge ai of P (see Fig. 1). n X signðTi Þ Ti ; P¼ i¼1
Fig. 1. Construction of a polygon by adding and subtracting triangles.
A.J. Rueda et al. / Graphical Models 66 (2004) 127–132
where Ti ¼ nOQi Qiþ1 . The sign of clockwise orientation, determines if A Ox signðnOABÞ ¼ sign x Bx Ox
129
the triangle, that is, its clockwise or counterthe triangle must be added of subtracted: Ay Oy : By O y
A similar approach can be used in 3D: a polyhedron can be built as additions and subtractions of tetrahedra between a common arbitrary point and every face of the polyhedron [4,6]. The addition and subtraction operations of triangles or tetrahedra are applied on presence values of 2D or 3D space, which represent the degree of occupation in a point by graphic objects. A presence value v indicates that v objects cover that point of space. A presence value 0 means absence of objects in that point.
3. Rasterization procedure A straightforward algorithm for the rasterization of polygons can be defined applying the ideas of Section 2. A polygon can be rasterized by decomposing it into triangles, rasterizing these triangles in a special presence buffer, and finally transferring the information to the framebuffer, applying the corresponding color or texture. In a practical implementation, the common vertex of the triangles should be the centroid of the polygon. This makes the algorithm faster because it minimizes the area of the triangles and the operations on points outside the polygon (see Fig. 2). Nevertheless, it is impossible to avoid useless operations completely. As shown in Fig. 2, non-convex or holed polygons often present triangles with negative sign outside the polygon and overwriting areas covered by several triangles. This never happens in the case of convex polygons, where all triangles have a positive sign. The presence buffer or P-buffer stores the presence values for all the pixels of the framebuffer. Each triangle can be rasterized in this buffer by adding or subtracting (depending on its sign), a presence value 1 to the presence values of the pixels covered by it. This approach has a very interesting property: it can properly rasterize
Fig. 2. Decomposition of several polygons into triangles with a common vertex O located at the centroid. The signs of the triangles are also shown.
130
A.J. Rueda et al. / Graphical Models 66 (2004) 127–132
Fig. 3. Rasterization steps.
self-interesting polygons, which cannot be directly handled by scanline or tessellation approaches. After the rasterization of polygon in the P-buffer has been completed, its information is transferred to the framebuffer applying the corresponding color. Two values are possible in the P-buffer: 0 or 1. Pixels with presence value 0 have no effect on the framebuffer, but pixels with presence value 1 are rendered in the framebuffer using a color that can be the same for the entire polygon or be taken from a texture buffer. The rasterization process can be summarized in the next five steps, as shown in Fig. 3. (1) Compute the minimal bounding box and the centroid of the polygon. These could be precomputed and stored in the polygon data structure. (2) Construct a triangle between the origin vertex and one edge of the polygon, computing its corresponding sign. (3) Rasterize the triangle in the P-buffer, adding 0 subtracting a presence value 1, depending on its sign, to the positions covered by it. (4) Return to step 2 until no edges are left. (5) Transfer all positions from the P-buffer in the minimal bounding box of the polygon and presence values equal to 1 to the framebuffer, applying its corresponding color. The texture can be set up in memory, applying a proper projection before the transfer to framebuffer. The algorithm works well with a flat illumination model, although the application of Gouraud interpolation does not seem immediate.
4. Hardware implementation The described procedure can be easily implemented in software, but a full or partial hardware implementation is necessary in order to get a good performance. Two steps are specially suitable for its hardware acceleration: the triangle rasterization in the P-buffer and the transfer to the framebuffer.
A.J. Rueda et al. / Graphical Models 66 (2004) 127–132
131
A straightforward and efficient implementation of the algorithm can be done using the stencil buffer of OpenGL [10]. This can be used like a P-buffer with two passes: setting the stencil operation to inc, rasterizing all the triangles with positive sign, and setting the stencil operation to dec to rasterize the triangles with positive sign. A similar and more efficient approach inspired in the point-in-polygon test of Berlin [1] is using a parity for each pixel and changing this parity every time a triangle covers a pixel. At the end of the process, all the pixels inside the polygon have their parity value set to 1. This allows rasterizing all the triangles in the stencil buffer in one single pass, setting the stencil mask to 1 and the stencil operation to invert, and makes irrelevant the sign computation. After triangle rasterization in the stencil buffer has been completed, the stencil function must be set to pass when the pixel in the stencil buffer has a value equal to 1. If the minimal bounding box of the polygon is filled in the framebuffer with the color information, then only pixels inside the polygon will be displayed. In our tests, this implementation has shown an very good performance with complex polygons, beating by far the scanline or tessellation approaches.
5. Conclusions and future work In this paper we have presented a simple, robust, general, and efficient method for rasterizing polygons. Because of its simplicity (no complex data structures or algorithms are used), its hardware implementation is extremely easy, in particular by using the stencil buffer of OpenGL, giving a competitive alternative to current approaches. As we mentioned in Section 3, interpolation methods like Gouraud cannot be directly included in the rasterization process. This is a serious restriction which still requires a deeper study. The theoretical principles behind the algorithm are completely general, which allows its extension to handle curved-edge polygons or the development of a similar method for 3D polyhedra voxelization [9].
Acknowledgments This work has been partially granted by the Ministry of Science and Technology of Spain and the European Union by means of the ERDF funds, under the research project TIC2001-2003-C03-03.
References [1] E.P. Berlin Jr., Efficiency considerations in image synthesis, SIGGRAPHÕ85 Course Notes 11 (1985). [2] M. Bern, D. Eppstein, Mesh generation and optimal triangulation. Computing in Euclidean geometry, second ed., World Scientific, Singapore, 1995, pp. 189–196. [3] F. Feito, J.C. Torres, A. Ure~ na, Orientation, simplicity and inclusion test for planar polygons, Comput. Graph. 19 (1995) 595–600.
132
A.J. Rueda et al. / Graphical Models 66 (2004) 127–132
[4] F. Feito, J.C. Torres, Boundary representation of polyhedral heterogeneous solids in the context of a graphic object algebra, Visual Comput. 13 (1997) 64–77. [5] F. Feito, J.C. Torres, Inclusion test in general polyhedra, Comput. Graph. 21 (1997) 23–30. [6] F.R. Feito, M. Rivero, Geometric modelling based on simplicial chains, Comput. Graph. 22 (1998) 611–619. [7] J.D. Foley, A. van Dam, S.K. Feiner, J.F. Hughes, Computer and Graphics: Principles and Practice, second ed., Addison Wesley, Reading, MA, 1990. [8] M.L. Rivero, F.R. Feito, Boolean operations on general planar polygons, Comput. Graph. 24 (2000) 881–896. [9] A.J. Rueda, R.J. Segura, F.R. Feito, J. Ruiz, C. Ogayar, Voxelization of solids using simplicial coverings, Proc. of WSCG 2004 (to appear), Plzen, Czech Republic, 2004. [10] M. Woo, J. Nedider, T. Davis, D. Shreiner, The OpenGL Programming Guide, third ed., Addison Wesley, Reading, MA, 1999.