varying vec4 ClrDiffuse;
varying vec2 TexCoord;
varying vec4 ViewCoords;
varying vec3 Normal, HalfVec;

uniform vec2 Eng_ObjID;
uniform sampler2D DiffuseMap;
uniform sampler2D Eng_EnvScreenShadows;
uniform vec3 Eng_LocalLighting;
uniform float Shine, SpecularLevel; 

#ifdef _EngDef_ShadowsEnabled
const float sd = 1.0 / _EngDef_ShadowsScreenTextureSize;

bool compareID(in vec2 I1, in vec2 I2)
{
	return (abs((I1.x*255.0) - I2.x) < 0.5) && (abs((I1.y*255.0) - I2.y) < 0.5);
}

vec4 getShadow(in vec2 C)
{
	float ret = 0.0;
	vec4 shadow = texture2D(Eng_EnvScreenShadows, C + vec2(-sd, -sd));
	ret = compareID(shadow.gb, Eng_ObjID) ? shadow.r: 1.0;
	
	shadow = texture2D(Eng_EnvScreenShadows, C + vec2(sd, -sd));
	ret = min(ret, compareID(shadow.gb, Eng_ObjID) ? shadow.r: 1.0);

	shadow = texture2D(Eng_EnvScreenShadows, C + vec2(sd, sd));
 	ret = min(ret, compareID(shadow.gb, Eng_ObjID) ? shadow.r: 1.0);

	shadow = texture2D(Eng_EnvScreenShadows, C + vec2(-sd, sd));
	ret = min(ret, compareID(shadow.gb, Eng_ObjID) ? shadow.r: 1.0);

	return vec4(ret);
}
#endif

void main()
{
#ifdef _EngDef_ShadowsEnabled
    vec2 ShadowCoords = ((ViewCoords.xy / ViewCoords.w) + 1.0) * 0.5;
	vec4 shadow = getShadow(ShadowCoords);
#else
	vec4 shadow = vec4(1.0);
#endif

	float s = pow(max(0.0, dot(normalize(HalfVec), normalize(Normal))), Shine) * shadow.r;
	 
	gl_FragColor = (gl_LightSource[0].ambient + (ClrDiffuse * shadow.r)) * texture2D(DiffuseMap, TexCoord) + gl_LightSource[0].specular * s * SpecularLevel;
}
