//
// Bumpy surface (normal map in texunit1)
// 22-09-08: Created (Ruud van Gaal)
// Edited by David I 9/1/09

void main(
	  float4 inPosition : POSITION,
	  float3 inNormal   : NORMAL,
	  float2 inTC0      : TEXCOORD0,
	  float3 inTangent  : TANGENT,
	  // Output
      out float4 outPosition      : POSITION,
      //out float3 outNormalOrg     : COLOR,
      //out float3 outEyePosW       : COLOR,
	  out float2 outTC0           : TEXCOORD0,
	  //out float3 outNormal        : TEXCOORD1,
	  out float3 outPosW          : TEXCOORD2,
	  out float  reflectionFactor : TEXCOORD3,
	  out float3 R                : TEXCOORD4,			// Reflection vector
	  out float3 lightDirTS       : TEXCOORD5,          // Light direction in tangent space
	  out float3 outEyeDir        : TEXCOORD6,			// Varying over polygon
  
  // Params
  uniform float4x4 modelViewProj,
  uniform float4x4 modelView,
  uniform float4x4 modelMatrix,
  uniform float4x4 modelMatrixInv,
  uniform float3   eyePosW,
  uniform float    time,
  uniform float3   lightDirection
)
{
  float fresnelBias=0.01;
  float fresnelScale=0.01;
  float fresnelPower=0.01;
float Sd=sin(1.2*time);
float Cd=cos(0.9*time+1);
float Td=cos(0.3*time+inPosition.y);
//sincos((0.3*time),Sd,Cd);
//float displacement = sin(distance(inPosition.xyz, float3 (0,0,0))) * 0.5 * (sin(0.3*time) + 1);
//float displacement = 0.5 * (sin(time) + 1);
//float displacement = 0.01;
float4 oPosition= inPosition;
oPosition.z= inPosition.z-0.5+(sin(distance(inPosition.xyz, float3 (0,inPosition.y,0))) * 0.5 * Sd)+(sin(distance(inPosition.xyz, float3 (inPosition.x,0,inPosition.z))) * 0.5 * Td);
oPosition.x= inPosition.x-0.5+(sin(distance(inPosition.xyz, float3 (0,inPosition.y,0))) * 0.5 * Cd)+(sin(distance(inPosition.xyz, float3 (inPosition.x,0,inPosition.z))) * 0.5 * Td);

  outPosition=mul(modelViewProj,oPosition);
   
  // Pass texcoords
  outTC0=inTC0;

  // Create coordinate system for object-> texture space
  // Also rotates along with the model
  float3 t=mul(float3x3(modelMatrix),inTangent);
  float3 n=mul(float3x3(modelMatrix),inNormal);
  float3 b=cross(t,n);
  
  // Create TBN matrix
  float3x3 tbnMatrix=float3x3(t,b,n);
  
  // Calculate lightDir in tangent space
  lightDirTS=mul(tbnMatrix,lightDirection);

  // Calculate eyeDir in tangent space
  float3 v;
  // First calculate eye to vertex direction in world coords
  v=eyePosW-mul(modelMatrix,inPosition).xyz;
  // Transform into surface coords
  outEyeDir=normalize(mul(tbnMatrix,v));
  
  // Specular approximation
  //outEyeDir=1.0-dot(normalize(lightDirTS),normalize(outEyeDir));
  
  // Convert eye world pos to tangent space
  //float3 epw=mul(modelView,float4(eyePosW,1)).xyz;
  //outEyePosW=mul(tbnMatrix,eyePosW);
  //outEyePosW=outEyeDir;

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

  // Compute the incident, reflected, and refracted vectors
  float3 I;
  I=posW-eyePosW;
  R=reflect(I,Nwc);		// Not working yet
  I=normalize(I);

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