// CubeTunnel.vtx
// Author - MrC
// Based on CubeTunnel.r4 By Gordon Williams

Texture	texture;
Texture	sky;

int	x,y;
float tm,tn,ofs,c,ox,oy,stretch;

void Init()
{
	sky.LoadTexture("skybox.dds");
	texture.LoadTexture("plasma_edge.jpg");
	
	tm = 0;
	tn = 0;
}

float tiltfunc(float p)
{
   float a =  (Sin((ofs+p)*0.1)	+ Cos((ofs+p)*0.423))*15.0;
   return a;
}

void Render()
{
	tm = tm	+ ((TREBLE+1.5)*TIMEPASS*8);
	while (tm>1)
	{
		tm = tm-1;
		ofs =	ofs	+ 1;
	}


	tn =	tn + ((BASS+1.5)*TIMEPASS);

	stretch = 3.5+(Sin(tn*1.23)*2);

	gfxSetAspect(0);
	gfxLookAt(Cos(tn*1.23)*2.5, Sin(tn)*2.5,(tm*stretch)-1,Cos(tn*0.6734)*2,Sin(tn*0.2143)*2,(tm*stretch)+6,0.0,1.0,0.0);   
	gfxRotate(-tiltfunc(0)*tm,0,0,1);

	gfxSetTexture(sky);
	gfxPushMatrix();
	gfxTranslate(0,	0, tm*stretch);
	gfxRotate(tiltfunc(0)*tm,0,0,1);
	gfxCube(-400,-400,-400,400,400,400);
	gfxPopMatrix();

	gfxSetTexture(texture);

	gfxSetBlendMode(BLEND_ADD);
	for (y=0;y<15;y=y+1)
	{
		c = 1.0 - ((y-tm)/15.0);      
		for (x=0;x<8;x=x+1)
		{
			gfxColour(c,c,c,1);
			ox = Sin(x*3.14159*0.25)*5;
			oy = Cos(x*3.14159*0.25)*5;
			gfxCube(ox-0.75,oy-0.75,(y*stretch)-(0.75*stretch), 0.75+ox,oy+0.75,(y*stretch)+(0.75*stretch));
		}
		gfxRotate(tiltfunc(y),0,0,1);
	}
}
