// KNBuildFromDarkness.vert
#version 100

precision highp float;

uniform mat4 MVPMatrix;

attribute vec3 Position;
attribute vec2 TexCoord;

varying vec2 v_TexCoord;

#ifndef MOTION_BLUR
#define MOTION_BLUR 0
#endif
#if MOTION_BLUR
uniform vec2 VelocityScale;
uniform mat4 PreviousMVPMatrix;
varying vec2 v_Velocity;
#endif

void main()
{       
    /* output */
    gl_Position = MVPMatrix * vec4(Position, 1);
    v_TexCoord = TexCoord;
    
#if MOTION_BLUR
    vec2 thisPos = gl_Position.xy / gl_Position.w;
    vec4 prevPos = PreviousMVPMatrix * vec4(Position, 1);
    prevPos.xy /= prevPos.w;
    
    vec2 veloc = (thisPos - prevPos.xy) * VelocityScale;
    
    /* Clamp the velocity to a max magnitude of 1.0. */
    float vmax = max(abs(veloc.x), abs(veloc.y));
    veloc = veloc.xy/max(vmax, 1.0);
    /* Remap from [-1,1] to [0,1] */
    veloc = vec2(0.5) + 0.5*veloc;
    
    v_Velocity = veloc;
#endif
}    
