
#include <stdio.h>
#include <math.h>

int
main( argc, argv, envp )
int argc;
char * argv[];
char * envp[];
{
	float ** f;                 /* 2D array to hold function values         */
	float ** y;                 /* 2D array to hold scaled function values  */
	float w = 1.0;              /* Width of each cell                       */
	float d = 1.0;              /* Depth of each cell                       */
	float x0 = -3.14159 * 2.0;  /* Starting function evaluation bound in X  */
	float x1 = +3.14159 * 2.0;  /* Ending function evaluation bound in X    */
	float z0 = -3.14159 * 2.0;  /* Starting function evaluation bound in Z  */
	float z1 = +3.14159 * 2.0;  /* Ending function evaluation bound in Z    */
	float x;                    /* Current function evaluation point        */
	float z;                    /* Current function evaluation point        */
	int samples = 30;           /* # points to generate in each dimension   */
	int sample_x;               /* Current cell being built in X            */
	int sample_z;               /* Current cell being built in Z            */
	int i;

	/* TASK1 */
	float r;  /* Red color component   */
	float g;  /* Green color component */
	float b;  /* Blue color component  */


	/* Allocate storage for the terrain function values */

	f = (float**) malloc( samples * sizeof( float ) );
	y = (float**) malloc( samples * sizeof( float ) );
	for ( i=0; i<samples; i++ )
	{
		f[i] = (float*) malloc( samples * sizeof( float ) );
		y[i] = (float*) malloc( samples * sizeof( float ) );
	}


	/* Calculate all of the function values we need for the terrain */

	for ( sample_x=0; sample_x<samples; sample_x++ )
	{
		for ( sample_z=0; sample_z<samples; sample_z++ )
		{
			/* Where do we evaluate the function then compute the value ?  */
			x = x0 + ((x1 - x0)/(float)samples) * sample_x;
			z = z0 + ((z1 - z0)/(float)samples) * sample_z;
			f[sample_x][sample_z] = sin( x ) * sin( z );       /* Raw value */
			y[sample_x][sample_z] = 5.0 * f[sample_x][sample_z];  /* Scaled */
		}
	}


	/* Output a VRML header */

	printf( "#VRML V2.0 utf8\n" );
	printf( "\n" );

	/* Generate an ElevationGrid representing the function */

	printf( "Shape {\n" );

		printf( "\tappearance Appearance {\n" );
		printf( "\t\tmaterial Material {}\n" );
		printf( "\t}\n" );

		/* Build the ElevationGrid geometry */

		printf( "geometry ElevationGrid {\n" );
			printf( "\txDimension %d\n", samples );
			printf( "\tzDimension %d\n", samples );
			printf( "\txSpacing %f\n", w );
			printf( "\tzSpacing %f\n", d );
			printf( "\tsolid FALSE\n" );
			printf( "\tcreaseAngle 0.785\n" );
			printf( "\theight [\n" );
			for ( sample_x=0; sample_x<samples; sample_x++ )
			{
				printf( "\t\t" );
				for ( sample_z=0; sample_z<samples; sample_z++ )
				{
					printf( "%3.2f, ", y[sample_x][sample_z] );
				}
				printf( "\n" );
			}
			printf( "\t]\n" );


			/* TASK1 */

			/* Build the ElevationGrid colors */

			printf( "\tcolorPerVertex TRUE\n" );
			printf( "\tcolor Color {\n" );
				printf( "\t\tcolor [\n" );
				for ( sample_x=0; sample_x<samples; sample_x++ )
				{
					for ( sample_z=0; sample_z<samples; sample_z++ )
					{
						r = fabs( f[sample_x][sample_z] );
						g = 0.0;
						b = 1.0 - r;    /* Make a ramp from red to blue */
						printf( "\t\t\t%3.2f %3.2f %3.2f,\n", r, g, b );
					}
					printf( "\t\t\t# sample_x %d\n", sample_x );
				}
				printf( "\t]\n" );
			printf( "\t}\n" );



		printf( "}\n" );

	printf( "}\n" );

}

