////////////////////////////////////////////////////////////////////////
//
//   Harvard Computer Science
//	    CS 175: Introduction to Computer Graphics
//   Professor: Hanspeter Pfister
//         TFs: Moritz Baecher, Yuanchen Zhu, Kalyan Sunkavalli, Kevin Dale
//
//   File: wet_object.frag
//   Name: Felix Gonda (felix.e.gonda@gmail.com)
//   Date: Dec 3rd, 2010
//   Desc: fragment shader for wet object
//   
//////////////////////////////////////////////////////////////////////////
#version 110

#define MAX_LIGHTS 5

uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform sampler2D waterBump;
uniform sampler2D detailedNormalMap;
uniform float TimeScale;
uniform float wetness;

varying vec3 lightDir[MAX_LIGHTS];
varying vec3 viewDir;
varying vec4 vertexColor;

void main()
{

    int lightTypes[MAX_LIGHTS];
    
    // note: 1=spotlight, 0=pointlight
    lightTypes[0] = 0;  // point light
    lightTypes[1] = 0;  // point light
    lightTypes[2] = 0;  // point light
    lightTypes[3] = 0;  // point light
   
    
    vec2 Texcoord = gl_TexCoord[0].st;
	gl_FragColor = texture2D(colorMap, Texcoord);

    float const1=1.0;
    float tiling1 = 1.0;
    vec2 tiledTexcoord1 = vec2(Texcoord.x*tiling1,Texcoord.y);
    vec2  rumbleSphereMapTex = texture2D( waterBump, tiledTexcoord1 ).xy;
    rumbleSphereMapTex = rumbleSphereMapTex * const1;


   // pan the uv
   float speedX=clamp(wetness, 0.0, 2.0);
   float speedY=0.0;
   vec2 speed = vec2(speedX, speedY);
   vec2 panner = speed * TimeScale;// + Texcoord;

   
   vec2 pannerTexcoord = rumbleSphereMapTex + panner;
   
   float const2 = 4.0;
   vec4 waterBumpTex = texture2D( waterBump, pannerTexcoord );
   waterBumpTex = waterBumpTex * const2;  
         
   vec4 normalTex = texture2D( normalMap, Texcoord );
   
   float const4=6.0;
   float alpha = normalTex.z*const4*0.4;
   alpha = alpha * alpha;
   
   vec4 normalColor = mix(normalTex, waterBumpTex, alpha);
          

    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    vec3 l,n,v,h;
    float nDotL;
    float nDotH;
    float power;
    float dist;
    
    int i;
    // Sample all lights in the environment
    for(i=0; i<MAX_LIGHTS; i++)
    {
		// attenuate base on distance from light source
        dist = length(lightDir[i]);
		float atten = 1.0/dist;//max(0.0, 1.0 - dot(l, l));

		l = lightDir[i];

		l = normalize(l);
		n = normalize(texture2D(normalMap, gl_TexCoord[0].st).xyz * 2.0 - 1.0) * normalColor.xyz;
		v = normalize(viewDir);
		h = normalize(l + v);

		nDotL = max(0.0, dot(n, l));
	    
		if (nDotL > 0.0)
		{
		    // Compute spotlight attenuation based on fall inner, outer angles
		    // and distance of light from the pixel
		    if (lightTypes[i] == 1)
		    {
               float currentAngle = dot(normalize(gl_LightSource[i].spotDirection), -l);
               float innerAngle = gl_LightSource[i].spotCosCutoff;
               float outerAngle = 0.8;
               float innerMinusOuter = innerAngle - outerAngle;
			   atten = atten*clamp((currentAngle - outerAngle) / innerMinusOuter, 0.0, 1.0);               
		    }

			nDotH = max(0.0, dot(n, h));
			power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);
		    
			ambient += gl_FrontLightProduct[i].ambient * atten;
			diffuse += gl_FrontLightProduct[i].diffuse * nDotL * atten;
			specular += gl_FrontLightProduct[i].specular * power * atten;

		}
    }
    
    vec4 color = gl_FrontLightModelProduct.sceneColor + ambient + diffuse;// + specular;
    
    float wetness=0.154;
    vec4  fvBaseColor = texture2D( colorMap, Texcoord )*wetness;
    
    gl_FragColor = vertexColor*color*fvBaseColor;
}
