#version 410 core

layout(PATCH_TYPE, fractional_odd_spacing, ccw) in;

uniform mat4 uModelViewProjectionMatrix;
uniform mat4 uModelViewMatrix;

in vec3 vert_ES_in[];
out vec3 vObjPos;

void main()
{
  vec4
    cFinalVert = vec4(0.0);

  float
    rU = gl_TessCoord.x,
    rV = gl_TessCoord.y;

  float
    arB_u[] = float[DEGREEU+1](BASIS_U),
    arB_v[] = float[DEGREEV+1](BASIS_V);

  for (int iJ = 0; iJ <= DEGREEV; iJ++)
  {
    for (int iI = 0; iI <= DEGREEU; iI++)
    {
      vec4 
        vert_w = vec4(vert_ES_in[iI + iJ*(DEGREEU+1)], 1.0);

      float
        rB = arB_u[iI] * arB_v[iJ];

      cFinalVert = cFinalVert + vert_w*rB;
    }
  }

  vec3
    cFinalPos = cFinalVert.xyz/cFinalVert.w;

  vObjPos = vec3(uModelViewMatrix* vec4(cFinalPos , 1.0));
  gl_Position = uModelViewProjectionMatrix * vec4(cFinalPos, 1.0);
}
