// WalkingCube.vtx
// Author - MrC

float time = 0;
float rx,rxamt,ry,ryamt;
float y = 0;
Texture checker;
Texture sky;
float tx, ty;

float d = 0;

void Init()
{
	checker.LoadTexture("checker.png");
	sky.LoadTexture("sky.dds");
	
	tx = 0;
	ty = 0;
	ryamt = 0;
	
}

void Render()
{

	gfxPushMatrix();

	d = TIMEPASS*((BASS + 1.5)*4);
	d = TIMEPASS*6 + (BASS * 4 * TIMEPASS);

	time += d;

	ry += TIMEPASS * ryamt * 2;

	gfxSetTexture(sky);

	gfxSetBlendMode(BLEND_MOD);
	gfxPushMatrix();
	gfxSetAspect(0);
	gfxTranslate(0,0,2.414);

	gfxBegin(PRIM_QUADLIST);
	gfxColour(1, 1, 1, 1);
	gfxTexCoord(0, 0);
	gfxVertex(-1, 1, 0);

	gfxTexCoord(1, 0);
	gfxVertex(1, 1, 0);

	gfxTexCoord(1, 1);
	gfxVertex(1, -0.2, 0);

	gfxTexCoord(0, 1);
	gfxVertex(-1, -0.2, 0);

	gfxColour(0.7, 0.7, 0.7, 1);

	gfxTexCoord(0, 1);
	gfxVertex(-1, -0.2, 0);

	gfxTexCoord(1, 1);
	gfxVertex(1, -0.2, 0);

	gfxTexCoord(1, 0);
	gfxVertex(1, -1, 0);

	gfxTexCoord(0, 0);
	gfxVertex(-1, -1, 0);

	gfxEnd();

	gfxSetAspect(1);
	gfxPopMatrix();

	gfxSetTexture(TEXTURE_ALBUMART);

	if (BASS>0.3) rxamt = 1;
	if (BASS<-0.3) rxamt = -1;
	if (TREBLE>0.4) ryamt = Rand()-0.5f;



	gfxPushMatrix();
	gfxSetBlendMode(BLEND_OFF);
	gfxScale(1,-1,1);
	gfxTranslate(0, 0.5 + Fabs(Sin(time * 2 *45 * 3.1415 / 180))* 0.125, 2.0f);
	gfxRotate(ry*90,0,1,0);
	gfxRotate(time*45,0,0,1);

	gfxColour(1, 1, 1, 1);
	gfxCube(-0.25f, -0.25f, -0.25f, 0.25f, 0.25f, 0.25f);

	gfxPopMatrix();	

	gfxPushMatrix();

	gfxTranslate(0, Fabs(Sin(time * 2 *45 * 3.1415 / 180))* 0.125, 2.0f);
	gfxRotate(ry*90,0,1,0);
	gfxRotate(time*45,0,0,1);

	gfxColour(1, 1, 1, 1);
	gfxCube(-0.25f, -0.25f, -0.25f, 0.25f, 0.25f, 0.25f);

	gfxPopMatrix();	


	float c = Cos(ry*90 * 3.1415 / 180);
	float s = Sin(ry * 90 * 3.1415 / 180);

	ty -= d * 0.06f * s;
	tx -= d * 0.06f * c;

	if (ty > 2)
	ty -= 1.0f;

	if (ty < 2)
	ty += 1.0f;

	if (tx > 2)
	tx -= 1.0f;

	if (tx < 2)
	tx += 1.0f;


	// Floor


	gfxSetTexture(checker);
	gfxTranslate(0, -0.245, 2.0f);
	gfxSetBlendMode(BLEND_MOD);

	gfxBegin(PRIM_QUADLIST);
	gfxColour(0, 0, 0, 0.4);
	gfxTexCoord((0+tx)*2, (0+ty)*2);
	gfxVertex(-2, 0, 2);
	gfxTexCoord((1+tx)*2, (0+ty)*2);
	gfxVertex(2, 0, 2);
	gfxColour(1, 1, 1, 0.8);
	gfxTexCoord((1+tx)*2, (1+ty)*2);
	gfxVertex(2, 0, -2);
	gfxTexCoord((0+tx)*2, (1+ty)*2);
	gfxVertex(-2, 0, -2);
	gfxEnd();


	gfxPopMatrix();	

	
}