//$c4 MFC 06/16/14 Shader optimization for intersection zones
//$c3 AWK 01/29/14 prj 19828 enable intersection sectioning of edges
//$c2 MFC 03/14/13 SPR 678755:Added gl_ClipVertex call for NVIDIA and Intel
//$c1 AWK 03/13/13 spr 678741,678743,678750,678755,679883,680446,681176,681180,681184,681349 redisgned the edge bleed through fix

// this line is needed to get the associated xml files
#define SW_PARAMETER_FILES "global.xml,renderedgeszbufferfix42.xml"

varying vec4 v_fragCoord;
varying vec3 v_wcVertex;

/////////////////////////////////////////////////////////////////////////////////

#ifdef _VERTEX_PROGRAM
void main()
{
	// transform the vertex position
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
#ifdef SW_SHADER_USE_CLIPVERTEX
	// set position for clip planes
	gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
#endif

	v_fragCoord = gl_Position;

	v_wcVertex = gl_Vertex.xyz;
}
#endif //_VERTEX_PROGRAM





#ifdef _FRAGMENT_PROGRAM

bool checkFace(uint ifaceID, uint icompID, uint edgeFaceId, vec2 uv)
{
	uvec2 val = texture(renderEdgeZbufferFixFaceIDs42, uv).xy;
	uint sfaceID = val.x;
	uint scompID = val.y;
	if(sfaceID == 0 || sfaceID == edgeFaceId)
		return true;

	if(ifaceID != sfaceID || icompID != scompID)
		return true;

	return false;  
}

bool checkFaces(uint ifaceID, uint icompID, uint edgeFaceId, vec2 texcoord, vec2 offset)
{
	vec2 uv;
	uv.x = texcoord.x + offset.x;
	uv.y = texcoord.y;
	if(checkFace(ifaceID, icompID, edgeFaceId, uv))
		return true;

	uv.x = texcoord.x - offset.x;
	if(checkFace(ifaceID, icompID, edgeFaceId, uv))
		return true;

	uv.x = texcoord.x;
	uv.y = texcoord.y + offset.y;
	if(checkFace(ifaceID, icompID, edgeFaceId, uv))
		return true;

	uv.y = texcoord.y - offset.y;
	if(checkFace(ifaceID, icompID, edgeFaceId, uv))
		return true;

	return false;  
}

void main()
{
	if(edge42IntersectingSection != 0)
	{
		//plane1
		vec3 pvec1 = v_wcVertex - edge42Plane1Origin;
		float pval1 = dot(pvec1, edge42Plane1Normal);
		//plane2
		vec3 pvec2 = v_wcVertex - edge42Plane2Origin;
		float pval2 = dot(pvec2, edge42Plane2Normal);

		if(edge42SectionPlane3Active == 0)
		{
			if ( (edge42SectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0)
				|| (edge42SectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0)
				|| (edge42SectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0)
				|| (edge42SectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0))
			{
				discard;
			}
		}
		else
		{
			//plane3
			vec3 pvec3 = v_wcVertex - edge42Plane3Origin;
			float pval3 = dot(pvec3, edge42Plane3Normal);
		
			if ( (edge42SectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (edge42SectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (edge42SectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (edge42SectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (edge42SectioningRegion5 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (edge42SectioningRegion6 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (edge42SectioningRegion7 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 > 0.0)
				|| (edge42SectioningRegion8 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 > 0.0))
			{
				discard;
			}
		}
	}

	vec2 coord = ((v_fragCoord.xy/v_fragCoord.w) + 1.0)/2.0;
	uvec2 val = texture(renderEdgeZbufferFixFaceIDs42, coord).xy;
	uint l_edgeFaceID = val.x;
	uint l_edgeCompID = val.y;

	vec2 offset = vec2(renderEdgeZBufferFixSampleOffsetX42,renderEdgeZBufferFixSampleOffsetY42);

	if(l_edgeFaceID == 0 || l_edgeFaceID == edgeFaceID42)
		gl_FragColor = edgeColor42;
	else
	{
		if(checkFaces(l_edgeFaceID, l_edgeCompID, edgeFaceID42, coord, offset))
			gl_FragColor = edgeColor42;
		else
			discard;
	}
}

#endif //_FRAGMENT_PROGRAM


