aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/com/sun/opengl/impl/glu/GLUquadricImpl.java
blob: b348601c8ec9223e0b7afd5ba2a0e06cfcd2a0ef (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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
** 
** http://oss.sgi.com/projects/FreeB
** 
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
** 
** NOTE:  The Original Code (as defined below) has been licensed to Sun
** Microsystems, Inc. ("Sun") under the SGI Free Software License B
** (Version 1.1), shown above ("SGI License").   Pursuant to Section
** 3.2(3) of the SGI License, Sun is distributing the Covered Code to
** you under an alternative license ("Alternative License").  This
** Alternative License includes all of the provisions of the SGI License
** except that Section 2.2 and 11 are omitted.  Any differences between
** the Alternative License and the SGI License are offered solely by Sun
** and not by SGI.
**
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
** 
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
** $Date$ $Revision$
** $Header$
*/

/* 
 * Copyright (c) 2002-2004 LWJGL Project
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * * Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'LWJGL' nor the names of 
 *   its contributors may be used to endorse or promote products derived 
 *   from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 */

package com.sun.opengl.impl.glu;

import javax.media.opengl.util.ImmModeSink;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.nio.*;

/**
 * GLUquadricImpl.java
 * 
 * 
 * Created 22-dec-2003 (originally Quadric.java)
 * @author Erik Duijs
 * @author Kenneth Russell, Sven Gothel
 */

public class GLUquadricImpl implements GLUquadric {
  private int drawStyle;
  private int orientation;
  private boolean textureFlag;
  private int normals;
  private boolean immModeSinkEnabled;
  private boolean immModeSinkImmediate;

  public static final boolean USE_NORM_TXT = true;

  private ImmModeSink immModeSink;

  public GLUquadricImpl() {
    drawStyle = GLU.GLU_FILL;
    orientation = GLU.GLU_OUTSIDE;
    textureFlag = false;
    normals = GLU.GLU_SMOOTH;
    if(USE_NORM_TXT) {
        immModeSink = new ImmModeSink(GL.GL_FLOAT, GL.GL_STATIC_DRAW, 3, 3, 0, 3, 32);
    } else {
        immModeSink = new ImmModeSink(GL.GL_FLOAT, GL.GL_STATIC_DRAW, 3, 0, 0, 0, 32);
    }
    immModeSinkImmediate=true;
    immModeSinkEnabled=GLProfile.isGLES();
  }

  public void enableImmModeSink(boolean val) {
    if(!GLProfile.isGLES()) {
        immModeSinkEnabled=val;
    } else {
        immModeSinkEnabled=true;
    }
  }

  public boolean isImmModeSinkEnabled() {
    return immModeSinkEnabled;
  }

  public void setImmMode(boolean val) {
    immModeSinkImmediate=val;
  }

  public boolean getImmMode() {
    return immModeSinkImmediate;
  }

  public ImmModeSink replaceImmModeSink() {
    ImmModeSink res = immModeSink;
    if(USE_NORM_TXT) {
        immModeSink = new ImmModeSink(GL.GL_FLOAT, GL.GL_STATIC_DRAW, 3, 3, 0, 3, 32);
    } else {
        immModeSink = new ImmModeSink(GL.GL_FLOAT, GL.GL_STATIC_DRAW, 3, 0, 0, 0, 32);
    }
    return res;
  }

  public void resetImmModeSink(GL2ES1 gl) {
    immModeSink.reset(gl);
  }

  /**
   * specifies the draw style for quadrics.  
   *
   * The legal values are as follows:
   *
   * GLU.FILL:       Quadrics are rendered with polygon primitives. The polygons
   *                 are drawn in a counterclockwise fashion with respect to
   *                 their normals (as defined with glu.quadricOrientation).
   *
   * GLU.LINE:       Quadrics are rendered as a set of lines.
   *
   * GLU.SILHOUETTE: Quadrics are rendered as a set of lines, except that edges
   * 		   separating coplanar faces will not be drawn.
   *
   * GLU.POINT:       Quadrics are rendered as a set of points.
   * 
   * @param drawStyle The drawStyle to set
   */
  public void setDrawStyle(int drawStyle) {
    this.drawStyle = drawStyle;
  }

  /**
   * specifies what kind	of normals are desired for quadrics.
   * The legal values	are as follows:
   *
   * GLU.NONE:     No normals are generated.
   *
   * GLU.FLAT:     One normal is generated for every facet of a quadric.
   *
   * GLU.SMOOTH:   One normal is generated for every vertex of a quadric.  This
   *               is the default.
   * 
   * @param normals The normals to set
   */
  public void setNormals(int normals) {
    this.normals = normals;
  }

  /**
   * specifies what kind of orientation is desired for.
   * The orientation	values are as follows:
   *
   * GLU.OUTSIDE:  Quadrics are drawn with normals pointing outward.
   *
   * GLU.INSIDE:   Normals point inward. The default is GLU.OUTSIDE.
   *
   * Note that the interpretation of outward and inward depends on the quadric
   * being drawn.
   * 
   * @param orientation The orientation to set
   */
  public void setOrientation(int orientation) {
    this.orientation = orientation;
  }

  /**
   * specifies if texture coordinates should be generated for
   * quadrics rendered with qobj. If the value of textureCoords is true,
   * then texture coordinates are generated, and if textureCoords is false,
   * they are not.. The default is false.
   *
   * The manner in which texture coordinates are generated depends upon the
   * specific quadric rendered.
   * 
   * @param textureFlag The textureFlag to set
   */
  public void setTextureFlag(boolean textureFlag) {
    this.textureFlag = textureFlag;
  }

  /**
   * Returns the drawStyle.
   * @return int
   */
  public int getDrawStyle() {
    return drawStyle;
  }

  /**
   * Returns the normals.
   * @return int
   */
  public int getNormals() {
    return normals;
  }

  /**
   * Returns the orientation.
   * @return int
   */
  public int getOrientation() {
    return orientation;
  }

  /**
   * Returns the textureFlag.
   * @return boolean
   */
  public boolean getTextureFlag() {
    return textureFlag;
  }


  /**
   * draws a cylinder oriented along the z axis. The base of the
   * cylinder is placed at z = 0, and the top at z=height. Like a sphere, a
   * cylinder is subdivided around the z axis into slices, and along the z axis
   * into stacks.
   *
   * Note that if topRadius is set to zero, then this routine will generate a
   * cone.
   *
   * If the orientation is set to GLU.OUTSIDE (with glu.quadricOrientation), then
   * any generated normals point away from the z axis. Otherwise, they point
   * toward the z axis.
   *
   * If texturing is turned on (with glu.quadricTexture), then texture
   * coordinates are generated so that t ranges linearly from 0.0 at z = 0 to
   * 1.0 at z = height, and s ranges from 0.0 at the +y axis, to 0.25 at the +x
   * axis, to 0.5 at the -y axis, to 0.75 at the -x axis, and back to 1.0 at the
   * +y axis.
   *
   * @param baseRadius  Specifies the radius of the cylinder at z = 0.
   * @param topRadius   Specifies the radius of the cylinder at z = height.
   * @param height      Specifies the height of the cylinder.
   * @param slices      Specifies the number of subdivisions around the z axis.
   * @param stacks      Specifies the number of subdivisions along the z axis.
   */
  public void drawCylinder(GL2ES1 gl, float baseRadius, float topRadius, float height, int slices, int stacks) {

    float da, r, dr, dz;
    float x, y, z, nz, nsign;
    int i, j;

    if (orientation == GLU.GLU_INSIDE) {
      nsign = -1.0f;
    } else {
      nsign = 1.0f;
    }

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

    if (drawStyle == GLU.GLU_POINT) {
      glBegin(gl, GL2ES1.GL_POINTS);
      for (i = 0; i < slices; i++) {
        x = cos((i * da));
        y = sin((i * da));
        if(USE_NORM_TXT) {
            normal3f(gl, x * nsign, y * nsign, nz * nsign);
        }

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

  /**
   * renders a disk on the z = 0  plane.  The disk has a radius of
   * outerRadius, and contains a concentric circular hole with a radius of
   * innerRadius. If innerRadius is 0, then no hole is generated. The disk is
   * subdivided around the z axis into slices (like pizza slices), and also
   * about the z axis into rings (as specified by slices and loops,
   * respectively).
   *
   * With respect to orientation, the +z side of the disk is considered to be
   * "outside" (see glu.quadricOrientation).  This means that if the orientation
   * is set to GLU.OUTSIDE, then any normals generated point along the +z axis.
   * Otherwise, they point along the -z axis.
   *
   * If texturing is turned on (with glu.quadricTexture), texture coordinates are
   * generated linearly such that where r=outerRadius, the value at (r, 0, 0) is
   * (1, 0.5), at (0, r, 0) it is (0.5, 1), at (-r, 0, 0) it is (0, 0.5), and at
   * (0, -r, 0) it is (0.5, 0).
   */
  public void drawDisk(GL2ES1 gl, float innerRadius, float outerRadius, int slices, int loops)
  {
    float da, dr;

    /* Normal vectors */
    if (normals != GLU.GLU_NONE) {
      if (orientation == GLU.GLU_OUTSIDE) {
        glNormal3f(gl, 0.0f, 0.0f, +1.0f);
      }
      else {
        glNormal3f(gl, 0.0f, 0.0f, -1.0f);
      }
    }
	
    da = 2.0f * PI / slices;
    dr = (outerRadius - innerRadius) /  loops;
	
    switch (drawStyle) {
    case GLU.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)
         */
        float dtc = 2.0f * outerRadius;
        float sa, ca;
        float r1 = innerRadius;
        int l;
        for (l = 0; l < loops; l++) {
          float r2 = r1 + dr;
          if (orientation == GLU.GLU_OUTSIDE) {
            int s;
            glBegin(gl, immModeSink.GL_QUAD_STRIP);
            for (s = 0; s <= slices; s++) {
              float a;
              if (s == slices)
                a = 0.0f;
              else
                a = s * da;
              sa = sin(a);
              ca = cos(a);
              TXTR_COORD(gl, 0.5f + sa * r2 / dtc, 0.5f + ca * r2 / dtc);
              glVertex2f(gl, r2 * sa, r2 * ca);
              TXTR_COORD(gl, 0.5f + sa * r1 / dtc, 0.5f + ca * r1 / dtc);
              glVertex2f(gl, r1 * sa, r1 * ca);
            }
            glEnd(gl);
          }
          else {
            int s;
            glBegin(gl, immModeSink.GL_QUAD_STRIP);
            for (s = slices; s >= 0; s--) {
              float a;
              if (s == slices)
                a = 0.0f;
              else
                a = s * da;
              sa = sin(a);
              ca = cos(a);
              TXTR_COORD(gl, 0.5f - sa * r2 / dtc, 0.5f + ca * r2 / dtc);
              glVertex2f(gl, r2 * sa, r2 * ca);
              TXTR_COORD(gl, 0.5f - sa * r1 / dtc, 0.5f + ca * r1 / dtc);
              glVertex2f(gl, r1 * sa, r1 * ca);
            }
            glEnd(gl);
          }
          r1 = r2;
        }
        break;
      }
    case GLU.GLU_LINE:
      {
        int l, s;
        /* draw loops */
        for (l = 0; l <= loops; l++) {
          float r = innerRadius + l * dr;
          glBegin(gl, GL2ES1.GL_LINE_LOOP);
          for (s = 0; s < slices; s++) {
            float a = s * da;
            glVertex2f(gl, r * sin(a), r * cos(a));
          }
          glEnd(gl);
        }
        /* draw spokes */
        for (s = 0; s < slices; s++) {
          float a = s * da;
          float x = sin(a);
          float y = cos(a);
          glBegin(gl, GL2ES1.GL_LINE_STRIP);
          for (l = 0; l <= loops; l++) {
            float r = innerRadius + l * dr;
            glVertex2f(gl, r * x, r * y);
          }
          glEnd(gl);
        }
        break;
      }
    case GLU.GLU_POINT:
      {
        int s;
        glBegin(gl, GL2ES1.GL_POINTS);
        for (s = 0; s < slices; s++) {
          float a = s * da;
          float x = sin(a);
          float y = cos(a);
          int l;
          for (l = 0; l <= loops; l++) {
            float r = innerRadius * l * dr;
            glVertex2f(gl, r * x, r * y);
          }
        }
        glEnd(gl);
        break;
      }
    case GLU.GLU_SILHOUETTE:
      {
        if (innerRadius != 0.0) {
          float a;
          glBegin(gl, GL2ES1.GL_LINE_LOOP);
          for (a = 0.0f; a < 2.0 * PI; a += da) {
            float x = innerRadius * sin(a);
            float y = innerRadius * cos(a);
            glVertex2f(gl, x, y);
          }
          glEnd(gl);
        }
        {
          float a;
          glBegin(gl, GL2ES1.GL_LINE_LOOP);
          for (a = 0; a < 2.0f * PI; a += da) {
            float x = outerRadius * sin(a);
            float y = outerRadius * cos(a);
            glVertex2f(gl, x, y);
          }
          glEnd(gl);
        }
        break;
      }
    default:
      return;
    }
  }

  /**
   * renders a partial disk on the z=0 plane. A partial disk is similar to a
   * full disk, except that only the subset of the disk from startAngle
   * through startAngle + sweepAngle is included (where 0 degrees is along
   * the +y axis, 90 degrees along the +x axis, 180 along the -y axis, and
   * 270 along the -x axis).
   * 
   * The partial disk has a radius of outerRadius, and contains a concentric
   * circular hole with a radius of innerRadius. If innerRadius is zero, then
   * no hole is generated. The partial disk is subdivided around the z axis
   * into slices (like pizza slices), and also about the z axis into rings
   * (as specified by slices and loops, respectively).
   * 
   * With respect to orientation, the +z side of the partial disk is
   * considered to be outside (see gluQuadricOrientation). This means that if
   * the orientation is set to GLU.GLU_OUTSIDE, then any normals generated point
   * along the +z axis. Otherwise, they point along the -z axis.
   * 
   * If texturing is turned on (with gluQuadricTexture), texture coordinates
   * are generated linearly such that where r=outerRadius, the value at (r, 0, 0)
   * is (1, 0.5), at (0, r, 0) it is (0.5, 1), at (-r, 0, 0) it is (0, 0.5),
   * and at (0, -r, 0) it is (0.5, 0).
   */
  public void drawPartialDisk(GL2ES1 gl,
                              float innerRadius,
                              float outerRadius,
                              int slices,
                              int loops,
                              float startAngle,
                              float sweepAngle) {
    int i, j, max;
    float[] sinCache = new float[CACHE_SIZE];
    float[] cosCache = new float[CACHE_SIZE];
    float angle;
    float x, y;
    float sintemp, costemp;
    float deltaRadius;
    float radiusLow, radiusHigh;
    float texLow = 0, texHigh = 0;
    float angleOffset;
    int slices2;
    int finish;

    if (slices >= CACHE_SIZE)
      slices = CACHE_SIZE - 1;
    if (slices < 2
        || loops < 1
        || outerRadius <= 0.0f
        || innerRadius < 0.0f
        || innerRadius > outerRadius) {
      //gluQuadricError(qobj, GLU.GLU_INVALID_VALUE);
      System.err.println("PartialDisk: GLU_INVALID_VALUE");
      return;
    }

    if (sweepAngle < -360.0f)
      sweepAngle = 360.0f;
    if (sweepAngle > 360.0f)
      sweepAngle = 360.0f;
    if (sweepAngle < 0) {
      startAngle += sweepAngle;
      sweepAngle = -sweepAngle;
    }

    if (sweepAngle == 360.0f) {
      slices2 = slices;
    } else {
      slices2 = slices + 1;
    }

    /* Compute length (needed for normal calculations) */
    deltaRadius = outerRadius - innerRadius;

    /* Cache is the vertex locations cache */

    angleOffset = startAngle / 180.0f * PI;
    for (i = 0; i <= slices; i++) {
      angle = angleOffset + ((PI * sweepAngle) / 180.0f) * i / slices;
      sinCache[i] = sin(angle);
      cosCache[i] = cos(angle);
    }

    if (sweepAngle == 360.0f) {
      sinCache[slices] = sinCache[0];
      cosCache[slices] = cosCache[0];
    }

    switch (normals) {
    case GLU.GLU_FLAT :
    case GLU.GLU_SMOOTH :
      if (orientation == GLU.GLU_OUTSIDE) {
        glNormal3f(gl, 0.0f, 0.0f, 1.0f);
      } else {
        glNormal3f(gl, 0.0f, 0.0f, -1.0f);
      }
      break;
    default :
    case GLU.GLU_NONE :
      break;
    }

    switch (drawStyle) {
    case GLU.GLU_FILL :
      if (innerRadius == .0f) {
        finish = loops - 1;
        /* Triangle strip for inner polygons */
        glBegin(gl, GL2ES1.GL_TRIANGLE_FAN);
        if (textureFlag) {
          glTexCoord2f(gl, 0.5f, 0.5f);
        }
        glVertex3f(gl, 0.0f, 0.0f, 0.0f);
        radiusLow = outerRadius - deltaRadius * ((float) (loops - 1) / loops);
        if (textureFlag) {
          texLow = radiusLow / outerRadius / 2;
        }

        if (orientation == GLU.GLU_OUTSIDE) {
          for (i = slices; i >= 0; i--) {
            if (textureFlag) {
              glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                              texLow * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);
          }
        } else {
          for (i = 0; i <= slices; i++) {
            if (textureFlag) {
              glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                              texLow * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);
          }
        }
        glEnd(gl);
      } else {
        finish = loops;
      }
      for (j = 0; j < finish; j++) {
        radiusLow = outerRadius - deltaRadius * ((float) j / loops);
        radiusHigh = outerRadius - deltaRadius * ((float) (j + 1) / loops);
        if (textureFlag) {
          texLow = radiusLow / outerRadius / 2;
          texHigh = radiusHigh / outerRadius / 2;
        }

        glBegin(gl, immModeSink.GL_QUAD_STRIP);
        for (i = 0; i <= slices; i++) {
          if (orientation == GLU.GLU_OUTSIDE) {
            if (textureFlag) {
              glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                              texLow * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);

            if (textureFlag) {
              glTexCoord2f(gl, texHigh * sinCache[i] + 0.5f,
                              texHigh * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusHigh * sinCache[i],
                          radiusHigh * cosCache[i],
                          0.0f);
          } else {
            if (textureFlag) {
              glTexCoord2f(gl, texHigh * sinCache[i] + 0.5f,
                              texHigh * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusHigh * sinCache[i],
                          radiusHigh * cosCache[i],
                          0.0f);

            if (textureFlag) {
              glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                              texLow * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);
          }
        }
        glEnd(gl);
      }
      break;
    case GLU.GLU_POINT :
      glBegin(gl, GL2ES1.GL_POINTS);
      for (i = 0; i < slices2; i++) {
        sintemp = sinCache[i];
        costemp = cosCache[i];
        for (j = 0; j <= loops; j++) {
          radiusLow = outerRadius - deltaRadius * ((float) j / loops);

          if (textureFlag) {
            texLow = radiusLow / outerRadius / 2;

            glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                            texLow * cosCache[i] + 0.5f);
          }
          glVertex3f(gl, radiusLow * sintemp, radiusLow * costemp, 0.0f);
        }
      }
      glEnd(gl);
      break;
    case GLU.GLU_LINE :
      if (innerRadius == outerRadius) {
        glBegin(gl, GL2ES1.GL_LINE_STRIP);

        for (i = 0; i <= slices; i++) {
          if (textureFlag) {
            glTexCoord2f(gl, sinCache[i] / 2 + 0.5f, cosCache[i] / 2 + 0.5f);
          }
          glVertex3f(gl, innerRadius * sinCache[i], innerRadius * cosCache[i], 0.0f);
        }
        glEnd(gl);
        break;
      }
      for (j = 0; j <= loops; j++) {
        radiusLow = outerRadius - deltaRadius * ((float) j / loops);
        if (textureFlag) {
          texLow = radiusLow / outerRadius / 2;
        }

        glBegin(gl, GL2ES1.GL_LINE_STRIP);
        for (i = 0; i <= slices; i++) {
          if (textureFlag) {
            glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                            texLow * cosCache[i] + 0.5f);
          }
          glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);
        }
        glEnd(gl);
      }
      for (i = 0; i < slices2; i++) {
        sintemp = sinCache[i];
        costemp = cosCache[i];
        glBegin(gl, GL2ES1.GL_LINE_STRIP);
        for (j = 0; j <= loops; j++) {
          radiusLow = outerRadius - deltaRadius * ((float) j / loops);
          if (textureFlag) {
            texLow = radiusLow / outerRadius / 2;
          }

          if (textureFlag) {
            glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                            texLow * cosCache[i] + 0.5f);
          }
          glVertex3f(gl, radiusLow * sintemp, radiusLow * costemp, 0.0f);
        }
        glEnd(gl);
      }
      break;
    case GLU.GLU_SILHOUETTE :
      if (sweepAngle < 360.0f) {
        for (i = 0; i <= slices; i += slices) {
          sintemp = sinCache[i];
          costemp = cosCache[i];
          glBegin(gl, GL2ES1.GL_LINE_STRIP);
          for (j = 0; j <= loops; j++) {
            radiusLow = outerRadius - deltaRadius * ((float) j / loops);

            if (textureFlag) {
              texLow = radiusLow / outerRadius / 2;
              glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                              texLow * cosCache[i] + 0.5f);
            }
            glVertex3f(gl, radiusLow * sintemp, radiusLow * costemp, 0.0f);
          }
          glEnd(gl);
        }
      }
      for (j = 0; j <= loops; j += loops) {
        radiusLow = outerRadius - deltaRadius * ((float) j / loops);
        if (textureFlag) {
          texLow = radiusLow / outerRadius / 2;
        }

        glBegin(gl, GL2ES1.GL_LINE_STRIP);
        for (i = 0; i <= slices; i++) {
          if (textureFlag) {
            glTexCoord2f(gl, texLow * sinCache[i] + 0.5f,
                            texLow * cosCache[i] + 0.5f);
          }
          glVertex3f(gl, radiusLow * sinCache[i], radiusLow * cosCache[i], 0.0f);
        }
        glEnd(gl);
        if (innerRadius == outerRadius)
          break;
      }
      break;
    default :
      break;
    }
  }

  /**
   * draws a sphere of the given	radius centered	around the origin.
   * The sphere is subdivided around the z axis into slices and along the z axis
   * into stacks (similar to lines of longitude and latitude).
   *
   * If the orientation is set to GLU.OUTSIDE (with glu.quadricOrientation), then
   * any normals generated point away from the center of the sphere. Otherwise,
   * they point toward the center of the sphere.

   * If texturing is turned on (with glu.quadricTexture), then texture
   * coordinates are generated so that t ranges from 0.0 at z=-radius to 1.0 at
   * z=radius (t increases linearly along longitudinal lines), and s ranges from
   * 0.0 at the +y axis, to 0.25 at the +x axis, to 0.5 at the -y axis, to 0.75
   * at the -x axis, and back to 1.0 at the +y axis.
   */
  public void drawSphere(GL2ES1 gl, float radius, int slices, int stacks) {
    // TODO

    float rho, drho, theta, dtheta;
    float x, y, z;
    float s, t, ds, dt;
    int i, j, imin, imax;
    boolean normals;
    float nsign;

    normals = (this.normals != GLU.GLU_NONE);

    if (orientation == GLU.GLU_INSIDE) {
      nsign = -1.0f;
    } else {
      nsign = 1.0f;
    }

    drho = PI / stacks;
    dtheta = 2.0f * PI / slices;

    if (drawStyle == GLU.GLU_FILL) {
      if (!textureFlag) {
        // draw +Z end as a triangle fan
        glBegin(gl, GL2ES1.GL_TRIANGLE_FAN);
        if(USE_NORM_TXT) {
            glNormal3f(gl, 0.0f, 0.0f, 1.0f);
        }
        glVertex3f(gl, 0.0f, 0.0f, nsign * radius);
        for (j = 0; j <= slices; j++) {
          theta = (j == slices) ? 0.0f : j * dtheta;
          x = -sin(theta) * sin(drho);
          y = cos(theta) * sin(drho);
          z = nsign * cos(drho);
          if(USE_NORM_TXT) {
              if (normals) {
                glNormal3f(gl, x * nsign, y * nsign, z * nsign);
              }
          }
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
        glEnd(gl);
      }

      ds = 1.0f / slices;
      dt = 1.0f / stacks;
      t = 1.0f; // because loop now runs from 0
      if (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, immModeSink.GL_QUAD_STRIP);
        s = 0.0f;
        for (j = 0; j <= slices; j++) {
          theta = (j == slices) ? 0.0f : j * dtheta;
          x = -sin(theta) * sin(rho);
          y = cos(theta) * sin(rho);
          z = nsign * cos(rho);
          if(USE_NORM_TXT) {
              if (normals) {
                glNormal3f(gl, x * nsign, y * nsign, z * nsign);
              }
              TXTR_COORD(gl, s, t);
          }
          glVertex3f(gl, x * radius, y * radius, z * radius);
          x = -sin(theta) * sin(rho + drho);
          y = cos(theta) * sin(rho + drho);
          z = nsign * cos(rho + drho);
          if(USE_NORM_TXT) {
              if (normals) {
                glNormal3f(gl, x * nsign, y * nsign, z * nsign);
              }
              TXTR_COORD(gl, s, t - dt);
          }
          s += ds;
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
        glEnd(gl);
        t -= dt;
      }

      if (!textureFlag) {
        // draw -Z end as a triangle fan
        glBegin(gl, GL2ES1.GL_TRIANGLE_FAN);
        if(USE_NORM_TXT) {
            glNormal3f(gl, 0.0f, 0.0f, -1.0f);
        }
        glVertex3f(gl, 0.0f, 0.0f, -radius * nsign);
        rho = PI - drho;
        s = 1.0f;
        for (j = slices; j >= 0; j--) {
          theta = (j == slices) ? 0.0f : j * dtheta;
          x = -sin(theta) * sin(rho);
          y = cos(theta) * sin(rho);
          z = nsign * cos(rho);
          if(USE_NORM_TXT) {
              if (normals)
                glNormal3f(gl, x * nsign, y * nsign, z * nsign);
          }
          s -= ds;
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
        glEnd(gl);
      }
    } else if (
               drawStyle == GLU.GLU_LINE
               || drawStyle == GLU.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, GL2ES1.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(gl, x * nsign, y * nsign, z * nsign);
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
        glEnd(gl);
      }
      // draw slice lines
      for (j = 0; j < slices; j++) {
        theta = j * dtheta;
        glBegin(gl, GL2ES1.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(gl, x * nsign, y * nsign, z * nsign);
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
        glEnd(gl);
      }
    } else if (drawStyle == GLU.GLU_POINT) {
      // top and bottom-most points
      glBegin(gl, GL2ES1.GL_POINTS);
      if (normals)
        glNormal3f(gl, 0.0f, 0.0f, nsign);
      glVertex3f(gl, 0.0f, 0.0f, radius);
      if (normals)
        glNormal3f(gl, 0.0f, 0.0f, -nsign);
      glVertex3f(gl, 0.0f, 0.0f, -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(gl, x * nsign, y * nsign, z * nsign);
          glVertex3f(gl, x * radius, y * radius, z * radius);
        }
      }
      glEnd(gl);
    }
  }


  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private static final float PI = (float)Math.PI;
  private static final int CACHE_SIZE = 240;

  private final void glBegin(GL2ES1 gl, int mode) {
      if(immModeSinkEnabled) {
          immModeSink.glBegin(mode);
      } else {
          ((GL2)gl).glBegin(mode);
      }
  }

  private final void glEnd(GL2ES1 gl) {
      if(immModeSinkEnabled) {
          immModeSink.glEnd(gl, immModeSinkImmediate);
      } else {
          ((GL2)gl).glEnd();
      }
  }

  private final void glVertex2f(GL2ES1 gl, float x, float y) {
      if(immModeSinkEnabled) {
          immModeSink.glVertex2f(x, y);
      } else {
          ((GL2)gl).glVertex2f(x, y);
      }
  }

  private final void glVertex3f(GL2ES1 gl, float x, float y, float z) {
      if(immModeSinkEnabled) {
          immModeSink.glVertex3f(x, y, z);
      } else {
          ((GL2)gl).glVertex3f(x, y, z);
      }
  }

  private final void glNormal3f(GL2ES1 gl, float x, float y, float z) {
      if(immModeSinkEnabled) {
          immModeSink.glNormal3f(x, y, z);
      } else {
          ((GL2)gl).glNormal3f(x, y, z);
      }
  }

  private final void glTexCoord2f(GL2ES1 gl, float x, float y) {
      if(immModeSinkEnabled) {
          immModeSink.glTexCoord2f(x, y);
      } else {
          ((GL2)gl).glTexCoord2f(x, y);
      }
  }

  /**
   * Call glNormal3f after scaling normal to unit length.
   *
   * @param x
   * @param y
   * @param z
   */
  private void normal3f(GL2ES1 gl, float x, float y, float z) {
    float mag;
	
    mag = (float)Math.sqrt(x * x + y * y + z * z);
    if (mag > 0.00001F) {
      x /= mag;
      y /= mag;
      z /= mag;
    }
    immModeSink.glNormal3f(x, y, z);
  }

  private final void TXTR_COORD(GL2ES1 gl, float x, float y) {
    if (textureFlag) glTexCoord2f(gl, x,y);
  }

  private float sin(float r) {
    return (float)Math.sin(r);
  }

  private float cos(float r) {
    return (float)Math.cos(r);
  }
}