#version 150

// On NVIDIA, compatibiliy is required:
// error C7531: global variable gl_ClipDistance requires "#extension GL_ARB_compatibility : enable" before use

//NVIDIA: error C0207: ARB_compatibility is not supported in GLSL version 150. Use compatibility profile.
//#extension GL_ARB_compatibility : enable

out vec2 gDistance;
out vec3 gNormal;
out vec2 gTexcoord;

in vec3 Vertex_Position[3];
in vec4 Vertex_UV[3];

layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

uniform mat4 gxl3d_ModelViewProjectionMatrix;
uniform mat4 gxl3d_ModelViewMatrix;
uniform mat4 gxl3d_ModelMatrix;

void main()
{
  vec3 A = Vertex_Position[0];
  vec3 B = Vertex_Position[1];
  vec3 C = Vertex_Position[2];
  mat3 NormalMatrix = mat3(gxl3d_ModelViewMatrix);
  gNormal = NormalMatrix * normalize(cross(B - A, C - A));
  gTexcoord = Vertex_UV[0].xy;
  gDistance = vec2(1, 0);
  gl_Position = gl_in[0].gl_Position;
  gl_ClipDistance[0] = gl_in[0].gl_ClipDistance[0];
  EmitVertex();
  gTexcoord = Vertex_UV[1].xy;
  gDistance = vec2(0, 0);
  gl_Position = gl_in[1].gl_Position;
  gl_ClipDistance[0] = gl_in[1].gl_ClipDistance[0];
  EmitVertex();
  gTexcoord = Vertex_UV[2].xy;
  gDistance = vec2(0, 1);
  gl_Position = gl_in[2].gl_Position;
  gl_ClipDistance[0] = gl_in[2].gl_ClipDistance[0];
  EmitVertex();
  EndPrimitive();
  
  
}
