#version 400 core

layout( quads, fractional_even_spacing, ccw ) in;

out EyeSpaceVertex {
    vec3 position;
    vec3 normal;
} te_out;

uniform mat4 modelView;
uniform mat3 modelViewNormal;
uniform mat4 projectionMatrix;
uniform mat4 mvp;

void main()
{
    float u = gl_TessCoord.x;
    float v = gl_TessCoord.y;

    vec4 p00 = gl_in[0].gl_Position;
    vec4 p10 = gl_in[1].gl_Position;
    vec4 p11 = gl_in[2].gl_Position;
    vec4 p01 = gl_in[3].gl_Position;

    // Linearly interpolate to the vertex position using the
    // (u,v) barycentric coords
    vec4 pos = p00 * ( 1.0 - u ) * ( 1.0 - v )
             + p10 * u * ( 1.0 - v )
             + p01 * v * ( 1.0 - u )
             + p11 * u * v;

    // Transform to eye space (for lighting calcs)
    te_out.position = vec3( modelView * pos );

    // Assume normal points along z
    te_out.normal = normalize( modelViewNormal * vec3( 0.0, 0.0, 1.0 ) );

    // Transform to clip-space
    gl_Position = mvp * pos;
}
