/* cc -o cc_anim cc_anim.c -L /usr/local/sof3/pixar/prman-11.0/lib -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"


/* Create a Unit Cube object	*/

void UnitCube()
   {
	static RtPoint square[4] = { {.5,.5,.5}, {-.5,.5,.5},
				     {-.5,-.5,.5},{.5,-.5,.5}};

	/* far square	*/
	RiPolygon(4,RI_P, square, RI_NULL);

	/* right face	*/
	RiRotate(90.0,0.0,1.0,0.0);
	RiPolygon(4,RI_P, square, RI_NULL);

	/* near face	*/
	RiRotate(90.0,0.0,1.0,0.0);
	RiPolygon(4,RI_P, square, RI_NULL);

	/* left face	*/
	RiRotate(90.0,0.0,1.0,0.0);
	RiPolygon(4,RI_P, square, RI_NULL);

	/* bottom face	*/
	RiRotate(90.0,1.0,0.0,0.0);
	RiPolygon(4,RI_P, square, RI_NULL);

	/* top face	*/
	RiRotate(180.0,1.0,0.0,0.0);
	RiPolygon(4,RI_P, square, RI_NULL);
  }

/* ColorCube() - create a color cube from smaller cubes	*/

void ColorCube(int n, float s)
  {
	int x, y, z;
	RtColor color;

	if(n<=0) return;

	RiAttributeBegin();
		RiTranslate(-.5,-.5,-.5);
		RiScale(1.0/n, 1.0/n, 1.0/n);

		for(x=0; x<n; x++)
		   for(y=0; y<n; y++)
			for(z=0; z<n; z++)
			  {
			   color[0] = ((float) x+1)/((float)n);
			   color[1] = ((float) y+1)/((float)n);
			   color[2] = ((float) z+1)/((float)n);
			   RiColor(color);
			   RiTransformBegin();
				RiTranslate(x+.5, y+.5, z+.5);
				RiScale(s, s, s);
				UnitCube();
	   		   RiTransformEnd();
			  };
	RiAttributeEnd();
  }

/* Simple Cubes Animation	*/

#define NFRAMES  10	/* number of animation frames	*/
#define NCUBES   5	/* number of minicubes per side	*/
#define FRAMEROT 5.0	/* # of degrees to rotate each frame	*/

main()
  {
	int frame;
	float scale;
	char filename[20];

	RiBegin(RI_NULL);	/* star the renderer	*/

	RiLightSource("distantlight", RI_NULL);

	RiProjection("perspective",RI_NULL);
	RiTranslate(0.0,0.0,1.5);
	RiRotate(40.0,-1.0,1.0,0.0);

	for(frame=1; frame<=NFRAMES; frame++)
	  {  sprintf(filename,"anim%d.pic",frame);
	     RiFrameBegin(frame);
		RiDisplay(filename,RI_FILE,RI_RGBA,RI_NULL);
	        RiWorldBegin();
		   scale = (float)(NFRAMES -(frame-1))/(float)NFRAMES;
		   RiRotate(FRAMEROT*frame, 0.0,0.0,1.0);
		   RiSurface("matte", RI_NULL);
		   ColorCube(NCUBES,scale);
		RiWorldEnd();
	     RiFrameEnd();
	  };
	RiEnd();
   }
		
