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

int x,y,z;
float t,c;
float bt,bs;
float time;

const int XMAX = 3;
const int YMAX = 3;
const int ZMAX = 5;

Texture envTexture;

void Init()
{
   t = 0;
   bt = 0;
   bs = 0;
   time = 0;
   envTexture.LoadTexture("env2.jpg");
}

void Render()
{
	bt = bt + (TIMEPASS*(MIDDLE+0.5f));
	bs = bs + (TIMEPASS*(TREBLE+0.5f));
	t = t + (TIMEPASS*2.5);
	while (t>1.0)
	{
		t=t-1.0;
	}

	time = time + TIMEPASS;

	gfxRotate(time*60.0f, 0.0f, 0.0f, 1.0f);
	gfxRotate(bt*53.0f, 1.0f, 0.0f, 1.0f);
	gfxRotate(bs*62.0f, 0.0f, 1.0f, 0.0f);

	if (TREBLE < 0.5)
	{
		gfxSetTexture(NULL);
		gfxSetFillMode(FILLMODE_WIREFRAME);
	}
	else
		gfxSetEnvTexture(envTexture);

	gfxTranslate(-XMAX*0.5f, -YMAX*0.5f, -3.0f+t);
	for (z=0;z<=ZMAX;z=z+1)
	{    
		c=1;
		if (z==0)
			c = t;
		if (z==ZMAX)
			c = 1.0f - t;
		gfxColour(c,c,c,1);
		for (y=0;y<=YMAX;y=y+1) 
		{
			for (x=0;x<=XMAX;x=x+1)
			{
				gfxCube(x-0.15f, y-0.15f, z-0.15f, x+0.15f, y+0.15f, z+0.15f);
				if (x<XMAX)
				{
					gfxCube(x+0.15f, y-0.05f, z-0.05f, x+0.85f, y+0.05f, z+0.05f);
				}
				if (y<YMAX)
				{
					gfxCube(x-0.05f, y+0.15f, z-0.05f, x+0.05f, y+0.85f, z+0.05f);
				}
				gfxCube(x-0.05f, y-0.05f, z+0.15f, x+0.05f, y+0.05f, z+0.85f);
			}
		}
	}
}