// SpectrumCylinder.vtx
// Author - MrC
// Based on SpectrumCylinder.r4 by Gordon Williams

float time = 0;

float tm;
int x,y;

float rx,rxamt,ry,ryamt;
int currBuffer;

float[] buffers(12*12);

float[] savr(12);
int[] sn(12);
float[] sp(12);


Map	map;
Texture envTexture;

void Init()
{
	envTexture.LoadTexture("env3.png");
	currBuffer = 0;

	rxamt = 1;
	ryamt = -1;
}

void Render()
{
	time += TIMEPASS;

	for (y=0;y<24;y=y+1)
	{
		for (x=0;x<32;x=x+1)
		{
			map.SetValues(x, y, Sin((x-15.5)*3)*0.01, Sin((y-11.5)*3)*0.01, 1, 1, 1);
		}
	}
	map.Render();

	gfxSetRenderTarget(map);

	gfxPushMatrix();

	gfxTranslate(0,0,17.5f);
	gfxRotate(-70+(Sin(time)*12),1,0,0);

	if (BASS>0.3) rxamt = 1;
	if (BASS<-0.3) rxamt = -1;
	if (TREBLE>0.3) ryamt = 1;
	if (TREBLE<-0.3) ryamt = -1;
	rx = rx + (rxamt*TIMEPASS);
	ry = ry + (ryamt*TIMEPASS);

	gfxRotate(rx*90,0,0,1);
	gfxRotate(ry*90,0,1,0);


	for (x = 0; x < 12; x++) 
	{
		sp[x] = GetSpec(x*2);
	}
	gfxSetEnvTexture(envTexture);

	// draw buffer out of cubes  
	for (y=0;y<12;y=y+1)
	{
		for (x=0;x<12;x=x+1)
		{
			gfxColour(x/12.0,y/12.0,1.0-(y/12.0),1.0); // fade colour
			gfxCube(-5.5+x,-0.25f,1.0,-5.5+x+0.5f,0.25f,1.0+(sp[x]*1.5));
		}
		gfxRotate(360.0/12.0,1,0,0);
	}

	gfxPopMatrix();

	gfxSetRenderTarget(0);
	gfxSetEnvTexture(NULL);
	gfxSetTexture(map);

	gfxSetAspect(0);
	gfxTranslate(0, 0, 2.414);
	gfxColour(1, 1, 1, 1);
	gfxTexRect(-1, 1, 1, -1);            

}