// Cube.tra
// Author - MrC
// Based on FadeCube.r4 By Gordon Williams

int x,y;
float c,tm,td,ftime,fric;
float[] c_tx(64),c_ty(64),c_x(64),c_y(64),c_z(64),c_vx(64),c_vy(64),c_vz(64),c_rx(64),c_ry(64);
const float dx = 0.125;
const float dy = 0.125;

void Init()
{
	reset();
}

void makenew(int n)
{
   c_tx[n]=(n%8)/8.0;
   c_ty[n]=(n-(n%8))/64.0;
   c_x[n] = (c_tx[n]-0.5)*2+dx;
   c_y[n] = (c_ty[n]-0.5)*2+dy;
   c_z[n] = 0;
   c_vx[n] = (c_x[n]+Rand()-0.5);
   c_vy[n] = (c_y[n]+Rand());
   c_vz[n] = -(1.0+Rand());   
   c_rx[n] = (1.0+Rand())*160;
   c_ry[n] = (1.0+Rand())*160;
}

void drawcube(int n)
{
   gfxBegin(PRIM_QUADLIST);
   // FRONT FACE
   gfxTexCoord(c_tx[n],c_ty[n]);
   gfxVertex(-1,-1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(1,-1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(1,1,-1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(-1,1,-1);
   // BACK
   gfxTexCoord(c_tx[n],1-(c_ty[n]));
   gfxVertex(-1,-1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(1,-1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(1,1,1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(-1,1,1);
   // LEFT
   gfxTexCoord(c_tx[n],1-(c_ty[n]));
   gfxVertex(-1,-1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(-1,-1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(-1,1,1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(-1,1,-1);
   // RIGHT
   gfxTexCoord(c_tx[n],1-(c_ty[n]));
   gfxVertex(1,-1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(1,-1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(1,1,1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(1,1,-1);
   // UP
   gfxTexCoord(c_tx[n],1-(c_ty[n]));
   gfxVertex(1,-1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(1,-1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(-1,-1,1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(-1,-1,-1);
   // DOWN
   gfxTexCoord(c_tx[n],1-(c_ty[n]));
   gfxVertex(1,1,-1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]));
   gfxVertex(1,1,1);
   gfxTexCoord(c_tx[n]+dx,1-(c_ty[n]+dy));
   gfxVertex(-1,1,1);
   gfxTexCoord(c_tx[n],1-(c_ty[n]+dy));
   gfxVertex(-1,1,-1);

   gfxEnd();
}

void reset()
{
   for (x=0;x<64;x=x+1)
   {
      makenew(x);
   }
   tm = 0;
   ftime = 0;
}

void Render()
{
	gfxPushMatrix();
	gfxTranslate(0, 0, 2.414);
	gfxSetAspect(0);
	gfxSetTexture(TEXTURE_NEXTPRESET);
	gfxColour(1, 1, 1, 1);
	gfxTexRect(-1, 1, 1, -1);
	gfxPopMatrix();
	gfxSetAspect(1);
	gfxSetTexture(TEXTURE_CURRPRESET);

	if (ftime<0.1)
		td = TIMEPASS*(ftime*10);
	else
		td = TIMEPASS;
	tm = tm+td;

	ftime = ftime + (TIMEPASS*0.225);
	if (ftime>1)
	{
		ftime=1;
		FINISHED = true;
		return;
	}

	gfxPushMatrix();

	gfxSetAspect(0);
	gfxScale(-1,1,1);
	gfxTranslate(0,0,2.414);
	gfxTranslate(0,0,dx);

	gfxRotate(180,0,1,0);


	fric = Pow(0.75,td);


	for (x=0;x<64;x=x+1)
	{
		c_vy[x] = c_vy[x] - (td*2);
		c_x[x] = c_x[x] + (td*c_vx[x]);
		c_y[x] = c_y[x] + (td*c_vy[x]);
		c_z[x] = c_z[x] + (td*c_vz[x]);
		c_vx[x] = c_vx[x]*fric;
		c_vy[x] = c_vy[x]*fric;
		c_vz[x] = c_vz[x]*fric;

		gfxPushMatrix();
		gfxTranslate(c_x[x],c_y[x],c_z[x]);
		gfxRotate(c_rx[x]*tm,1,0,0);
		gfxRotate(c_ry[x]*tm,0,1,0);
		gfxScale(dx,dx,dx);
		drawcube(x);

		gfxPopMatrix();

	}

	gfxPopMatrix();
}