//$c2 MFC 06/16/14 Shader optimization for intersection zones
//$c1 AWK 01/29/14 prj 19828 created

// this line is needed to get the associated xml files
#define SW_PARAMETER_FILES "global.xml,sectionedge.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

void main()
{
	if(sectionEdgeIntersectingSection != 0)
	{
		//plane1
		vec3 pvec1 = v_wcVertex - sectionEdgePlane1Origin;
		float pval1 = dot(pvec1, sectionEdgePlane1Normal);
		//plane2
		vec3 pvec2 = v_wcVertex - sectionEdgePlane2Origin;
		float pval2 = dot(pvec2, sectionEdgePlane2Normal);

		if(sectionEdgeSectionPlane3Active == 0)
		{
			if ( (sectionEdgeSectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0)
				|| (sectionEdgeSectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0)
				|| (sectionEdgeSectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0)
				|| (sectionEdgeSectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0))
			{
				discard;
			}
		}
		else
		{
			//plane3
			vec3 pvec3 = v_wcVertex - sectionEdgePlane3Origin;
			float pval3 = dot(pvec3, sectionEdgePlane3Normal);
		
			if ( (sectionEdgeSectioningRegion1 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (sectionEdgeSectioningRegion2 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 < 0.0)
				|| (sectionEdgeSectioningRegion3 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (sectionEdgeSectioningRegion4 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 < 0.0)
				|| (sectionEdgeSectioningRegion5 != 0 && pval1 < 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (sectionEdgeSectioningRegion6 != 0 && pval1 > 0.0 && pval2 < 0.0 && pval3 > 0.0)
				|| (sectionEdgeSectioningRegion7 != 0 && pval1 < 0.0 && pval2 > 0.0 && pval3 > 0.0)
				|| (sectionEdgeSectioningRegion8 != 0 && pval1 > 0.0 && pval2 > 0.0 && pval3 > 0.0))
			{
				discard;
			}
		}
	}

	gl_FragColor = sectionEdgeColor;
}

#endif //_FRAGMENT_PROGRAM


