/* cc -o rman_wave rman_wave.c -L /usr/local/sof3/pixar/prman-11.0/lib -lrib -ltarget -lzip -lprmutil -lm   					*/
/*								*/
/*	Renderman version of a simple wave propogation		*/
/*				F.I.Parke  3 March 2003		*/
/*								*/
/*	Link with:	-lrib -ltarget -lzip lprmutil -lm	*/
/*								*/
#include "stdio.h"
#include "/usr/local/sof3/pixar/prman-11.0/include/ri.h"
#include "device.h"
#include "math.h"

void profile(int frame, float ht, float spacing, float prof[][2])
	{
	float ang = 3.14159/18.0;
	int i;

	for (i=0; i<100; i++)
	   {
	    prof[i][1] = (ht * (99-i)/99.) * cos(ang*(i+frame)); 
	    /* prof[i][1] = ht * cos(ang*(i+frame)); */
	    prof[i][0] = spacing * i;	
	   };
	}

void bands(int bd, float prof[][2])

/*	routine which uses the profile results to	*/
/*	create the wave polygons			*/
/*							*/
	{
	 int i,j,n;
	 float ang = 3.14159/18.0;

	 RtPoint Band [4];

	 for (i=0; i<=36; i++)
	   {
	    Band[0][0] = prof[bd+1][0] * sin(ang*i);
 	    Band[0][2] = prof[bd+1][0] * cos(ang*i);
	    Band[1][0] = prof[bd][0] * sin(ang*i);
 	    Band[1][2] = prof[bd][0] * cos(ang*i);

	    Band[3][0] = prof[bd+1][0] * sin(ang*(i+1));
 	    Band[3][2] = prof[bd+1][0] * cos(ang*(i+1));
	    Band[2][0] = prof[bd][0] * sin(ang*(i+1));
 	    Band[2][2] = prof[bd][0] * cos(ang*(i+1));

	    Band[1][1] = Band[2][1] = prof[bd][1];
	    Band[0][1] = Band[3][1] = prof[bd+1][1];
	    
	    RiPolygon((RtInt) 4, RI_P, (RtPointer) Band, RI_NULL);				    };
	}

/*	routine which displays a new wave image		*/

void show (int frame)
      {
	int i;

	float prof[100][2];
	float ht = 20.0;
	float spacing = 1.0;
	RtFloat kd = .8;
	RtFloat ka = .2;
	RtFloat ks = .8;
	RtFloat fov = 60;
	RtColor bcolor;
	float ang = 3.14159/18.0;

/*	Initialize Frame	*/

	RiFrameBegin(frame);

	RiProjection("perspective", RI_FOV, (RtPointer)&fov, RI_NULL);
	RiDisplay("window", RI_FRAMEBUFFER, RI_RGBA, RI_NULL);
	RiFormat((RtInt) 640, (RtInt) 480, 1.0);

	RiTranslate(0, 0, 150);
	RiRotate(-60, 1, 0, 0);

	RiWorldBegin(); 
	
	RiLightSource("ambientlight", RI_NULL);

	RiTransformBegin();
	RiRotate(60, 0, 1, 0); 
	RiLightSource("distantlight",RI_NULL);
	RiTransformEnd();

	RiSurface("plastic","Ka",(RtPointer)&ka,"Kd",(RtPointer)&kd,
					"Ks",(RtPointer)&ks, RI_NULL);

	/*	display the wave	*/
	/*	output wave rings	*/

	profile(frame, ht, spacing, prof);

        for(i=0; i<99; i++)
	  { 

	/* compute band color */

	   bcolor[0] = .5 + .5*sin(ang*((i+frame)%36));
	   bcolor[1] = bcolor[2] = 1.0;

	   RiColor(bcolor); 
	   bands(i, prof); 
	  }; 

	RiWorldEnd();
	RiFrameEnd();

	}


void main(void)
	{
        int frame = 0;

	/*	Initialize Renderman		*/

	RiBegin(RI_NULL); 

	/*	MAIN LOOP			*/

	while(frame < 36)  /*  want 36 frame cycle  */
		{ 
		  show(frame);
		  frame++;	
                };
	RiEnd();
        }
