// MeteorShower.vtx
// Author - MrC
// Based on MeteorShower.r4 by Gordon Williams
Texture trail;
Texture sky;

float[] pvel(256),ppos(256),ptheta(256),prad(256),plen(256),psnd(256);
int x;
float f,g,bt;
const int cnt = 256;
float time;

void Init()
{
	sky.LoadTexture("skybox.dds");
	trail.LoadTexture("trail2.dds");
	
	for (x=0;x<cnt;x=x+1) newpt(x);
	bt = 0;
	time = 0;
}

void drawit(float ax,float ay,float bx, float by,float r)
{
   gfxTexCoord(0,0);
   gfxVertex(r*Cos(ax),r*Sin(ax),ay);
   gfxTexCoord(0,1);
   gfxVertex(r*Cos(bx),r*Sin(bx),ay);
   gfxTexCoord(1,1);
   gfxVertex(r*Cos(bx),r*Sin(bx),by);
   gfxTexCoord(1,0);
   gfxVertex(r*Cos(ax),r*Sin(ax),by);
}

void newpt(int p)
{
   pvel[p] = (Rand()+0.5)*2.0;
   ppos[p] = 0.0;
   ptheta[p] = Rand()*3.14159*2.0;
   prad[p] = (Rand()*2.0)+0.25;
   plen[p] = 1.5+Rand();
   psnd[p] = Rand();
} 

void Render()
{
	gfxClear(0.1, 0, 0);

	bt = bt + (TIMEPASS*(0.5+BASS));
	time += TIMEPASS;
	gfxSetBlendMode(BLEND_ADD);

	f = Sin(bt+Sin(time*1.353));
	g=f;
	gfxTranslate(0,0,3);
	gfxRotate(90*(g+1.0),Sin(time*0.4),Cos(time*0.4),0);
	gfxSetTexture(sky);

	gfxCube(-20,-20,-20,20,20,20);
	gfxSetTexture(trail);
	gfxTranslate((Sin(bt*1.61))*0.3,(Sin(bt*1.23))*0.3,-8+(g*5.0));
	gfxBegin(PRIM_QUADLIST);
	for (x=0;x<cnt;x=x+1)
	{
		ppos[x] = ppos[x] + (pvel[x]*TIMEPASS*(0.5+(psnd[x]*BASS)+((1.0-psnd[x])*TREBLE)));
		if (ppos[x]<0) ppos[x] = 0;
		if (ppos[x]>1.0) newpt(x);
		gfxColour(1,1,1,(1.0-Cos(ppos[x]*3.14159*2.0))*0.5);
		drawit(ptheta[x],(ppos[x]*16.0)+plen[x],ptheta[x]+0.08+(prad[x]*0.025),(ppos[x]*16.0),prad[x]);
	}
	gfxEnd();

}