varying vec2 v_texture;
varying vec2 v_texture1;
varying vec3 v_normal;
#ifndef DISABLE_SHADOW_MAPPING
varying vec4 v_shadowCoords;
#endif

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////					   VERTEX SHADER						/////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#ifdef VERTEX_SHADER

uniform mat4 u_mvpMatrix;
uniform mat4 u_mvMatrix;
#ifndef DISABLE_SHADOW_MAPPING
uniform mat4 u_lightMatrix;
#endif

attribute vec4 a_vertex;
attribute vec3 a_normal;
attribute vec4 a_colour;
attribute vec2 a_texture;
attribute vec2 a_texture1;

void main()
{
    // Transform object-space normals to eye-space
    vec4 vNormal = u_mvMatrix * vec4(a_normal, 0.0);

    // Pass everything off to the fragment shader
    gl_Position = u_mvpMatrix * a_vertex;
    v_normal	= normalize(vNormal.xyz);
	v_texture	= a_texture;
	v_texture1  = a_texture1;
#ifndef DISABLE_SHADOW_MAPPING
	v_shadowCoords = u_lightMatrix * a_vertex;
#endif

}
#endif // VERTEX_SHADER

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////					  FRAGMENT SHADER						/////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#ifdef FRAGMENT_SHADER

uniform sampler2D u_texCrowd0;
uniform sampler2D u_texSeat1;
#ifdef SPLIT_ALPHA
uniform sampler2D u_texCrowdAlpha2;
#endif
uniform vec3 u_LightDir;
uniform vec3 u_AmbientMat;

#ifndef DISABLE_SHADOW_MAPPING
  #ifdef GLES
    #ifdef GL_EXT_shadow_samplers
      #define SHADOWSAMPLER sampler2DShadow
      #define SHADOW2DPROJ(a,b) shadow2DProjEXT(a,b)
    #else
      #define SHADOWSAMPLER sampler2D
      #define SHADOW2DPROJ(a,b) clamp((b.w * texture2DProj(a, b).x - b.z) * 1000000.0 + 1.0, 0.0, 1.0)
    #endif
  #else
    #define SHADOWSAMPLER sampler2DShadow
    #define SHADOW2DPROJ(a,b) shadow2DProj(a,b).r
  #endif
  uniform lowp SHADOWSAMPLER u_tex5;
#endif

void main()
{
#ifndef DISABLE_SHADOW_MAPPING
	float fLightDepth = SHADOW2DPROJ( u_tex5, v_shadowCoords );
	fLightDepth += 1.0;
	fLightDepth *= 0.5;
#endif

#ifdef SPLIT_ALPHA
	vec4 vTexCrowd = vec4(texture2D(u_texCrowd0, v_texture).rgb, texture2D(u_texCrowdAlpha2, v_texture).a);
	vec4 vTexDiffuse = mix(texture2D(u_texSeat1, v_texture1), vTexCrowd, vTexCrowd.a);
	//vec4 vTexDiffuse = mix(texture2D(u_texSeat1, v_texture1), texture2D(u_texCrowd0, v_texture), texture2D(u_texCrowdAlpha2, v_texture1).a);
#else
	vec4 vTexDiffuse = mix(texture2D(u_texSeat1, v_texture1), texture2D(u_texCrowd0, v_texture), texture2D(u_texCrowd0, v_texture).a);
#endif
	
    // Compute the lighting in eye-space
	float fLightMult  = max( 0.0, dot( v_normal, u_LightDir ) );
#ifndef DISABLE_SHADOW_MAPPING
	fLightMult *= fLightDepth;
#endif
    
    // Combine lighting with the material properties
	vec3 vAmbient = vTexDiffuse.rgb * u_AmbientMat;
    gl_FragColor  = vec4(vAmbient + vTexDiffuse.rgb * fLightMult, vTexDiffuse.a);
}
#endif
