// FRAGMENT SHADER :  TEXTURE + ENVIRONMENT + BUMP

// how do we mix the color from the environment and from the diffuse texture
// (can be changed into a uniform)
uniform float fMixEnvDiff;	// has to be in range [0-1]

uniform sampler2D   diffMap2D;
uniform sampler2D   normalMap2D;
uniform samplerCube envMapCUBE;

uniform float specular;
uniform float shininess;
uniform float textureScale;
uniform vec4 matColor;

varying vec4 vLight0;
varying vec4 vHalfAngleVec0;
varying vec4 vLight1;
varying mat3 matTBN;
varying vec3 vVertex;
varying vec3 vNormal;

vec3 reflect(vec3 vI, vec3 vN)
{
	return vI - 2.0 * vN*dot(vN, vI);
}

vec2 tGetAutomaticTexCoords(vec3 pos, vec3 normal)
{
      float dotX = dot(normal,vec3(1.0,0.0,0.0));
      float dotY = dot(normal,vec3(0.0,1.0,0.0));
      float dotZ = dot(normal,vec3(0.0,0.0,1.0));
      float adotX = abs(dotX);
      float adotY = abs(dotY);
      float adotZ = abs(dotZ);
      
      vec3 npos = pos - vec3(0,0,0);
      vec2 texCoordOut = npos.xy;
      if(dotZ > 0.0)
      {
            texCoordOut = npos.xy;
      }
      else
      {
            texCoordOut = vec2(npos.x,-npos.y);
      }
      if(adotX > adotY)
      {
            if(adotX > adotZ)
            {
                  if(dotX > 0.0)
                  {
                        texCoordOut = npos.yz;
                  }
                  else
                  {
                        texCoordOut = vec2(-npos.y,npos.z);
                  }
            }
      }
      else if(adotY > adotZ)
      {
            if(dotY > 0.0)
            {
                  texCoordOut = vec2(-npos.x, npos.z);
            }
            else
            {
                  texCoordOut = vec2(npos.x,npos.z);
            }
      }
      return texCoordOut;
}

void main()
{
	vec2 localTexCoord = tGetAutomaticTexCoords(vVertex.xyz, vNormal.xyz);

	// fetch the bump vector
	vec3 bumpNormal = vec3(texture2D(normalMap2D, localTexCoord.xy*textureScale, 0.0)) - vec3(0.5);
	bumpNormal *= 2.0;
	bumpNormal = normalize(bumpNormal);	// avoid weird specular pixels

	//HACK: vViewVec is passed in other varying to bypass resource limitations
	vec3 vViewVec = normalize(vec3(vLight0.w, vHalfAngleVec0.w, vLight1.w));

	// transform from Tangent space to World space
	// Note the order of the multiplication : the inverse of an othonormal matrix is its transpose
	vec3 perturbN = normalize(matTBN * bumpNormal);
	
	vec3 reflectVec = reflect(vec3(0,0,1), perturbN);
	
	// calculate reflection and look up 
	vec4 colorEnv = textureCube(envMapCUBE, reflectVec);
	// Fetch texture color from diffuse
	vec4 colorDiff = texture2D(diffMap2D, localTexCoord.xy*textureScale, 0.0);

	vec4 color = mix(colorEnv, colorDiff, fMixEnvDiff);


	vec4 spec = vec4(0.0);
	vec4 illumination = vec4(0.0);

// Calc light0 products
	float LdotB = clamp(dot(vLight0.xyz, bumpNormal), 0.0, 1.0);

// Look up illumination
	illumination += LdotB * vec4(matColor.xyz, 1);

	float ndoth = dot(bumpNormal, vHalfAngleVec0.xyz);
	ndoth = clamp(ndoth, 0.0, 1.0);
	ndoth = pow(ndoth, shininess);
	spec += ndoth * vec4(specular, specular, specular, 1.0);

// Combine all
//	gl_FragColor = clamp(spec + (illumination * color * 1.5) + color*gl_FrontLightModelProduct.sceneColor, 0.0, 1.0);
	gl_FragColor = clamp(spec + (illumination * color), 0.0, 1.0);
}