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
|
/*
* freeglut_geometry.c
*
* Freeglut geometry rendering methods.
*
* Copyright (c) 1999-2010 Pawel W. Olszta. All Rights Reserved.
* Written by Pawel W. Olszta, <olszta@sourceforge.net>
* Creation date: Fri Dec 3 1999
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <FL/glut.H>
#include <FL/math.h>
#include <stdlib.h>
/*
* TODO BEFORE THE STABLE RELEASE:
*
* Following functions have been contributed by Andreas Umbach.
*
* glutWireCube() -- looks OK
* glutSolidCube() -- OK
*
* Those functions have been implemented by John Fay.
*
* glutWireTorus() -- looks OK
* glutSolidTorus() -- looks OK
* glutWireDodecahedron() -- looks OK
* glutSolidDodecahedron() -- looks OK
* glutWireOctahedron() -- looks OK
* glutSolidOctahedron() -- looks OK
* glutWireTetrahedron() -- looks OK
* glutSolidTetrahedron() -- looks OK
* glutWireIcosahedron() -- looks OK
* glutSolidIcosahedron() -- looks OK
*
* The Following functions have been updated by Nigel Stewart, based
* on FreeGLUT 2.0.0 implementations:
*
* glutWireSphere() -- looks OK
* glutSolidSphere() -- looks OK
* glutWireCone() -- looks OK
* glutSolidCone() -- looks OK
*/
/* -- INTERFACE FUNCTIONS -------------------------------------------------- */
/*
* Draws a wireframed cube. Code contributed by Andreas Umbach <marvin@dataway.ch>
*/
void glutWireCube( GLdouble dSize )
{
double size = dSize * 0.5;
# define V(a,b,c) glVertex3d( a size, b size, c size );
# define N(a,b,c) glNormal3d( a, b, c );
/* PWO: I dared to convert the code to use macros... */
glBegin( GL_LINE_LOOP ); N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+); glEnd();
glBegin( GL_LINE_LOOP ); N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+); glEnd();
glBegin( GL_LINE_LOOP ); N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+); glEnd();
glBegin( GL_LINE_LOOP ); N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-); glEnd();
glBegin( GL_LINE_LOOP ); N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+); glEnd();
glBegin( GL_LINE_LOOP ); N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-); glEnd();
# undef V
# undef N
}
/*
* Draws a solid cube. Code contributed by Andreas Umbach <marvin@dataway.ch>
*/
void glutSolidCube( GLdouble dSize )
{
double size = dSize * 0.5;
# define V(a,b,c) glVertex3d( a size, b size, c size );
# define N(a,b,c) glNormal3d( a, b, c );
/* PWO: Again, I dared to convert the code to use macros... */
glBegin( GL_QUADS );
N( 1.0, 0.0, 0.0); V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
N( 0.0, 1.0, 0.0); V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
N( 0.0, 0.0, 1.0); V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
N(-1.0, 0.0, 0.0); V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
N( 0.0,-1.0, 0.0); V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
N( 0.0, 0.0,-1.0); V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
glEnd();
# undef V
# undef N
}
/*
* Compute lookup table of cos and sin values forming a cirle
*
* Notes:
* It is the responsibility of the caller to free these tables
* The size of the table is (n+1) to form a connected loop
* The last entry is exactly the same as the first
* The sign of n can be flipped to get the reverse loop
*/
static void fghCircleTable(double **sint,double **cost,const int n)
{
int i;
/* Table size, the sign of n flips the circle direction */
const int size = abs(n);
/* Determine the angle between samples */
const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n );
/* Allocate memory for n samples, plus duplicate of first entry at the end */
*sint = (double *) calloc(sizeof(double), size+1);
*cost = (double *) calloc(sizeof(double), size+1);
/* Bail out if memory allocation fails, fgError never returns */
if (!(*sint) || !(*cost))
{
if (*sint) free(*sint);
if (*cost) free(*cost);
return;
}
/* Compute cos and sin around the circle */
(*sint)[0] = 0.0;
(*cost)[0] = 1.0;
for (i=1; i<size; i++)
{
(*sint)[i] = sin(angle*i);
(*cost)[i] = cos(angle*i);
}
/* Last sample is duplicate of the first */
(*sint)[size] = (*sint)[0];
(*cost)[size] = (*cost)[0];
}
/*
* Draws a solid sphere
*/
void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks)
{
int i,j;
/* Adjust z and radius as stacks are drawn. */
double z0,z1;
double r0,r1;
/* Pre-computed circle */
double *sint1,*cost1;
double *sint2,*cost2;
fghCircleTable(&sint1,&cost1,-slices);
fghCircleTable(&sint2,&cost2,stacks*2);
/* The top stack is covered with a triangle fan */
z0 = 1.0;
z1 = cost2[(stacks>0)?1:0];
r0 = 0.0;
r1 = sint2[(stacks>0)?1:0];
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0,0,1);
glVertex3d(0,0,radius);
for (j=slices; j>=0; j--)
{
glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
}
glEnd();
/* Cover each stack with a quad strip, except the top and bottom stacks */
for( i=1; i<stacks-1; i++ )
{
z0 = z1; z1 = cost2[i+1];
r0 = r1; r1 = sint2[i+1];
glBegin(GL_QUAD_STRIP);
for(j=0; j<=slices; j++)
{
glNormal3d(cost1[j]*r1, sint1[j]*r1, z1 );
glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
}
glEnd();
}
/* The bottom stack is covered with a triangle fan */
z0 = z1;
r0 = r1;
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0,0,-1);
glVertex3d(0,0,-radius);
for (j=0; j<=slices; j++)
{
glNormal3d(cost1[j]*r0, sint1[j]*r0, z0 );
glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
}
glEnd();
/* Release sin and cos tables */
free(sint1);
free(cost1);
free(sint2);
free(cost2);
}
/*
* Draws a wire sphere
*/
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
{
int i,j;
/* Adjust z and radius as stacks and slices are drawn. */
double r;
double x,y,z;
/* Pre-computed circle */
double *sint1,*cost1;
double *sint2,*cost2;
fghCircleTable(&sint1,&cost1,-slices );
fghCircleTable(&sint2,&cost2, stacks*2);
/* Draw a line loop for each stack */
for (i=1; i<stacks; i++)
{
z = cost2[i];
r = sint2[i];
glBegin(GL_LINE_LOOP);
for(j=0; j<=slices; j++)
{
x = cost1[j];
y = sint1[j];
glNormal3d(x,y,z);
glVertex3d(x*r*radius,y*r*radius,z*radius);
}
glEnd();
}
/* Draw a line loop for each slice */
for (i=0; i<slices; i++)
{
glBegin(GL_LINE_STRIP);
for(j=0; j<=stacks; j++)
{
x = cost1[i]*sint2[j];
y = sint1[i]*sint2[j];
z = cost2[j];
glNormal3d(x,y,z);
glVertex3d(x*radius,y*radius,z*radius);
}
glEnd();
}
/* Release sin and cos tables */
free(sint1);
free(cost1);
free(sint2);
free(cost2);
}
/*
* Draws a solid cone
*/
void glutSolidCone( GLdouble base, GLdouble height, GLint slices, GLint stacks )
{
int i,j;
/* Step in z and radius as stacks are drawn. */
double z0,z1;
double r0,r1;
const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
/* Scaling factors for vertex normals */
const double cosn = ( height / sqrt ( height * height + base * base ));
const double sinn = ( base / sqrt ( height * height + base * base ));
/* Pre-computed circle */
double *sint,*cost;
fghCircleTable(&sint,&cost,-slices);
/* Cover the circular base with a triangle fan... */
z0 = 0.0;
z1 = zStep;
r0 = base;
r1 = r0 - rStep;
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0.0,0.0,-1.0);
glVertex3d(0.0,0.0, z0 );
for (j=0; j<=slices; j++)
glVertex3d(cost[j]*r0, sint[j]*r0, z0);
glEnd();
/* Cover each stack with a quad strip, except the top stack */
for( i=0; i<stacks-1; i++ )
{
glBegin(GL_QUAD_STRIP);
for(j=0; j<=slices; j++)
{
glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn);
glVertex3d(cost[j]*r0, sint[j]*r0, z0 );
glVertex3d(cost[j]*r1, sint[j]*r1, z1 );
}
z0 = z1; z1 += zStep;
r0 = r1; r1 -= rStep;
glEnd();
}
/* The top stack is covered with individual triangles */
glBegin(GL_TRIANGLES);
glNormal3d(cost[0]*sinn, sint[0]*sinn, cosn);
for (j=0; j<slices; j++)
{
glVertex3d(cost[j+0]*r0, sint[j+0]*r0, z0 );
glVertex3d(0, 0, height);
glNormal3d(cost[j+1]*sinn, sint[j+1]*sinn, cosn );
glVertex3d(cost[j+1]*r0, sint[j+1]*r0, z0 );
}
glEnd();
/* Release sin and cos tables */
free(sint);
free(cost);
}
/*
* Draws a wire cone
*/
void glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks)
{
int i,j;
/* Step in z and radius as stacks are drawn. */
double z = 0.0;
double r = base;
const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
const double rStep = base / ( ( stacks > 0 ) ? stacks : 1 );
/* Scaling factors for vertex normals */
const double cosn = ( height / sqrt ( height * height + base * base ));
const double sinn = ( base / sqrt ( height * height + base * base ));
/* Pre-computed circle */
double *sint,*cost;
fghCircleTable(&sint,&cost,-slices);
/* Draw the stacks... */
for (i=0; i<stacks; i++)
{
glBegin(GL_LINE_LOOP);
for( j=0; j<slices; j++ )
{
glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn);
glVertex3d(cost[j]*r, sint[j]*r, z );
}
glEnd();
z += zStep;
r -= rStep;
}
/* Draw the slices */
r = base;
glBegin(GL_LINES);
for (j=0; j<slices; j++)
{
glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn );
glVertex3d(cost[j]*r, sint[j]*r, 0.0 );
glVertex3d(0.0, 0.0, height);
}
glEnd();
/* Release sin and cos tables */
free(sint);
free(cost);
}
/*
* Draws a solid cylinder
*/
void glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
{
int i,j;
/* Step in z and radius as stacks are drawn. */
double z0,z1;
const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
/* Pre-computed circle */
double *sint,*cost;
fghCircleTable(&sint,&cost,-slices);
/* Cover the base and top */
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0.0, 0.0, -1.0 );
glVertex3d(0.0, 0.0, 0.0 );
for (j=0; j<=slices; j++)
glVertex3d(cost[j]*radius, sint[j]*radius, 0.0);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glNormal3d(0.0, 0.0, 1.0 );
glVertex3d(0.0, 0.0, height);
for (j=slices; j>=0; j--)
glVertex3d(cost[j]*radius, sint[j]*radius, height);
glEnd();
/* Do the stacks */
z0 = 0.0;
z1 = zStep;
for (i=1; i<=stacks; i++)
{
if (i==stacks)
z1 = height;
glBegin(GL_QUAD_STRIP);
for (j=0; j<=slices; j++ )
{
glNormal3d(cost[j], sint[j], 0.0 );
glVertex3d(cost[j]*radius, sint[j]*radius, z0 );
glVertex3d(cost[j]*radius, sint[j]*radius, z1 );
}
glEnd();
z0 = z1; z1 += zStep;
}
/* Release sin and cos tables */
free(sint);
free(cost);
}
/*
* Draws a wire cylinder
*/
void glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks)
{
int i,j;
/* Step in z and radius as stacks are drawn. */
double z = 0.0;
const double zStep = height / ( ( stacks > 0 ) ? stacks : 1 );
/* Pre-computed circle */
double *sint,*cost;
fghCircleTable(&sint,&cost,-slices);
/* Draw the stacks... */
for (i=0; i<=stacks; i++)
{
if (i==stacks)
z = height;
glBegin(GL_LINE_LOOP);
for( j=0; j<slices; j++ )
{
glNormal3d(cost[j], sint[j], 0.0);
glVertex3d(cost[j]*radius, sint[j]*radius, z );
}
glEnd();
z += zStep;
}
/* Draw the slices */
glBegin(GL_LINES);
for (j=0; j<slices; j++)
{
glNormal3d(cost[j], sint[j], 0.0 );
glVertex3d(cost[j]*radius, sint[j]*radius, 0.0 );
glVertex3d(cost[j]*radius, sint[j]*radius, height);
}
glEnd();
/* Release sin and cos tables */
free(sint);
free(cost);
}
/*
* Draws a wire torus
*/
void glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
{
double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
double *vertex, *normal;
int i, j;
double spsi, cpsi, sphi, cphi ;
if ( nSides < 1 ) nSides = 1;
if ( nRings < 1 ) nRings = 1;
/* Allocate the vertices array */
vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings );
normal = (double *)calloc( sizeof(double), 3 * nSides * nRings );
glPushMatrix();
dpsi = 2.0 * M_PI / (double)nRings ;
dphi = -2.0 * M_PI / (double)nSides ;
psi = 0.0;
for( j=0; j<nRings; j++ )
{
cpsi = cos ( psi ) ;
spsi = sin ( psi ) ;
phi = 0.0;
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
cphi = cos ( phi ) ;
sphi = sin ( phi ) ;
*(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
*(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
*(vertex + offset + 2) = sphi * iradius ;
*(normal + offset + 0) = cpsi * cphi ;
*(normal + offset + 1) = spsi * cphi ;
*(normal + offset + 2) = sphi ;
phi += dphi;
}
psi += dpsi;
}
for( i=0; i<nSides; i++ )
{
glBegin( GL_LINE_LOOP );
for( j=0; j<nRings; j++ )
{
int offset = 3 * ( j * nSides + i ) ;
glNormal3dv( normal + offset );
glVertex3dv( vertex + offset );
}
glEnd();
}
for( j=0; j<nRings; j++ )
{
glBegin(GL_LINE_LOOP);
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
glNormal3dv( normal + offset );
glVertex3dv( vertex + offset );
}
glEnd();
}
free ( vertex ) ;
free ( normal ) ;
glPopMatrix();
}
/*
* Draws a solid torus
*/
void glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings )
{
double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi;
double *vertex, *normal;
int i, j;
double spsi, cpsi, sphi, cphi ;
if ( nSides < 1 ) nSides = 1;
if ( nRings < 1 ) nRings = 1;
/* Increment the number of sides and rings to allow for one more point than surface */
nSides ++ ;
nRings ++ ;
/* Allocate the vertices array */
vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings );
normal = (double *)calloc( sizeof(double), 3 * nSides * nRings );
glPushMatrix();
dpsi = 2.0 * M_PI / (double)(nRings - 1) ;
dphi = -2.0 * M_PI / (double)(nSides - 1) ;
psi = 0.0;
for( j=0; j<nRings; j++ )
{
cpsi = cos ( psi ) ;
spsi = sin ( psi ) ;
phi = 0.0;
for( i=0; i<nSides; i++ )
{
int offset = 3 * ( j * nSides + i ) ;
cphi = cos ( phi ) ;
sphi = sin ( phi ) ;
*(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ;
*(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ;
*(vertex + offset + 2) = sphi * iradius ;
*(normal + offset + 0) = cpsi * cphi ;
*(normal + offset + 1) = spsi * cphi ;
*(normal + offset + 2) = sphi ;
phi += dphi;
}
psi += dpsi;
}
glBegin( GL_QUADS );
for( i=0; i<nSides-1; i++ )
{
for( j=0; j<nRings-1; j++ )
{
int offset = 3 * ( j * nSides + i ) ;
glNormal3dv( normal + offset );
glVertex3dv( vertex + offset );
glNormal3dv( normal + offset + 3 );
glVertex3dv( vertex + offset + 3 );
glNormal3dv( normal + offset + 3 * nSides + 3 );
glVertex3dv( vertex + offset + 3 * nSides + 3 );
glNormal3dv( normal + offset + 3 * nSides );
glVertex3dv( vertex + offset + 3 * nSides );
}
}
glEnd();
free ( vertex ) ;
free ( normal ) ;
glPopMatrix();
}
/*
*
*/
void glutWireDodecahedron( void )
{
/* Magic Numbers: It is possible to create a dodecahedron by attaching two pentagons to each face of
* of a cube. The coordinates of the points are:
* (+-x,0, z); (+-1, 1, 1); (0, z, x )
* where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
* x = 0.61803398875 and z = 1.61803398875.
*/
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
glBegin ( GL_LINE_LOOP ) ;
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 ) ;
glEnd () ;
}
/*
*
*/
void glutSolidDodecahedron( void )
{
/* Magic Numbers: It is possible to create a dodecahedron by attaching two pentagons to each face of
* of a cube. The coordinates of the points are:
* (+-x,0, z); (+-1, 1, 1); (0, z, x )
* where x = (-1 + sqrt(5))/2, z = (1 + sqrt(5))/2 or
* x = 0.61803398875 and z = 1.61803398875.
*/
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
glBegin ( GL_POLYGON ) ;
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 ) ;
glEnd () ;
}
/*
*
*/
void glutWireOctahedron( void )
{
#define RADIUS 1.0f
glBegin( GL_LINE_LOOP );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
glEnd();
#undef RADIUS
}
/*
*
*/
void glutSolidOctahedron( void )
{
#define RADIUS 1.0f
glBegin( GL_TRIANGLES );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
glEnd();
#undef RADIUS
}
/* Magic Numbers: r0 = ( 1, 0, 0 )
* r1 = ( -1/3, 2 sqrt(2) / 3, 0 )
* r2 = ( -1/3, -sqrt(2) / 3, sqrt(6) / 3 )
* r3 = ( -1/3, -sqrt(2) / 3, -sqrt(6) / 3 )
* |r0| = |r1| = |r2| = |r3| = 1
* Distance between any two points is 2 sqrt(6) / 3
*
* Normals: The unit normals are simply the negative of the coordinates of the point not on the surface.
*/
#define NUM_TETR_FACES 4
static GLdouble tet_r[4][3] = { { 1.0, 0.0, 0.0 },
{ -0.333333333333, 0.942809041582, 0.0 },
{ -0.333333333333, -0.471404520791, 0.816496580928 },
{ -0.333333333333, -0.471404520791, -0.816496580928 } } ;
static GLint tet_i[4][3] = /* Vertex indices */
{
{ 1, 3, 2 }, { 0, 2, 3 }, { 0, 3, 1 }, { 0, 1, 2 }
} ;
/*
*
*/
void glutWireTetrahedron( void )
{
glBegin( GL_LINE_LOOP ) ;
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] ) ;
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] ) ;
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] ) ;
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] ) ;
glEnd() ;
}
/*
*
*/
void glutSolidTetrahedron( void )
{
glBegin( GL_TRIANGLES ) ;
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] ) ;
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] ) ;
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] ) ;
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] ) ;
glEnd() ;
}
/*
*
*/
double icos_r[12][3] = { { 1.0, 0.0, 0.0 },
{ 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 },
{ -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 },
{ -1.0, 0.0, 0.0 } } ;
int icos_v [20][3] = { { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 5 }, { 0, 5, 1 },
{ 1, 8, 2 }, { 2, 7, 3 }, { 3, 6, 4 }, { 4, 10, 5 }, { 5, 9, 1 },
{ 1, 9, 8 }, { 2, 8, 7 }, { 3, 7, 6 }, { 4, 6, 10 }, { 5, 10, 9 },
{ 11, 9, 10 }, { 11, 8, 9 }, { 11, 7, 8 }, { 11, 6, 7 }, { 11, 10, 6 } } ;
void glutWireIcosahedron( void )
{
int i ;
for ( i = 0; i < 20; i++ )
{
double normal[3] ;
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] ) ;
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] ) ;
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] ) ;
glBegin ( GL_LINE_LOOP ) ;
glNormal3dv ( normal ) ;
glVertex3dv ( icos_r[icos_v[i][0]] ) ;
glVertex3dv ( icos_r[icos_v[i][1]] ) ;
glVertex3dv ( icos_r[icos_v[i][2]] ) ;
glEnd () ;
}
}
/*
*
*/
void glutSolidIcosahedron( void )
{
int i ;
glBegin ( GL_TRIANGLES ) ;
for ( i = 0; i < 20; i++ )
{
double normal[3] ;
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] ) ;
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] ) ;
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] ) ;
glNormal3dv ( normal ) ;
glVertex3dv ( icos_r[icos_v[i][0]] ) ;
glVertex3dv ( icos_r[icos_v[i][1]] ) ;
glVertex3dv ( icos_r[icos_v[i][2]] ) ;
}
glEnd () ;
}
/*
*
*/
double rdod_r[14][3] = { { 0.0, 0.0, 1.0 },
{ 0.707106781187, 0.000000000000, 0.5 }, { 0.000000000000, 0.707106781187, 0.5 }, { -0.707106781187, 0.000000000000, 0.5 }, { 0.000000000000, -0.707106781187, 0.5 },
{ 0.707106781187, 0.707106781187, 0.0 }, { -0.707106781187, 0.707106781187, 0.0 }, { -0.707106781187, -0.707106781187, 0.0 }, { 0.707106781187, -0.707106781187, 0.0 },
{ 0.707106781187, 0.000000000000, -0.5 }, { 0.000000000000, 0.707106781187, -0.5 }, { -0.707106781187, 0.000000000000, -0.5 }, { 0.000000000000, -0.707106781187, -0.5 },
{ 0.0, 0.0, -1.0 } } ;
int rdod_v [12][4] = { { 0, 1, 5, 2 }, { 0, 2, 6, 3 }, { 0, 3, 7, 4 }, { 0, 4, 8, 1 },
{ 5, 10, 6, 2 }, { 6, 11, 7, 3 }, { 7, 12, 8, 4 }, { 8, 9, 5, 1 },
{ 5, 9, 13, 10 }, { 6, 10, 13, 11 }, { 7, 11, 13, 12 }, { 8, 12, 13, 9 } } ;
double rdod_n[12][3] = {
{ 0.353553390594, 0.353553390594, 0.5 }, { -0.353553390594, 0.353553390594, 0.5 }, { -0.353553390594, -0.353553390594, 0.5 }, { 0.353553390594, -0.353553390594, 0.5 },
{ 0.000000000000, 1.000000000000, 0.0 }, { -1.000000000000, 0.000000000000, 0.0 }, { 0.000000000000, -1.000000000000, 0.0 }, { 1.000000000000, 0.000000000000, 0.0 },
{ 0.353553390594, 0.353553390594, -0.5 }, { -0.353553390594, 0.353553390594, -0.5 }, { -0.353553390594, -0.353553390594, -0.5 }, { 0.353553390594, -0.353553390594, -0.5 }
} ;
void glutWireRhombicDodecahedron( void )
{
int i ;
for ( i = 0; i < 12; i++ )
{
glBegin ( GL_LINE_LOOP ) ;
glNormal3dv ( rdod_n[i] ) ;
glVertex3dv ( rdod_r[rdod_v[i][0]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][1]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][2]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][3]] ) ;
glEnd () ;
}
}
/*
*
*/
void glutSolidRhombicDodecahedron( void )
{
int i ;
glBegin ( GL_QUADS ) ;
for ( i = 0; i < 12; i++ )
{
glNormal3dv ( rdod_n[i] ) ;
glVertex3dv ( rdod_r[rdod_v[i][0]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][1]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][2]] ) ;
glVertex3dv ( rdod_r[rdod_v[i][3]] ) ;
}
glEnd () ;
}
void glutWireSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
{
int i, j ;
if ( num_levels == 0 )
{
for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
{
glBegin ( GL_LINE_LOOP ) ;
glNormal3d ( -tet_r[i][0], -tet_r[i][1], -tet_r[i][2] ) ;
for ( j = 0; j < 3; j++ )
{
double x = offset[0] + scale * tet_r[tet_i[i][j]][0] ;
double y = offset[1] + scale * tet_r[tet_i[i][j]][1] ;
double z = offset[2] + scale * tet_r[tet_i[i][j]][2] ;
glVertex3d ( x, y, z ) ;
}
glEnd () ;
}
}
else
{
GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
num_levels -- ;
scale /= 2.0 ;
for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
{
local_offset[0] = offset[0] + scale * tet_r[i][0] ;
local_offset[1] = offset[1] + scale * tet_r[i][1] ;
local_offset[2] = offset[2] + scale * tet_r[i][2] ;
glutWireSierpinskiSponge ( num_levels, local_offset, scale ) ;
}
}
}
void glutSolidSierpinskiSponge ( int num_levels, GLdouble offset[3], GLdouble scale )
{
int i, j ;
if ( num_levels == 0 )
{
glBegin ( GL_TRIANGLES ) ;
for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
{
glNormal3d ( -tet_r[i][0], -tet_r[i][1], -tet_r[i][2] ) ;
for ( j = 0; j < 3; j++ )
{
double x = offset[0] + scale * tet_r[tet_i[i][j]][0] ;
double y = offset[1] + scale * tet_r[tet_i[i][j]][1] ;
double z = offset[2] + scale * tet_r[tet_i[i][j]][2] ;
glVertex3d ( x, y, z ) ;
}
}
glEnd () ;
}
else
{
GLdouble local_offset[3] ; /* Use a local variable to avoid buildup of roundoff errors */
num_levels -- ;
scale /= 2.0 ;
for ( i = 0 ; i < NUM_TETR_FACES ; i++ )
{
local_offset[0] = offset[0] + scale * tet_r[i][0] ;
local_offset[1] = offset[1] + scale * tet_r[i][1] ;
local_offset[2] = offset[2] + scale * tet_r[i][2] ;
glutSolidSierpinskiSponge ( num_levels, local_offset, scale ) ;
}
}
}
/*** END OF FILE ***/
|