<cgfp>
<profile>arbfp1</profile>
<variablemap variable="light 0 diffuse" destination="lightcolor"/>
<variablemap variable="specular" destination="specular"/>
<program>
<![CDATA[

struct vertex2fragment { 
	float4 TexCoord : TEXCOORD0;
	float4 LightDir : TEXCOORD1;
	float4 EyeVec : TEXCOORD2;
	float4 Projcoords : TEXCOORD3;
	float4 Distance : TEXCOORD4;
	float4 Spot : TEXCOORD5;
};

float get_depth_value(float4 in_color, float dist)
{
	float4 extract = { 1.0, 0.00390625, 0.0000152587890625, 0.000000059604644775390625 };
	float res = dot(in_color, extract);
	return (float)(res >= dist);
}

float4 main (vertex2fragment IN,
			uniform float4 lightcolor,
			uniform float4 specular,
			uniform sampler2D TexDiffuse,
 			uniform sampler2D TexNormal,
 			uniform sampler2D TexAttn,
 			uniform samplerCUBE TexDistance
			) : COLOR
{
	float4 diffuseCol = tex2D(TexDiffuse, IN.TexCoord.xy);

	float fTexelSize = 1.0f / 20.0f;
	float4 vTexCoords[9];

	vTexCoords[0] = IN.Projcoords;
	vTexCoords[1] = IN.Projcoords + float4( -fTexelSize, 0.0f, 0.0f, 0.0f );
	vTexCoords[2] = IN.Projcoords + float4(  fTexelSize, 0.0f, 0.0f, 0.0f );
	vTexCoords[3] = IN.Projcoords + float4( 0.0f, -fTexelSize, 0.0f, 0.0f );
	vTexCoords[4] = IN.Projcoords + float4( 0.0f,  fTexelSize, 0.0f, 0.0f );
	vTexCoords[5] = IN.Projcoords + float4( -fTexelSize, -fTexelSize, 0.0f, 0.0f );
	//vTexCoords[6] = IN.Attenuation + float4(  fTexelSize, -fTexelSize, 0.0f, 0.0f );
	//vTexCoords[7] = IN.Attenuation + float4( -fTexelSize,  fTexelSize, 0.0f, 0.0f );
	//vTexCoords[8] = IN.Attenuation + float4(  fTexelSize,  fTexelSize, 0.0f, 0.0f );

	float4 shadow_term = 0.0f;
	float dist = length(IN.Distance.xyz)/16.0 -0.005;
	float4 SpotCol = tex2D(TexAttn, IN.Spot.xy)*tex2D(TexAttn, IN.Spot.zw);

	float4 distCol0 = texCUBE(TexDistance, vTexCoords[0]);
	float4 distCol1 = texCUBE(TexDistance, vTexCoords[1]);
	float4 distCol2 = texCUBE(TexDistance, vTexCoords[2]);
	float4 distCol3 = texCUBE(TexDistance, vTexCoords[3]);
	float4 distCol4 = texCUBE(TexDistance, vTexCoords[4]);
	float4 distCol5 = texCUBE(TexDistance, vTexCoords[5]);
	//float4 distCol6 = tex2Dproj(TexDistance, vTexCoords[6]);
	//float4 distCol7 = tex2Dproj(TexDistance, vTexCoords[7]);
	//float4 distCol8 = tex2Dproj(TexDistance, vTexCoords[8]);

	shadow_term = get_depth_value(distCol0, dist);
	shadow_term += get_depth_value(distCol1, dist);
	shadow_term += get_depth_value(distCol2, dist);
	shadow_term += get_depth_value(distCol3, dist);
	shadow_term += get_depth_value(distCol4, dist);
	shadow_term += get_depth_value(distCol5, dist);
	//shadow_term += get_depth_value(distCol6, dist);
	//shadow_term += get_depth_value(distCol7, dist);
	//shadow_term += get_depth_value(distCol8, dist);
	shadow_term /= 6;

	float3 normEye = normalize(IN.EyeVec);
	float3 normLight = normalize(IN.LightDir);
	float4 bumpNormal = 2*tex2D(TexNormal, IN.TexCoord.xy) - 1;
	float3 normBumpNormal = normalize(bumpNormal.xyz);
	float bump = dot(normLight, normBumpNormal);
	float3 halfv = normalize(normLight + normEye);
	float spec = pow(max(dot(halfv, normBumpNormal), 0), 32);
	//spec *= bumpNormal.a;
	float4 outCol = lightcolor*bump.xxxx;

	float4 result = SpotCol*shadow_term*(outCol*diffuseCol + lightcolor*specular*spec);
	return result;
}
]]>
</program>
</cgfp>
