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

int
main( argc, argv, envp )
int argc;
char * argv[];
char * envp[];
{
	float f;                   /* Current function value                    */
	float y;                   /* Scaled function value (for box height)    */
	float y_half;              /* Half height of a box (for translation)    */
	float h;                   /* Height of each box (computed from y)      */
	float w = 1.0;             /* Width of each box                         */
	float d = 1.0;             /* Depth of each box                         */
	float x0 = 0.0;            /* Starting function evaluation bound        */
	float x1 = 3.14159 * 2.0;  /* Ending function evaluation bound          */
	float x  = 0.0;            /* Current function evaluation point         */
	int samples = 30;          /* How many boxes to build inside the bounds */
	int sample;                /* Current box being built                   */

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

	/* Output a VRML header */

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

	/* Generate a series of boxes representing a function */

	for ( sample=0; sample<samples; sample++ )
	{

		/* Figure out where to evaluate the function then compute the value */

		x = x0 + ((x1 - x0)/(float)samples) * sample;   /* Evaluation point */
		f = sin( x );      /* Calculate the raw function value              */
		y = 5.0 * f;       /* Scale the function value to make it look nice */
		y_half = y / 2.0;  /* Half height of a box (for translation)        */
		h = fabs( y );     /* Box height must be positive !                 */



		/* Output a VRML comment */

		printf( "# Sample %d of %d\n", sample, samples-1 );



		/* Output a positioned box */

		printf( "Transform {\n" );
			printf( "\ttranslation %f %f %f\n", w*sample, y_half, 0 );
			printf( "\tchildren [\n" );
				printf( "\t\tShape {\n" );


					/* Apply a diffuse color to the box */
					r = fabs( f );  /* Color components must be >= 0 */
					g = 0.0;
					b = 1.0 - r;    /* Make a ramp from red to blue */
					printf( "\t\t\tappearance Appearance {\n" );
					printf( "\t\t\t\tmaterial Material {\n" );
						printf( "\t\t\t\t\tdiffuseColor %f %f %f\n", r, g, b );
						printf( "\t\t\t\t}\n" );
					printf( "\t\t\t}\n" );


					/* Generate the box's geometry */
					printf( "\t\t\tgeometry Box {\n" );
						printf( "\t\t\t\tsize %f %f %f\n", w, h, d );
					printf( "\t\t\t}\n" );

				printf( "\t\t}\n" );
			printf( "\t]\n" );
		printf( "}\n" );

		/* Output a text label */

		printf( "Transform {\n" );
			if ( y >= 0 )
				printf( "\ttranslation %f %f %f\n", w*sample, y+(w/2.1), 0 );
			else
				printf( "\ttranslation %f %f %f\n", w*sample, y-(w/2.1), 0 );
			printf( "\tchildren [\n" );

			/* TASK3 */

			/* Put each text label inside a Billboard node */

				printf( "Billboard {\n" );
					printf( "\taxisOfRotation 0.0 0.0 0.0\n" );
					printf( "\tchildren [\n" );

						printf( "\t\tShape {\n" );
							printf( "\t\t\tappearance Appearance {\n" );
								printf( "\t\t\t\tmaterial Material {\n" );
								printf( "\t\t\t\t\tdiffuseColor 0.8 0.8 0\n" );
								printf( "\t\t\t\t}\n" );
							printf( "\t\t\t}\n" );
							printf( "\t\t\tgeometry Text {\n" );
								printf( "\t\t\t\tstring \"%3.1f\"\n", y );
								printf( "\t\t\t\tfontStyle FontStyle {\n" );
									printf( "\t\t\t\tsize %f\n", w/2.2 );
									printf( "\t\t\t\tjustify [ \"MIDDLE\", \"MIDDLE\" ]\n" );
								printf( "\t\t\t\t}\n" );

							printf( "\t\t\t}\n" );
						printf( "\t\t}\n" );

					printf( "\t\t]\n" );
				printf( "\t\t}\n" );

			printf( "\t]\n" );
		printf( "}\n" );

		printf( "\n" );
	}
}



