home *** CD-ROM | disk | FTP | other *** search
/ Using VRML (Special Edition) / Special_Edition_Using_VRML_CDROM_Que_1996.iso / webpages / objects / chap22 / trees / forest.c next >
C/C++ Source or Header  |  1995-11-02  |  3KB  |  89 lines

  1. /* Generate a random VRML forest */
  2.  
  3. /* Written by Bernie Roehl, October 1995 */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. float placement_threshold = 0.75;  /* if rand is greater than this, place a tree */
  10. float autumn_threshold = 0.75;     /* if rand is greater than this, use fall colors */
  11.  
  12. int nrows = 32, ncols = 32;   /* number of rows and columns in the grid */
  13.  
  14. float horizontal_spacing = 5, vertical_spacing = 5;  /* sizes of the cells */
  15.  
  16. /* process command-line arguments */
  17. void procarg(char *arg)
  18.     {
  19.     switch (*arg++)
  20.         {
  21.         case 'r': nrows = atoi(arg); break;
  22.         case 'c': ncols = atoi(arg); break;
  23.         case 'a': autumn_threshold = atof(arg); break;
  24.         case 'p': placement_threshold = atof(arg); break;
  25.         case 'h': horizontal_spacing = atof(arg); break;
  26.         case 'v': vertical_spacing = atof(arg); break;
  27.         case '?':
  28.             fprintf(stderr, "Usage: forest [options]\noptions include:\n\t-rrows -ccols -aautumn_threshold -pplacement_threshold\n\t-hhorizontal_spacing -vvertical_spacing -?\n");
  29.             exit(0);
  30.             break;
  31.         default: fprintf(stderr, "Warning: unrecognized option\n"); break;
  32.         }
  33.     }
  34.  
  35. /* create a tree */
  36. void make_tree(int row, int col)
  37.     {
  38.     float size_factor = (rand() / (float) RAND_MAX) * (2.0 - 0.25) + 0.25;
  39.     printf("\tSeparator {\n");
  40.     printf("\t\tTransform {\n");
  41.     printf("\t\t\tscaleFactor %f %f %f\n", size_factor, size_factor, size_factor);
  42.     printf("\t\t\trotation 0 1 0 %f\n", (rand() * 2 * 3.1415926) / RAND_MAX);
  43.     printf("\t\t\ttranslation %f 0 %f\n",
  44.         horizontal_spacing * (col + rand() / (float) RAND_MAX - 0.5),
  45.         vertical_spacing * (row + rand() / (float) RAND_MAX - 0.5));
  46.     printf("\t\t}\n");
  47.     /* determine which set of colors to use */
  48.     if ((rand() / (float) RAND_MAX) > autumn_threshold)
  49.         printf("\t\tUSE spring_material\n");
  50.     else
  51.         printf("\t\tUSE autumn_material\n");
  52.     printf("\t\tUSE tree\n");
  53.     printf("\t}\n");
  54.     }
  55.  
  56. void main(int argc, char *argv[])
  57.     {
  58.     int i, j;
  59.     /* start by processing the command-line arguments */
  60.     while (argc > 1)
  61.         {
  62.         if (*argv[1] == '-')
  63.             {
  64.             procarg(&argv[1][1]);
  65.             --argc;
  66.             ++argv;
  67.             }
  68.         }
  69.     /* put out header */
  70.     printf("#VRML V1.0 ascii\n\n# Generated by FOREST\n\nSeparator {\n");
  71.  
  72.     printf("\tPerspectiveCamera { position %f %f %f }\n",
  73.         ncols * horizontal_spacing / 2, 12.0, nrows * vertical_spacing * 1.5);
  74.  
  75.     /* define (but don't instance!) a tree */
  76.     printf("\tSwitch {\n\t\twhichChild -1\n\t\tDEF tree WWWInline { name \"tree1.wrl\" }\n\t}\n");
  77.  
  78.     /* define the two sets of colors */
  79.     printf("\tDEF spring_material Material { diffuseColor [ 0.0 0.666667 0.0, 0.466667 0.466667 0.0 ] }\n");
  80.     printf("\tDEF autumn_material Material { diffuseColor [ 0.25 0.25 0.0, 0.466667 0.466667 0.0 ] }\n");
  81.  
  82.     /* go through the array, putting out trees when appropriate */
  83.     for (i = 0; i < nrows; ++i)
  84.         for (j = 0; j < ncols; ++j)
  85.             if ((rand() / (float) RAND_MAX) > placement_threshold)
  86.                 make_tree(i, j);
  87.     printf("}\n");
  88.     }
  89.