////////////////////////////////////////////////////////////////////////////////////////////////////
// Nailah eye shader for oasis scene
////////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL ES require that precision is defined for a fragment shader

#ifndef NO_PRECISION_DEFINITION
	#ifdef GL_FRAGMENT_PRECISION_HIGH
	   precision highp float;
	#else
	   precision mediump float;
	#endif
#endif


// Custom macros to handle special cases
#ifdef SEPARATE_ALPHA_TEXTURE
	#define READ_MULTI_TEXTURE_ALPHA(_texture, _texture2, _uv) vec4(texture2D( _texture, _uv ).rgb, texture2D( _texture2, _uv ).r)
	#define READ_ALPHA_TEXTURE_ATLAS(_texture, _uv) vec4(texture2D( _texture, _uv * vec2(1.0, 0.5) ).rgb, texture2D( _texture, _uv * vec2(1.0, 0.5) + vec2(0.0, 0.5) ).r)
#else
	#define READ_MULTI_TEXTURE_ALPHA(_texture, _texture2, _uv) (texture2D( _texture, _uv ))	
	#define READ_ALPHA_TEXTURE_ATLAS(_texture, _uv) (texture2D( _texture, _uv ))
#endif


////////////////////////////////////////////////////////////////////////////////////////////////////
// uniforms
uniform sampler2D   g_texture;              // PARAM_DEFAULT_TEXTURE1   // Reflection
uniform sampler2D   g_texture2;             // PARAM_DEFAULT_TEXTURE2   // Albedo
uniform sampler2D   g_texture3;             // PARAM_DEFAULT_TEXTURE3   // Cloud
uniform vec3        g_vLightDir;            // PARAM_LIGHT_DIR1
uniform vec3        g_vLightDir2;           // PARAM_LIGHT_DIR2
uniform vec4        g_lightColor;           // PARAM_LIGHT_COLOR1
uniform vec4        g_lightColor2;          // PARAM_LIGHT_COLOR2
uniform vec3        g_vCamPos;              // PARAM_CAM_POS



////////////////////////////////////////////////////////////////////////////////////////////////////
// input
varying vec4        vCol;
varying vec4        vTC;
varying vec3        vNrm;
varying vec4        vWPos;
varying vec2        vReflTC;



////////////////////////////////////////////////////////////////////////////////////////////////////
// fragment program
////////////////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
    vec4 vAlbedo = READ_ALPHA_TEXTURE_ATLAS( g_texture2, vTC.xy ); 
    
    float fLightmap = texture2D( g_texture3, vTC.zw ).a;
    float fShadowDirect = max( fLightmap - 0.3, 0.0 ) * 3.0;
    float fShadowSpec = fShadowDirect*0.5 + 0.5;
   
    // Fetch texture
    vec3 vN = normalize( vNrm ); 
    float fDiff1 = max( dot( vN, g_vLightDir.xyz ), 0.0 );

    // Output final color
    vec3 texColor = vAlbedo.rgb * vCol.rgb * 2.0;             // Ambient
    texColor += vAlbedo.rgb * fDiff1 * fShadowDirect;   // Diff
    vec3 vSpec = texture2D( g_texture, vReflTC.xy ).rgb * 3.0; 
    vSpec *= vCol.a * fShadowSpec;
    gl_FragColor = vec4( texColor.rgb + vSpec, 1.0 );
}
