// ChessMania.vtx
// Author - MrC
// Based on Rovastar - Chessmania.r4 by John Baker

Texture checker;
Texture tex1;
Texture tex2;

float[] cubeRot(3*3);
float[] cubePos(3*3);

void Init()
{
	checker.LoadTexture("checker.png");
	tex1.CreateTexture();
	tex2.CreateTexture();

	for (int i=0; i<9;i++)
	{
		cubePos[i] = 0.0f;
		cubeRot[i] = 0.0f;
	}

}

void RenderCube(int cube, float dist)
{
	gfxPushMatrix();
	gfxTranslate(0, 0, dist);

	gfxRotate(cubePos[0+cube*3],1,0,0);
	gfxRotate(cubePos[1+cube*3],0,0,1);
	gfxRotate(cubePos[2+cube*3],0,1,0);

	gfxCube(-0.25f, -0.25f, -0.25f, 0.25f, 0.25f, 0.25f);
	gfxPopMatrix();
}

void Render()
{
	gfxSetTexture(checker);

	gfxTranslate(0,0,2.414);

	for (int i=0; i<9;i++)
	{
		cubePos[i] = cubePos[i] + cubeRot[i];
		cubeRot[i] = cubeRot[i] * 0.95;
		if (cubeRot[i] > 1)
			cubeRot[i] = 1;
		if (cubeRot[i] < -1)
			cubeRot[i] = -1;
	}

	cubeRot[0] += BASS;
	cubeRot[1] += MIDDLE;
	cubeRot[2] += TREBLE;
	
	cubeRot[4] += BASS;
	cubeRot[5] += MIDDLE;
	cubeRot[3] += TREBLE;

	cubeRot[8] += BASS;
	cubeRot[6] += MIDDLE;
	cubeRot[7] += TREBLE;
	
	gfxSetRenderTarget(tex1);
	gfxClear(0);
	gfxSetTexture(checker);
	gfxSetAspect(0);
	gfxTexRect(-1, -1, 1, 1);
	RenderCube(2, -1.0);

	gfxSetRenderTarget(tex2);
	gfxClear(0);
	gfxSetTexture(checker);
	gfxSetAspect(0);
	gfxTexRect(-1, -1, 1, 1);
	gfxSetTexture(tex1);
	RenderCube(1, -1.0);

	gfxSetRenderTarget(TEXTURE_FRAMEBUFFER);
	gfxSetTexture(checker);
	gfxSetAspect(0);
	gfxTexRect(-1, -1, 1, 1);
	gfxSetTexture(tex2);
	gfxSetAspect(1);
	RenderCube(0, -1.15);
	
}