[[FX]]

// Samplers
sampler2D tex0 = sampler_state
{
	Address = Clamp;
	Filter = Bilinear;
	MaxAnisotropy = 1;
};

// Contexts
context ATTRIBPASS
{
	VertexShader = compile GLSL VS_ATTRIBPASS;
	PixelShader = compile GLSL FS_ATTRIBPASS;
}

context SHADOWMAP
{
	VertexShader = compile GLSL VS_SHADOWMAP;
	PixelShader = compile GLSL FS_SHADOWMAP;
}

context LIGHTING
{
	VertexShader = compile GLSL VS_LIGHTING;
	PixelShader = compile GLSL FS_LIGHTING;

	ZWriteEnable = false;
	BlendMode = Add;
}

context AMBIENT
{
	VertexShader = compile GLSL VS_AMBIENT;
	PixelShader = compile GLSL FS_AMBIENT;
}

[[VS_SHADOWMAP]]
// =================================================================================================
	#include "shaders/utilityLib/vertCommon.glsl"

	uniform vec4 lightPos;
	attribute vec2 texCoords0;
	varying float dist;
	varying vec2 texCoords;

	void main( void )
	{
		vec4 pos = calcWorldPos( gl_Vertex );
		dist = length( lightPos.xyz - pos.xyz ) / lightPos.w;

		// Calculate texture coordinates and clip space position
		texCoords = texCoords0;
		gl_Position = gl_ModelViewProjectionMatrix * pos;
	}

[[FS_SHADOWMAP]]
// =================================================================================================

	uniform float shadowBias;
	uniform sampler2D tex0;
	varying float dist;
	varying vec2 texCoords;

	void main( void )
	{
		vec4 albedo = texture2D( tex0, texCoords );
		if( albedo.a < 0.5 ) discard;

		gl_FragDepth = dist + shadowBias;
	}

[[VS_ATTRIBPASS]]
// =================================================================================================
	#include "shaders/utilityLib/vertCommon.glsl"

	const float Eta = 0.67;         // Ratio of indices of refraction (air -> glass)
	const float FresnelPower = 10.0; // Controls degree of reflectivity at grazing angles
	const float F  = ((1.0 - Eta) * (1.0 - Eta)) / ((1.0 + Eta) * (1.0 + Eta));

	varying vec3  Reflect;
	varying vec3  Refract;
	varying float Ratio;
	attribute vec3 normal;
	uniform vec3 viewer;

	void main( void )
	{
	    vec4 worldPosition = calcWorldPos( gl_Vertex );
	    vec3 ecPosition3  = worldPosition.xyz - viewer;//viewer - worldPosition.xyz;
	    vec3 i = normalize(ecPosition3);
	    vec3 n = normalize( calcWorldVec(normal) );

	    Ratio = F + (1.0 - F) * pow((1.0 - dot(-i, n)), FresnelPower);

	    //Ratio = 1.0 - Ratio;
	    Refract = refract(i, n, Eta);
	    Refract = vec3(gl_TextureMatrix[0] * vec4(Refract, 1.0));
	    Reflect = reflect(i, n);
	    Reflect = vec3(gl_TextureMatrix[0] * vec4(Reflect, 1.0));

	    gl_Position   = gl_ModelViewProjectionMatrix * worldPosition;
	}

[[FS_ATTRIBPASS]]
// =================================================================================================
	varying vec3  Reflect;
	varying vec3  Refract;
	varying float Ratio;
	uniform samplerCube tex0;

	void main(void)
	{
	    vec3 refractColor = vec3 (textureCube(tex0, Refract));
	    vec3 reflectColor = vec3 (textureCube(tex0, Reflect));
	    vec3 color   = mix(refractColor, reflectColor, Ratio);

	    gl_FragColor = vec4(color, 1.0);
	}

[[VS_AMBIENT]]
// =================================================================================================
	#include "shaders/utilityLib/vertCommon.glsl"

	const float Eta = 0.8;         // Ratio of indices of refraction (air -> glass)
	const float FresnelPower = 2.0; // Controls degree of reflectivity at grazing angles
	const float F  = ((1.0 - Eta) * (1.0 - Eta)) / ((1.0 + Eta) * (1.0 + Eta));

	varying vec3  Reflect;
	varying vec3  Refract;
	varying float Ratio;
	attribute vec3 normal;
	uniform vec3 viewer;

	void main( void )
	{
		vec4 worldPosition = calcWorldPos( gl_Vertex );
		vec3 ecPosition3  = worldPosition.xyz - viewer;//viewer - worldPosition.xyz;
	    vec3 i = normalize(ecPosition3);
	    vec3 n = normalize( calcWorldVec(normal) );
	    Ratio = F + (1.0 - F) * pow((1.0 - dot(-i, n)), FresnelPower);

	    //Ratio = 1.0 - Ratio;
	    Refract = refract(i, n, Eta);
	    Refract = vec3(gl_TextureMatrix[0] * vec4(Refract, 1.0));
	    Reflect = reflect(i, n);
	    Reflect = vec3(gl_TextureMatrix[0] * vec4(Reflect, 1.0));
	    gl_Position   = gl_ModelViewProjectionMatrix * worldPosition;
	}

[[FS_AMBIENT]]
// =================================================================================================

	varying vec3  Reflect;
	varying vec3  Refract;
	varying float Ratio;
	uniform samplerCube tex0;

	void main(void)
	{
	    vec3 refractColor = vec3 (textureCube(tex0, Refract));
	    vec3 reflectColor = vec3 (textureCube(tex0, Reflect));
	    vec3 color   = mix(refractColor, reflectColor, Ratio);
	    gl_FragColor = vec4(color, 1.0);
	}

[[VS_LIGHTING]]
// =================================================================================================
	#include "shaders/utilityLib/vertCommon.glsl"

	uniform vec3 viewer;
	attribute vec2 texCoords0;
	attribute vec3 normal;
	varying vec3 tsbNormal;
	varying vec4 pos, vsPos;
	varying vec2 texCoords;

	void main( void )
	{
		// Calculate normal
		tsbNormal = calcWorldVec( normal );

		// Calculate world and view space positions
		pos = calcWorldPos( gl_Vertex );
		vsPos = calcViewPos( pos );

		// Calculate texture coordinates and clip space position
		texCoords = texCoords0;
		gl_Position = gl_ModelViewProjectionMatrix * pos;
	}

[[FS_LIGHTING]]
// =================================================================================================
	#include "shaders/utilityLib/fragLighting.glsl"

	varying vec3 tsbNormal;
	varying vec4 pos, vsPos;
	varying vec2 texCoords;

	void main( void )
	{
		gl_FragColor.rgb =
			calcPhongSpotLight( pos.xyz, normalize( tsbNormal ), vec3(0.3,0.3,0.3), 0.2, 16.0, -vsPos.z, 0.3 );
	}