////////////////////////////////////////////////////////////////////////////////////////////////////
// Oasis - dynamic lightmap fragment shader
////////////////////////////////////////////////////////////////////////////////////////////////////

// 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

#ifndef NO_PRECISION_DEFINITION
	#define HIGHP	highp
	#define MEDIUMP mediump
	#define LOWP	lowp
#else
	#define HIGHP	
	#define MEDIUMP 
	#define LOWP	
#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
                                     



////////////////////////////////////////////////////////////////////////////////////////////////////
uniform sampler2D     g_texture;                // PARAM_DEFAULT_TEXTURE1
uniform sampler2D     g_texture2;               // PARAM_DEFAULT_TEXTURE2
uniform sampler2D     g_texture3;               // PARAM_DEFAULT_TEXTURE3
uniform sampler2D     g_texture6;               // PARAM_DEFAULT_TEXTURE6 (alpha for g_texture)
uniform vec3          g_vCamPos;                // PARAM_CAM_POS



////////////////////////////////////////////////////////////////////////////////////////////////////
varying MEDIUMP vec4        vTexCoord;
varying MEDIUMP vec4        vColor;
varying MEDIUMP vec3        vWorldPos;



////////////////////////////////////////////////////////////////////////////////////////////////////
void main(void)
{
    // Sample texture
    vec4 vMarble = READ_MULTI_TEXTURE_ALPHA( g_texture, g_texture6, vTexCoord.xy );
    vec4 vSand = texture2D( g_texture2, vTexCoord.xy * 3.0 );
    vec4 vLightmap = READ_ALPHA_TEXTURE_ATLAS( g_texture3, vTexCoord.zw );
    
    vec3 vEye = normalize( g_vCamPos - vWorldPos.xyz );
    float fFresnel = max( dot( vEye, vec3( 0.0, -1.0, 0.0 ) ), 0.0 );
    fFresnel = min( fFresnel + 0.75, 1.0 ) * 0.90; // HDR
        
    // Compute floor color (blend in sand over marble)
    float fBlend = clamp( ( (vColor.a-0.3)*2.0 + (vMarble.a-0.6) ) * 1.5, 0.0, 1.0 );
    vec3 vFloor = mix( vSand.rgb, vMarble.rgb, clamp( (fBlend-0.5)*2.0, 0.0, 1.0 ) );

#ifdef BOOST_COLORS

    // Boost colors
    vec3 vFinalColor = vFloor * vLightmap.rgb * vLightmap.a;
    vec3 vHighPassColor = max( vFinalColor - 0.7, 0.0 );
    
    // Output final color
    gl_FragColor = vec4( max(vFinalColor+vHighPassColor - 0.02, 0.0) * 1.655, mix( 1.0, fFresnel, fBlend ) );

#else

    // Output final color
    gl_FragColor = vec4( vFloor * vLightmap.rgb * vLightmap.a, mix( 1.0, fFresnel, fBlend ) );

#endif

}


