#version 400
#extension GL_EXT_geometry_shader4 : enable

layout (triangles, invocations = 1) in;
layout (triangle_strip, max_vertices = 6) out;

in float NdotE[];
in float distToEye[];
in vec3  esNorm[];

out float dToEye;
out vec3  fragNorm;

void main( void )
{ 
	bool isFront   = (NdotE[0] > 0.0) || (NdotE[1] > 0.0) || (NdotE[2] > 0.0);
	bool isBack    = (NdotE[0] < 0.0) || (NdotE[1] < 0.0) || (NdotE[2] < 0.0);
	
	if (isFront)
	{
		gl_Layer       = 0;
		gl_Position    = gl_PositionIn[0];
		fragNorm       = esNorm[0].xyz;
		dToEye         = distToEye[0];
		EmitVertex();
		gl_Position    = gl_PositionIn[1];
		fragNorm       = esNorm[1].xyz;
		dToEye         = distToEye[1];
		EmitVertex();
		gl_Position    = gl_PositionIn[2];
		fragNorm       = esNorm[2].xyz;
		dToEye         = distToEye[2];
		EmitVertex();
		EndPrimitive();	
	}
	
	if (isBack)
	{
		gl_Layer       = 1;
		gl_Position    = gl_PositionIn[0];
		fragNorm       = esNorm[0].xyz;
		dToEye         = distToEye[0];
		EmitVertex();
		gl_Position    = gl_PositionIn[1];
		fragNorm       = esNorm[1].xyz;
		dToEye         = distToEye[1];
		EmitVertex();
		gl_Position    = gl_PositionIn[2];
		fragNorm       = esNorm[2].xyz;
		dToEye         = distToEye[2];
		EmitVertex();
		EndPrimitive();	
	}
}

