// VoicePrintSprectrum.vtx
// Author - MrC
// Based on Spectrum.r4 by Gordon Williams

int x;

float[] vl(256);
float[] tm(256);
float[] mx(256);

VoicePrint	vp;

void Init()
{
	vp.LoadColourMap("huemap.jpg");
	vp.SetRect(0.99f, 0.7f, 0.01f, 0.7f);
	vp.SetSpeed(0.4f);
	for (int i = 0; i < 256; i++)
	{
		mx[i] = 0;
		tm[i] = 0;
	}

}

void Render()
{
	vp.Render();
	gfxSetRenderTarget(0);

	gfxSetTexture(vp);

	gfxSetAspect(0);
	gfxTranslate(0, 0, 2.414);
	gfxTexRect(-1, 1, 1, -0.5);


   // calculate spectrum
   for (x=0;x<256;x=x+1)
	vl[x] = 0;
   for (x=0;x<512;x=x+1)
	vl[x/2] = vl[x/2] + GetSpecLeft(x)+GetSpecRight(x);

   // now draw spectrum
   gfxTranslate(-1,-0.9,0);
   gfxBegin(PRIM_QUADLIST);
   gfxSetTexture(NULL);
   
   for (x = 0; x < 256; x++)
   {
      vl[x] = vl[x]/8;
      if (vl[x]>1.0) vl[x]=1.0;

      tm[x] = tm[x] + TIMEPASS;
      mx[x] = mx[x] - (TIMEPASS*tm[x]*4.0);      

      if (vl[x]>=mx[x])
      {
         mx[x]=vl[x];
         tm[x]=0.0;
      }
      
      float xPos = x / 128.0f;

      gfxColour(0,0,1,1);
      gfxVertex(xPos+(1/128.0f),0,0);
      gfxVertex(xPos,0,0);
      gfxColour(0,vl[x],1, 1);
      gfxVertex(xPos,vl[x]*0.4,0);      
      gfxVertex(xPos+(1/128.0f),vl[x]*0.4,0);

      gfxColour(1,1,1,1);
      gfxVertex(xPos+(1/128.0f),mx[x]*0.4,0);
      gfxVertex(xPos,mx[x]*0.4,0);
      gfxVertex(xPos,(mx[x]-0.01)*0.4,0);
      gfxVertex(xPos+(1/128.0f),(mx[x]-0.01)*0.4,0);
   }
   gfxEnd();
}