//
// Fresnel+decal texture
// Per-fragment lighting
//

void main(
	  float4 inPosition : POSITION,
	  float3 inNormal   : NORMAL,
	  float2 inTC0      : TEXCOORD0,
	  // Output
      out float4 outPosition      : POSITION,
      //out float4 outColor         : COLOR,
	  out float2 outTC0           : TEXCOORD0,
	  out float3 outNormal        : TEXCOORD1,
	  out float3 outPosW          : TEXCOORD2,
	  out float  reflectionFactor : TEXCOORD3,
  
  // Params
  uniform float4x4 modelViewProj,
  uniform float4x4 modelMatrix,
  uniform float3   eyePosW
)
{
  float fresnelBias=0;
  float fresnelScale=1.0;
  float fresnelPower=4.0;
  
  // Transform vertex
  outPosition = mul(modelViewProj,inPosition);
  outPosW = mul(modelMatrix,inPosition).xyz;			// Object space to eye space with model transform
  
  // Pass texcoords
  outTC0 = inTC0;

  // Transform normal with model transformation
  outNormal = mul(float3x3(modelMatrix),inNormal);		// Don't translate

  // Fresnel
  // 
  // Compute position and normal in world space
  float3 posW = mul(modelMatrix, inPosition).xyz;
  float3 N = mul((float3x3)modelMatrix, inNormal);
  N=normalize(N);

  // Compute the incident, reflected, and refracted vectors
  float3 II;
  II=posW-eyePosW;
  // R=reflect(I,N);
  II=normalize(II);

  // Compute reflection factor according to Fresnel
  reflectionFactor=fresnelBias+fresnelScale*pow(1+dot(II,N),fresnelPower);
}
