//$c1 LH 01/29/08 created
uniform sampler2D depthTex;
uniform int less;
uniform vec3 texelSize;
varying vec4 pos;
varying vec3 normal;

void main()
{
	float depth = gl_FragCoord.z;

	vec3 nn = normalize(normal);
	vec3 coord = pos.xyz;

	vec3 coords[5];
	coords[0] = coord;		
	coords[1] = coord + vec3(-texelSize.x, -texelSize.y, 0);
	coords[2] = coord + vec3( texelSize.x, -texelSize.y, 0);
	coords[3] = coord + vec3(-texelSize.x, texelSize.y, 0);
	coords[4] = coord + vec3( texelSize.x, texelSize.y, 0);

	int count = 0;
	float d = 0.0;
	for (int i=0; i<5; i++)
	{
		d = texture2D(depthTex, coords[i].xy).x;

		if (pos.z >= d)
			count += 1;	
	}

	if (count == 5)
	{
		depth = 1.0;
		if (less == 1)
			discard;
	}
	else if (count == 0)
	{
		depth = 0.0;
		if (less == 0)
			discard;
	}
	else
	{
		discard;
	}
		
	gl_FragDepth = depth;
}