home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / SPD.3.0.shar.gz / SPD.3.0.shar / rings.c < prev    next >
C/C++ Source or Header  |  1991-01-13  |  6KB  |  198 lines

  1. /*
  2.  * rings.c - Create objects with 6 pentagonal rings which connect the midpoints
  3.  *    of the edges of a dodecahedron.  A pyramid of these objects is formed,
  4.  *    which the viewer looks upon from the point.  A plane is placed behind
  5.  *    the pyramid for shadows.  Three light sources.
  6.  *
  7.  * Author:  Eric Haines, 3D/Eye, Inc.
  8.  *
  9.  * SizeFactor determines the number of objects output.
  10.  *    Each object has 30 cylinders and 30 spheres.
  11.  *    Total objects = SF*SF + (SF-1)*(SF-1) + ... + 1 plus 1 backdrop square.
  12.  *    formula for # of spheres or cylinders = 5*SF*(SF+1)*(2*SF+1)
  13.  *
  14.  *    SizeFactor    # spheres    # cylinders    # squares
  15.  *         1            30              30         1
  16.  *         2           150             150         1
  17.  *         3           420             420         1
  18.  *
  19.  *         7          4200            4200         1
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include "def.h"
  25. #include "lib.h"
  26.  
  27. static    int    SizeFactor = 7 ;
  28.  
  29. #define    OUTPUT_FORMAT        OUTPUT_CURVES
  30.  
  31. /* if spread out is > 1, succeeding layers spread out more */
  32. #define    SPREAD_OUT        1
  33.  
  34. main(argc,argv)
  35. int    argc ;
  36. char    *argv[] ;
  37. {
  38. int    prev_elem, num_elem, num_depth, num_objx, num_objz ;
  39. double    radius, spread, y_diff, xz_diff ;
  40. COORD4    base_pt, apex_pt ;
  41. COORD3    from, at, up ;
  42. COORD3    wvec, light ;
  43. COORD3    back_color, ring_color[6] ;
  44. COORD3    wall[4], offset, dodec[30] ;
  45.  
  46.     if ( !lib_get_size( argc, argv, &SizeFactor ) ) {
  47.     fprintf( stderr, "usage: %s [size]\n", *argv ) ;
  48.     exit(EXIT_FAIL) ;
  49.     }
  50.  
  51.     radius = 0.07412 ;    /* cone and sphere radius */
  52.  
  53.     /* calculate spread of objects */
  54.     spread = 1 / sin( (double)( PI/8.0 ) ) ;
  55.     if ( SPREAD_OUT <= spread ) {
  56.     y_diff = spread / SPREAD_OUT ;
  57.     xz_diff = 1.0 ;
  58.     }
  59.     else {
  60.     y_diff = 1.0 ;
  61.     xz_diff = SPREAD_OUT / spread ;
  62.     }
  63.  
  64.     /* output viewpoint */
  65.     SET_COORD3( from, -1.0, -spread, 0.5 ) ;
  66.     SET_COORD3( at, from[X], from[Y] + 1.0, from[Z] ) ;
  67.     SET_COORD3( up, 0.0, 0.0, 1.0 ) ;
  68.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 512, 512 ) ;
  69.  
  70.     /* output background color - UNC sky blue */
  71.     /* note that the background color should never be seen */
  72.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  73.     lib_output_background_color( back_color ) ;
  74.  
  75.     /* output light source */
  76.     SET_COORD3( light, 3.0, -spread, 3.0 ) ;
  77.     lib_output_light( light ) ;
  78.     SET_COORD3( light, -4.0, -spread, 1.0 ) ;
  79.     lib_output_light( light ) ;
  80.     SET_COORD3( light, 2.0, -spread, -4.0 ) ;
  81.     lib_output_light( light ) ;
  82.  
  83.     /* output wall polygon - white */
  84.     SET_COORD3( back_color, 1.0, 1.0, 1.0 ) ;
  85.     lib_output_color( back_color, 1.0, 0.0, 0.0, 0.0, 0.0 ) ;
  86.     /* just spans 45 degree view + 1% */
  87.     wvec[Y] = y_diff * ( SizeFactor + 1 ) ;
  88.     wvec[X] = wvec[Z] = 1.01 * ( wvec[Y] - from[Y] ) * tan( PI / 8.0 ) ;
  89.     SET_COORD3( wall[0],  wvec[X]+from[X], wvec[Y],  wvec[Z]+from[Z] ) ;
  90.     SET_COORD3( wall[1], -wvec[X]+from[X], wvec[Y],  wvec[Z]+from[Z] ) ;
  91.     SET_COORD3( wall[2], -wvec[X]+from[X], wvec[Y], -wvec[Z]+from[Z] ) ;
  92.     SET_COORD3( wall[3],  wvec[X]+from[X], wvec[Y], -wvec[Z]+from[Z] ) ;
  93.     lib_output_polygon( 4, wall ) ;
  94.  
  95.     /* set up ring colors - RGB and complements */
  96.     SET_COORD3( ring_color[0], 1.0, 0.0, 0.0 ) ;
  97.     SET_COORD3( ring_color[1], 0.0, 1.0, 0.0 ) ;
  98.     SET_COORD3( ring_color[2], 0.0, 0.0, 1.0 ) ;
  99.     SET_COORD3( ring_color[3], 0.0, 1.0, 1.0 ) ;
  100.     SET_COORD3( ring_color[4], 1.0, 0.0, 1.0 ) ;
  101.     SET_COORD3( ring_color[5], 1.0, 1.0, 0.0 ) ;
  102.  
  103.     create_dodec( radius, dodec ) ;
  104.     /* radius of osculating cylinders and spheres (no derivation given) */
  105.     base_pt[W] = apex_pt[W] = radius ;
  106.  
  107.     for ( num_depth = 0 ; num_depth < SizeFactor ; ++num_depth ) {
  108.     offset[Y] = y_diff * (double)(num_depth+1) ;
  109.     for ( num_objz = 0 ; num_objz <= num_depth ; ++num_objz ) {
  110.         offset[Z] = xz_diff * (double)(2*num_objz - num_depth) ;
  111.         for ( num_objx = 0 ; num_objx <= num_depth ; ++num_objx ) {
  112.         offset[X] = xz_diff * (double)(2*num_objx - num_depth) ;
  113.         for ( num_elem = 0 ; num_elem < 30 ; ++num_elem ) {
  114.             COPY_COORD3( base_pt, dodec[num_elem] ) ;
  115.             ADD2_COORD3( base_pt, offset ) ;
  116.             if ( num_elem%5 == 0 ) {
  117.             prev_elem = num_elem + 4 ;
  118.             /* new ring beginning - output color */
  119.             lib_output_color( ring_color[num_elem/5]
  120.                     , 0.5
  121.                     , 0.5, 3.0
  122.                     , 0.0, 0.0
  123.                     ) ;
  124.             }
  125.             else {
  126.             prev_elem = num_elem - 1 ;
  127.             }
  128.             COPY_COORD3( apex_pt, dodec[prev_elem] ) ;
  129.             ADD2_COORD3( apex_pt, offset ) ;
  130.  
  131.             lib_output_cylcone( base_pt, apex_pt, OUTPUT_FORMAT ) ;
  132.             lib_output_sphere( base_pt, OUTPUT_FORMAT ) ;
  133.         }
  134.         }
  135.     }
  136.     }
  137.     exit(EXIT_SUCCESS) ;
  138. }
  139.  
  140. /* Create the set of 30 points needed to generate the rings */
  141. create_dodec( minor_radius, vertex )
  142. double    minor_radius ;
  143. COORD3    vertex[30] ;
  144. {
  145. int    num_vertex, num_pentagon ;
  146. double    scale, x_rotation, z_rotation ;
  147. COORD3    temp_vertex ;
  148. MATRIX    x_matrix, z_matrix ;
  149.  
  150.     /* scale object to fit in a sphere of radius 1 */
  151.     scale = 1.0 / ( 1.0 + minor_radius ) ;
  152.  
  153.     /*
  154.      * define one pentagon as on the XY plane, with points starting along +X
  155.      * and N fifths of the way around the Z axis.
  156.      */
  157.     for ( num_vertex = 0 ; num_vertex < 5 ; ++num_vertex ) {
  158.     vertex[num_vertex][X] = scale * cos((double)num_vertex * 2.0*PI/5.0 ) ;
  159.     vertex[num_vertex][Y] = scale * sin((double)num_vertex * 2.0*PI/5.0 ) ;
  160.     vertex[num_vertex][Z] = 0.0 ;
  161.     vertex[num_vertex][W] = 1.0 ;
  162.     }
  163.  
  164.     /*
  165.      * find the rotation angle (in radians) along the X axis:
  166.      * angle between two adjacent dodecahedron faces.
  167.      */
  168.     x_rotation = 2.0 *
  169.         acos( cos( (double)(PI/3.0) ) / sin( (double)(PI/5.0) ) ) ;
  170.     lib_create_rotate_matrix( x_matrix, X_AXIS, x_rotation ) ;
  171.  
  172.     /*
  173.      * Find each of the other 5 pentagons:  rotate along the X axis,
  174.      * then rotate on the Z axis.
  175.      */
  176.     for ( num_pentagon = 1 ; num_pentagon < 6 ; ++num_pentagon ) {
  177.     /*
  178.      * find the rotation angle (in radians) along the Z axis:
  179.      * 1/10th plus N fifths of the way around * 2 * PI.
  180.      */
  181.     z_rotation = PI*( 2.0*(double)(num_pentagon-1)+1.0 ) / 5.0 ;
  182.     lib_create_rotate_matrix( z_matrix, Z_AXIS, z_rotation ) ;
  183.  
  184.     for ( num_vertex = 0 ; num_vertex < 5 ; ++num_vertex ) {
  185.  
  186.         lib_transform_point( temp_vertex
  187.                    , vertex[num_vertex]
  188.                    , x_matrix
  189.                    ) ;
  190.  
  191.         lib_transform_point( vertex[5*num_pentagon+num_vertex]
  192.                    , temp_vertex
  193.                    , z_matrix
  194.                    ) ;
  195.     }
  196.     }
  197. }
  198.