//Author : Vrej Melkonian
//Date : Friday, June 25, 2004
//
//Fragment Shader, GLSL 1.00
//Derived from ARBfp1.0 with filename f_glass.txt
//

uniform sampler2D Tex0;
uniform sampler1D Tex1;
uniform samplerCube Tex2;
uniform vec4 Blur;
uniform vec2 InvTex0Dimensions;

void main()
{
//Fetch backface depth with normalized
//fragment coordinate
vec2 Texcoord;
Texcoord = gl_FragCoord .xy * InvTex0Dimensions;
vec4 z0 = texture2D (Tex0, Texcoord);

//Get frontface depth
vec4 z1;
z1.x = dot ( gl_TexCoord [0], gl_TexCoord [0]);
z1 = vec4 (z1.x, z1.x, z1.x, z1.x);
z1.w = inversesqrt (z1.w);
z1.x = z1.x * z1.w;

//Calculate estimated thickness
float Thickness = clamp ( abs ((z1.x - z0.x) * 0.25), 0.0, 1.0);

//Do refraction lookup
vec4 n = - gl_TexCoord [2];
n.w = Thickness * Blur.x;
vec4 Refract = textureCube (Tex2, n.xyz, n.w);

//Lookup thickness in 1D texture to get color
vec4 Tint = texture1D (Tex1, Thickness);
Refract *= Tint;

//Do reflection lookup
vec4 Reflect = textureCube (Tex2, gl_TexCoord [1].xyz);

//Apply Fresnel term, add reflection adn refraction
gl_FragColor = Reflect * gl_TexCoord [3] + Refract;
}