#version 430 core
layout (lines) in;
layout (triangle_strip, max_vertices = 14) out;

in vec4 vBackPoss[];
uniform vec2 lightPos;
uniform mat4 worldMat;

void emitLine (vec2 pa)
{
    gl_Position = vec4 (pa, 0, 1);
    EmitVertex();
    gl_Position = vec4 (pa + (200 * (pa - lightPos)), 0 , 1);
    EmitVertex();
}

float isLHS (vec2 startV, vec2 testV)
{
    return sign( -startV.x * testV.y + startV.y * testV.x);
}

void main()
{
  vec4 frontL4 = worldMat * vec4(gl_in[0].gl_Position.xy, 0 ,1);
  vec4 frontR4 = worldMat * vec4(gl_in[0].gl_Position.zw, 0 ,1);
  vec2 frontL = frontL4.xy;
  vec2 frontR = frontR4.xy;
  vec4 backR4 = worldMat * vec4(vBackPoss[0].xy, 0 ,1);
  vec4 backL4 = worldMat * vec4(vBackPoss[0].zw, 0 ,1);
  vec2 backL = backL4.xy;
  vec2 backR = backR4.xy;

//    if (isLHS( lightPos - frontL, frontR - frontL) < 0)
//  //  if (isLHS( lightPos - backL, backR - backL) < 0)
//    {
//  //  emitLine (frontL);
//  //  emitLine (backL);
//  //  emitLine (frontL);
//  //  emitLine (frontR);
//  //  emitLine (backR);
//  //  emitLine (frontR);
//  
//    emitLine (backR);
//    emitLine (backL);
//    }
//    else
//    {
//    emitLine (backR);
//    emitLine (backL);
//    }

    emitLine (frontR);
    emitLine (frontL);
  EndPrimitive();
}
