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 / tree.c < prev   
C/C++ Source or Header  |  1991-01-13  |  5KB  |  175 lines

  1. /*
  2.  * tree.c - Creates a tree using Aono & Kunii's generation method.
  3.  *    (See IEEE CG&A May 1984).  A square polygon is placed beneath the
  4.  *    tree to act as a field.  Seven light sources.
  5.  *
  6.  * Author:  Eric Haines, 3D/Eye, Inc.
  7.  *
  8.  * SizeFactor determines the number of objects output.
  9.  *    Total objects = 2**(SF+1)-1 cones and spheres + 1 square polygon.
  10.  *
  11.  *    SizeFactor    # spheres       # cones    # squares
  12.  *         1             3               3         1
  13.  *         2             7               7         1
  14.  *         3            15              15         1
  15.  *
  16.  *        11          4095            4095         1
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include "def.h"
  22. #include "lib.h"
  23.  
  24. static    int    SizeFactor = 11 ;
  25.  
  26. #define    OUTPUT_FORMAT        OUTPUT_CURVES
  27.  
  28. /* the following affect the shape of the tree */
  29. #define    BR_ANGLE_0        40.0
  30. #define    BR_ANGLE_1        25.0
  31. #define    BR_CONTR_0        0.65
  32. #define    BR_CONTR_1        0.70
  33. #define    BR_DIAMETER        0.67
  34. #define    DIV_ANGLE        140.0
  35. #define    WIDTH_HEIGHTH_RATIO    0.15
  36.  
  37. static    MATRIX    Rst_mx[2] ;
  38.  
  39. main(argc,argv)
  40. int    argc ;
  41. char    *argv[] ;
  42. {
  43. COORD3    field[4] ;
  44. COORD3    from, at, up ;
  45. COORD3    light ;
  46. COORD3    back_color, tree_color ;
  47.  
  48.     if ( !lib_get_size( argc, argv, &SizeFactor ) ) {
  49.     fprintf( stderr, "usage: %s [size]\n", *argv ) ;
  50.     exit(EXIT_FAIL) ;
  51.     }
  52.  
  53.     /* output viewpoint */
  54.     SET_COORD3( from, 4.5, 0.4, 2.0 ) ;
  55.     SET_COORD3( at, 0.0, 0.0, 1.5 ) ;
  56.     SET_COORD3( up, 0.0, 0.0, 1.0 ) ;
  57.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 512, 512 ) ;
  58.  
  59.     /* output background color - UNC sky blue */
  60.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  61.     lib_output_background_color( back_color ) ;
  62.  
  63.     /* output light source */
  64.     SET_COORD3( light, -5.0, 5.0, 50.0 ) ;
  65.     lib_output_light( light ) ;
  66.     SET_COORD3( light, 30.0, -30.0, 30.0 ) ;
  67.     lib_output_light( light ) ;
  68.     SET_COORD3( light, -40.0, -30.0, 20.0 ) ;
  69.     lib_output_light( light ) ;
  70.     SET_COORD3( light, 10.0, 30.0, 40.0 ) ;
  71.     lib_output_light( light ) ;
  72.     SET_COORD3( light, -30.0, 40.0, 10.0 ) ;
  73.     lib_output_light( light ) ;
  74.     SET_COORD3( light, 50.0, 25.0, 20.0 ) ;
  75.     lib_output_light( light ) ;
  76.     SET_COORD3( light, -10.0, -60.0, 30.0 ) ;
  77.     lib_output_light( light ) ;
  78.  
  79.     /* output field polygon - green */
  80.     SET_COORD3( back_color, 0.2, 0.7, 0.2 ) ;
  81.     lib_output_color( back_color, 1.0, 0.0, 0.0, 0.0, 0.0 ) ;
  82.     SET_COORD3( field[0],  50.0,  50.0, 0.0 ) ;
  83.     SET_COORD3( field[1], -50.0,  50.0, 0.0 ) ;
  84.     SET_COORD3( field[2], -50.0, -50.0, 0.0 ) ;
  85.     SET_COORD3( field[3],  50.0, -50.0, 0.0 ) ;
  86.     lib_output_polygon( 4, field ) ;
  87.  
  88.     /* set up tree color - brown */
  89.     SET_COORD3( tree_color, 0.55, 0.4, 0.2 ) ;
  90.     lib_output_color( tree_color, 1.0, 0.0, 0.0, 0.0, 0.0 ) ;
  91.  
  92.     /* create tree */
  93.     create_tree() ;
  94.  
  95.     exit(EXIT_SUCCESS) ;
  96. }
  97.  
  98. /*
  99.  * Set up matrices for growth of each branch with respect to the
  100.  * parent branch, then grow each branch.
  101.  */
  102. create_tree()
  103. {
  104. int    i ;
  105. double    branch_angle, branch_contraction, divergence ;
  106. MATRIX    ident_mx, temp1_mx, temp2_mx, tempr_mx, tempst_mx ;
  107.  
  108.     for ( i = 0 ; i < 2 ; ++i ) {
  109.     if ( i == 0 ) {
  110.         branch_angle = BR_ANGLE_0 ;
  111.         divergence = 90.0 ;
  112.         branch_contraction = BR_CONTR_0 ;
  113.     }
  114.     else {
  115.         branch_angle = BR_ANGLE_1 ;
  116.         divergence = DIV_ANGLE + 90.0 ;
  117.         branch_contraction = BR_CONTR_1 ;
  118.     }
  119.  
  120.     /* rotate along X axis by branching angle */
  121.     lib_create_rotate_matrix( temp1_mx, X_AXIS, branch_angle*PI/180.0 ) ;
  122.  
  123.     /* rotate along Z axis by divergence angle */
  124.     lib_create_rotate_matrix( temp2_mx, Z_AXIS, divergence*PI/180.0 ) ;
  125.  
  126.     lib_matrix_multiply( tempr_mx, temp1_mx, temp2_mx ) ;
  127.  
  128.     /* include translation of branch, scaled */
  129.     lib_create_identity_matrix( tempst_mx ) ;
  130.     tempst_mx[0][0] = tempst_mx[1][1] = tempst_mx[2][2] =
  131.                             branch_contraction ;
  132.     tempst_mx[3][2] = 1.0 ;
  133.  
  134.     /* concatenate */
  135.     lib_matrix_multiply( Rst_mx[i], tempr_mx, tempst_mx ) ;
  136.     }
  137.  
  138.     /* set up initial matrix */
  139.     lib_create_identity_matrix( ident_mx ) ;
  140.     grow_tree( ident_mx, 1.0, SizeFactor ) ;
  141. }
  142.  
  143. /* grow tree branches recursively */
  144. grow_tree( cur_mx, scale, depth )
  145. MATRIX    cur_mx ;
  146. double    scale ;
  147. int    depth ;
  148. {
  149. int    i ;
  150. COORD3    vec ;
  151. COORD4    apex, base ;
  152. MATRIX    new_mx ;
  153.  
  154.     /* output branch */
  155.     SET_COORD3( vec, 0.0, 0.0, 0.0 ) ;
  156.     lib_transform_point( base, vec, cur_mx ) ;
  157.     base[W] = scale * WIDTH_HEIGHTH_RATIO ;
  158.  
  159.     SET_COORD3( vec, 0.0, 0.0, 1.0 ) ;
  160.     lib_transform_point( apex, vec, cur_mx ) ;
  161.     apex[W] = base[W] * BR_DIAMETER ;
  162.  
  163.     lib_output_cylcone( base, apex, OUTPUT_FORMAT ) ;
  164.     lib_output_sphere( apex, OUTPUT_FORMAT ) ;
  165.  
  166.     if ( depth > 0 ) {
  167.     --depth ;
  168.  
  169.     for ( i = 0 ; i < 2 ; ++i ) {
  170.         lib_matrix_multiply( new_mx, Rst_mx[i], cur_mx ) ;
  171.         grow_tree( new_mx, scale * BR_DIAMETER, depth ) ;
  172.     }
  173.     }
  174. }
  175.