summaryrefslogtreecommitdiffstats
path: root/src/glu/mesa/tess.c
blob: 4f91d135e854b766cb10f3e14ff6742340efe3a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/* $Id: tess.c,v 1.23 2000/01/20 21:52:01 gareth Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.1
 *
 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*****************************************************************************
 *
 * GLU 1.3 Polygon Tessellation by Gareth Hughes <garethh@bell-labs.com>
 *
 *****************************************************************************/

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "gluP.h"

#include "tess.h"
#include "tess_macros.h"
#include "tess_fist.h"
#if 0
#include "tess_grid.h"
#endif


#define TESS_CHECK_ERRORS(t)	if ( (t)->error != GLU_NO_ERROR ) goto cleanup

#ifdef DEBUG
GLint		tess_dbg_level;
#endif


/*****************************************************************************
 * tess_error_callback
 *
 * Internal error handler.  Call the user-registered error callback.
 *
 * 2nd arg changed from 'errno' to 'errnum' since MSVC defines errnum as
 *  a macro (of all things) and thus breaks the build -tjump
 *****************************************************************************/
void tess_error_callback( GLUtesselator *tobj, GLenum errnum )
{
    if ( tobj->error == GLU_NO_ERROR )
    {
	tobj->error = errnum;
    }

    if ( tobj->callbacks.errorData != NULL )
    {
	( tobj->callbacks.errorData )( errnum, tobj->data );
    }
    else if ( tobj->callbacks.error != NULL )
    {
	( tobj->callbacks.error )( errnum );
    }
}


/*****************************************************************************
 * init_callbacks
 *****************************************************************************/
static void init_callbacks( tess_callbacks_t *callbacks )
{
    callbacks->begin        = ( void (GLCALLBACKP)(GLenum) ) NULL;
    callbacks->beginData    = ( void (GLCALLBACKP)(GLenum, void *) ) NULL;
    callbacks->edgeFlag     = ( void (GLCALLBACKP)(GLboolean) ) NULL;
    callbacks->edgeFlagData = ( void (GLCALLBACKP)(GLboolean, void *) ) NULL;
    callbacks->vertex       = ( void (GLCALLBACKP)(void *) ) NULL;
    callbacks->vertexData   = ( void (GLCALLBACKP)(void *, void *) ) NULL;
    callbacks->end          = ( void (GLCALLBACKP)(void) ) NULL;
    callbacks->endData      = ( void (GLCALLBACKP)(void *) ) NULL;
    callbacks->error        = ( void (GLCALLBACKP)(GLenum) ) NULL;
    callbacks->errorData    = ( void (GLCALLBACKP)(GLenum, void *) ) NULL;
    callbacks->combine      = ( void (GLCALLBACKP)(GLdouble [3], void *[4],
						   GLfloat [4], void **) ) NULL;
    callbacks->combineData  = ( void (GLCALLBACKP)(GLdouble [3], void *[4],
						   GLfloat [4], void **,
						   void *) ) NULL;
}


/*****************************************************************************
 * find_normal
 *****************************************************************************/
static GLenum find_normal( GLUtesselator *tobj )
{
    tess_contour_t	*contour = tobj->current_contour;
    tess_vertex_t	*va, *vb, *vc;
    GLdouble		a[3], b[3], c[3];

    MSG( 15, "      --> find_normal( tobj:%p )\n", tobj );

    if ( contour == NULL ) { return GLU_ERROR; }

    va = contour->vertices;
    vb = va->next;

    /* If va and vb are the same point, keep looking for a different vertex. */

    while ( IS_EQUAL_3DV( va->coords, vb->coords ) && ( vb != va ) ) {
	vb = vb->next;
    }

    if ( vb == va ) {
	/* FIXME: What error is this? */
	tess_error_callback( tobj, GLU_TESS_ERROR7 );
    }

    SUB_3V( a, vb->coords, va->coords );

    for ( vc = vb->next; vc != va; vc = vc->next )
    {
	SUB_3V( b, vc->coords, va->coords );

	CROSS_3V( c, a, b );

	if ( ! IS_ZERO_3DV( c ) )
	{
	    MSG( 15, "            using (%.2f,%.2f) -> (%.2f,%.2f) -> (%.2f,%.2f)\n",
		 va->coords[X], va->coords[Y],
		 vb->coords[X], vb->coords[Y],
		 vc->coords[X], vc->coords[Y] );

	    COPY_3V( contour->plane.normal, c );
	    NORMALIZE_3DV( contour->plane.normal );

	    contour->plane.dist = - DOT_3V( contour->plane.normal,
					    va->coords );

	    MSG( 15, "      <-- find_normal( tobj:%p ) n: (%.2f, %.2f, %.2f)\n", tobj, contour->plane.normal[X], contour->plane.normal[Y], contour->plane.normal[Z] );
	    return GLU_NO_ERROR;
	}
    }
    /* FIXME: What error is this? */
    tess_error_callback( tobj, GLU_TESS_ERROR7 );

    return GLU_ERROR;
}

/*****************************************************************************
 * twice_contour_area
 *
 * Calculate the twice the signed area of the given contour.  Used to
 * determine the contour's orientation amongst other things.
 *****************************************************************************/
GLdouble twice_contour_area( tess_contour_t *contour )
{
    tess_vertex_t	*vertex = contour->vertices;
    GLdouble		area, x, y;

    area = 0.0;

    x = vertex->v[X];
    y = vertex->v[Y];

    vertex = vertex->next;

    do
    {
	area += ( (vertex->v[X] - x) * (vertex->next->v[Y] - y) -
		  (vertex->v[Y] - y) * (vertex->next->v[X] - x) );
	vertex = vertex->next;
    }
    while ( vertex != contour->vertices );

    return area;
}

/*****************************************************************************
 * project_current_contour
 *
 * Project the contour's vertices onto the tessellation plane.  We perform
 * a complex rotation here to allow non-axis-aligned tessellation normals.
 *****************************************************************************/
static void project_current_contour( GLUtesselator *tobj )
{
    tess_contour_t	*current = tobj->current_contour;
    tess_vertex_t	*vertex;
    GLdouble		zaxis[3] = { 0.0, 0.0, 1.0 }, znormal[3], xnormal[3];
    GLdouble		dot, rotx, roty;
    GLint		i;

    MSG( 15, "      --> project_current_contour( tobj:%p )\n", tobj );

    if ( current == NULL ) { return; }

    /* Rotate the plane normal around the y-axis. */

    znormal[X] = current->plane.normal[X];
    znormal[Y] = 0.0;
    znormal[Z] = current->plane.normal[Z];

    dot = DOT_3V( znormal, zaxis );
    current->roty = roty = acos( dot );

    /* Rotate the plane normal around the x-axis. */

    xnormal[X] = cos( roty ) * znormal[X] - sin( roty ) * znormal[Z];
    xnormal[Y] = znormal[Y];
    xnormal[Z] = sin( roty ) * znormal[X] + cos( roty ) * znormal[Z];

    dot = DOT_3V( xnormal, zaxis );
    current->rotx = rotx = acos( dot );

    for ( vertex = current->vertices, i = 0;
	  i < current->num_vertices; vertex = vertex->next, i++ )
    {
	tess_plane_t	*plane = &current->plane;
	GLdouble	proj[3], yrot[3], xrot[3];

	/* FIXME: This needs a cleanup, 'cos I'm sure it's inefficient. */

	proj[X] = vertex->coords[X] - plane->dist * plane->normal[X];
	proj[Y] = vertex->coords[Y] - plane->dist * plane->normal[Y];
	proj[Z] = vertex->coords[Z] - plane->dist * plane->normal[Z];

	yrot[X] = cos( roty ) * proj[X] - sin( roty ) * proj[Z];
	yrot[Y] = proj[Y];
	yrot[Z] = sin( roty ) * proj[X] + cos( roty ) * proj[Z];

	xrot[X] = yrot[X];
	xrot[Y] = cos( rotx ) * yrot[Y] - sin( rotx ) * yrot[Z];
	xrot[Z] = sin( rotx ) * yrot[Y] + cos( rotx ) * yrot[Z];

	vertex->v[X] = xrot[X];
	vertex->v[Y] = xrot[Y];

	ACC_BBOX_2V( vertex->v, tobj->mins, tobj->maxs );
	ACC_BBOX_2V( vertex->v, current->mins, current->maxs );
    }

    current->area = twice_contour_area( current );
    current->orientation = ( current->area >= 0.0 ) ? GLU_CCW : GLU_CW;

    MSG( 15, "            area: %.2f orientation: %s\n",
	 current->area, ( current->orientation == GLU_CCW ) ? "CCW" : "CW" );

    MSG( 15, "      <-- project_current_contour( tobj:%p )\n", tobj );
}

/*****************************************************************************
 * save_current_contour
 *****************************************************************************/
static GLenum save_current_contour( GLUtesselator *tobj )
{
    tess_contour_t	*current = tobj->current_contour;
    tess_vertex_t	*vertex;
    GLint		i;

    if ( current == NULL ) { return GLU_ERROR; }

    if ( tobj->contours == NULL )
    {
	tobj->contours = tobj->last_contour = current;
	current->next = current->prev = NULL;

	tobj->orientation = current->orientation;
    }
    else
    {
	current->prev = tobj->last_contour;

	tobj->last_contour->next = current;
	tobj->last_contour = current;

	current->next = NULL;
    }

    for ( vertex = current->vertices, i = 0;
	  i < current->num_vertices; vertex = vertex->next, i++ )
    {
	vertex->edge_flag = GL_TRUE;
    }

    current->type = GLU_UNKNOWN;

    tobj->num_contours++;
    tobj->current_contour = NULL;

    return GLU_NO_ERROR;
}

/*****************************************************************************
 * inspect_current_contour
 *****************************************************************************/
static void inspect_current_contour( GLUtesselator *tobj )
{
    tess_contour_t	*current = tobj->current_contour;
    GLdouble		origin[3] = { 0.0, 0.0, 0.0 };
    GLboolean		calc_normal = GL_FALSE;

    MSG( 15, "    --> inspect_current_contour( tobj:%p )\n", tobj );

    if ( current->num_vertices < 3 )
    {
	MSG( 15, "          count %d < 3, deleting\n", current->num_vertices );
	delete_contour( &tobj->current_contour );
	return;
    }

    current->last_vertex->next = current->vertices;
    current->vertices->prev = current->last_vertex;

    MSG( 15, "          current normal: (%.2f, %.2f, %.2f)\n", current->plane.normal[X], current->plane.normal[Y], current->plane.normal[Z] );

    if ( IS_EQUAL_3DV( current->plane.normal, origin ) )
    {
	/* We haven't been given a normal, so let's take a guess. */
	if ( find_normal( tobj ) == GLU_ERROR ) {
	    return;
	}

	COPY_3V( tobj->plane.normal, current->plane.normal );
	tobj->plane.dist = current->plane.dist;

	calc_normal = GL_TRUE;
    }

    project_current_contour( tobj );

    if ( calc_normal && ( tobj->current_contour->orientation == GLU_CW ) )
    {
	MSG( 15, "          oops, let's try that again...\n" );

	/*
	 * FIXME: We've used a reflex angle to calculate the normal.  At
	 * the moment, we simply reverse the normal and re-project the
	 * contour, but this is sloooow...
	 */
	NEG_3V( tobj->plane.normal );
	NEG_3V( tobj->current_contour->plane.normal );

	project_current_contour( tobj );
    }

    if ( save_current_contour( tobj ) == GLU_ERROR ) {
	return;
    }

    MSG( 15, "    <-- inspect_current_contour( tobj:%p )\n", tobj );
}

/*****************************************************************************
 * reverse_contour
 *****************************************************************************/
void reverse_contour( tess_contour_t *contour )
{
    tess_vertex_t	*current = contour->vertices;
    GLint		i;

    for ( i = 0 ; i < contour->num_vertices ; i++ )
    {
	tess_vertex_t	*next = current->next;
	tess_vertex_t	*prev = current->prev;

	current->next = prev;
	current->prev = next;

	current = next;
    }

    contour->orientation =
	( contour->orientation == GLU_CCW ) ? GLU_CW : GLU_CCW;

    contour->last_vertex = contour->vertices->prev;
}

/*****************************************************************************
 * orient_contours
 *
 * Sum the signed areas of the contours, and orient the contours such that
 * this sum is nonnegative.
 *****************************************************************************/
static void orient_contours( GLUtesselator *tobj )
{
    tess_contour_t	*contour = tobj->contours;
    GLdouble		sum = 0.0;
    GLint		i;

    MSG( 15, "    --> orient_contours( tobj:%p )\n", tobj );

    /* Sum the signed areas of all contours */
    for ( i = 0 ; i < tobj->num_contours ; i++ )
    {
	sum += contour->area;
	contour = contour->next;
    }

    MSG( 15, "          signed area: %.2f\n", sum );

    if ( sum < -GLU_TESS_EPSILON )
    {
	for ( i = 0 ; i < tobj->num_contours ; i++ )
	{
	    contour->area = ABSD( contour->area );
	    reverse_contour( contour );

	    contour = contour->next;
	}
    }
    else
    {
	for ( i = 0 ; i < tobj->num_contours ; i++ )
	{
	    contour->area = ABSD( contour->area );

	    contour = contour->next;
	}
    }

    tobj->orientation = tobj->contours->orientation;

    MSG( 15, "    <-- orient_contours( tobj:%p ) orient: %s\n",
	 tobj, ( tobj->orientation == GLU_CCW ) ? "GLU_CCW" : "GLU_CW" );
}


/*****************************************************************************
 * delete_contour
 *
 * Delete the given contour and set the pointer to NULL.
 *****************************************************************************/
void delete_contour( tess_contour_t **contour )
{
    tess_vertex_t	*vertex, *next;
    GLint		i;

    if ( *contour == NULL ) { return; }

    vertex = (*contour)->vertices;

    for ( i = 0 ; i < (*contour)->num_vertices ; i++ )
    {
	next = vertex->next;
	free( vertex );
	vertex = next;
    }

    free( *contour );
    *contour = NULL;
}

/*****************************************************************************
 * delete_all_contours
 *****************************************************************************/
static void delete_all_contours( GLUtesselator *tobj )
{
    tess_contour_t	*current, *next_contour;
    GLint		i;

    if ( tobj->current_contour != NULL ) {
	delete_contour( &tobj->current_contour );
    }

    for ( current = tobj->contours, i = 0 ; i < tobj->num_contours ; i++ )
    {
	tess_vertex_t	*vertex = current->vertices, *next_vertex;
	GLint		j;

	for ( j = 0 ; j < current->num_vertices ; j ++ )
	{
	    next_vertex = vertex->next;
	    free( vertex );
	    vertex = next_vertex;
	}
	next_contour = current->next;

	free( current );
	current = next_contour;
    }

    tobj->num_contours = tobj->num_vertices = 0;
    tobj->contours = tobj->last_contour = NULL;

    CLEAR_BBOX_2DV( tobj->mins, tobj->maxs );
}


/*****************************************************************************
 * tess_cleanup
 *****************************************************************************/
static void tess_cleanup( GLUtesselator *tobj )
{
    MSG( 15, "  -> tess_cleanup( tobj:%p )\n", tobj );

    if ( tobj->current_contour != NULL ) {
	delete_contour( &tobj->current_contour );
    }
    if ( tobj->contours != NULL ) {
	delete_all_contours( tobj );
    }

    MSG( 15, "  <- tess_cleanup( tobj:%p )\n", tobj );
}


/*****************************************************************************
 * tess_msg
 *****************************************************************************/
INLINE void tess_msg( GLint level, char *format, ... )
{
#ifdef DEBUG
    va_list ap;
    va_start( ap, format );

    if ( level <= tess_dbg_level ) {
	vfprintf( DBG_STREAM, format, ap );
	fflush( DBG_STREAM );
    }

    va_end( ap );
#endif
}

INLINE void tess_info( char *file, GLint line )
{
#ifdef DEBUG
    fprintf( DBG_STREAM, "%9.9s:%d:\t ", file, line );
#endif
}



/*****************************************************************************
 *
 *			GLU TESSELLATION FUNCTIONS
 *
 *****************************************************************************/


/*****************************************************************************
 * gluNewTess
 *****************************************************************************/
GLUtesselator* GLAPIENTRY gluNewTess( void )
{
    GLUtesselator *tobj;

#ifdef DEBUG
    if ( getenv( "MESA_TESS_DBG_LEVEL" ) ) {
	tess_dbg_level = atoi( getenv( "MESA_TESS_DBG_LEVEL" ) );
    } else {
	tess_dbg_level = DBG_LEVEL_BASE;
    }
#endif

    MSG( 15, "-> gluNewTess()\n" );

    tobj = malloc( sizeof(GLUtesselator) );
    if ( tobj == NULL ) {
	return NULL;
    }

    init_callbacks( &tobj->callbacks );

    tobj->winding_rule = GLU_TESS_WINDING_ODD;
    tobj->boundary_only = GL_FALSE;
    tobj->tolerance = GLU_TESS_EPSILON;
    tobj->orientation = GLU_UNKNOWN;

    tobj->data = NULL;

    tobj->num_contours = 0;
    tobj->contours = tobj->last_contour = NULL;
    tobj->current_contour = NULL;

    CLEAR_BBOX_2DV( tobj->mins, tobj->maxs );

    tobj->num_vertices = 0;
    tobj->sorted_vertices = NULL;
#if 0
    tobj->grid = NULL;
#endif
    tobj->edge_flag = GL_FALSE;
    tobj->label = 0;

    ZERO_3V( tobj->plane.normal );
    tobj->plane.dist = 0.0;

    tobj->error = GLU_NO_ERROR;

    MSG( 15, "<- gluNewTess() tobj:%p\n", tobj );
    return tobj;
}


/*****************************************************************************
 * gluDeleteTess
 *****************************************************************************/
void GLAPIENTRY gluDeleteTess( GLUtesselator *tobj )
{
    MSG( 15, "-> gluDeleteTess( tobj:%p )\n", tobj );

    if ( ( tobj->error == GLU_NO_ERROR ) && ( tobj->num_contours > 0 ) )
    {
	/* gluEndPolygon was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR3 );
    }

    /* Delete all internal structures. */
    tess_cleanup( tobj );
    free( tobj );

    MSG( 15, "<- gluDeleteTess()\n" );
}


/*****************************************************************************
 * gluTessBeginPolygon
 *****************************************************************************/
void GLAPIENTRY gluTessBeginPolygon( GLUtesselator *tobj, void *polygon_data )
{
    MSG( 15, "-> gluTessBeginPolygon( tobj:%p data:%p )\n", tobj, polygon_data );

    tobj->error = GLU_NO_ERROR;

    if ( tobj->current_contour != NULL )
    {
	/* gluEndPolygon was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR3 );
	tess_cleanup( tobj );
    }

    tobj->data = polygon_data;
    tobj->num_vertices = 0;
    tobj->edge_flag = GL_FALSE;
    tobj->label = 0;

    MSG( 15, "<- gluTessBeginPolygon( tobj:%p data:%p )\n", tobj, polygon_data );
}


/*****************************************************************************
 * gluTessBeginContour
 *****************************************************************************/
void GLAPIENTRY gluTessBeginContour( GLUtesselator *tobj )
{
    MSG( 15, "  -> gluTessBeginContour( tobj:%p )\n", tobj );
    TESS_CHECK_ERRORS( tobj );

    if ( tobj->current_contour != NULL )
    {
	/* gluTessEndContour was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR4 );
	return;
    }

    tobj->current_contour = malloc( sizeof(tess_contour_t) );
    if ( tobj->current_contour == NULL ) {
	tess_error_callback( tobj, GLU_OUT_OF_MEMORY );
	return;
    }

    COPY_3V( tobj->current_contour->plane.normal, tobj->plane.normal );
    tobj->current_contour->plane.dist = tobj->plane.dist;

    tobj->current_contour->area = 0.0;
    tobj->current_contour->orientation = GLU_UNKNOWN;

    tobj->current_contour->label = 0;
    tobj->current_contour->winding = 0;

    /*tobj->current_contour->rotx = tobj->current_contour->roty = 0.0;*/

    CLEAR_BBOX_2DV( tobj->current_contour->mins,
		    tobj->current_contour->maxs );

    tobj->current_contour->num_vertices = 0;
    tobj->current_contour->vertices =
	tobj->current_contour->last_vertex = NULL;

    tobj->current_contour->reflex_vertices = NULL;

 cleanup:
    MSG( 15, "  <- gluTessBeginContour( tobj:%p )\n", tobj );
    return;
}


/*****************************************************************************
 * gluTessVertex
 *****************************************************************************/
void GLAPIENTRY gluTessVertex( GLUtesselator *tobj, GLdouble coords[3],
			       void *vertex_data )
{
    tess_contour_t		*current = tobj->current_contour;
    tess_vertex_t		*last_vertex;

    MSG( 15, "    -> gluTessVertex( tobj:%p coords:(%.2f,%.2f,%.2f) )\n", tobj, coords[0], coords[1], coords[2] );
    TESS_CHECK_ERRORS( tobj );

    if ( current == NULL )
    {
	/* gluTessBeginContour was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR2 );
	return;
    }

    tobj->num_vertices++;

    last_vertex = current->last_vertex;

    if ( last_vertex == NULL )
    {
	last_vertex = malloc( sizeof(tess_vertex_t) );
	if ( last_vertex == NULL ) {
	    tess_error_callback( tobj, GLU_OUT_OF_MEMORY );
	    return;
	}

	current->vertices = last_vertex;
	current->last_vertex = last_vertex;

	last_vertex->index = -1;
	last_vertex->data = vertex_data;

	last_vertex->coords[X] = coords[X];
	last_vertex->coords[Y] = coords[Y];
	last_vertex->coords[Z] = coords[Z];

	last_vertex->v[X] = 0.0;
	last_vertex->v[Y] = 0.0;

	last_vertex->edge_flag = GL_TRUE;

	last_vertex->side = 0.0;

	last_vertex->next = NULL;
	last_vertex->prev = NULL;

	current->num_vertices++;
    }
    else
    {
	tess_vertex_t	*vertex;

	vertex = malloc( sizeof(tess_vertex_t) );
	if ( vertex == NULL ) {
	    tess_error_callback( tobj, GLU_OUT_OF_MEMORY );
	    return;
	}

	vertex->index = -1;
	vertex->data = vertex_data;

	vertex->coords[X] = coords[X];
	vertex->coords[Y] = coords[Y];
	vertex->coords[Z] = coords[Z];

	vertex->v[X] = 0.0;
	vertex->v[Y] = 0.0;

	vertex->edge_flag = GL_TRUE;

	vertex->side = 0.0;

	vertex->next = NULL;
	vertex->prev = last_vertex;

	current->num_vertices++;

	last_vertex->next = vertex;
	current->last_vertex = vertex;
    }

 cleanup:
    MSG( 15, "    <- gluTessVertex( tobj:%p )\n", tobj );
    return;
}


/*****************************************************************************
 * gluTessEndContour
 *****************************************************************************/
void GLAPIENTRY gluTessEndContour( GLUtesselator *tobj )
{
    MSG( 15, "  -> gluTessEndContour( tobj:%p )\n", tobj );
    TESS_CHECK_ERRORS( tobj );

    if ( tobj->current_contour == NULL )
    {
	/* gluTessBeginContour was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR2 );
	return;
    }

    if ( tobj->current_contour->num_vertices > 0 ) {
	inspect_current_contour( tobj );
    } else {
	delete_contour( &tobj->current_contour );
    }

 cleanup:
    MSG( 15, "  <- gluTessEndContour( tobj:%p )\n", tobj );
    return;
}


/*****************************************************************************
 * gluTessEndPolygon
 *****************************************************************************/
void GLAPIENTRY gluTessEndPolygon( GLUtesselator *tobj )
{
    MSG( 15, "-> gluTessEndPolygon( tobj:%p )\n", tobj );
    TESS_CHECK_ERRORS( tobj );

    if ( tobj->current_contour != NULL )
    {
	/* gluTessBeginPolygon was not called. */
	tess_error_callback( tobj, GLU_TESS_ERROR1 );
	return;
    }
    TESS_CHECK_ERRORS( tobj );

    /*
     * Ensure we have at least one contour to tessellate.  If we have none,
     *  clean up and exit gracefully.
     */
    if ( tobj->num_contours == 0 ) {
	tess_cleanup( tobj );
	return;
    }

    /* Wrap the contour list. */

    tobj->last_contour->next = tobj->contours;
    tobj->contours->prev = tobj->last_contour;

    TESS_CHECK_ERRORS( tobj );

    /* Orient the contours correctly */
    orient_contours( tobj );

    /*
     * Before we tessellate the contours, ensure we have the appropriate
     *  callbacks registered.  We at least need the begin, vertex and end
     *  callbacks to do any meaningful work.
     */
    if ( ( ( tobj->callbacks.begin != NULL ) ||
	   ( tobj->callbacks.beginData != NULL ) ) &&
	 ( ( tobj->callbacks.vertex != NULL ) ||
	   ( tobj->callbacks.vertexData != NULL ) ) &&
	 ( ( tobj->callbacks.end != NULL ) ||
	   ( tobj->callbacks.endData != NULL ) ) )
    {
	fist_tessellation( tobj );
    }

 cleanup:
    delete_all_contours( tobj );
    MSG( 15, "<- gluTessEndPolygon( tobj:%p )\n", tobj );
}


/*****************************************************************************
 * gluTessCallback
 *****************************************************************************/
void GLAPIENTRY gluTessCallback( GLUtesselator *tobj, GLenum which,
				 void (GLCALLBACKP fn)() )
{
    switch ( which )
    {
	/* Register the begin callbacks. */
    case GLU_TESS_BEGIN:
	tobj->callbacks.begin = (void (GLCALLBACKP)(GLenum)) fn;
	break;
    case GLU_TESS_BEGIN_DATA:
	tobj->callbacks.beginData = (void (GLCALLBACKP)(GLenum, void *)) fn;
	break;

	/* Register the edge flag callbacks. */
    case GLU_TESS_EDGE_FLAG:
	tobj->callbacks.edgeFlag = (void (GLCALLBACKP)(GLboolean)) fn;
	break;
    case GLU_TESS_EDGE_FLAG_DATA:
	tobj->callbacks.edgeFlagData =
	    (void (GLCALLBACKP)(GLboolean, void *)) fn;
	break;

	/* Register the vertex callbacks. */
    case GLU_TESS_VERTEX:
	tobj->callbacks.vertex = (void (GLCALLBACKP)(void *)) fn;
	break;
    case GLU_TESS_VERTEX_DATA:
	tobj->callbacks.vertexData = (void (GLCALLBACKP)(void *, void *)) fn;
	break;

	/* Register the end callbacks. */
    case GLU_TESS_END:
	tobj->callbacks.end = (void (GLCALLBACKP)(void)) fn;
	break;
    case GLU_TESS_END_DATA:
	tobj->callbacks.endData = (void (GLCALLBACKP)(void *)) fn;
	break;

	/* Register the error callbacks. */
    case GLU_TESS_ERROR:
	tobj->callbacks.error = (void (GLCALLBACKP)(GLenum)) fn;
	break;
    case GLU_TESS_ERROR_DATA:
	tobj->callbacks.errorData = (void (GLCALLBACKP)(GLenum, void *)) fn;
	break;

	/* Register the combine callbacks. */
    case GLU_TESS_COMBINE:
	tobj->callbacks.combine =
	    (void (GLCALLBACKP)(GLdouble[3], void *[4],
				GLfloat [4], void **)) fn;
	break;
    case GLU_TESS_COMBINE_DATA:
	tobj->callbacks.combineData =
	    (void (GLCALLBACKP)(GLdouble[3], void *[4], GLfloat [4],
				void **, void *)) fn;
	break;

    default:
	MSG( 1, "  gluTessCallback( tobj:%p which:%d ) invalid enum\n", tobj, which );
	tobj->error = GLU_INVALID_ENUM;
	break;
    }
}


/*****************************************************************************
 * gluTessProperty
 *
 * Set the current value of the given property.
 *****************************************************************************/
void GLAPIENTRY gluTessProperty( GLUtesselator *tobj, GLenum which,
				 GLdouble value )
{
    switch ( which )
    {
    case GLU_TESS_BOUNDARY_ONLY:
	tobj->boundary_only = (GLboolean) value;
	break;

    case GLU_TESS_TOLERANCE:
	MSG( 15, "   gluTessProperty( tobj:%p ) tolerance: %0.9f\n", tobj, value );
	tobj->tolerance = value;
	break;

    case GLU_TESS_WINDING_RULE:
	tobj->winding_rule = (GLenum) value;
	break;

    default:
	MSG( 1, "   gluTessProperty( tobj:%p which:%d ) invalid enum\n", tobj, which );
	tobj->error = GLU_INVALID_ENUM;
	break;
    }
}


/*****************************************************************************
 * gluGetTessProperty
 *
 * Return the current value of the given property.
 *****************************************************************************/
void GLAPIENTRY gluGetTessProperty( GLUtesselator *tobj, GLenum which,
				    GLdouble *value )
{
    switch ( which )
    {
    case GLU_TESS_BOUNDARY_ONLY:
	*value = tobj->boundary_only;
	break;

    case GLU_TESS_TOLERANCE:
	*value = tobj->tolerance;
	break;

    case GLU_TESS_WINDING_RULE:
	*value = tobj->winding_rule;
	break;

    default:
	MSG( 1, "   gluGetTessProperty( tobj:%p which:%d ) invalid enum\n", tobj, which );
	tobj->error = GLU_INVALID_ENUM;
	break;
    }
}


/*****************************************************************************
 * gluTessNormal
 *
 * Set the current tessellation normal.
 *****************************************************************************/
void GLAPIENTRY gluTessNormal( GLUtesselator *tobj, GLdouble x,
			       GLdouble y, GLdouble z )
{
    MSG( 15, "   gluTessNormal( tobj:%p n:(%.2f,%.2f,%.2f) )\n", tobj, x, y, z );

    ASSIGN_3V( tobj->plane.normal, x, y, z );
}



/*****************************************************************************
 *
 *			OBSOLETE TESSELLATION FUNCTIONS
 *
 *****************************************************************************/

void GLAPIENTRY gluBeginPolygon( GLUtesselator *tobj )
{
    gluTessBeginPolygon( tobj, NULL );
    gluTessBeginContour( tobj );
}

void GLAPIENTRY gluNextContour( GLUtesselator *tobj, GLenum type )
{
    gluTessEndContour( tobj );
    gluTessBeginContour( tobj );
}

void GLAPIENTRY gluEndPolygon( GLUtesselator *tobj )
{
    gluTessEndContour( tobj );
    gluTessEndPolygon( tobj );
}