summaryrefslogtreecommitdiffstats
path: root/src/glu/mesa/quadric.c
blob: 5d7bc8f5c6160338e0c0b6c641667612bb176e95 (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
/* $Id: quadric.c,v 1.8 2000/07/11 14:11:04 brianp Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * Copyright (C) 1999-2000  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/* TODO:
 *   texture coordinate support
 *   flip normals according to orientation
 *   there's still some inside/outside orientation bugs in possibly all
 *     but the sphere function
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "gluP.h"
#endif



#ifndef M_PI
#  define M_PI (3.1415926)
#endif


/*
 * Convert degrees to radians:
 */
#define DEG_TO_RAD(A)   ((A)*(M_PI/180.0))


/*
 * Sin and Cos for degree angles:
 */
#define SIND( A )   sin( (A)*(M_PI/180.0) )
#define COSD( A)    cos( (A)*(M_PI/180.0) )


/*
 * Texture coordinates if texture flag is set
 */
#define TXTR_COORD(x,y)    if (qobj->TextureFlag) glTexCoord2f(x,y);



struct GLUquadric
{
   GLenum DrawStyle;		/* GLU_FILL, LINE, SILHOUETTE, or POINT */
   GLenum Orientation;		/* GLU_INSIDE or GLU_OUTSIDE */
   GLboolean TextureFlag;	/* Generate texture coords? */
   GLenum Normals;		/* GLU_NONE, GLU_FLAT, or GLU_SMOOTH */
   void (GLCALLBACK * ErrorFunc) (GLenum err);	/* Error handler callback function */
};



/*
 * Process a GLU error.
 */
static void
quadric_error(GLUquadricObj * qobj, GLenum error, const char *msg)
{
   /* Call the error call back function if any */
   if (qobj->ErrorFunc) {
      (*qobj->ErrorFunc) (error);
   }
   /* Print a message to stdout if MESA_DEBUG variable is defined */
   if (getenv("MESA_DEBUG")) {
      fprintf(stderr, "GLUError: %s: %s\n", (char *) gluErrorString(error),
	      msg);
   }
}




GLUquadricObj *GLAPIENTRY
gluNewQuadric(void)
{
   GLUquadricObj *q;

   q = (GLUquadricObj *) malloc(sizeof(struct GLUquadric));
   if (q) {
      q->DrawStyle = GLU_FILL;
      q->Orientation = GLU_OUTSIDE;
      q->TextureFlag = GL_FALSE;
      q->Normals = GLU_SMOOTH;
      q->ErrorFunc = NULL;
   }
   return q;
}



void GLAPIENTRY
gluDeleteQuadric(GLUquadricObj * state)
{
   if (state) {
      free((void *) state);
   }
}



/*
 * Set the drawing style to be GLU_FILL, GLU_LINE, GLU_SILHOUETTE,
 * or GLU_POINT.
 */
void GLAPIENTRY
gluQuadricDrawStyle(GLUquadricObj * quadObject, GLenum drawStyle)
{
   if (quadObject && (drawStyle == GLU_FILL || drawStyle == GLU_LINE
		      || drawStyle == GLU_SILHOUETTE
		      || drawStyle == GLU_POINT)) {
      quadObject->DrawStyle = drawStyle;
   }
   else {
      quadric_error(quadObject, GLU_INVALID_ENUM, "qluQuadricDrawStyle");
   }
}



/*
 * Set the orientation to GLU_INSIDE or GLU_OUTSIDE.
 */
void GLAPIENTRY
gluQuadricOrientation(GLUquadricObj * quadObject, GLenum orientation)
{
   if (quadObject
       && (orientation == GLU_INSIDE || orientation == GLU_OUTSIDE)) {
      quadObject->Orientation = orientation;
   }
   else {
      quadric_error(quadObject, GLU_INVALID_ENUM, "qluQuadricOrientation");
   }
}



/*
 * Set the error handler callback function.
 */
void GLAPIENTRY
gluQuadricCallback(GLUquadricObj * qobj,
		   GLenum which, void (GLCALLBACK * fn) ())
{
   /*
    * UGH, this is a mess!  I thought ANSI was a standard.
    */
   if (qobj && which == GLU_ERROR) {
#ifdef __CYGWIN32__
      qobj->ErrorFunc = (void (GLCALLBACKPCAST) (GLenum)) fn;
#elif defined(OPENSTEP)
      qobj->ErrorFunc = (void (*)(GLenum)) fn;
#elif defined(_WIN32)
      qobj->ErrorFunc = (void (GLCALLBACK *) (int)) fn;
#elif defined(__STORM__)
      qobj->ErrorFunc = (void (GLCALLBACK *) (GLenum)) fn;
#elif defined(__BEOS__)
      qobj->ErrorFunc = (void (*)(GLenum)) fn;
#else
      qobj->ErrorFunc = (void (GLCALLBACK *) ()) fn;
#endif
   }
}


void GLAPIENTRY
gluQuadricNormals(GLUquadricObj * quadObject, GLenum normals)
{
   if (quadObject
       && (normals == GLU_NONE || normals == GLU_FLAT
	   || normals == GLU_SMOOTH)) {
      quadObject->Normals = normals;
   }
}


void GLAPIENTRY
gluQuadricTexture(GLUquadricObj * quadObject, GLboolean textureCoords)
{
   if (quadObject) {
      quadObject->TextureFlag = textureCoords;
   }
}




/*
 * Call glNormal3f after scaling normal to unit length.
 */
static void
normal3f(GLfloat x, GLfloat y, GLfloat z)
{
   GLdouble mag;

   mag = sqrt(x * x + y * y + z * z);
   if (mag > 0.00001F) {
      x /= mag;
      y /= mag;
      z /= mag;
   }
   glNormal3f(x, y, z);
}



void GLAPIENTRY
gluCylinder(GLUquadricObj * qobj,
	    GLdouble baseRadius, GLdouble topRadius,
	    GLdouble height, GLint slices, GLint stacks)
{
   GLdouble da, r, dr, dz;
   GLfloat x, y, z, nz, nsign;
   GLint i, j;

   if (qobj->Orientation == GLU_INSIDE) {
      nsign = -1.0;
   }
   else {
      nsign = 1.0;
   }

   da = 2.0 * M_PI / slices;
   dr = (topRadius - baseRadius) / stacks;
   dz = height / stacks;
   nz = (baseRadius - topRadius) / height;	/* Z component of normal vectors */

   if (qobj->DrawStyle == GLU_POINT) {
      glBegin(GL_POINTS);
      for (i = 0; i < slices; i++) {
	 x = cos(i * da);
	 y = sin(i * da);
	 normal3f(x * nsign, y * nsign, nz * nsign);

	 z = 0.0;
	 r = baseRadius;
	 for (j = 0; j <= stacks; j++) {
	    glVertex3f(x * r, y * r, z);
	    z += dz;
	    r += dr;
	 }
      }
      glEnd();
   }
   else if (qobj->DrawStyle == GLU_LINE || qobj->DrawStyle == GLU_SILHOUETTE) {
      /* Draw rings */
      if (qobj->DrawStyle == GLU_LINE) {
	 z = 0.0;
	 r = baseRadius;
	 for (j = 0; j <= stacks; j++) {
	    glBegin(GL_LINE_LOOP);
	    for (i = 0; i < slices; i++) {
	       x = cos(i * da);
	       y = sin(i * da);
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       glVertex3f(x * r, y * r, z);
	    }
	    glEnd();
	    z += dz;
	    r += dr;
	 }
      }
      else {
	 /* draw one ring at each end */
	 if (baseRadius != 0.0) {
	    glBegin(GL_LINE_LOOP);
	    for (i = 0; i < slices; i++) {
	       x = cos(i * da);
	       y = sin(i * da);
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       glVertex3f(x * baseRadius, y * baseRadius, 0.0);
	    }
	    glEnd();
	    glBegin(GL_LINE_LOOP);
	    for (i = 0; i < slices; i++) {
	       x = cos(i * da);
	       y = sin(i * da);
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       glVertex3f(x * topRadius, y * topRadius, height);
	    }
	    glEnd();
	 }
      }
      /* draw length lines */
      glBegin(GL_LINES);
      for (i = 0; i < slices; i++) {
	 x = cos(i * da);
	 y = sin(i * da);
	 normal3f(x * nsign, y * nsign, nz * nsign);
	 glVertex3f(x * baseRadius, y * baseRadius, 0.0);
	 glVertex3f(x * topRadius, y * topRadius, height);
      }
      glEnd();
   }
   else if (qobj->DrawStyle == GLU_FILL) {
      GLfloat ds = 1.0 / slices;
      GLfloat dt = 1.0 / stacks;
      GLfloat t = 0.0;
      z = 0.0;
      r = baseRadius;
      for (j = 0; j < stacks; j++) {
	 GLfloat s = 0.0;
	 glBegin(GL_QUAD_STRIP);
	 for (i = 0; i <= slices; i++) {
	    GLfloat x, y;
	    if (i == slices) {
	       x = sin(0.0);
	       y = cos(0.0);
	    }
	    else {
	       x = sin(i * da);
	       y = cos(i * da);
	    }
	    if (nsign == 1.0) {
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       TXTR_COORD(s, t);
	       glVertex3f(x * r, y * r, z);
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       TXTR_COORD(s, t + dt);
	       glVertex3f(x * (r + dr), y * (r + dr), z + dz);
	    }
	    else {
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       TXTR_COORD(s, t);
	       glVertex3f(x * r, y * r, z);
	       normal3f(x * nsign, y * nsign, nz * nsign);
	       TXTR_COORD(s, t + dt);
	       glVertex3f(x * (r + dr), y * (r + dr), z + dz);
	    }
	    s += ds;
	 }			/* for slices */
	 glEnd();
	 r += dr;
	 t += dt;
	 z += dz;
      }				/* for stacks */
   }
}





void GLAPIENTRY
gluSphere(GLUquadricObj * qobj, GLdouble radius, GLint slices, GLint stacks)
{
   GLfloat rho, drho, theta, dtheta;
   GLfloat x, y, z;
   GLfloat s, t, ds, dt;
   GLint i, j, imin, imax;
   GLboolean normals;
   GLfloat nsign;

   if (qobj->Normals == GLU_NONE) {
      normals = GL_FALSE;
   }
   else {
      normals = GL_TRUE;
   }
   if (qobj->Orientation == GLU_INSIDE) {
      nsign = -1.0;
   }
   else {
      nsign = 1.0;
   }

   drho = M_PI / (GLfloat) stacks;
   dtheta = 2.0 * M_PI / (GLfloat) slices;

   /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y axis */
   /* t goes from -1.0/+1.0 at z = -radius/+radius (linear along longitudes) */
   /* cannot use triangle fan on texturing (s coord. at top/bottom tip varies) */

   if (qobj->DrawStyle == GLU_FILL) {
      if (!qobj->TextureFlag) {
	 /* draw +Z end as a triangle fan */
	 glBegin(GL_TRIANGLE_FAN);
	 glNormal3f(0.0, 0.0, 1.0);
	 TXTR_COORD(0.5, 1.0);
	 glVertex3f(0.0, 0.0, nsign * radius);
	 for (j = 0; j <= slices; j++) {
	    theta = (j == slices) ? 0.0 : j * dtheta;
	    x = -sin(theta) * sin(drho);
	    y = cos(theta) * sin(drho);
	    z = nsign * cos(drho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
	 glEnd();
      }

      ds = 1.0 / slices;
      dt = 1.0 / stacks;
      t = 1.0;			/* because loop now runs from 0 */
      if (qobj->TextureFlag) {
	 imin = 0;
	 imax = stacks;
      }
      else {
	 imin = 1;
	 imax = stacks - 1;
      }

      /* draw intermediate stacks as quad strips */
      for (i = imin; i < imax; i++) {
	 rho = i * drho;
	 glBegin(GL_QUAD_STRIP);
	 s = 0.0;
	 for (j = 0; j <= slices; j++) {
	    theta = (j == slices) ? 0.0 : j * dtheta;
	    x = -sin(theta) * sin(rho);
	    y = cos(theta) * sin(rho);
	    z = nsign * cos(rho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    TXTR_COORD(s, t);
	    glVertex3f(x * radius, y * radius, z * radius);
	    x = -sin(theta) * sin(rho + drho);
	    y = cos(theta) * sin(rho + drho);
	    z = nsign * cos(rho + drho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    TXTR_COORD(s, t - dt);
	    s += ds;
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
	 glEnd();
	 t -= dt;
      }

      if (!qobj->TextureFlag) {
	 /* draw -Z end as a triangle fan */
	 glBegin(GL_TRIANGLE_FAN);
	 glNormal3f(0.0, 0.0, -1.0);
	 TXTR_COORD(0.5, 0.0);
	 glVertex3f(0.0, 0.0, -radius * nsign);
	 rho = M_PI - drho;
	 s = 1.0;
	 t = dt;
	 for (j = slices; j >= 0; j--) {
	    theta = (j == slices) ? 0.0 : j * dtheta;
	    x = -sin(theta) * sin(rho);
	    y = cos(theta) * sin(rho);
	    z = nsign * cos(rho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    TXTR_COORD(s, t);
	    s -= ds;
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
	 glEnd();
      }
   }
   else if (qobj->DrawStyle == GLU_LINE || qobj->DrawStyle == GLU_SILHOUETTE) {
      /* draw stack lines */
      for (i = 1; i < stacks; i++) {	/* stack line at i==stacks-1 was missing here */
	 rho = i * drho;
	 glBegin(GL_LINE_LOOP);
	 for (j = 0; j < slices; j++) {
	    theta = j * dtheta;
	    x = cos(theta) * sin(rho);
	    y = sin(theta) * sin(rho);
	    z = cos(rho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
	 glEnd();
      }
      /* draw slice lines */
      for (j = 0; j < slices; j++) {
	 theta = j * dtheta;
	 glBegin(GL_LINE_STRIP);
	 for (i = 0; i <= stacks; i++) {
	    rho = i * drho;
	    x = cos(theta) * sin(rho);
	    y = sin(theta) * sin(rho);
	    z = cos(rho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
	 glEnd();
      }
   }
   else if (qobj->DrawStyle == GLU_POINT) {
      /* top and bottom-most points */
      glBegin(GL_POINTS);
      if (normals)
	 glNormal3f(0.0, 0.0, nsign);
      glVertex3d(0.0, 0.0, radius);
      if (normals)
	 glNormal3f(0.0, 0.0, -nsign);
      glVertex3d(0.0, 0.0, -radius);

      /* loop over stacks */
      for (i = 1; i < stacks - 1; i++) {
	 rho = i * drho;
	 for (j = 0; j < slices; j++) {
	    theta = j * dtheta;
	    x = cos(theta) * sin(rho);
	    y = sin(theta) * sin(rho);
	    z = cos(rho);
	    if (normals)
	       glNormal3f(x * nsign, y * nsign, z * nsign);
	    glVertex3f(x * radius, y * radius, z * radius);
	 }
      }
      glEnd();
   }

}



void GLAPIENTRY
gluDisk(GLUquadricObj * qobj,
	GLdouble innerRadius, GLdouble outerRadius, GLint slices, GLint loops)
{
   GLfloat da, dr;
#if 0
   GLdouble a, da;
   GLfloat r, dr;
   GLfloat x, y;
   GLfloat r1, r2, dtc;
   GLint s, l;
#endif

   /* Normal vectors */
   if (qobj->Normals != GLU_NONE) {
      if (qobj->Orientation == GLU_OUTSIDE) {
	 glNormal3f(0.0, 0.0, +1.0);
      }
      else {
	 glNormal3f(0.0, 0.0, -1.0);
      }
   }

   da = 2.0 * M_PI / slices;
   dr = (outerRadius - innerRadius) / (GLfloat) loops;

   switch (qobj->DrawStyle) {
   case GLU_FILL:
      {
	 /* texture of a gluDisk is a cut out of the texture unit square
	  * x, y in [-outerRadius, +outerRadius]; s, t in [0, 1]
	  * (linear mapping)
	  */
	 GLfloat dtc = 2.0f * outerRadius;
	 GLfloat sa, ca;
	 GLfloat r1 = innerRadius;
	 GLint l;
	 for (l = 0; l < loops; l++) {
	    GLfloat r2 = r1 + dr;
	    if (qobj->Orientation == GLU_OUTSIDE) {
	       GLint s;
	       glBegin(GL_QUAD_STRIP);
	       for (s = 0; s <= slices; s++) {
		  GLfloat a;
		  if (s == slices)
		     a = 0.0;
		  else
		     a = s * da;
		  sa = sin(a);
		  ca = cos(a);
		  TXTR_COORD(0.5 + sa * r2 / dtc, 0.5 + ca * r2 / dtc);
		  glVertex2f(r2 * sa, r2 * ca);
		  TXTR_COORD(0.5 + sa * r1 / dtc, 0.5 + ca * r1 / dtc);
		  glVertex2f(r1 * sa, r1 * ca);
	       }
	       glEnd();
	    }
	    else {
	       GLint s;
	       glBegin(GL_QUAD_STRIP);
	       for (s = slices; s >= 0; s--) {
		  GLfloat a;
		  if (s == slices)
		     a = 0.0;
		  else
		     a = s * da;
		  sa = sin(a);
		  ca = cos(a);
		  TXTR_COORD(0.5 - sa * r2 / dtc, 0.5 + ca * r2 / dtc);
		  glVertex2f(r2 * sa, r2 * ca);
		  TXTR_COORD(0.5 - sa * r1 / dtc, 0.5 + ca * r1 / dtc);
		  glVertex2f(r1 * sa, r1 * ca);
	       }
	       glEnd();
	    }
	    r1 = r2;
	 }
	 break;
      }
   case GLU_LINE:
      {
	 GLint l, s;
	 /* draw loops */
	 for (l = 0; l <= loops; l++) {
	    GLfloat r = innerRadius + l * dr;
	    glBegin(GL_LINE_LOOP);
	    for (s = 0; s < slices; s++) {
	       GLfloat a = s * da;
	       glVertex2f(r * sin(a), r * cos(a));
	    }
	    glEnd();
	 }
	 /* draw spokes */
	 for (s = 0; s < slices; s++) {
	    GLfloat a = s * da;
	    GLfloat x = sin(a);
	    GLfloat y = cos(a);
	    glBegin(GL_LINE_STRIP);
	    for (l = 0; l <= loops; l++) {
	       GLfloat r = innerRadius + l * dr;
	       glVertex2f(r * x, r * y);
	    }
	    glEnd();
	 }
	 break;
      }
   case GLU_POINT:
      {
	 GLint s;
	 glBegin(GL_POINTS);
	 for (s = 0; s < slices; s++) {
	    GLfloat a = s * da;
	    GLfloat x = sin(a);
	    GLfloat y = cos(a);
	    GLint l;
	    for (l = 0; l <= loops; l++) {
	       GLfloat r = innerRadius * l * dr;
	       glVertex2f(r * x, r * y);
	    }
	 }
	 glEnd();
	 break;
      }
   case GLU_SILHOUETTE:
      {
	 if (innerRadius != 0.0) {
	    GLfloat a;
	    glBegin(GL_LINE_LOOP);
	    for (a = 0.0; a < 2.0 * M_PI; a += da) {
	       GLfloat x = innerRadius * sin(a);
	       GLfloat y = innerRadius * cos(a);
	       glVertex2f(x, y);
	    }
	    glEnd();
	 }
	 {
	    GLfloat a;
	    glBegin(GL_LINE_LOOP);
	    for (a = 0; a < 2.0 * M_PI; a += da) {
	       GLfloat x = outerRadius * sin(a);
	       GLfloat y = outerRadius * cos(a);
	       glVertex2f(x, y);
	    }
	    glEnd();
	 }
	 break;
      }
   default:
      abort();
   }
}



void GLAPIENTRY
gluPartialDisk(GLUquadricObj * qobj, GLdouble innerRadius,
	       GLdouble outerRadius, GLint slices, GLint loops,
	       GLdouble startAngle, GLdouble sweepAngle)
{
   if (qobj->Normals != GLU_NONE) {
      if (qobj->Orientation == GLU_OUTSIDE) {
	 glNormal3f(0.0, 0.0, +1.0);
      }
      else {
	 glNormal3f(0.0, 0.0, -1.0);
      }
   }

   if (qobj->DrawStyle == GLU_POINT) {
      GLint loop, slice;
      GLdouble radius, delta_radius;
      GLdouble angle, delta_angle;
      delta_radius = (outerRadius - innerRadius) / (loops - 1);
      delta_angle = DEG_TO_RAD((sweepAngle) / (slices - 1));
      glBegin(GL_POINTS);
      radius = innerRadius;
      for (loop = 0; loop < loops; loop++) {
	 angle = DEG_TO_RAD(startAngle);
	 for (slice = 0; slice < slices; slice++) {
	    glVertex2d(radius * sin(angle), radius * cos(angle));
	    angle += delta_angle;
	 }
	 radius += delta_radius;
      }
      glEnd();
   }
   else if (qobj->DrawStyle == GLU_LINE) {
      GLint loop, slice;
      GLdouble radius, delta_radius;
      GLdouble angle, delta_angle;
      delta_radius = (outerRadius - innerRadius) / loops;
      delta_angle = DEG_TO_RAD(sweepAngle / slices);
      /* draw rings */
      radius = innerRadius;
      for (loop = 0; loop < loops; loop++) {
	 angle = DEG_TO_RAD(startAngle);
	 glBegin(GL_LINE_STRIP);
	 for (slice = 0; slice <= slices; slice++) {
	    glVertex2d(radius * sin(angle), radius * cos(angle));
	    angle += delta_angle;
	 }
	 glEnd();
	 radius += delta_radius;
      }
      /* draw spokes */
      angle = DEG_TO_RAD(startAngle);
      for (slice = 0; slice <= slices; slice++) {
	 radius = innerRadius;
	 glBegin(GL_LINE_STRIP);
	 for (loop = 0; loop < loops; loop++) {
	    glVertex2d(radius * sin(angle), radius * cos(angle));
	    radius += delta_radius;
	 }
	 glEnd();
	 angle += delta_angle;
      }
   }
   else if (qobj->DrawStyle == GLU_SILHOUETTE) {
      GLint slice;
      GLdouble angle, delta_angle;
      delta_angle = DEG_TO_RAD(sweepAngle / slices);
      /* draw outer ring */
      glBegin(GL_LINE_STRIP);
      angle = DEG_TO_RAD(startAngle);
      for (slice = 0; slice <= slices; slice++) {
	 glVertex2d(outerRadius * sin(angle), outerRadius * cos(angle));
	 angle += delta_angle;
      }
      glEnd();
      /* draw inner ring */
      if (innerRadius > 0.0) {
	 glBegin(GL_LINE_STRIP);
	 angle = DEG_TO_RAD(startAngle);
	 for (slice = 0; slice < slices; slice++) {
	    glVertex2d(innerRadius * sin(angle), innerRadius * cos(angle));
	    angle += delta_angle;
	 }
	 glEnd();
      }
      /* draw spokes */
      if (sweepAngle < 360.0) {
	 GLdouble stopAngle = startAngle + sweepAngle;
	 glBegin(GL_LINES);
	 glVertex2d(innerRadius * SIND(startAngle),
		    innerRadius * COSD(startAngle));
	 glVertex2d(outerRadius * SIND(startAngle),
		    outerRadius * COSD(startAngle));
	 glVertex2d(innerRadius * SIND(stopAngle),
		    innerRadius * COSD(stopAngle));
	 glVertex2d(outerRadius * SIND(stopAngle),
		    outerRadius * COSD(stopAngle));
	 glEnd();
      }
   }
   else if (qobj->DrawStyle == GLU_FILL) {
      GLint loop, slice;
      GLdouble radius, delta_radius;
      GLdouble angle, delta_angle;
      delta_radius = (outerRadius - innerRadius) / loops;
      delta_angle = DEG_TO_RAD(sweepAngle / slices);
      radius = innerRadius;
      for (loop = 0; loop < loops; loop++) {
	 glBegin(GL_QUAD_STRIP);
	 angle = DEG_TO_RAD(startAngle);
	 for (slice = 0; slice <= slices; slice++) {
	    if (qobj->Orientation == GLU_OUTSIDE) {
	       glVertex2d((radius + delta_radius) * sin(angle),
			  (radius + delta_radius) * cos(angle));
	       glVertex2d(radius * sin(angle), radius * cos(angle));
	    }
	    else {
	       glVertex2d(radius * sin(angle), radius * cos(angle));
	       glVertex2d((radius + delta_radius) * sin(angle),
			  (radius + delta_radius) * cos(angle));
	    }
	    angle += delta_angle;
	 }
	 glEnd();
	 radius += delta_radius;
      }
   }
}