home *** CD-ROM | disk | FTP | other *** search
- /* Generate a random VRML forest */
-
- /* Written by Bernie Roehl, October 1995 */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- float placement_threshold = 0.75; /* if rand is greater than this, place a tree */
- float autumn_threshold = 0.75; /* if rand is greater than this, use fall colors */
-
- int nrows = 32, ncols = 32; /* number of rows and columns in the grid */
-
- float horizontal_spacing = 5, vertical_spacing = 5; /* sizes of the cells */
-
- /* process command-line arguments */
- void procarg(char *arg)
- {
- switch (*arg++)
- {
- case 'r': nrows = atoi(arg); break;
- case 'c': ncols = atoi(arg); break;
- case 'a': autumn_threshold = atof(arg); break;
- case 'p': placement_threshold = atof(arg); break;
- case 'h': horizontal_spacing = atof(arg); break;
- case 'v': vertical_spacing = atof(arg); break;
- case '?':
- fprintf(stderr, "Usage: forest [options]\noptions include:\n\t-rrows -ccols -aautumn_threshold -pplacement_threshold\n\t-hhorizontal_spacing -vvertical_spacing -?\n");
- exit(0);
- break;
- default: fprintf(stderr, "Warning: unrecognized option\n"); break;
- }
- }
-
- /* create a tree */
- void make_tree(int row, int col)
- {
- float size_factor = (rand() / (float) RAND_MAX) * (2.0 - 0.25) + 0.25;
- printf("\tSeparator {\n");
- printf("\t\tTransform {\n");
- printf("\t\t\tscaleFactor %f %f %f\n", size_factor, size_factor, size_factor);
- printf("\t\t\trotation 0 1 0 %f\n", (rand() * 2 * 3.1415926) / RAND_MAX);
- printf("\t\t\ttranslation %f 0 %f\n",
- horizontal_spacing * (col + rand() / (float) RAND_MAX - 0.5),
- vertical_spacing * (row + rand() / (float) RAND_MAX - 0.5));
- printf("\t\t}\n");
- /* determine which set of colors to use */
- if ((rand() / (float) RAND_MAX) > autumn_threshold)
- printf("\t\tUSE spring_material\n");
- else
- printf("\t\tUSE autumn_material\n");
- printf("\t\tUSE tree\n");
- printf("\t}\n");
- }
-
- void main(int argc, char *argv[])
- {
- int i, j;
- /* start by processing the command-line arguments */
- while (argc > 1)
- {
- if (*argv[1] == '-')
- {
- procarg(&argv[1][1]);
- --argc;
- ++argv;
- }
- }
- /* put out header */
- printf("#VRML V1.0 ascii\n\n# Generated by FOREST\n\nSeparator {\n");
-
- printf("\tPerspectiveCamera { position %f %f %f }\n",
- ncols * horizontal_spacing / 2, 12.0, nrows * vertical_spacing * 1.5);
-
- /* define (but don't instance!) a tree */
- printf("\tSwitch {\n\t\twhichChild -1\n\t\tDEF tree WWWInline { name \"tree1.wrl\" }\n\t}\n");
-
- /* define the two sets of colors */
- printf("\tDEF spring_material Material { diffuseColor [ 0.0 0.666667 0.0, 0.466667 0.466667 0.0 ] }\n");
- printf("\tDEF autumn_material Material { diffuseColor [ 0.25 0.25 0.0, 0.466667 0.466667 0.0 ] }\n");
-
- /* go through the array, putting out trees when appropriate */
- for (i = 0; i < nrows; ++i)
- for (j = 0; j < ncols; ++j)
- if ((rand() / (float) RAND_MAX) > placement_threshold)
- make_tree(i, j);
- printf("}\n");
- }
-