<include>
<variablemap variable="light 0 diffuse" destination="lightcolor"/>
<variablemap variable="specular" destination="specular"/>
<program>


struct vertex2fragment { 
	float4 Position : POSITION;
	float2 TexCoord : TEXCOORD0;
	float4 LightDir : TEXCOORD1;
	float4 EyeVec : TEXCOORD2;
	float4 Attenuation : TEXCOORD3;
};


float4 main (vertex2fragment IN,
			uniform float4 lightcolor,
			uniform float4 specular,
			uniform sampler2D TexNormal,
			uniform sampler2D TexDiffuse,
			uniform sampler2D TexHeight,
			uniform sampler2D TexAttn) : COLOR
{
	float3 normEye = IN.EyeVec.xyz;
	float3 normLight = IN.LightDir.xyz;

	float selfOcclusion = saturate(normLight.z*5);
	
	float4 height = 0.04 * tex2D(TexHeight, IN.TexCoord) - 0.02;
	float2 newTexcoord = (height * normEye).xy + IN.TexCoord;
	
	float4 diffuseCol = tex2D(TexDiffuse, newTexcoord);
	float4 glowCol = float4(0);
	float4 bumpNormal = tex2D(TexNormal, newTexcoord);
	bumpNormal *= 2;
	bumpNormal -= 1;
	float3 normBumpNormal = normalize(bumpNormal.xyz);
		
	float bump = dot(normLight, normBumpNormal);
	<?if vars."specular" ?>
		float spec = pow(dot(normalize(normEye + normLight), normBumpNormal), 32) * (bumpNormal.w+1)/2;
	<?endif?>
	float4 outCol = saturate(lightcolor*bump.xxxx);

	<?if vars."specular" ?>
		float4 result = saturate(outCol*diffuseCol + spec*lightcolor*specular);
	<?else?>
		float4 result = saturate(outCol*diffuseCol);
	<?endif?>
	/*
	float4 AttnCol = tex2D(TexAttn, IN.Attenuation.xy)*tex2D(TexAttn, IN.Attenuation.zw);
	return AttnCol*result;
	 */
	/*
	 * This is a very crappy work around for buggy intel graphics
	 * drivers. We really should multiply with AttnCol here.
	 */
	return result;
	  /* 	return AttnCol*(result + glowCol); */
}

</program>
</include>

