1 /*
2 * freeglut_geometry.c
3 *
4 * Freeglut geometry rendering methods.
5 *
6 * Copyright (c) 1999-2010 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 1999
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include <FL/glut.H>
29 #include <FL/math.h>
30 #include <stdlib.h>
31
32 /*
33 * TODO BEFORE THE STABLE RELEASE:
34 *
35 * Following functions have been contributed by Andreas Umbach.
36 *
37 * glutWireCube() -- looks OK
38 * glutSolidCube() -- OK
39 *
40 * Those functions have been implemented by John Fay.
41 *
42 * glutWireTorus() -- looks OK
43 * glutSolidTorus() -- looks OK
44 * glutWireDodecahedron() -- looks OK
45 * glutSolidDodecahedron() -- looks OK
46 * glutWireOctahedron() -- looks OK
47 * glutSolidOctahedron() -- looks OK
48 * glutWireTetrahedron() -- looks OK
49 * glutSolidTetrahedron() -- looks OK
50 * glutWireIcosahedron() -- looks OK
51 * glutSolidIcosahedron() -- looks OK
52 *
53 * The Following functions have been updated by Nigel Stewart, based
54 * on FreeGLUT 2.0.0 implementations:
55 *
56 * glutWireSphere() -- looks OK
57 * glutSolidSphere() -- looks OK
58 * glutWireCone() -- looks OK
59 * glutSolidCone() -- looks OK
60 */
61
62
63 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
64
65 /*
66 * Draws a wireframed cube. Code contributed by Andreas Umbach <marvin@dataway.ch>
67 */
glutWireCube(GLdouble dSize)68 void glutWireCube( GLdouble dSize )
69 {
70 double size = dSize * 0.5;
71
72 # define V(a,b,c) glVertex3d( a size, b size, c size );
73 # define N(a,b,c) glNormal3d( a, b, c );
74
75 /* PWO: I dared to convert the code to use macros... */
76 glBegin( GL_LINE_LOOP ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); glEnd();
77 glBegin( GL_LINE_LOOP ); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); glEnd();
78 glBegin( GL_LINE_LOOP ); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); glEnd();
79 glBegin( GL_LINE_LOOP ); N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); glEnd();
80 glBegin( GL_LINE_LOOP ); N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); glEnd();
81 glBegin( GL_LINE_LOOP ); N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); glEnd();
82
83 # undef V
84 # undef N
85 }
86
87 /*
88 * Draws a solid cube. Code contributed by Andreas Umbach <marvin@dataway.ch>
89 */
glutSolidCube(GLdouble dSize)90 void glutSolidCube( GLdouble dSize )
91 {
92 double size = dSize * 0.5;
93
94 # define V(a,b,c) glVertex3d( a size, b size, c size );
95 # define N(a,b,c) glNormal3d( a, b, c );
96
97 /* PWO: Again, I dared to convert the code to use macros... */
98 glBegin( GL_QUADS );
99 N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
100 N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
101 N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
102 N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
103 N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
104 N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
105 glEnd();
106
107 # undef V
108 # undef N
109 }
110
111 /*
112 * Compute lookup table of cos and sin values forming a cirle
113 *
114 * Notes:
115 * It is the responsibility of the caller to free these tables
116 * The size of the table is (n+1) to form a connected loop
117 * The last entry is exactly the same as the first
118 * The sign of n can be flipped to get the reverse loop
119 */
120
fghCircleTable(double ** sint,double ** cost,const int n)121 static void fghCircleTable(double **sint,double **cost,const int n)
122 {
123 int i;
124
125 /* Table size, the sign of n flips the circle direction */
126
127 const int size = abs(n);
128
129 /* Determine the angle between samples */
130
131 const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n );
132
133 /* Allocate memory for n samples, plus duplicate of first entry at the end */
134
135 *sint = (double *) calloc(sizeof(double), size+1);
136 *cost = (double *) calloc(sizeof(double), size+1);
137
138 /* Bail out if memory allocation fails, fgError never returns */
139
140 if (!(*sint) || !(*cost))
141 {
142 if (*sint) free(*sint);
143 if (*cost) free(*cost);
144 return;
145 }
146
147 /* Compute cos and sin around the circle */
148
149 (*sint)[0] = 0.0;
150 (*cost)[0] = 1.0;
151
152 for (i=1; i<size; i++)
153 {
154 (*sint)[i] = sin(angle*i);
155 (*cost)[i] = cos(angle*i);
156 }
157
158 /* Last sample is duplicate of the first */
159
160 (*sint)[size] = (*sint)[0];
161 (*cost)[size] = (*cost)[0];
162 }
163
164 /*
165 * Draws a solid sphere
166 */
glutSolidSphere(GLdouble radius,GLint slices,GLint stacks)167 void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
168 {
169 int i,j;
170
171 /* Adjust z and radius as stacks are drawn. */
172
173 double z0,z1;
174 double r0,r1;
175
176 /* Pre-computed circle */
177
178 double *sint1,*cost1;
179 double *sint2,*cost2;
180
181 fghCircleTable(&sint1,&cost1,-slices);
182 fghCircleTable(&sint2,&cost2,stacks*2);
183
184 /* The top stack is covered with a triangle fan */
185
186 z0 = 1.0;
187 z1 = cost2[(stacks>0)?1:0];
188 r0 = 0.0;
189 r1 = sint2[(stacks>0)?1:0];
190
191 glBegin(GL_TRIANGLE_FAN);
192
193 glNormal3d(0,0,1);
194 glVertex3d(0,0,radius);
195
196 for (j=slices; j>=0; j--)
197 {
198 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
199 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
200 }
201
202 glEnd();
203
204 /* Cover each stack with a quad strip, except the top and bottom stacks */
205
206 for( i=1; i<stacks-1; i++ )
207 {
208 z0 = z1; z1 = cost2[i+1];
209 r0 = r1; r1 = sint2[i+1];
210
211 glBegin(GL_QUAD_STRIP);
212
213 for(j=0; j<=slices; j++)
214 {
215 glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
216 glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
217 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
218 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
219 }
220
221 glEnd();
222 }
223
224 /* The bottom stack is covered with a triangle fan */
225
226 z0 = z1;
227 r0 = r1;
228
229 glBegin(GL_TRIANGLE_FAN);
230
231 glNormal3d(0,0,-1);
232 glVertex3d(0,0,-radius);
233
234 for (j=0; j<=slices; j++)
235 {
236 glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
237 glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
238 }
239
240 glEnd();
241
242 /* Release sin and cos tables */
243
244 free(sint1);
245 free(cost1);
246 free(sint2);
247 free(cost2);
248 }
249
250 /*
251 * Draws a wire sphere
252 */
glutWireSphere(GLdouble radius,GLint slices,GLint stacks)253 void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
254 {
255 int i,j;
256
257 /* Adjust z and radius as stacks and slices are drawn. */
258
259 double r;
260 double x,y,z;
261
262 /* Pre-computed circle */
263
264 double *sint1,*cost1;
265 double *sint2,*cost2;
266
267 fghCircleTable(&sint1,&cost1,-slices );
268 fghCircleTable(&sint2,&cost2, stacks*2);
269
270 /* Draw a line loop for each stack */
271
272 for (i=1; i<stacks; i++)
273 {
274 z = cost2[i];
275 r = sint2[i];
276
277 glBegin(GL_LINE_LOOP);
278
279 for(j=0; j<=slices; j++)
280 {
281 x = cost1[j];
282 y = sint1[j];
283
284 glNormal3d(x,y,z);
285 glVertex3d(x*r*radius,y*r*radius,z*radius);
286 }
287
288 glEnd();
289 }
290
291 /* Draw a line loop for each slice */
292
293 for (i=0; i<slices; i++)
294 {
295 glBegin(GL_LINE_STRIP);
296
297 for(j=0; j<=stacks; j++)
298 {
299 x = cost1[i]*sint2[j];
300 y = sint1[i]*sint2[j];
301 z = cost2[j];
302
303 glNormal3d(x,y,z);
304 glVertex3d(x*radius,y*radius,z*radius);
305 }
306
307 glEnd();
308 }
309
310 /* Release sin and cos tables */
311
312 free(sint1);
313 free(cost1);
314 free(sint2);
315 free(cost2);
316 }
317
318 /*
319 * Draws a solid cone
320 */
glutSolidCone(GLdouble base,GLdouble height,GLint slices,GLint stacks)321 void glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks )
322 {
323 int i,j;
324
325 /* Step in z and radius as stacks are drawn. */
326
327 double z0,z1;
328 double r0,r1;
329
330 const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
331 const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
332
333 /* Scaling factors for vertex normals */
334
335 const double cosn = ( height / sqrt ( height * height + base * base ));
336 const double sinn = ( base / sqrt ( height * height + base * base ));
337
338 /* Pre-computed circle */
339
340 double *sint,*cost;
341
342 fghCircleTable(&sint,&cost,-slices);
343
344 /* Cover the circular base with a triangle fan... */
345
346 z0 = 0.0;
347 z1 = zStep;
348
349 r0 = base;
350 r1 = r0 - rStep;
351
352 glBegin(GL_TRIANGLE_FAN);
353
354 glNormal3d(0.0,0.0,-1.0);
355 glVertex3d(0.0,0.0, z0 );
356
357 for (j=0; j<=slices; j++)
358 glVertex3d(cost[j]*r0, sint[j]*r0, z0);
359
360 glEnd();
361
362 /* Cover each stack with a quad strip, except the top stack */
363
364 for( i=0; i<stacks-1; i++ )
365 {
366 glBegin(GL_QUAD_STRIP);
367
368 for(j=0; j<=slices; j++)
369 {
370 glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn);
371 glVertex3d(cost[j]*r0, sint[j]*r0, z0 );
372 glVertex3d(cost[j]*r1, sint[j]*r1, z1 );
373 }
374
375 z0 = z1; z1 += zStep;
376 r0 = r1; r1 -= rStep;
377
378 glEnd();
379 }
380
381 /* The top stack is covered with individual triangles */
382
383 glBegin(GL_TRIANGLES);
384
385 glNormal3d(cost[0]*sinn, sint[0]*sinn, cosn);
386
387 for (j=0; j<slices; j++)
388 {
389 glVertex3d(cost[j+0]*r0, sint[j+0]*r0, z0 );
390 glVertex3d(0, 0, height);
391 glNormal3d(cost[j+1]*sinn, sint[j+1]*sinn, cosn );
392 glVertex3d(cost[j+1]*r0, sint[j+1]*r0, z0 );
393 }
394
395 glEnd();
396
397 /* Release sin and cos tables */
398
399 free(sint);
400 free(cost);
401 }
402
403 /*
404 * Draws a wire cone
405 */
glutWireCone(GLdouble base,GLdouble height,GLint slices,GLint stacks)406 void glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks)
407 {
408 int i,j;
409
410 /* Step in z and radius as stacks are drawn. */
411
412 double z = 0.0;
413 double r = base;
414
415 const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
416 const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
417
418 /* Scaling factors for vertex normals */
419
420 const double cosn = ( height / sqrt ( height * height + base * base ));
421 const double sinn = ( base / sqrt ( height * height + base * base ));
422
423 /* Pre-computed circle */
424
425 double *sint,*cost;
426
427 fghCircleTable(&sint,&cost,-slices);
428
429 /* Draw the stacks... */
430
431 for (i=0; i<stacks; i++)
432 {
433 glBegin(GL_LINE_LOOP);
434
435 for( j=0; j<slices; j++ )
436 {
437 glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn);
438 glVertex3d(cost[j]*r, sint[j]*r, z );
439 }
440
441 glEnd();
442
443 z += zStep;
444 r -= rStep;
445 }
446
447 /* Draw the slices */
448
449 r = base;
450
451 glBegin(GL_LINES);
452
453 for (j=0; j<slices; j++)
454 {
455 glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn );
456 glVertex3d(cost[j]*r, sint[j]*r, 0.0 );
457 glVertex3d(0.0, 0.0, height);
458 }
459
460 glEnd();
461
462 /* Release sin and cos tables */
463
464 free(sint);
465 free(cost);
466 }
467
468
469 /*
470 * Draws a solid cylinder
471 */
glutSolidCylinder(GLdouble radius,GLdouble height,GLint slices,GLint stacks)472 void glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
473 {
474 int i,j;
475
476 /* Step in z and radius as stacks are drawn. */
477
478 double z0,z1;
479 const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
480
481 /* Pre-computed circle */
482
483 double *sint,*cost;
484
485 fghCircleTable(&sint,&cost,-slices);
486
487 /* Cover the base and top */
488
489 glBegin(GL_TRIANGLE_FAN);
490 glNormal3d(0.0, 0.0, -1.0 );
491 glVertex3d(0.0, 0.0, 0.0 );
492 for (j=0; j<=slices; j++)
493 glVertex3d(cost[j]*radius, sint[j]*radius, 0.0);
494 glEnd();
495
496 glBegin(GL_TRIANGLE_FAN);
497 glNormal3d(0.0, 0.0, 1.0 );
498 glVertex3d(0.0, 0.0, height);
499 for (j=slices; j>=0; j--)
500 glVertex3d(cost[j]*radius, sint[j]*radius, height);
501 glEnd();
502
503 /* Do the stacks */
504
505 z0 = 0.0;
506 z1 = zStep;
507
508 for (i=1; i<=stacks; i++)
509 {
510 if (i==stacks)
511 z1 = height;
512
513 glBegin(GL_QUAD_STRIP);
514 for (j=0; j<=slices; j++ )
515 {
516 glNormal3d(cost[j], sint[j], 0.0 );
517 glVertex3d(cost[j]*radius, sint[j]*radius, z0 );
518 glVertex3d(cost[j]*radius, sint[j]*radius, z1 );
519 }
520 glEnd();
521
522 z0 = z1; z1 += zStep;
523 }
524
525 /* Release sin and cos tables */
526
527 free(sint);
528 free(cost);
529 }
530
531 /*
532 * Draws a wire cylinder
533 */
glutWireCylinder(GLdouble radius,GLdouble height,GLint slices,GLint stacks)534 void glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
535 {
536 int i,j;
537
538 /* Step in z and radius as stacks are drawn. */
539
540 double z = 0.0;
541 const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
542
543 /* Pre-computed circle */
544
545 double *sint,*cost;
546
547 fghCircleTable(&sint,&cost,-slices);
548
549 /* Draw the stacks... */
550
551 for (i=0; i<=stacks; i++)
552 {
553 if (i==stacks)
554 z = height;
555
556 glBegin(GL_LINE_LOOP);
557
558 for( j=0; j<slices; j++ )
559 {
560 glNormal3d(cost[j], sint[j], 0.0);
561 glVertex3d(cost[j]*radius, sint[j]*radius, z );
562 }
563
564 glEnd();
565
566 z += zStep;
567 }
568
569 /* Draw the slices */
570
571 glBegin(GL_LINES);
572
573 for (j=0; j<slices; j++)
574 {
575 glNormal3d(cost[j], sint[j], 0.0 );
576 glVertex3d(cost[j]*radius, sint[j]*radius, 0.0 );
577 glVertex3d(cost[j]*radius, sint[j]*radius, height);
578 }
579
580 glEnd();
581
582 /* Release sin and cos tables */
583
584 free(sint);
585 free(cost);
586 }
587
588 /*
589 * Draws a wire torus
590 */
glutWireTorus(GLdouble dInnerRadius,GLdouble dOuterRadius,GLint nSides,GLint nRings)591 void glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
592 {
593 double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
594 double *vertex, *normal;
595 int i, j;
596 double spsi, cpsi, sphi, cphi ;
597
598 if ( nSides < 1 ) nSides = 1;
599 if ( nRings < 1 ) nRings = 1;
600
601 /* Allocate the vertices array */
602 vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings );
603 normal = (double *)calloc( sizeof(double), 3 * nSides * nRings );
604
605 glPushMatrix();
606
607 dpsi = 2.0 * M_PI / (double)nRings ;
608 dphi = -2.0 * M_PI / (double)nSides ;
609 psi = 0.0;
610
611 for( j=0; j<nRings; j++ )
612 {
613 cpsi = cos ( psi ) ;
614 spsi = sin ( psi ) ;
615 phi = 0.0;
616
617 for( i=0; i<nSides; i++ )
618 {
619 int offset = 3 * ( j * nSides + i ) ;
620 cphi = cos ( phi ) ;
621 sphi = sin ( phi ) ;
622 *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
623 *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
624 *(vertex + offset + 2) = sphi * iradius ;
625 *(normal + offset + 0) = cpsi * cphi ;
626 *(normal + offset + 1) = spsi * cphi ;
627 *(normal + offset + 2) = sphi ;
628 phi += dphi;
629 }
630
631 psi += dpsi;
632 }
633
634 for( i=0; i<nSides; i++ )
635 {
636 glBegin( GL_LINE_LOOP );
637
638 for( j=0; j<nRings; j++ )
639 {
640 int offset = 3 * ( j * nSides + i ) ;
641 glNormal3dv( normal + offset );
642 glVertex3dv( vertex + offset );
643 }
644
645 glEnd();
646 }
647
648 for( j=0; j<nRings; j++ )
649 {
650 glBegin(GL_LINE_LOOP);
651
652 for( i=0; i<nSides; i++ )
653 {
654 int offset = 3 * ( j * nSides + i ) ;
655 glNormal3dv( normal + offset );
656 glVertex3dv( vertex + offset );
657 }
658
659 glEnd();
660 }
661
662 free ( vertex ) ;
663 free ( normal ) ;
664 glPopMatrix();
665 }
666
667 /*
668 * Draws a solid torus
669 */
glutSolidTorus(GLdouble dInnerRadius,GLdouble dOuterRadius,GLint nSides,GLint nRings)670 void glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
671 {
672 double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
673 double *vertex, *normal;
674 int i, j;
675 double spsi, cpsi, sphi, cphi ;
676
677 if ( nSides < 1 ) nSides = 1;
678 if ( nRings < 1 ) nRings = 1;
679
680 /* Increment the number of sides and rings to allow for one more point than surface */
681 nSides ++ ;
682 nRings ++ ;
683
684 /* Allocate the vertices array */
685 vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings );
686 normal = (double *)calloc( sizeof(double), 3 * nSides * nRings );
687
688 glPushMatrix();
689
690 dpsi = 2.0 * M_PI / (double)(nRings - 1) ;
691 dphi = -2.0 * M_PI / (double)(nSides - 1) ;
692 psi = 0.0;
693
694 for( j=0; j<nRings; j++ )
695 {
696 cpsi = cos ( psi ) ;
697 spsi = sin ( psi ) ;
698 phi = 0.0;
699
700 for( i=0; i<nSides; i++ )
701 {
702 int offset = 3 * ( j * nSides + i ) ;
703 cphi = cos ( phi ) ;
704 sphi = sin ( phi ) ;
705 *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
706 *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
707 *(vertex + offset + 2) = sphi * iradius ;
708 *(normal + offset + 0) = cpsi * cphi ;
709 *(normal + offset + 1) = spsi * cphi ;
710 *(normal + offset + 2) = sphi ;
711 phi += dphi;
712 }
713
714 psi += dpsi;
715 }
716
717 glBegin( GL_QUADS );
718 for( i=0; i<nSides-1; i++ )
719 {
720 for( j=0; j<nRings-1; j++ )
721 {
722 int offset = 3 * ( j * nSides + i ) ;
723 glNormal3dv( normal + offset );
724 glVertex3dv( vertex + offset );
725 glNormal3dv( normal + offset + 3 );
726 glVertex3dv( vertex + offset + 3 );
727 glNormal3dv( normal + offset + 3 * nSides + 3 );
728 glVertex3dv( vertex + offset + 3 * nSides + 3 );
729 glNormal3dv( normal + offset + 3 * nSides );
730 glVertex3dv( vertex + offset + 3 * nSides );
731 }
732 }
733
734 glEnd();
735
736 free ( vertex ) ;
737 free ( normal ) ;
738 glPopMatrix();
739 }
740
741 /*
742 *
743 */
glutWireDodecahedron(void)744 void glutWireDodecahedron( void )
745 {
746 /* Magic Numbers: It is possible to create a dodecahedron by attaching two pentagons to each face of
747 * of a cube. The coordinates of the points are:
748 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
749 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
750 * x = 0.61803398875 and z = 1.61803398875.
751 */
752 glBegin ( GL_LINE_LOOP ) ;
753 glNormal3d ( 0.0, 0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
754 glEnd () ;
755 glBegin ( GL_LINE_LOOP ) ;
756 glNormal3d ( 0.0, 0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
757 glEnd () ;
758 glBegin ( GL_LINE_LOOP ) ;
759 glNormal3d ( 0.0, -0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
760 glEnd () ;
761 glBegin ( GL_LINE_LOOP ) ;
762 glNormal3d ( 0.0, -0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
763 glEnd () ;
764
765 glBegin ( GL_LINE_LOOP ) ;
766 glNormal3d ( 0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
767 glEnd () ;
768 glBegin ( GL_LINE_LOOP ) ;
769 glNormal3d ( -0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
770 glEnd () ;
771 glBegin ( GL_LINE_LOOP ) ;
772 glNormal3d ( 0.850650808354, 0.0, -0.525731112119 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
773 glEnd () ;
774 glBegin ( GL_LINE_LOOP ) ;
775 glNormal3d ( -0.850650808354, 0.0, -0.525731112119 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
776 glEnd () ;
777
778 glBegin ( GL_LINE_LOOP ) ;
779 glNormal3d ( 0.525731112119, 0.850650808354, 0.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
780 glEnd () ;
781 glBegin ( GL_LINE_LOOP ) ;
782 glNormal3d ( 0.525731112119, -0.850650808354, 0.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
783 glEnd () ;
784 glBegin ( GL_LINE_LOOP ) ;
785 glNormal3d ( -0.525731112119, 0.850650808354, 0.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
786 glEnd () ;
787 glBegin ( GL_LINE_LOOP ) ;
788 glNormal3d ( -0.525731112119, -0.850650808354, 0.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
789 glEnd () ;
790 }
791
792 /*
793 *
794 */
glutSolidDodecahedron(void)795 void glutSolidDodecahedron( void )
796 {
797 /* Magic Numbers: It is possible to create a dodecahedron by attaching two pentagons to each face of
798 * of a cube. The coordinates of the points are:
799 * (+-x,0, z); (+-1, 1, 1); (0, z, x )
800 * where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
801 * x = 0.61803398875 and z = 1.61803398875.
802 */
803 glBegin ( GL_POLYGON ) ;
804 glNormal3d ( 0.0, 0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
805 glEnd () ;
806 glBegin ( GL_POLYGON ) ;
807 glNormal3d ( 0.0, 0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
808 glEnd () ;
809 glBegin ( GL_POLYGON ) ;
810 glNormal3d ( 0.0, -0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
811 glEnd () ;
812 glBegin ( GL_POLYGON ) ;
813 glNormal3d ( 0.0, -0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
814 glEnd () ;
815
816 glBegin ( GL_POLYGON ) ;
817 glNormal3d ( 0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
818 glEnd () ;
819 glBegin ( GL_POLYGON ) ;
820 glNormal3d ( -0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
821 glEnd () ;
822 glBegin ( GL_POLYGON ) ;
823 glNormal3d ( 0.850650808354, 0.0, -0.525731112119 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
824 glEnd () ;
825 glBegin ( GL_POLYGON ) ;
826 glNormal3d ( -0.850650808354, 0.0, -0.525731112119 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
827 glEnd () ;
828
829 glBegin ( GL_POLYGON ) ;
830 glNormal3d ( 0.525731112119, 0.850650808354, 0.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ;
831 glEnd () ;
832 glBegin ( GL_POLYGON ) ;
833 glNormal3d ( 0.525731112119, -0.850650808354, 0.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ;
834 glEnd () ;
835 glBegin ( GL_POLYGON ) ;
836 glNormal3d ( -0.525731112119, 0.850650808354, 0.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ;
837 glEnd () ;
838 glBegin ( GL_POLYGON ) ;
839 glNormal3d ( -0.525731112119, -0.850650808354, 0.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ;
840 glEnd () ;
841 }
842
843 /*
844 *
845 */
glutWireOctahedron(void)846 void glutWireOctahedron( void )
847 {
848 #define RADIUS 1.0f
849 glBegin( GL_LINE_LOOP );
850 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, RADIUS, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS );
851 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS, 0.0 );
852 glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, 0.0 );
853 glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0,-RADIUS, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS );
854 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, 0.0 );
855 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, RADIUS, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS );
856 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0,-RADIUS, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS );
857 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS, 0.0 );
858 glEnd();
859 #undef RADIUS
860 }
861
862 /*
863 *
864 */
glutSolidOctahedron(void)865 void glutSolidOctahedron( void )
866 {
867 #define RADIUS 1.0f
868 glBegin( GL_TRIANGLES );
869 glNormal3d( 0.577350269189, 0.577350269189, 0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, RADIUS, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS );
870 glNormal3d( 0.577350269189, 0.577350269189,-0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS ); glVertex3d( 0.0, RADIUS, 0.0 );
871 glNormal3d( 0.577350269189,-0.577350269189, 0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS ); glVertex3d( 0.0,-RADIUS, 0.0 );
872 glNormal3d( 0.577350269189,-0.577350269189,-0.577350269189); glVertex3d( RADIUS, 0.0, 0.0 ); glVertex3d( 0.0,-RADIUS, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS );
873 glNormal3d(-0.577350269189, 0.577350269189, 0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS ); glVertex3d( 0.0, RADIUS, 0.0 );
874 glNormal3d(-0.577350269189, 0.577350269189,-0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, RADIUS, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS );
875 glNormal3d(-0.577350269189,-0.577350269189, 0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0,-RADIUS, 0.0 ); glVertex3d( 0.0, 0.0, RADIUS );
876 glNormal3d(-0.577350269189,-0.577350269189,-0.577350269189); glVertex3d(-RADIUS, 0.0, 0.0 ); glVertex3d( 0.0, 0.0,-RADIUS ); glVertex3d( 0.0,-RADIUS, 0.0 );
877 glEnd();
878 #undef RADIUS
879 }
880
881 /* Magic Numbers: r0 = ( 1, 0, 0 )
882 * r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
883 * r2 = ( -1/3, -sqrt(2) / 3, sqrt(6) / 3 )
884 * r3 = ( -1/3, -sqrt(2) / 3, -sqrt(6) / 3 )
885 * |r0| = |r1| = |r2| = |r3| = 1
886 * Distance between any two points is 2 sqrt(6) / 3
887 *
888 * Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
889 */
890
891 #define NUM_TETR_FACES 4
892
893 static GLdouble tet_r[4][3] = { { 1.0, 0.0, 0.0 },
894 { -0.333333333333, 0.942809041582, 0.0 },
895 { -0.333333333333, -0.471404520791, 0.816496580928 },
896 { -0.333333333333, -0.471404520791, -0.816496580928 } } ;
897
898 static GLint tet_i[4][3] = /* Vertex indices */
899 {
900 { 1, 3, 2 }, { 0, 2, 3 }, { 0, 3, 1 }, { 0, 1, 2 }
901 } ;
902
903 /*
904 *
905 */
glutWireTetrahedron(void)906 void glutWireTetrahedron( void )
907 {
908 glBegin( GL_LINE_LOOP ) ;
909 glNormal3d ( -tet_r[0][0], -tet_r[0][1], -tet_r[0][2] ) ; glVertex3dv ( tet_r[1] ) ; glVertex3dv ( tet_r[3] ) ; glVertex3dv ( tet_r[2] ) ;
910 glNormal3d ( -tet_r[1][0], -tet_r[1][1], -tet_r[1][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[2] ) ; glVertex3dv ( tet_r[3] ) ;
911 glNormal3d ( -tet_r[2][0], -tet_r[2][1], -tet_r[2][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[3] ) ; glVertex3dv ( tet_r[1] ) ;
912 glNormal3d ( -tet_r[3][0], -tet_r[3][1], -tet_r[3][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[1] ) ; glVertex3dv ( tet_r[2] ) ;
913 glEnd() ;
914 }
915
916 /*
917 *
918 */
glutSolidTetrahedron(void)919 void glutSolidTetrahedron( void )
920 {
921 glBegin( GL_TRIANGLES ) ;
922 glNormal3d ( -tet_r[0][0], -tet_r[0][1], -tet_r[0][2] ) ; glVertex3dv ( tet_r[1] ) ; glVertex3dv ( tet_r[3] ) ; glVertex3dv ( tet_r[2] ) ;
923 glNormal3d ( -tet_r[1][0], -tet_r[1][1], -tet_r[1][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[2] ) ; glVertex3dv ( tet_r[3] ) ;
924 glNormal3d ( -tet_r[2][0], -tet_r[2][1], -tet_r[2][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[3] ) ; glVertex3dv ( tet_r[1] ) ;
925 glNormal3d ( -tet_r[3][0], -tet_r[3][1], -tet_r[3][2] ) ; glVertex3dv ( tet_r[0] ) ; glVertex3dv ( tet_r[1] ) ; glVertex3dv ( tet_r[2] ) ;
926 glEnd() ;
927 }
928
929 /*
930 *
931 */
932 double icos_r[12][3] = { { 1.0, 0.0, 0.0 },
933 { 0.447213595500, 0.894427191000, 0.0 }, { 0.447213595500, 0.276393202252, 0.850650808354 }, { 0.447213595500, -0.723606797748, 0.525731112119 }, { 0.447213595500, -0.723606797748, -0.525731112119 }, { 0.447213595500, 0.276393202252, -0.850650808354 },
934 { -0.447213595500, -0.894427191000, 0.0 }, { -0.447213595500, -0.276393202252, 0.850650808354 }, { -0.447213595500, 0.723606797748, 0.525731112119 }, { -0.447213595500, 0.723606797748, -0.525731112119 }, { -0.447213595500, -0.276393202252, -0.850650808354 },
935 { -1.0, 0.0, 0.0 } } ;
936 int icos_v [20][3] = { { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 5 }, { 0, 5, 1 },
937 { 1, 8, 2 }, { 2, 7, 3 }, { 3, 6, 4 }, { 4, 10, 5 }, { 5, 9, 1 },
938 { 1, 9, 8 }, { 2, 8, 7 }, { 3, 7, 6 }, { 4, 6, 10 }, { 5, 10, 9 },
939 { 11, 9, 10 }, { 11, 8, 9 }, { 11, 7, 8 }, { 11, 6, 7 }, { 11, 10, 6 } } ;
940
glutWireIcosahedron(void)941 void glutWireIcosahedron( void )
942 {
943 int i ;
944
945 for ( i = 0; i < 20; i++ )
946 {
947 double normal[3] ;
948 normal[0] = ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) - ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) ;
949 normal[1] = ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) - ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) ;
950 normal[2] = ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) - ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) ;
951 glBegin ( GL_LINE_LOOP ) ;
952 glNormal3dv ( normal ) ;
953 glVertex3dv ( icos_r[icos_v[i][0]] ) ;
954 glVertex3dv ( icos_r[icos_v[i][1]] ) ;
955 glVertex3dv ( icos_r[icos_v[i][2]] ) ;
956 glEnd () ;
957 }
958 }
959
960 /*
961 *
962 */
glutSolidIcosahedron(void)963 void glutSolidIcosahedron( void )
964 {
965 int i ;
966
967 glBegin ( GL_TRIANGLES ) ;
968 for ( i = 0; i < 20; i++ )
969 {
970 double normal[3] ;
971 normal[0] = ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) - ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) ;
972 normal[1] = ( icos_r[icos_v[i][1]][2] - icos_r[icos_v[i][0]][2] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) - ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][2] - icos_r[icos_v[i][0]][2] ) ;
973 normal[2] = ( icos_r[icos_v[i][1]][0] - icos_r[icos_v[i][0]][0] ) * ( icos_r[icos_v[i][2]][1] - icos_r[icos_v[i][0]][1] ) - ( icos_r[icos_v[i][1]][1] - icos_r[icos_v[i][0]][1] ) * ( icos_r[icos_v[i][2]][0] - icos_r[icos_v[i][0]][0] ) ;
974 glNormal3dv ( normal ) ;
975 glVertex3dv ( icos_r[icos_v[i][0]] ) ;
976 glVertex3dv ( icos_r[icos_v[i][1]] ) ;
977 glVertex3dv ( icos_r[icos_v[i][2]] ) ;
978 }
979
980 glEnd () ;
981 }
982
983 /*
984 *
985 */
986 double rdod_r[14][3] = { { 0.0, 0.0, 1.0 },
987 { 0.707106781187, 0.000000000000, 0.5 }, { 0.000000000000, 0.707106781187, 0.5 }, { -0.707106781187, 0.000000000000, 0.5 }, { 0.000000000000, -0.707106781187, 0.5 },
988 { 0.707106781187, 0.707106781187, 0.0 }, { -0.707106781187, 0.707106781187, 0.0 }, { -0.707106781187, -0.707106781187, 0.0 }, { 0.707106781187, -0.707106781187, 0.0 },
989 { 0.707106781187, 0.000000000000, -0.5 }, { 0.000000000000, 0.707106781187, -0.5 }, { -0.707106781187, 0.000000000000, -0.5 }, { 0.000000000000, -0.707106781187, -0.5 },
990 { 0.0, 0.0, -1.0 } } ;
991 int rdod_v [12][4] = { { 0, 1, 5, 2 }, { 0, 2, 6, 3 }, { 0, 3, 7, 4 }, { 0, 4, 8, 1 },
992 { 5, 10, 6, 2 }, { 6, 11, 7, 3 }, { 7, 12, 8, 4 }, { 8, 9, 5, 1 },
993 { 5, 9, 13, 10 }, { 6, 10, 13, 11 }, { 7, 11, 13, 12 }, { 8, 12, 13, 9 } } ;
994 double rdod_n[12][3] = {
995 { 0.353553390594, 0.353553390594, 0.5 }, { -0.353553390594, 0.353553390594, 0.5 }, { -0.353553390594, -0.353553390594, 0.5 }, { 0.353553390594, -0.353553390594, 0.5 },
996 { 0.000000000000, 1.000000000000, 0.0 }, { -1.000000000000, 0.000000000000, 0.0 }, { 0.000000000000, -1.000000000000, 0.0 }, { 1.000000000000, 0.000000000000, 0.0 },
997 { 0.353553390594, 0.353553390594, -0.5 }, { -0.353553390594, 0.353553390594, -0.5 }, { -0.353553390594, -0.353553390594, -0.5 }, { 0.353553390594, -0.353553390594, -0.5 }
998 } ;
999
glutWireRhombicDodecahedron(void)1000 void glutWireRhombicDodecahedron( void )
1001 {
1002 int i ;
1003
1004 for ( i = 0; i < 12; i++ )
1005 {
1006 glBegin ( GL_LINE_LOOP ) ;
1007 glNormal3dv ( rdod_n[i] ) ;
1008 glVertex3dv ( rdod_r[rdod_v[i][0]] ) ;
1009 glVertex3dv ( rdod_r[rdod_v[i][1]] ) ;
1010 glVertex3dv ( rdod_r[rdod_v[i][2]] ) ;
1011 glVertex3dv ( rdod_r[rdod_v[i][3]] ) ;
1012 glEnd () ;
1013 }
1014 }
1015
1016 /*
1017 *
1018 */
glutSolidRhombicDodecahedron(void)1019 void glutSolidRhombicDodecahedron( void )
1020 {
1021 int i ;
1022
1023 glBegin ( GL_QUADS ) ;
1024 for ( i = 0; i < 12; i++ )
1025 {
1026 glNormal3dv ( rdod_n[i] ) ;
1027 glVertex3dv ( rdod_r[rdod_v[i][0]] ) ;
1028 glVertex3dv ( rdod_r[rdod_v[i][1]] ) ;
1029 glVertex3dv ( rdod_r[rdod_v[i][2]] ) ;
1030 glVertex3dv ( rdod_r[rdod_v[i][3]] ) ;
1031 }
1032
1033 glEnd () ;
1034 }
1035
glutWireSierpinskiSponge(int num_levels,GLdouble offset[3],GLdouble scale)1036 void glutWireSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
1037 {
1038 int i, j ;
1039
1040 if ( num_levels == 0 )
1041 {
1042
1043 for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
1044 {
1045 glBegin ( GL_LINE_LOOP ) ;
1046 glNormal3d ( -tet_r[i][0], -tet_r[i][1], -tet_r[i][2] ) ;
1047 for ( j = 0; j < 3; j++ )
1048 {
1049 double x = offset[0] + scale * tet_r[tet_i[i][j]][0] ;
1050 double y = offset[1] + scale * tet_r[tet_i[i][j]][1] ;
1051 double z = offset[2] + scale * tet_r[tet_i[i][j]][2] ;
1052 glVertex3d ( x, y, z ) ;
1053 }
1054
1055 glEnd () ;
1056 }
1057 }
1058 else
1059 {
1060 GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
1061 num_levels -- ;
1062 scale /= 2.0 ;
1063 for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
1064 {
1065 local_offset[0] = offset[0] + scale * tet_r[i][0] ;
1066 local_offset[1] = offset[1] + scale * tet_r[i][1] ;
1067 local_offset[2] = offset[2] + scale * tet_r[i][2] ;
1068 glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
1069 }
1070 }
1071 }
1072
glutSolidSierpinskiSponge(int num_levels,GLdouble offset[3],GLdouble scale)1073 void glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
1074 {
1075 int i, j ;
1076
1077 if ( num_levels == 0 )
1078 {
1079 glBegin ( GL_TRIANGLES ) ;
1080
1081 for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
1082 {
1083 glNormal3d ( -tet_r[i][0], -tet_r[i][1], -tet_r[i][2] ) ;
1084 for ( j = 0; j < 3; j++ )
1085 {
1086 double x = offset[0] + scale * tet_r[tet_i[i][j]][0] ;
1087 double y = offset[1] + scale * tet_r[tet_i[i][j]][1] ;
1088 double z = offset[2] + scale * tet_r[tet_i[i][j]][2] ;
1089 glVertex3d ( x, y, z ) ;
1090 }
1091 }
1092
1093 glEnd () ;
1094 }
1095 else
1096 {
1097 GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
1098 num_levels -- ;
1099 scale /= 2.0 ;
1100 for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
1101 {
1102 local_offset[0] = offset[0] + scale * tet_r[i][0] ;
1103 local_offset[1] = offset[1] + scale * tet_r[i][1] ;
1104 local_offset[2] = offset[2] + scale * tet_r[i][2] ;
1105 glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
1106 }
1107 }
1108 }
1109
1110 /*** END OF FILE ***/
1111