//
// Motion blur vertex shader
// From: http://developer.nvidia.com/docs/IO/8230/GDC2003_OpenGLShaderTricks.pdf
// RvG, 28-11-2008
//

//#include "motion_blur_const.cg"

struct a2v
{
  float4 coord      : POSITION;
  float2 texture    : TEXCOORD0;
  float4 prevCoord  : TEXCOORD1;
  float3 normal     : NORMAL;
};

struct v2f
{
  float4 position   : POSITION;
  float4 P          : TEXCOORD0;
  float4 Pprev      : TEXCOORD1;
  float  scale      : TEXCOORD2;
  float  dt         : TEXCOORD3;
};

v2f main(a2v IN,
  uniform float4x4 modelViewProj,
  uniform float4x4 prevModelViewProj,
  uniform float4x4 modelMatrix,
  uniform float    scale,
  uniform float    dt
  //uniform float3   halfWinSize
)
{
  v2f OUT;
  // Transform previous and current pos to eye space
  float4 inCoord=IN.coord;
  //float4 inCoord=mul(modelMatrix,IN.coord);
  float4 P=mul(modelViewProj,inCoord);
  float4 Pprev=mul(prevModelViewProj,inCoord);

  // Pprev
  //inCoord=mul(modelMatrix,IN.coord);
  //Pprev=mul(prevModelViewProj,inCoord);

  // Movable objects
  //P=mul(modelMatrix,P);
  //Pprev=mul(modelMatrix,Pprev);
  
  // Geometry output adds any model matrix
  OUT.position=P;
  //OUT.position=mul(modelMatrix,P);

  // Do calculations of velocity per-pixel
  OUT.P=P;                // POSITION semantic is not present in pixel shader for input, so use TEXCOORD
  OUT.Pprev=Pprev;
  // Pass scaling of effect (for non-motion-blurred shaders)
  OUT.scale=scale;
  OUT.dt=dt;
  
  //inCoord=IN.coord;
  //P=mul(modelViewProj,inCoord);
  //Pprev=mul(prevModelViewProj,inCoord);
  return OUT;
}

/*
From PDF:

v2f main(a2v in,
  uniform float4x4 modelView,
  uniform float4x4 prevModelView,
  uniform float4x4 modelViewProj,
  uniform float4x4 prevModelViewProj,
  uniform float3 halfWinSize
)
{
  v2f out;
  // transform previous and current pos to eye space
  float4 P = mul(modelView, in.coord);
  float4 Pprev = mul(prevModelView, in.prevCoord);
  // transform normal to eye space
  float3 N = vecMul(modelView, in.normal);
  
  // calculate eye space motion vector
  float3 motionVector = P.xyz - Pprev.xyz;
  // calculate clip space motion vector
  P = mul(modelViewProj, in.coord);
  Pprev = mul(prevModelViewProj, in.prevCoord);
  // choose previous or current position based
  // on dot product between motion vector and normal
  float flag = dot(motionVector, N) > 0;
  float4 Pstretch = flag ? P : Pprev;
  out.hpos = Pstretch;
  // do divide by W -> NDC coordinates
  P.xyz = P.xyz / P.w;
  Pprev.xyz = Pprev.xyz / Pprev.w;
  Pstretch.xyz = Pstretch.xyz / Pstretch.w;
  // calculate window space velocity 
  float3 dP = halfWinSize.xyz * (P.xyz - Pprev.xyz);
  out.velocity = dP;
  return v2f;
}
*/
