//
// Wobble object (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 angle = (0.1*sin(time*30)+(0.08*cos(time*400))+(0.08*sin(time*0.1)))/3;
//  float sinLength, cosLength;
//  sincos(angle, sinLength, cosLength);

  float angle = (0.01*cos(time*1))/10;
  float sinLength, cosLength;
  sincos(angle, sinLength, cosLength);

  //moves the rotate center of the object slightly up the y axis, change or remove as necessary
  inPosition[1]-=0.400;
  float4 oPosition;
  //the 0.5 is an offset, u can change it as needed and/or add offsets to the [0] & [2] lines to offset in x & z
  oPosition[0] = 0.5+ (cosLength * (inPosition[0]) +  -sinLength * (inPosition[1]));
  oPosition[1] =  (sinLength * (inPosition[0]) +   cosLength * (inPosition[1]));

  oPosition[2] = inPosition[2];
  oPosition[3] = inPosition[3];

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

float3 iNormal= inNormal;

float3 oNormal= iNormal;
  // 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));
  

  /*
  // Fresnel
  // 
  // Compute position and normal in world space
  float3 posW=mul(modelMatrix,inPosition).xyz;
  float3 Nwc=mul(float3x3(modelMatrix),inNormal);
  // Compute the incident, reflected, and refracted vectors
  float3 I;
  I=posW-eyePosW;
  I=normalize(I);

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