README 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. */
  3. General Polygon Tesselation
  4. ---------------------------
  5. This note describes a tesselator for polygons consisting of one or
  6. more closed contours. It is backward-compatible with the current
  7. OpenGL Utilities tesselator, and is intended to replace it. Here is
  8. a summary of the major differences:
  9. - input contours can be intersecting, self-intersecting, or degenerate.
  10. - supports a choice of several winding rules for determining which parts
  11. of the polygon are on the "interior". This makes it possible to do
  12. CSG operations on polygons.
  13. - boundary extraction: instead of tesselating the polygon, returns a
  14. set of closed contours which separate the interior from the exterior.
  15. - returns the output as a small number of triangle fans and strips,
  16. rather than a list of independent triangles (when possible).
  17. - output is available as an explicit mesh (a quad-edge structure),
  18. in addition to the normal callback interface.
  19. - the algorithm used is extremely robust.
  20. The interface
  21. -------------
  22. The tesselator state is maintained in a "tesselator object".
  23. These are allocated and destroyed using
  24. GLUtesselator *gluNewTess( void );
  25. void gluDeleteTess( GLUtesselator *tess );
  26. Several tesselator objects may be used simultaneously.
  27. Inputs
  28. ------
  29. The input contours are specified with the following routines:
  30. void gluTessBeginPolygon( GLUtesselator *tess );
  31. void gluTessBeginContour( GLUtesselator *tess );
  32. void gluTessVertex( GLUtesselator *tess, GLUcoord coords[3], void *data );
  33. void gluTessEndContour( GLUtesselator *tess );
  34. void gluTessEndPolygon( GLUtesselator *tess );
  35. Within each BeginPolygon/EndPolygon pair, there can be zero or more
  36. calls to BeginContour/EndContour. Within each contour, there are zero
  37. or more calls to gluTessVertex(). The vertices specify a closed
  38. contour (the last vertex of each contour is automatically linked to
  39. the first).
  40. "coords" give the coordinates of the vertex in 3-space. For useful
  41. results, all vertices should lie in some plane, since the vertices
  42. are projected onto a plane before tesselation. "data" is a pointer
  43. to a user-defined vertex structure, which typically contains other
  44. information such as color, texture coordinates, normal, etc. It is
  45. used to refer to the vertex during rendering.
  46. The library can be compiled in single- or double-precision; the type
  47. GLUcoord represents either "float" or "double" accordingly. The GLU
  48. version will be available in double-precision only. Compile with
  49. GLU_TESS_API_FLOAT defined to get the single-precision version.
  50. When EndPolygon is called, the tesselation algorithm determines
  51. which regions are interior to the given contours, according to one
  52. of several "winding rules" described below. The interior regions
  53. are then tesselated, and the output is provided as callbacks.
  54. Rendering Callbacks
  55. -------------------
  56. Callbacks are specified by the client using
  57. void gluTessCallback( GLUtesselator *tess, GLenum which, void (*fn)());
  58. If "fn" is NULL, any previously defined callback is discarded.
  59. The callbacks used to provide output are: /* which == */
  60. void begin( GLenum type ); /* GLU_TESS_BEGIN */
  61. void edgeFlag( GLboolean flag ); /* GLU_TESS_EDGE_FLAG */
  62. void vertex( void *data ); /* GLU_TESS_VERTEX */
  63. void end( void ); /* GLU_TESS_END */
  64. Any of the callbacks may be left undefined; if so, the corresponding
  65. information will not be supplied during rendering.
  66. The "begin" callback indicates the start of a primitive; type is one
  67. of GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, or GL_TRIANGLES (but see the
  68. notes on "boundary extraction" below).
  69. It is followed by any number of "vertex" callbacks, which supply the
  70. vertices in the same order as expected by the corresponding glBegin()
  71. call. After the last vertex of a given primitive, there is a callback
  72. to "end".
  73. If the "edgeFlag" callback is provided, no triangle fans or strips
  74. will be used. When edgeFlag is called, if "flag" is GL_TRUE then each
  75. vertex which follows begins an edge which lies on the polygon boundary
  76. (ie. an edge which separates an interior region from an exterior one).
  77. If "flag" is GL_FALSE, each vertex which follows begins an edge which lies
  78. in the polygon interior. "edgeFlag" will be called before the first
  79. call to "vertex".
  80. Other Callbacks
  81. ---------------
  82. void mesh( GLUmesh *mesh ); /* GLU_TESS_MESH */
  83. - Returns an explicit mesh, represented using the quad-edge structure
  84. (Guibas/Stolfi '85). Other implementations of this interface might
  85. use a different mesh structure, so this is available only only as an
  86. SGI extension. When the mesh is no longer needed, it should be freed
  87. using
  88. void gluDeleteMesh( GLUmesh *mesh );
  89. There is a brief description of this data structure in the include
  90. file "mesh.h". For the full details, see L. Guibas and J. Stolfi,
  91. Primitives for the manipulation of general subdivisions and the
  92. computation of Voronoi diagrams, ACM Transactions on Graphics,
  93. 4(2):74-123, April 1985. For an introduction, see the course notes
  94. for CS348a, "Mathematical Foundations of Computer Graphics",
  95. available at the Stanford bookstore (and taught during the fall
  96. quarter).
  97. void error( GLenum errno ); /* GLU_TESS_ERROR */
  98. - errno is one of GLU_TESS_MISSING_BEGIN_POLYGON,
  99. GLU_TESS_MISSING_END_POLYGON,
  100. GLU_TESS_MISSING_BEGIN_CONTOUR,
  101. GLU_TESS_MISSING_END_CONTOUR,
  102. GLU_TESS_COORD_TOO_LARGE,
  103. GLU_TESS_NEED_COMBINE_CALLBACK
  104. The first four are obvious. The interface recovers from these
  105. errors by inserting the missing call(s).
  106. GLU_TESS_COORD_TOO_LARGE says that some vertex coordinate exceeded
  107. the predefined constant GLU_TESS_MAX_COORD in absolute value, and
  108. that the value has been clamped. (Coordinate values must be small
  109. enough so that two can be multiplied together without overflow.)
  110. GLU_TESS_NEED_COMBINE_CALLBACK says that the algorithm detected an
  111. intersection between two edges in the input data, and the "combine"
  112. callback (below) was not provided. No output will be generated.
  113. void combine( GLUcoord coords[3], void *data[4], /* GLU_TESS_COMBINE */
  114. GLUcoord weight[4], void **outData );
  115. - When the algorithm detects an intersection, or wishes to merge
  116. features, it needs to create a new vertex. The vertex is defined
  117. as a linear combination of up to 4 existing vertices, referenced
  118. by data[0..3]. The coefficients of the linear combination are
  119. given by weight[0..3]; these weights always sum to 1.0. All vertex
  120. pointers are valid even when some of the weights are zero.
  121. "coords" gives the location of the new vertex.
  122. The user must allocate another vertex, interpolate parameters
  123. using "data" and "weights", and return the new vertex pointer in
  124. "outData". This handle is supplied during rendering callbacks.
  125. For example, if the polygon lies in an arbitrary plane in 3-space,
  126. and we associate a color with each vertex, the combine callback might
  127. look like this:
  128. void myCombine( GLUcoord coords[3], VERTEX *d[4],
  129. GLUcoord w[4], VERTEX **dataOut )
  130. {
  131. VERTEX *new = new_vertex();
  132. new->x = coords[0];
  133. new->y = coords[1];
  134. new->z = coords[2];
  135. new->r = w[0]*d[0]->r + w[1]*d[1]->r + w[2]*d[2]->r + w[3]*d[3]->r;
  136. new->g = w[0]*d[0]->g + w[1]*d[1]->g + w[2]*d[2]->g + w[3]*d[3]->g;
  137. new->b = w[0]*d[0]->b + w[1]*d[1]->b + w[2]*d[2]->b + w[3]*d[3]->b;
  138. new->a = w[0]*d[0]->a + w[1]*d[1]->a + w[2]*d[2]->a + w[3]*d[3]->a;
  139. *dataOut = new;
  140. }
  141. If the algorithm detects an intersection, then the "combine" callback
  142. must be defined, and must write a non-NULL pointer into "dataOut".
  143. Otherwise the GLU_TESS_NEED_COMBINE_CALLBACK error occurs, and no
  144. output is generated. This is the only error that can occur during
  145. tesselation and rendering.
  146. Control over Tesselation
  147. ------------------------
  148. void gluTessProperty( GLUtesselator *tess, GLenum which, GLUcoord value );
  149. Properties defined:
  150. - GLU_TESS_WINDING_RULE. Possible values:
  151. GLU_TESS_WINDING_ODD
  152. GLU_TESS_WINDING_NONZERO
  153. GLU_TESS_WINDING_POSITIVE
  154. GLU_TESS_WINDING_NEGATIVE
  155. GLU_TESS_WINDING_ABS_GEQ_TWO
  156. The input contours parition the plane into regions. A winding
  157. rule determines which of these regions are inside the polygon.
  158. For a single contour C, the winding number of a point x is simply
  159. the signed number of revolutions we make around x as we travel
  160. once around C (where CCW is positive). When there are several
  161. contours, the individual winding numbers are summed. This
  162. procedure associates a signed integer value with each point x in
  163. the plane. Note that the winding number is the same for all
  164. points in a single region.
  165. The winding rule classifies a region as "inside" if its winding
  166. number belongs to the chosen category (odd, nonzero, positive,
  167. negative, or absolute value of at least two). The current GLU
  168. tesselator implements the "odd" rule. The "nonzero" rule is another
  169. common way to define the interior. The other three rules are
  170. useful for polygon CSG operations (see below).
  171. - GLU_TESS_BOUNDARY_ONLY. Values: TRUE (non-zero) or FALSE (zero).
  172. If TRUE, returns a set of closed contours which separate the
  173. polygon interior and exterior (rather than a tesselation).
  174. Exterior contours are oriented CCW with respect to the normal,
  175. interior contours are oriented CW. The GLU_TESS_BEGIN callback
  176. uses the type GL_LINE_LOOP for each contour.
  177. - GLU_TESS_TOLERANCE. Value: a real number between 0.0 and 1.0.
  178. This specifies a tolerance for merging features to reduce the size
  179. of the output. For example, two vertices which are very close to
  180. each other might be replaced by a single vertex. The tolerance
  181. is multiplied by the largest coordinate magnitude of any input vertex;
  182. this specifies the maximum distance that any feature can move as the
  183. result of a single merge operation. If a single feature takes part
  184. in several merge operations, the total distance moved could be larger.
  185. Feature merging is completely optional; the tolerance is only a hint.
  186. The implementation is free to merge in some cases and not in others,
  187. or to never merge features at all. The default tolerance is zero.
  188. The current implementation merges vertices only if they are exactly
  189. coincident, regardless of the current tolerance. A vertex is
  190. spliced into an edge only if the implementation is unable to
  191. distinguish which side of the edge the vertex lies on.
  192. Two edges are merged only when both endpoints are identical.
  193. void gluTessNormal( GLUtesselator *tess,
  194. GLUcoord x, GLUcoord y, GLUcoord z )
  195. - Lets the user supply the polygon normal, if known. All input data
  196. is projected into a plane perpendicular to the normal before
  197. tesselation. All output triangles are oriented CCW with
  198. respect to the normal (CW orientation can be obtained by
  199. reversing the sign of the supplied normal). For example, if
  200. you know that all polygons lie in the x-y plane, call
  201. "gluTessNormal(tess, 0.0, 0.0, 1.0)" before rendering any polygons.
  202. - If the supplied normal is (0,0,0) (the default value), the
  203. normal is determined as follows. The direction of the normal,
  204. up to its sign, is found by fitting a plane to the vertices,
  205. without regard to how the vertices are connected. It is
  206. expected that the input data lies approximately in plane;
  207. otherwise projection perpendicular to the computed normal may
  208. substantially change the geometry. The sign of the normal is
  209. chosen so that the sum of the signed areas of all input contours
  210. is non-negative (where a CCW contour has positive area).
  211. - The supplied normal persists until it is changed by another
  212. call to gluTessNormal.
  213. Backward compatibility with the GLU tesselator
  214. ----------------------------------------------
  215. The preferred interface is the one described above. The following
  216. routines are obsolete, and are provided only for backward compatibility:
  217. typedef GLUtesselator GLUtriangulatorObj; /* obsolete name */
  218. void gluBeginPolygon( GLUtesselator *tess );
  219. void gluNextContour( GLUtesselator *tess, GLenum type );
  220. void gluEndPolygon( GLUtesselator *tess );
  221. "type" is one of GLU_EXTERIOR, GLU_INTERIOR, GLU_CCW, GLU_CW, or
  222. GLU_UNKNOWN. It is ignored by the current GLU tesselator.
  223. GLU_BEGIN, GLU_VERTEX, GLU_END, GLU_ERROR, and GLU_EDGE_FLAG are defined
  224. as synonyms for GLU_TESS_BEGIN, GLU_TESS_VERTEX, GLU_TESS_END,
  225. GLU_TESS_ERROR, and GLU_TESS_EDGE_FLAG.
  226. Polygon CSG operations
  227. ----------------------
  228. The features of the tesselator make it easy to find the union, difference,
  229. or intersection of several polygons.
  230. First, assume that each polygon is defined so that the winding number
  231. is 0 for each exterior region, and 1 for each interior region. Under
  232. this model, CCW contours define the outer boundary of the polygon, and
  233. CW contours define holes. Contours may be nested, but a nested
  234. contour must be oriented oppositely from the contour that contains it.
  235. If the original polygons do not satisfy this description, they can be
  236. converted to this form by first running the tesselator with the
  237. GLU_TESS_BOUNDARY_ONLY property turned on. This returns a list of
  238. contours satisfying the restriction above. By allocating two
  239. tesselator objects, the callbacks from one tesselator can be fed
  240. directly to the input of another.
  241. Given two or more polygons of the form above, CSG operations can be
  242. implemented as follows:
  243. Union
  244. Draw all the input contours as a single polygon. The winding number
  245. of each resulting region is the number of original polygons
  246. which cover it. The union can be extracted using the
  247. GLU_TESS_WINDING_NONZERO or GLU_TESS_WINDING_POSITIVE winding rules.
  248. Note that with the nonzero rule, we would get the same result if
  249. all contour orientations were reversed.
  250. Intersection (two polygons at a time only)
  251. Draw a single polygon using the contours from both input polygons.
  252. Extract the result using GLU_TESS_WINDING_ABS_GEQ_TWO. (Since this
  253. winding rule looks at the absolute value, reversing all contour
  254. orientations does not change the result.)
  255. Difference
  256. Suppose we want to compute A \ (B union C union D). Draw a single
  257. polygon consisting of the unmodified contours from A, followed by
  258. the contours of B,C,D with the vertex order reversed (this changes
  259. the winding number of the interior regions to -1). To extract the
  260. result, use the GLU_TESS_WINDING_POSITIVE rule.
  261. If B,C,D are the result of a GLU_TESS_BOUNDARY_ONLY call, an
  262. alternative to reversing the vertex order is to reverse the sign of
  263. the supplied normal. For example in the x-y plane, call
  264. gluTessNormal( tess, 0.0, 0.0, -1.0 ).
  265. Performance
  266. -----------
  267. The tesselator is not intended for immediate-mode rendering; when
  268. possible the output should be cached in a user structure or display
  269. list. General polygon tesselation is an inherently difficult problem,
  270. especially given the goal of extreme robustness.
  271. The implementation makes an effort to output a small number of fans
  272. and strips; this should improve the rendering performance when the
  273. output is used in a display list.
  274. Single-contour input polygons are first tested to see whether they can
  275. be rendered as a triangle fan with respect to the first vertex (to
  276. avoid running the full decomposition algorithm on convex polygons).
  277. Non-convex polygons may be rendered by this "fast path" as well, if
  278. the algorithm gets lucky in its choice of a starting vertex.
  279. For best performance follow these guidelines:
  280. - supply the polygon normal, if available, using gluTessNormal().
  281. This represents about 10% of the computation time. For example,
  282. if all polygons lie in the x-y plane, use gluTessNormal(tess,0,0,1).
  283. - render many polygons using the same tesselator object, rather than
  284. allocating a new tesselator for each one. (In a multi-threaded,
  285. multi-processor environment you may get better performance using
  286. several tesselators.)
  287. Comparison with the GLU tesselator
  288. ----------------------------------
  289. On polygons which make it through the "fast path", the tesselator is
  290. 3 to 5 times faster than the GLU tesselator.
  291. On polygons which don't make it through the fast path (but which don't
  292. have self-intersections or degeneracies), it is about 2 times slower.
  293. On polygons with self-intersections or degeneraces, there is nothing
  294. to compare against.
  295. The new tesselator generates many more fans and strips, reducing the
  296. number of vertices that need to be sent to the hardware.
  297. Key to the statistics:
  298. vert number of input vertices on all contours
  299. cntr number of input contours
  300. tri number of triangles in all output primitives
  301. strip number of triangle strips
  302. fan number of triangle fans
  303. ind number of independent triangles
  304. ms number of milliseconds for tesselation
  305. (on a 150MHz R4400 Indy)
  306. Convex polygon examples:
  307. New: 3 vert, 1 cntr, 1 tri, 0 strip, 0 fan, 1 ind, 0.0459 ms
  308. Old: 3 vert, 1 cntr, 1 tri, 0 strip, 0 fan, 1 ind, 0.149 ms
  309. New: 4 vert, 1 cntr, 2 tri, 0 strip, 1 fan, 0 ind, 0.0459 ms
  310. Old: 4 vert, 1 cntr, 2 tri, 0 strip, 0 fan, 2 ind, 0.161 ms
  311. New: 36 vert, 1 cntr, 34 tri, 0 strip, 1 fan, 0 ind, 0.153 ms
  312. Old: 36 vert, 1 cntr, 34 tri, 0 strip, 0 fan, 34 ind, 0.621 ms
  313. Concave single-contour polygons:
  314. New: 5 vert, 1 cntr, 3 tri, 0 strip, 1 fan, 0 ind, 0.052 ms
  315. Old: 5 vert, 1 cntr, 3 tri, 0 strip, 0 fan, 3 ind, 0.252 ms
  316. New: 19 vert, 1 cntr, 17 tri, 2 strip, 2 fan, 1 ind, 0.911 ms
  317. Old: 19 vert, 1 cntr, 17 tri, 0 strip, 0 fan, 17 ind, 0.529 ms
  318. New: 151 vert, 1 cntr, 149 tri, 13 strip, 18 fan, 3 ind, 6.82 ms
  319. Old: 151 vert, 1 cntr, 149 tri, 0 strip, 3 fan, 143 ind, 2.7 ms
  320. New: 574 vert, 1 cntr, 572 tri, 59 strip, 54 fan, 11 ind, 26.6 ms
  321. Old: 574 vert, 1 cntr, 572 tri, 0 strip, 31 fan, 499 ind, 12.4 ms
  322. Multiple contours, but no intersections:
  323. New: 7 vert, 2 cntr, 7 tri, 1 strip, 0 fan, 0 ind, 0.527 ms
  324. Old: 7 vert, 2 cntr, 7 tri, 0 strip, 0 fan, 7 ind, 0.274 ms
  325. New: 81 vert, 6 cntr, 89 tri, 9 strip, 7 fan, 6 ind, 3.88 ms
  326. Old: 81 vert, 6 cntr, 89 tri, 0 strip, 13 fan, 61 ind, 2.2 ms
  327. New: 391 vert, 19 cntr, 413 tri, 37 strip, 32 fan, 26 ind, 20.2 ms
  328. Old: 391 vert, 19 cntr, 413 tri, 0 strip, 25 fan, 363 ind, 8.68 ms
  329. Self-intersecting and degenerate examples:
  330. Bowtie: 4 vert, 1 cntr, 2 tri, 0 strip, 0 fan, 2 ind, 0.483 ms
  331. Star: 5 vert, 1 cntr, 5 tri, 0 strip, 0 fan, 5 ind, 0.91 ms
  332. Random: 24 vert, 7 cntr, 46 tri, 2 strip, 12 fan, 7 ind, 5.32 ms
  333. Font: 333 vert, 2 cntr, 331 tri, 32 strip, 16 fan, 3 ind, 14.1 ms
  334. : 167 vert, 35 cntr, 254 tri, 8 strip, 56 fan, 52 ind, 46.3 ms
  335. : 78 vert, 1 cntr, 2675 tri, 148 strip, 207 fan, 180 ind, 243 ms
  336. : 12480 vert, 2 cntr, 12478 tri, 736 strip,1275 fan, 5 ind, 1010 ms