/*****************************************
* Texture.fx - handles a single texture
******************************************/

// globals
float4x4	gWVP;
texture		tex1;
float gDT;
float gAccel;
float gGrow;
float gFade;

//For scaling over distance:
float3 gPlayerPos;
float gScreenHeight;

// texture sampler
sampler		samp1
{
	Texture = <tex1>;
	// texture filtering
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	// texture address modes (available options are WRAP, MIRROR, BORDER, and CLAMP)
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};
// avilable filter options are NONE, POINT (Nearest Neighbor), NONE (average 2x2), and ANIOSOTROPIC. 
	
// Define a vertex shader output structure;
struct OutputVS
{
    float4 posH:POSITION0;    
    float2 texCoord0:TEXCOORD1;
    float4 color:COLOR0;
    float size: PSIZE0;
    
};

// Vertex shader -------------------------------------------------------------------------------------
OutputVS TransformVS(float3 position:POSITION0, float3 velocity:TEXCOORD0, float2 uv0:TEXCOORD1, float4 col:COLOR0, float size:PSIZE0, float time:TEXCOORD2, float max_time:TEXCOORD3)
{
	// output structure
	OutputVS outVS;
	
	//Apply velocity
	float3 moveThisMuch = mul(velocity, time);
	float3 newPosition = position + moveThisMuch;

	//float3 targetPosition = float3(0.0f, 0.0f, 0.0f);
	//float3 vecTo = targetPosition - position;
	
	//Gravity:
	//newPosition.x = position.x + moveThisMuch.x + (0.5f * vecTo.x * time * time);
	newPosition.y = position.y + moveThisMuch.y + (0.5f * gAccel * time * time);
	//newPosition.z = position.z + moveThisMuch.z + (0.5f * gAccel * time * time);

	
	// create the transformed vertex position
	outVS.posH = mul(float4(newPosition, 1.0f), gWVP);	
	
	float colorLerp = 1.0f - (time / max_time);
	float colorMod = mul(colorLerp, gFade);
	// pass through the color
	outVS.color = mul(col, colorLerp);
	
	// pass through the texture coordinates
	outVS.texCoord0 = uv0;
	
	//outVS.size = size + mul(gGrow,gDT);
	//outVS.size = size;
	float3 distVec = gPlayerPos - position;
	float distSq = dot(distVec, distVec);
	float heightSq = gScreenHeight * gScreenHeight;
	outVS.size = heightSq * size / (1.0f + 64.0f * distSq);
    return outVS;
}

// Pixel Shader ---------------------------------------------------------------------------
float4 TransformPS(float4 c:COLOR0, float2 uv0:TEXCOORD1):COLOR
{
	//return float4(1,1,1,1);
	// determine the pixel to sampler from
    float4 texCol = tex2D(samp1, uv0);
    
    return texCol * c;
}

// ----------------------------------------------------------------------------------------
technique Basic
{
    pass P0
    {
        vertexShader = compile vs_2_0 TransformVS();
        pixelShader  = compile ps_2_0 TransformPS();
        
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;
	
		ZWriteEnable = false;
		
		PointSpriteEnable = true;
		PointScaleEnable = true;
		PointSize = 1.0f;
		PointSize_Min = 1.0f;
		PointSize_Max = 100.0f;
		
		//NewSize = BaseSize * sqrt( 1/ (A + B*Dis + C*Dis*Dis) )
		PointScale_A = 1.0f;
		PointScale_B = 0.0f;
		PointScale_C = 0.0f;
    }
}