#version 330

precision highp float;

layout (triangles) in;
layout (line_strip, max_vertices = 8) out;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

uniform vec3 sunPosition;

in VertexData
{
	vec3 worldPos;
	vec3 normal;
	vec2 texCoord;
	vec3 tangent;
	vec3 bitangent;		
} inData[];

out VertexData
{
	vec3 worldPos;
	vec3 normal;
	vec3 tangent;
	vec3 bitangent;	
	vec2 texCoord;
	vec4 color;
} outData;

void main(void)
{
	mat4 mvp = projectionMatrix * modelViewMatrix;

/* Draw model

    for (int i = 0; i < gl_in.length (); ++i)
    {
        outData.color = vec4(1, 1, 1, 0.4);
        outData.texCoord = inData[i].texCoord;
        outData.normal = inData[i].normal;
        gl_Position =  mvp * vec4(inData[i].worldPos, 1);
        EmitVertex ();
    } 
    EndPrimitive ();
 */
  
/* Draw normals */

    for (int i = 0; i < gl_in.length (); ++i)
    {
        outData.color = vec4(0, 0, 1, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos, 1);
        EmitVertex ();
        outData.color = vec4(0, 0, 1, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos + inData[i].normal * 0.2, 1);
        EmitVertex ();
		EndPrimitive (); 

        outData.color = vec4(1, 0, 0, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos, 1);
        EmitVertex ();
        outData.color = vec4(1, 0, 0, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos + inData[i].bitangent * 0.2, 1);
        EmitVertex ();
		EndPrimitive (); 	
 
        outData.color = vec4(0, 1, 0, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos, 1);
        EmitVertex ();
        outData.color = vec4(0, 1, 0, 1) * 2;
        gl_Position = mvp * vec4(inData[i].worldPos + inData[i].tangent * 0.2, 1);
        EmitVertex ();
		EndPrimitive (); 	
		
		vec3 LightDir = normalize(sunPosition - inData[i].worldPos);
        outData.color = vec4(1, 1, 0.7, 0.6);
        gl_Position = mvp * vec4(inData[i].worldPos, 1);
        EmitVertex ();
        outData.color = vec4(1, 1, 0.7, 0.6);
        gl_Position = mvp * vec4(inData[i].worldPos + LightDir, 1);
        EmitVertex ();
		EndPrimitive (); 	
		
    }    

}

