//$c4 MFC 06/16/14 Shader optimization for intersection zones
//$c3 AWK 01/29/14 prj 19828 enable intersection sectioning of edges
//$c2 AWK 04/17/13 SPR 699617:Added gl_ClipVertex call for NVIDIA and Intel
//$c1 AWK 10/09/12 prj 14327 created

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



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

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_wcVertex = gl_Vertex.xyz;
}
#endif //_VERTEX_PROGRAM





#ifdef _FRAGMENT_PROGRAM

out uvec2 data;

void main()
{
	if(faceID42IntersectingSection != 0)
	{
		//plane1
		vec3 pvec1 = v_wcVertex - faceID42Plane1Origin;
		float pval1 = dot(pvec1, faceID42Plane1Normal);
		//plane2
		vec3 pvec2 = v_wcVertex - faceID42Plane2Origin;
		float pval2 = dot(pvec2, faceID42Plane2Normal);

		if(faceID42SectionPlane3Active == 0)
		{
			if ( (faceID42SectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0)
				|| (faceID42SectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0)
				|| (faceID42SectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0)
				|| (faceID42SectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0))
			{
				discard;
			}
		}
		else
		{
			//plane3
			vec3 pvec3 = v_wcVertex - faceID42Plane3Origin;
			float pval3 = dot(pvec3, faceID42Plane3Normal);
		
			if ( (faceID42SectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (faceID42SectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (faceID42SectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (faceID42SectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (faceID42SectioningRegion5 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (faceID42SectioningRegion6 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (faceID42SectioningRegion7 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 > 0.0)
				|| (faceID42SectioningRegion8 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 > 0.0))
			{
				discard;
			}
		}
	}

	data = uvec2(faceID42, compID42);
}

#endif //_FRAGMENT_PROGRAM


