#version 430 core
layout (points) in;
layout (triangle_strip, max_vertices = 8) out;
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
uniform vec3 lightPos;

vec4 shift (vec4 p)
{ return vec4 (p.xy + (200 * (p.xy-lightPos.xy)), 0 , 1);
}
float isLHS (vec2 startV, vec2 testV)
{
    return sign( -startV.x * testV.y + startV.y * testV.x);
}
// construct a box with openings on bottom face and face away from wall
void main()
{
vec4 p1 = vec4 (gl_in[0].gl_Position.xy,0,1);
vec4 p2 = vec4 (gl_in[0].gl_Position.zw,0,1);
if (isLHS (p1.xy - lightPos.xy, p2.xy - lightPos.xy) < 0)
{
vec4 p3 = vec4 (p1.xy,100,1);
vec4 p4 = vec4 (p2.xy,100,1);
vec4 p5 = shift(p1);
vec4 p6 = shift(p2);
vec4 p7 = vec4 (p6.xy,100,1);
vec4 p8 = vec4 (p5.xy,100,1);

vec4 a1 = theMat * p1;
vec4 a2 = theMat * p2;
vec4 a3 = theMat * p3;
vec4 a4 = theMat * p4;
vec4 a5 = theMat * p5;
vec4 a6 = theMat * p6;
vec4 a7 = theMat * p7;
vec4 a8 = theMat * p8;

  //gl_Position = a4; EmitVertex();
  //gl_Position = a3; EmitVertex();
  //gl_Position = a7; EmitVertex();
  //gl_Position = a8; EmitVertex();
  //gl_Position = a5; EmitVertex();
  //gl_Position = a3; EmitVertex();
  //gl_Position = a1; EmitVertex();
  //gl_Position = a4; EmitVertex();
  //gl_Position = a2; EmitVertex();
  //gl_Position = a7; EmitVertex();
  //gl_Position = a6; EmitVertex();

  gl_Position = a1; EmitVertex();
  gl_Position = a5; EmitVertex();
  gl_Position = a3; EmitVertex();
  gl_Position = a8; EmitVertex();
  gl_Position = a4; EmitVertex();
  gl_Position = a7; EmitVertex();
  gl_Position = a2; EmitVertex();
  gl_Position = a6; EmitVertex();

  //gl_Position = a5; EmitVertex();
  //gl_Position = a1; EmitVertex();
  //gl_Position = a8; EmitVertex();
  //gl_Position = a3; EmitVertex();
  //gl_Position = a7; EmitVertex();
  //gl_Position = a4; EmitVertex();
  //gl_Position = a6; EmitVertex();
  //gl_Position = a2; EmitVertex();

  EndPrimitive();
} else {}
}
