/* Phong Tessellation vertex shader

   Phong Tessellation reference : 
    Phong tessellation,
    Tamy Boubekeur and Marc Alexa
    ACM ToG, Vol. 27, No. 5, 2008.
*/

/* Only transforms norm by the rotation portion of the modelview matrix */
float3 transformNormal( float3 norm, float4x4 modelview )
{
        return float3 (
          dot(norm, float3(modelview[0][0], modelview[0][1], modelview[0][2])),
          dot(norm, float3(modelview[1][0], modelview[1][1], modelview[1][2])),
          dot(norm, float3(modelview[2][0], modelview[2][1], modelview[2][2]))
    );
}

void main( in float3 iPos   : POSITION,
           in float3 iNorm  : NORMAL,
           in float4 iColor : COLOR,
       
           uniform float4x4 uModelviewMatrix,
           uniform float4x4 uModelviewProjectionMatrix,
           uniform float2   uWh,
     
           out float3 oModelPos         : POSITION,
           out float4 oColor            : COLOR,
           out float3 oEyeNorm          : ATTR0,
           out float2 oScaledWindowPos  : ATTR1

       )
{
  
  /* Projected window space position for use in the LOD calculation. */
  precise float4 projPos = mul(uModelviewProjectionMatrix, float4(iPos,1));
  
  /* Project to screen space for use in LOD calculations */
  oScaledWindowPos = uWh * (projPos.xy / projPos.w);

  oModelPos = mul(uModelviewMatrix, float4(iPos, 1)).xyz;
  
  /* Use function to prevent unnecessary multiplications by 0 */
  oEyeNorm = normalize( transformNormal(iNorm, uModelviewMatrix) );

  /* Pass through application-specified color */
  oColor = iColor;
}

