Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
20 KiB

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