//Author : Vrej Melkonian
//Date : Friday, June 25, 2004
//
//Vertex Shader, GLSL 1.00
//Derived from ARBvp1.0 with filename v_glass.txt
//

uniform vec4 ior;
uniform mat3 ModelviewTranspose;

vec4 MyRefraction( vec4 NDotV, vec4 N_eye, vec4 V_eye)
{
vec4 t;
t.x = (-NDotV.x) * NDotV.x + 1.0;
t.x = t.x + ior.y - 1.0;
t.x = sqrt (t.x);
t.x = ior.x * NDotV.x + t.x;
t = N_eye * t.x;
t = ior.x * (-V_eye) + t;

return vec4 (ModelviewTranspose * vec3 (t), 0.0);
}

vec4 FakeFresnelTerm( vec4 NDotV)
{
return (1.0 + NDotV) * (1.0 + NDotV);
}

//Texcoord0 receives vertex in eye space
//Texcoord1 receives reflection vector
//Texcoord2 receives refraction vector
//Texcoord3 receives Fresnel term
void main()
{
gl_Position = ftransform ();

// Vertex in eye space
vec4 V_eye = gl_ModelViewMatrix * gl_Vertex ;
gl_TexCoord [0]=V_eye;

// R = 2(N dot V)N - V or I - (N dot I)N
// Normal in eye space
vec4 N_eye = vec4 ( gl_NormalMatrix * gl_Normal , 0.0);
// Normalize
V_eye = vec4 ( normalize ( vec3 (V_eye)), 1.0);

vec4 NDotV;
NDotV.x = dot ( vec3 (N_eye), vec3 (V_eye));
NDotV = vec4 (NDotV.x, NDotV.x, NDotV.x, NDotV.x);

// Compute R
vec4 R = vec4 (- reflect ( vec3 (V_eye), vec3 (N_eye)), 0.0);

//Multiply R by transpose of modelview
gl_TexCoord [1].xyz = ModelviewTranspose * vec3 (R);

//Since we are using GLSL 1.00, the library doesn't
//contain a refract function, so we write our own
gl_TexCoord [2] = MyRefraction(NDotV, N_eye, V_eye);

//Fake Fresnel - (1 + NDotV)^2
gl_TexCoord [3] = FakeFresnelTerm(NDotV);
}