2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 ** http://oss.sgi.com/projects/FreeB
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
36 ** Author: Eric Veach, July 1994.
38 ** $Date: 2002/11/01 23:45:31 $ $Revision: 1.3 $
39 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/tess.c,v 1.3 2002/11/01 23:45:31 brianp Exp $
54 #define GLU_TESS_DEFAULT_TOLERANCE 0.0
55 #define GLU_TESS_MESH 100112 /* void (*)(GLUmesh *mesh) */
60 /*ARGSUSED*/ static void GLAPIENTRY
noBegin( GLenum type
) {}
61 /*ARGSUSED*/ static void GLAPIENTRY
noEdgeFlag( GLboolean boundaryEdge
) {}
62 /*ARGSUSED*/ static void GLAPIENTRY
noVertex( void *data
) {}
63 /*ARGSUSED*/ static void GLAPIENTRY
noEnd( void ) {}
64 /*ARGSUSED*/ static void GLAPIENTRY
noError( GLenum errnum
) {}
65 /*ARGSUSED*/ static void GLAPIENTRY
noCombine( GLdouble coords
[3], void *data
[4],
66 GLfloat weight
[4], void **dataOut
) {}
67 /*ARGSUSED*/ static void GLAPIENTRY
noMesh( GLUmesh
*mesh
) {}
70 /*ARGSUSED*/ void GLAPIENTRY
__gl_noBeginData( GLenum type
,
71 void *polygonData
) {}
72 /*ARGSUSED*/ void GLAPIENTRY
__gl_noEdgeFlagData( GLboolean boundaryEdge
,
73 void *polygonData
) {}
74 /*ARGSUSED*/ void GLAPIENTRY
__gl_noVertexData( void *data
,
75 void *polygonData
) {}
76 /*ARGSUSED*/ void GLAPIENTRY
__gl_noEndData( void *polygonData
) {}
77 /*ARGSUSED*/ void GLAPIENTRY
__gl_noErrorData( GLenum errnum
,
78 void *polygonData
) {}
79 /*ARGSUSED*/ void GLAPIENTRY
__gl_noCombineData( GLdouble coords
[3],
83 void *polygonData
) {}
85 /* Half-edges are allocated in pairs (see mesh.c) */
86 typedef struct { GLUhalfEdge e
, eSym
; } EdgePair
;
88 #define MAX(a,b) ((a) > (b) ? (a) : (b))
89 #define MAX_FAST_ALLOC (MAX(sizeof(EdgePair), \
90 MAX(sizeof(GLUvertex),sizeof(GLUface))))
93 GLUtesselator
* GLAPIENTRY
98 /* Only initialize fields which can be changed by the api. Other fields
99 * are initialized where they are used.
102 if (memInit( MAX_FAST_ALLOC
) == 0) {
103 return 0; /* out of memory */
105 tess
= (GLUtesselator
*)memAlloc( sizeof( GLUtesselator
));
107 return 0; /* out of memory */
110 tess
->state
= T_DORMANT
;
116 tess
->relTolerance
= GLU_TESS_DEFAULT_TOLERANCE
;
117 tess
->windingRule
= GLU_TESS_WINDING_ODD
;
118 tess
->flagBoundary
= FALSE
;
119 tess
->boundaryOnly
= FALSE
;
121 tess
->callBegin
= &noBegin
;
122 tess
->callEdgeFlag
= &noEdgeFlag
;
123 tess
->callVertex
= &noVertex
;
124 tess
->callEnd
= &noEnd
;
126 tess
->callError
= &noError
;
127 tess
->callCombine
= &noCombine
;
128 tess
->callMesh
= &noMesh
;
130 tess
->callBeginData
= &__gl_noBeginData
;
131 tess
->callEdgeFlagData
= &__gl_noEdgeFlagData
;
132 tess
->callVertexData
= &__gl_noVertexData
;
133 tess
->callEndData
= &__gl_noEndData
;
134 tess
->callErrorData
= &__gl_noErrorData
;
135 tess
->callCombineData
= &__gl_noCombineData
;
137 tess
->polygonData
= NULL
;
142 static void MakeDormant( GLUtesselator
*tess
)
144 /* Return the tessellator to its original dormant state. */
146 if( tess
->mesh
!= NULL
) {
147 __gl_meshDeleteMesh( tess
->mesh
);
149 tess
->state
= T_DORMANT
;
150 tess
->lastEdge
= NULL
;
154 #define RequireState( tess, s ) if( tess->state != s ) GotoState(tess,s)
156 static void GotoState( GLUtesselator
*tess
, enum TessState newState
)
158 while( tess
->state
!= newState
) {
159 /* We change the current state one level at a time, to get to
162 if( tess
->state
< newState
) {
163 switch( tess
->state
) {
165 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_BEGIN_POLYGON
);
166 gluTessBeginPolygon( tess
, NULL
);
169 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_BEGIN_CONTOUR
);
170 gluTessBeginContour( tess
);
176 switch( tess
->state
) {
178 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_END_CONTOUR
);
179 gluTessEndContour( tess
);
182 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_END_POLYGON
);
183 /* gluTessEndPolygon( tess ) is too much work! */
195 gluDeleteTess( GLUtesselator
*tess
)
197 RequireState( tess
, T_DORMANT
);
203 gluTessProperty( GLUtesselator
*tess
, GLenum which
, GLdouble value
)
208 case GLU_TESS_TOLERANCE
:
209 if( value
< 0.0 || value
> 1.0 ) break;
210 tess
->relTolerance
= value
;
213 case GLU_TESS_WINDING_RULE
:
214 windingRule
= (GLenum
) value
;
215 if( windingRule
!= value
) break; /* not an integer */
217 switch( windingRule
) {
218 case GLU_TESS_WINDING_ODD
:
219 case GLU_TESS_WINDING_NONZERO
:
220 case GLU_TESS_WINDING_POSITIVE
:
221 case GLU_TESS_WINDING_NEGATIVE
:
222 case GLU_TESS_WINDING_ABS_GEQ_TWO
:
223 tess
->windingRule
= windingRule
;
229 case GLU_TESS_BOUNDARY_ONLY
:
230 tess
->boundaryOnly
= (value
!= 0);
234 CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM
);
237 CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_VALUE
);
240 /* Returns tessellator property */
242 gluGetTessProperty( GLUtesselator
*tess
, GLenum which
, GLdouble
*value
)
245 case GLU_TESS_TOLERANCE
:
246 /* tolerance should be in range [0..1] */
247 assert(0.0 <= tess
->relTolerance
&& tess
->relTolerance
<= 1.0);
248 *value
= tess
->relTolerance
;
250 case GLU_TESS_WINDING_RULE
:
251 assert(tess
->windingRule
== GLU_TESS_WINDING_ODD
||
252 tess
->windingRule
== GLU_TESS_WINDING_NONZERO
||
253 tess
->windingRule
== GLU_TESS_WINDING_POSITIVE
||
254 tess
->windingRule
== GLU_TESS_WINDING_NEGATIVE
||
255 tess
->windingRule
== GLU_TESS_WINDING_ABS_GEQ_TWO
);
256 *value
= tess
->windingRule
;
258 case GLU_TESS_BOUNDARY_ONLY
:
259 assert(tess
->boundaryOnly
== TRUE
|| tess
->boundaryOnly
== FALSE
);
260 *value
= tess
->boundaryOnly
;
264 CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM
);
267 } /* gluGetTessProperty() */
270 gluTessNormal( GLUtesselator
*tess
, GLdouble x
, GLdouble y
, GLdouble z
)
278 gluTessCallback( GLUtesselator
*tess
, GLenum which
, _GLUfuncptr fn
)
282 tess
->callBegin
= (fn
== NULL
) ? &noBegin
: (void (GLAPIENTRY
*)(GLenum
)) fn
;
284 case GLU_TESS_BEGIN_DATA
:
285 tess
->callBeginData
= (fn
== NULL
) ?
286 &__gl_noBeginData
: (void (GLAPIENTRY
*)(GLenum
, void *)) fn
;
288 case GLU_TESS_EDGE_FLAG
:
289 tess
->callEdgeFlag
= (fn
== NULL
) ? &noEdgeFlag
:
290 (void (GLAPIENTRY
*)(GLboolean
)) fn
;
291 /* If the client wants boundary edges to be flagged,
292 * we render everything as separate triangles (no strips or fans).
294 tess
->flagBoundary
= (fn
!= NULL
);
296 case GLU_TESS_EDGE_FLAG_DATA
:
297 tess
->callEdgeFlagData
= (fn
== NULL
) ?
298 &__gl_noEdgeFlagData
: (void (GLAPIENTRY
*)(GLboolean
, void *)) fn
;
299 /* If the client wants boundary edges to be flagged,
300 * we render everything as separate triangles (no strips or fans).
302 tess
->flagBoundary
= (fn
!= NULL
);
304 case GLU_TESS_VERTEX
:
305 tess
->callVertex
= (fn
== NULL
) ? &noVertex
:
306 (void (GLAPIENTRY
*)(void *)) fn
;
308 case GLU_TESS_VERTEX_DATA
:
309 tess
->callVertexData
= (fn
== NULL
) ?
310 &__gl_noVertexData
: (void (GLAPIENTRY
*)(void *, void *)) fn
;
313 tess
->callEnd
= (fn
== NULL
) ? &noEnd
: (void (GLAPIENTRY
*)(void)) fn
;
315 case GLU_TESS_END_DATA
:
316 tess
->callEndData
= (fn
== NULL
) ? &__gl_noEndData
:
317 (void (GLAPIENTRY
*)(void *)) fn
;
320 tess
->callError
= (fn
== NULL
) ? &noError
: (void (GLAPIENTRY
*)(GLenum
)) fn
;
322 case GLU_TESS_ERROR_DATA
:
323 tess
->callErrorData
= (fn
== NULL
) ?
324 &__gl_noErrorData
: (void (GLAPIENTRY
*)(GLenum
, void *)) fn
;
326 case GLU_TESS_COMBINE
:
327 tess
->callCombine
= (fn
== NULL
) ? &noCombine
:
328 (void (GLAPIENTRY
*)(GLdouble
[3],void *[4], GLfloat
[4], void ** )) fn
;
330 case GLU_TESS_COMBINE_DATA
:
331 tess
->callCombineData
= (fn
== NULL
) ? &__gl_noCombineData
:
332 (void (GLAPIENTRY
*)(GLdouble
[3],
339 tess
->callMesh
= (fn
== NULL
) ? &noMesh
: (void (GLAPIENTRY
*)(GLUmesh
*)) fn
;
342 CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM
);
347 static int AddVertex( GLUtesselator
*tess
, GLdouble coords
[3], void *data
)
353 /* Make a self-loop (one vertex, one edge). */
355 e
= __gl_meshMakeEdge( tess
->mesh
);
356 if (e
== NULL
) return 0;
357 if ( !__gl_meshSplice( e
, e
->Sym
) ) return 0;
359 /* Create a new vertex and edge which immediately follow e
360 * in the ordering around the left face.
362 if (__gl_meshSplitEdge( e
) == NULL
) return 0;
366 /* The new vertex is now e->Org. */
368 e
->Org
->coords
[0] = coords
[0];
369 e
->Org
->coords
[1] = coords
[1];
370 e
->Org
->coords
[2] = coords
[2];
372 /* The winding of an edge says how the winding number changes as we
373 * cross from the edge''s right face to its left face. We add the
374 * vertices in such an order that a CCW contour will add +1 to
375 * the winding number of the region inside the contour.
378 e
->Sym
->winding
= -1;
386 static void CacheVertex( GLUtesselator
*tess
, GLdouble coords
[3], void *data
)
388 CachedVertex
*v
= &tess
->cache
[tess
->cacheCount
];
391 v
->coords
[0] = coords
[0];
392 v
->coords
[1] = coords
[1];
393 v
->coords
[2] = coords
[2];
398 static int EmptyCache( GLUtesselator
*tess
)
400 CachedVertex
*v
= tess
->cache
;
403 tess
->mesh
= __gl_meshNewMesh();
404 if (tess
->mesh
== NULL
) return 0;
406 for( vLast
= v
+ tess
->cacheCount
; v
< vLast
; ++v
) {
407 if ( !AddVertex( tess
, v
->coords
, v
->data
) ) return 0;
409 tess
->cacheCount
= 0;
410 tess
->emptyCache
= FALSE
;
417 gluTessVertex( GLUtesselator
*tess
, GLdouble coords
[3], void *data
)
419 int i
, tooLarge
= FALSE
;
420 GLdouble x
, clamped
[3];
422 RequireState( tess
, T_IN_CONTOUR
);
424 if( tess
->emptyCache
) {
425 if ( !EmptyCache( tess
) ) {
426 CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY
);
429 tess
->lastEdge
= NULL
;
431 for( i
= 0; i
< 3; ++i
) {
433 if( x
< - GLU_TESS_MAX_COORD
) {
434 x
= - GLU_TESS_MAX_COORD
;
437 if( x
> GLU_TESS_MAX_COORD
) {
438 x
= GLU_TESS_MAX_COORD
;
444 CALL_ERROR_OR_ERROR_DATA( GLU_TESS_COORD_TOO_LARGE
);
447 if( tess
->mesh
== NULL
) {
448 if( tess
->cacheCount
< TESS_MAX_CACHE
) {
449 CacheVertex( tess
, clamped
, data
);
452 if ( !EmptyCache( tess
) ) {
453 CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY
);
457 if ( !AddVertex( tess
, clamped
, data
) ) {
458 CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY
);
464 gluTessBeginPolygon( GLUtesselator
*tess
, void *data
)
466 RequireState( tess
, T_DORMANT
);
468 tess
->state
= T_IN_POLYGON
;
469 tess
->cacheCount
= 0;
470 tess
->emptyCache
= FALSE
;
473 tess
->polygonData
= data
;
478 gluTessBeginContour( GLUtesselator
*tess
)
480 RequireState( tess
, T_IN_POLYGON
);
482 tess
->state
= T_IN_CONTOUR
;
483 tess
->lastEdge
= NULL
;
484 if( tess
->cacheCount
> 0 ) {
485 /* Just set a flag so we don't get confused by empty contours
486 * -- these can be generated accidentally with the obsolete
487 * NextContour() interface.
489 tess
->emptyCache
= TRUE
;
495 gluTessEndContour( GLUtesselator
*tess
)
497 RequireState( tess
, T_IN_CONTOUR
);
498 tess
->state
= T_IN_POLYGON
;
502 gluTessEndPolygon( GLUtesselator
*tess
)
506 if (setjmp(tess
->env
) != 0) {
507 /* come back here if out of memory */
508 CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY
);
512 RequireState( tess
, T_IN_POLYGON
);
513 tess
->state
= T_DORMANT
;
515 if( tess
->mesh
== NULL
) {
516 if( ! tess
->flagBoundary
&& tess
->callMesh
== &noMesh
) {
518 /* Try some special code to make the easy cases go quickly
519 * (eg. convex polygons). This code does NOT handle multiple contours,
520 * intersections, edge flags, and of course it does not generate
521 * an explicit mesh either.
523 if( __gl_renderCache( tess
)) {
524 tess
->polygonData
= NULL
;
528 if ( !EmptyCache( tess
) ) longjmp(tess
->env
,1); /* could've used a label*/
531 /* Determine the polygon normal and project vertices onto the plane
534 __gl_projectPolygon( tess
);
536 /* __gl_computeInterior( tess ) computes the planar arrangement specified
537 * by the given contours, and further subdivides this arrangement
538 * into regions. Each region is marked "inside" if it belongs
539 * to the polygon, according to the rule given by tess->windingRule.
540 * Each interior region is guaranteed be monotone.
542 if ( !__gl_computeInterior( tess
) ) {
543 longjmp(tess
->env
,1); /* could've used a label */
547 if( ! tess
->fatalError
) {
550 /* If the user wants only the boundary contours, we throw away all edges
551 * except those which separate the interior from the exterior.
552 * Otherwise we tessellate all the regions marked "inside".
554 if( tess
->boundaryOnly
) {
555 rc
= __gl_meshSetWindingNumber( mesh
, 1, TRUE
);
557 rc
= __gl_meshTessellateInterior( mesh
);
559 if (rc
== 0) longjmp(tess
->env
,1); /* could've used a label */
561 __gl_meshCheckMesh( mesh
);
563 if( tess
->callBegin
!= &noBegin
|| tess
->callEnd
!= &noEnd
564 || tess
->callVertex
!= &noVertex
|| tess
->callEdgeFlag
!= &noEdgeFlag
565 || tess
->callBeginData
!= &__gl_noBeginData
566 || tess
->callEndData
!= &__gl_noEndData
567 || tess
->callVertexData
!= &__gl_noVertexData
568 || tess
->callEdgeFlagData
!= &__gl_noEdgeFlagData
)
570 if( tess
->boundaryOnly
) {
571 __gl_renderBoundary( tess
, mesh
); /* output boundary contours */
573 __gl_renderMesh( tess
, mesh
); /* output strips and fans */
576 if( tess
->callMesh
!= &noMesh
) {
578 /* Throw away the exterior faces, so that all faces are interior.
579 * This way the user doesn't have to check the "inside" flag,
580 * and we don't need to even reveal its existence. It also leaves
581 * the freedom for an implementation to not generate the exterior
582 * faces in the first place.
584 __gl_meshDiscardExterior( mesh
);
585 (*tess
->callMesh
)( mesh
); /* user wants the mesh itself */
587 tess
->polygonData
= NULL
;
591 __gl_meshDeleteMesh( mesh
);
592 tess
->polygonData
= NULL
;
597 /*XXXblythe unused function*/
600 gluDeleteMesh( GLUmesh
*mesh
)
602 __gl_meshDeleteMesh( mesh
);
608 /*******************************************************/
610 /* Obsolete calls -- for backward compatibility */
613 gluBeginPolygon( GLUtesselator
*tess
)
615 gluTessBeginPolygon( tess
, NULL
);
616 gluTessBeginContour( tess
);
622 gluNextContour( GLUtesselator
*tess
, GLenum type
)
624 gluTessEndContour( tess
);
625 gluTessBeginContour( tess
);
630 gluEndPolygon( GLUtesselator
*tess
)
632 gluTessEndContour( tess
);
633 gluTessEndPolygon( tess
);