
// DOF
varying vec2 v_vTex;

#ifdef VERTEX_SHADER

attribute vec4 a_vertex;

void main()
{
    gl_Position = vec4( a_vertex.x, -a_vertex.y, 0.0, 1.0 );
    v_vTex		= vec2( a_vertex.z, a_vertex.w );
}

#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER

uniform sampler2D u_tex0;					// Sharp
uniform sampler2D u_tex1;					// Blurred
uniform sampler2D u_tex2;					// Depth

uniform float u_fRange;
uniform float u_fFocus;

#ifdef CRICKETCAP
const float zfar = 30.0;
const float znear = 7.0;
#else
const float zfar = 1000.0;
const float znear = 1.0;
#endif

void main()
{
	vec4 vSharpColour = texture2D(u_tex0, v_vTex);
    vec4 vBlurColour  = texture2D(u_tex1, v_vTex);

	float fDepth = texture2D(u_tex2, v_vTex).z;
	fDepth = (2.0 * znear) / (zfar + znear - fDepth * (zfar - znear));
#ifdef CRICKETCAP
	fDepth = fDepth * fDepth * fDepth;
#else
	fDepth = (fDepth - 1.0);
	fDepth = fDepth * fDepth * fDepth + 1.0;
#endif

#ifdef DOFDEPTH
	if (v_vTex.x < 0.5)
	{
		gl_FragColor = mix( vBlurColour, vSharpColour, clamp( u_fRange * ( u_fFocus - fDepth ), 0.0, 1.0 ) );
	}
	else
	{
		//gl_FragColor = vec4(fDepth, 0.0, 0.0, 1.0 );
		gl_FragColor = vec4(clamp( u_fRange * ( u_fFocus - fDepth ), 0.0, 1.0 ));
			
	}
#elif defined (DOFSIDEBYSIDE)
	if (v_vTex.x < 0.5)
	{
		gl_FragColor = mix( vBlurColour, vSharpColour, clamp( u_fRange * ( u_fFocus - fDepth ), 0.0, 1.0 ) );
	}
	else
	{
		if(v_vTex.x < 0.501)
			discard;
		gl_FragColor = vSharpColour;
	}
#else
	gl_FragColor = mix( vBlurColour, vSharpColour, clamp( u_fRange * ( u_fFocus - fDepth ), 0.0, 1.0 ) );
#endif
}

#endif
