home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / beach_ball / spheres.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-09  |  6.2 KB  |  212 lines

  1. /* $XConsortium: spheres.c,v 5.2 91/05/09 16:14:54 rws Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989,1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /*
  28.  * Copyright (c) 1989,1990, 1991 by M.I.T. and Sun Microsystems, Inc.
  29.  */
  30.  
  31. /* tessellate a sphere with triangles
  32.  
  33.     method: divide into latitute and longitude lines.  This gives
  34.     quadrilateral tesellation, then slash each quad to give triangles.
  35.     Exception: first and last latitude rows are already triangles, with
  36.     the N or S pole as a vertex
  37.  
  38. */
  39.  
  40. #include <phigs/phigs.h>
  41.  
  42. #include <math.h>
  43. #ifndef X_NOT_STDC_ENV
  44. #include <stdlib.h>
  45. #else
  46. char *malloc();
  47. #endif
  48. #if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
  49. char *malloc();
  50. #endif /* macII */
  51.  
  52. #define MAXLAT 100    /*max number of horiz and vert. divisions of sphere*/
  53. #define MAXLONG 100
  54.  
  55. #define BIGNUM 999.0
  56. #define PI 3.14159265358979
  57. #define TORAD(x)    ((x)*PI/180.0)
  58.  
  59. typedef struct {
  60.     Ppoint3    upleft, upright, downleft, downright;
  61. } Quad;
  62.  
  63.  
  64. static int
  65. tesselate_sphere( radius, nlat, nlong, facets )
  66.     double    radius;
  67.     int       nlat, nlong;    /*number of horiz, vert quads*/
  68.     Ppoint3   facets[][3];
  69. {
  70.     int       i, j, k, lat1, lat2, nfacets = 0;
  71.     double    theta, deltay, deltatheta;
  72.     float     sx, sy, sz, tx, ty, tz, rx, ry, rz;
  73.     Ppoint3   p1, p2;
  74.     Ppoint3   npole, spole;
  75.     Quad     (*q)[MAXLONG];
  76.  
  77.     if ( nlat < 2 || 0 != nlat % 2 ) {
  78.     fprintf( stderr, "num lats must be even and >=2\n");
  79.     return -1;
  80.     }
  81.     if( nlat >= MAXLAT ) {
  82.     fprintf( stderr, "current num lat limit is %d\n");
  83.     return -2;
  84.     }
  85.  
  86.     if( nlong < 3 ) {
  87.     fprintf( stderr, "num long must be >= 3\n");
  88.     return -3;
  89.     }
  90.     if( nlong >= MAXLONG ) {
  91.     fprintf( stderr, "current num long limit is %d\n");
  92.     return -4;
  93.     }
  94.  
  95.     q = (Quad(*)[MAXLONG])malloc( nlat * MAXLONG * sizeof(Quad));
  96.     if ( !q )
  97.     return 0;
  98.  
  99.     npole.x = 0.0; npole.y = 1.0; npole.z = 0.0;
  100.     spole.x = 0.0; spole.y = -1.0; spole.z = 0.0;
  101.     deltay = 2.0/nlat;    /*y size of horiz. slices*/
  102.     deltatheta = 2.0*PI/nlong;
  103.  
  104.     for(j=0; j<nlong; ++j){
  105.     q[0][j].upleft =  npole;
  106.     q[0][j].upright.x = BIGNUM;    /*this is a triangle, not a quad*/
  107.     q[nlat-1][j].downleft = spole;
  108.     q[nlat-1][j].downright.x = BIGNUM;
  109.     }
  110.  
  111.     p1.x = p2.x = 1.0;
  112.     p1.y = p2.y = p1.z = p2.z = 0.0;
  113.     lat2 = nlat/2;
  114.     lat1 = lat2-1;
  115.     theta = 0.0;
  116.  
  117.     for(j=0; j<nlong; ++j){
  118.     q[lat1][j].downright = q[lat2][j].upright = p1;
  119.     if(j>0) 
  120.         q[lat1][j-1].downleft = q[lat2][j-1].upleft = p1;
  121.     theta += deltatheta;
  122.     p1.x = radius*cos(theta);
  123.     p1.z = radius*sin(theta);
  124.     }
  125.     q[lat1][nlong-1].downleft = q[lat2][nlong-1].upleft = q[lat1][0].downright;
  126.  
  127.     for(;;) {
  128.     if( lat1 == 0 )
  129.         break;
  130.     --lat1; ++lat2;
  131.     p1.y += deltay; p2.y -= deltay;
  132.     radius = sqrt(1.0 - p1.y*p1.y);
  133.     p1.x = p2.x = radius;
  134.     p1.z = p2.z = 0.0;
  135.     theta = 0.0;
  136.  
  137.     for(j=0; j<nlong; ++j){
  138.         q[lat1][j].downright = q[lat1+1][j].upright = p1;
  139.         q[lat2][j].upright = q[lat2-1][j].downright = p2;
  140.         if(j>0){
  141.         q[lat1][j-1].downleft = q[lat1+1][j-1].upleft= p1;
  142.         q[lat2][j-1].upleft = q[lat2-1][j-1].downleft= p2;
  143.         }
  144.         theta += deltatheta;
  145.         p1.x = p2.x = radius*cos(theta);
  146.         p1.z = p2.z = radius*sin(theta);
  147.     }
  148.     q[lat1][nlong-1].downleft
  149.         = q[lat1+1][nlong-1].upleft = q[lat1][0].downright;
  150.     q[lat2][nlong-1].upleft
  151.         = q[lat2-1][nlong-1].downleft = q[lat2][0].upright;
  152.     }
  153.  
  154.     for( i=0; i < nlat; ++i ) {
  155.     for( j = 0; j < nlong; ++j ) {
  156.         if( q[i][j].upright.x == BIGNUM ) {
  157.         facets[nfacets][0] = q[i][j].downleft;
  158.         facets[nfacets][1] = q[i][j].downright;
  159.         facets[nfacets][2] = q[i][j].upleft;
  160.         ++nfacets;
  161.         } else if( q[i][j].downright.x == BIGNUM ) {
  162.         facets[nfacets][0] = q[i][j].downleft;
  163.         facets[nfacets][1] = q[i][j].upleft;
  164.         facets[nfacets][2] = q[i][j].upright;
  165.         ++nfacets;
  166.         } else {
  167.         facets[nfacets][0] = q[i][j].downleft;
  168.         facets[nfacets][1] = q[i][j].downright;
  169.         facets[nfacets][2] = q[i][j].upleft;
  170.         ++nfacets;
  171.         facets[nfacets][0] = q[i][j].downright;
  172.         facets[nfacets][1] = q[i][j].upleft;
  173.         facets[nfacets][2] = q[i][j].upright;
  174.         ++nfacets;
  175.         }
  176.     }
  177.     }
  178.  
  179.     free(q);
  180.     return nfacets;
  181. }
  182.  
  183. int
  184. facet_sphere( radius, num_lat, num_long, facetlist )
  185.     float     radius;
  186.     int       num_lat;
  187.     int       num_long;
  188.     Ppoint_list3  **facetlist;
  189. {
  190.     Ppoint3    (*facets)[3];
  191.     int        num_facets, farray_size, flist_size;
  192.     char       *buf;
  193.     register int        i;
  194.     register Ppoint_list3 *cfp;
  195.  
  196.     num_facets = (2 + 2 * (num_lat - 2)) * num_long;
  197.     farray_size = num_facets * 3 * sizeof(Ppoint3);
  198.     flist_size = num_facets * sizeof(Ppoint_list3);
  199.     buf = malloc(flist_size + farray_size); 
  200.     if ( buf ) { 
  201.     *facetlist = (Ppoint_list3*)buf;
  202.     facets = (Ppoint3(*)[3])(buf + flist_size);
  203.     num_facets = tesselate_sphere( radius, num_lat, num_long, facets);
  204.     for ( i = 0, cfp = *facetlist; i < num_facets; i++, cfp++ ) {
  205.         cfp->num_points = 3;
  206.         cfp->points = facets[i];
  207.     }
  208.     }
  209.  
  210.     return num_facets;
  211. }
  212.