home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / rings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  6.8 KB  |  217 lines  |  [TEXT/MMCC]

  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.  * size_factor 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.  *    size_factor    # 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 <stdlib.h>    /* atoi */
  25. #include "def.h"
  26. #include "drv.h"    /* display_close() */
  27. #include "lib.h"
  28.  
  29. static int size_factor = 7 ;
  30.  
  31. static int raytracer_format = OUTPUT_RT_DEFAULT;
  32. static int output_format    = OUTPUT_CURVES;
  33.  
  34.  
  35. /* if spread out is > 1, succeeding layers spread out more */
  36. #define    SPREAD_OUT        1
  37.  
  38. /* Create the set of 30 points needed to generate the rings */
  39. void
  40. create_dodec( minor_radius, vertex )
  41.     double minor_radius ;
  42.     COORD3 vertex[30] ;
  43. {
  44.     int num_vertex, num_pentagon ;
  45.     double scale, x_rotation, z_rotation ;
  46.     COORD3 temp_vertex ;
  47.     MATRIX x_matrix, z_matrix ;
  48.  
  49.     /* scale object to fit in a sphere of radius 1 */
  50.     scale = 1.0 / ( 1.0 + minor_radius ) ;
  51.  
  52.     /*
  53.      * define one pentagon as on the XY plane, with points starting along +X
  54.      * and N fifths of the way around the Z axis.
  55.      */
  56.     for ( num_vertex = 0 ; num_vertex < 5 ; ++num_vertex ) {
  57.     vertex[num_vertex][X] = scale * cos((double)num_vertex * 2.0*PI/5.0 ) ;
  58.     vertex[num_vertex][Y] = scale * sin((double)num_vertex * 2.0*PI/5.0 ) ;
  59.     vertex[num_vertex][Z] = 0.0 ;
  60.     vertex[num_vertex][W] = 1.0 ;
  61.     }
  62.  
  63.     /*
  64.      * find the rotation angle (in radians) along the X axis:
  65.      * angle between two adjacent dodecahedron faces.
  66.      */
  67.     x_rotation = 2.0 *
  68.         acos( cos( (double)(PI/3.0) ) / sin( (double)(PI/5.0) ) ) ;
  69.     lib_create_rotate_matrix( x_matrix, X_AXIS, x_rotation ) ;
  70.  
  71.     /*
  72.      * Find each of the other 5 pentagons:  rotate along the X axis,
  73.      * then rotate on the Z axis.
  74.      */
  75.     for ( num_pentagon = 1 ; num_pentagon < 6 ; ++num_pentagon ) {
  76.     /*
  77.      * find the rotation angle (in radians) along the Z axis:
  78.      * 1/10th plus N fifths of the way around * 2 * PI.
  79.      */
  80.     z_rotation = PI*( 2.0*(double)(num_pentagon-1)+1.0 ) / 5.0 ;
  81.     lib_create_rotate_matrix( z_matrix, Z_AXIS, z_rotation ) ;
  82.  
  83.     for ( num_vertex = 0 ; num_vertex < 5 ; ++num_vertex ) {
  84.  
  85.         lib_transform_point( temp_vertex
  86.                    , vertex[num_vertex]
  87.                    , x_matrix
  88.                    ) ;
  89.  
  90.         lib_transform_point( vertex[5*num_pentagon+num_vertex]
  91.                    , temp_vertex
  92.                    , z_matrix
  93.                    ) ;
  94.     }
  95.     }
  96. }
  97.  
  98. int
  99. main(argc,argv)
  100.     int argc;
  101.     char *argv[];
  102. {
  103.     int    prev_elem, num_elem, num_depth, num_objx, num_objz ;
  104.     double radius, spread, y_diff, xz_diff ;
  105.     COORD4 base_pt, apex_pt ;
  106.     COORD3 from, at, up ;
  107.     COORD3 wvec, light ;
  108.     COORD3 back_color, ring_color[6] ;
  109.     COORD3 wall[4], offset, dodec[30] ;
  110.     double lscale;
  111.  
  112.     PLATFORM_INIT(SPD_RINGS);
  113.  
  114.     /* Start by defining which raytracer we will be using */
  115.      if ( lib_gen_get_opts( argc, argv,
  116.             &size_factor, &raytracer_format, &output_format ) ) {
  117.     return EXIT_FAIL;
  118.     }
  119.     if ( lib_open( raytracer_format, "Rings.out" ) ) {
  120.     return EXIT_FAIL;
  121.     }
  122.  
  123.     radius = 0.07412 ;    /* cone and sphere radius */
  124.  
  125.     /* calculate spread of objects */
  126.     spread = 1 / sin( (double)( PI/8.0 ) ) ;
  127.     if ( SPREAD_OUT <= spread ) {
  128.     y_diff = spread / SPREAD_OUT ;
  129.     xz_diff = 1.0 ;
  130.     }
  131.     else {
  132.     y_diff = 1.0 ;
  133.     xz_diff = SPREAD_OUT / spread ;
  134.     }
  135.  
  136.     /* output background color - UNC sky blue */
  137.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  138.     /* note that the background color should never be seen */
  139.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  140.     lib_output_background_color( back_color ) ;
  141.  
  142.     /* output viewpoint */
  143.     SET_COORD3( from, -1.0, -spread, 0.5 ) ;
  144.     SET_COORD3( at, from[X], from[Y] + 1.0, from[Z] ) ;
  145.     SET_COORD3( up, 0.0, 0.0, 1.0 ) ;
  146.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 1.0, 512, 512);
  147.  
  148.     /* output light source */
  149.     lscale = (raytracer_format == OUTPUT_NFF ||
  150.           raytracer_format == OUTPUT_RTRACE ? 1.0 : 1.0/sqrt(3.0));
  151.     SET_COORD4( light, 3.0, -spread, 3.0, lscale ) ;
  152.     lib_output_light( light ) ;
  153.     SET_COORD4( light, -4.0, -spread, 1.0, lscale ) ;
  154.     lib_output_light( light ) ;
  155.     SET_COORD4( light, 2.0, -spread, -4.0, lscale ) ;
  156.     lib_output_light( light ) ;
  157.  
  158.     /* output wall polygon - white */
  159.     SET_COORD3( back_color, 1.0, 1.0, 1.0 ) ;
  160.     lib_output_color(NULL, back_color, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  161.  
  162.     /* just spans 45 degree view + 1% */
  163.     wvec[Y] = y_diff * ( size_factor + 1 ) ;
  164.     wvec[X] = wvec[Z] = 1.01 * ( wvec[Y] - from[Y] ) * tan( PI / 8.0 ) ;
  165.     SET_COORD3( wall[0],  wvec[X]+from[X], wvec[Y],  wvec[Z]+from[Z] ) ;
  166.     SET_COORD3( wall[1], -wvec[X]+from[X], wvec[Y],  wvec[Z]+from[Z] ) ;
  167.     SET_COORD3( wall[2], -wvec[X]+from[X], wvec[Y], -wvec[Z]+from[Z] ) ;
  168.     SET_COORD3( wall[3],  wvec[X]+from[X], wvec[Y], -wvec[Z]+from[Z] ) ;
  169.     lib_output_polygon( 4, wall ) ;
  170.  
  171.     /* set up ring colors - RGB and complements */
  172.     SET_COORD3( ring_color[0], 1.0, 0.0, 0.0 ) ;
  173.     SET_COORD3( ring_color[1], 0.0, 1.0, 0.0 ) ;
  174.     SET_COORD3( ring_color[2], 0.0, 0.0, 1.0 ) ;
  175.     SET_COORD3( ring_color[3], 0.0, 1.0, 1.0 ) ;
  176.     SET_COORD3( ring_color[4], 1.0, 0.0, 1.0 ) ;
  177.     SET_COORD3( ring_color[5], 1.0, 1.0, 0.0 ) ;
  178.  
  179.     create_dodec( radius, dodec ) ;
  180.     /* radius of osculating cylinders and spheres (no derivation given) */
  181.     base_pt[W] = apex_pt[W] = radius ;
  182.  
  183.     for ( num_depth = 0 ; num_depth < size_factor ; ++num_depth ) {
  184.     offset[Y] = y_diff * (double)(num_depth+1) ;
  185.     for ( num_objz = 0 ; num_objz <= num_depth ; ++num_objz ) {
  186.         offset[Z] = xz_diff * (double)(2*num_objz - num_depth) ;
  187.         for ( num_objx = 0 ; num_objx <= num_depth ; ++num_objx ) {
  188.         offset[X] = xz_diff * (double)(2*num_objx - num_depth) ;
  189.         for ( num_elem = 0 ; num_elem < 30 ; ++num_elem ) {
  190.             PLATFORM_MULTITASK();
  191.             COPY_COORD3( base_pt, dodec[num_elem] ) ;
  192.             ADD2_COORD3( base_pt, offset ) ;
  193.             if ( num_elem%5 == 0 ) {
  194.             prev_elem = num_elem + 4 ;
  195.             /* new ring beginning - output color */
  196.             lib_output_color(NULL, ring_color[num_elem/5],
  197.                      0.0, 0.5, 0.2, 0.3, 10.0, 0.0, 0.0);
  198.             }
  199.             else {
  200.             prev_elem = num_elem - 1 ;
  201.             }
  202.             COPY_COORD3( apex_pt, dodec[prev_elem] ) ;
  203.             ADD2_COORD3( apex_pt, offset ) ;
  204.  
  205.             lib_output_cylcone( base_pt, apex_pt, output_format ) ;
  206.             lib_output_sphere( base_pt, output_format ) ;
  207.         }
  208.         }
  209.     }
  210.     }
  211.  
  212.     lib_close();
  213.  
  214.     PLATFORM_SHUTDOWN();
  215.     return EXIT_SUCCESS;
  216. }
  217.