
// this line is needed to get the associated xml files
#define SW_PARAMETER_FILES "global.xml, envmapcommon.xml, commonlighting.xml, perpixelparameters.xml"
#define SW_INCLUDE "utils.glsl,common.glsl"
#define SW_SHADERFORMATERIAL






// TEMPLATE desciption
//
// this shader is doing most of it's work in the pixel shader
// it is more costly but more accurate than the per-vertex one
// Appart that, it's more or less doing the same thing


/////////////////////////////////////////////////////////////////////////////////
// varying
// this parameters are passed from the vertex to the pixel shader
// note that they are not always used, depeding on the configuration

// texture coordinate coming from the mesh (or stream) and interpolated per-pixel
// this is the one generated by parasolid
// NOTE: w is used for reflection fading (distance from floor term)
varying vec4 v_TexCoord;

// eye direction
varying vec3 v_EyeDirection;
// position in Eye Space
varying vec3 v_PositionEyeSpace;

varying vec3 v_Normal;

varying vec3 v_Tangent;

// ShadowMap
varying vec4 v_projCoordSM;

// AO ShadowMap : unfortunately gl_FragCoord makes ATI switch to software :( so we need to consume a varying
varying vec4 v_fragCoord;

// Reflective Floor
varying vec4 v_projCoordRF;

// floor radius fading
varying vec4 v_FloorPoint;

// floor radius fading
varying vec2 v_patternCoord;

/////////////////////////////////////////////////////////////////////////////////

// do we need the maxSpec value?
const bool	ga_bUseMaxSpec =
	(g_iFinalColorMode==e_FinalColorMode_Fac_SaturateAD_ModifyAD)
	|| (g_iFinalColorMode==e_FinalColorMode_Fac)
	||(g_iAlphaType==e_AlphaType_Glass);










#ifdef _VERTEX_PROGRAM
void main()
{
	///////////////////////////////////////////////////////////////////////
	// first, do some space-related computation to prepare for further use

	// get texture coords
	v_TexCoord.xyz = GetCommonTexCoord(gl_MultiTexCoord0, gl_Vertex, g_iTextureCoordinates, gl_TextureMatrix[0], gl_TextureMatrix[4]);

	// normal
	v_Normal = gl_Normal;

	if(g_iTextureCoordinates == e_TextureCoordinates_UV)
	{
		v_Tangent = vec3(gl_MultiTexCoord1);
	}
	else
	{
		v_Tangent = oGetTangent(v_TexCoord.xyz, v_Normal, g_iTextureCoordinates, gl_TextureMatrix[5]);
	}

	// transform the vertex position
	gl_Position = pGetProjectedPosition(gl_Vertex, gl_ModelViewProjectionMatrix);
	
#ifdef SW_SHADER_USE_CLIPVERTEX
	// set position for clip planes
	gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
#endif
	
	// get eye information
	EyeInfo_s eyeInfo = GetEyeInfo(gl_Vertex, gl_ModelViewMatrix);
	v_PositionEyeSpace = eyeInfo.ePosition.xyz;
	v_EyeDirection = eyeInfo.eEyeDirection;
	///////////////////////////////////////////////////////////////////////

	// ShadowMap
	if(g_bDynamicShadows)
	{
		v_projCoordSM = gl_TextureMatrix[1] * eyeInfo.ePosition;
	}

	// Ambient Occlusion Shadow term
	if (g_bAOShadow)
	{
		// gl_FragCoord makes ATI switch to software :( so we need to consume a varying
		v_fragCoord = gl_Position;
	}

	// Reflective Floor
	if(g_bPlanarReflection)
	{
		v_projCoordRF = gl_TextureMatrix[2] * gl_Vertex;
	}

	// Scene floor edge fading
	if (g_bPlanarFadeEdge)
	{
		v_FloorPoint = gl_ModelViewMatrix * vec4(floorPoint.xyz, 1.0);
	}
	
	if(g_bReflectionFading)
	{
		vec4 worldVertex = gl_TextureMatrix[3] * eyeInfo.ePosition;
		float planeDist = dot(vec3(worldVertex)-vec3(floorPoint), vec3(floorNormal));
		v_TexCoord.w = planeDist*floorNormal.w;	
	}
	///////////////////////////////////////////////////////////////////////
	
	if(g_cosmeticAlpha)
	{
		v_patternCoord = getCosmeticTexcoord(gl_Vertex,e2cCosmeticTextureTransform);
	}
	
}
#endif //_VERTEX_PROGRAM

















#ifdef _FRAGMENT_PROGRAM
void main()
{
	// we should not always use the tangent
	vec2 texCoords = vec2(0.0,0.0);
	if(g_iTextureCoordinates != e_TextureCoordinates_None)
	{
		texCoords = tGetTexCoords(v_TexCoord.xyz, v_Normal, g_iTextureCoordinates, gl_TextureMatrix[0]);
	}

	// we should not always use the tangent
	vec3 tangent = normalize(v_Tangent);

	// generate TBN matrix, all the lighing computation are done in tangeant space
	// that's why we need a way to transform eye posision/vector into tangent space posision/vector.
	mat3 t2eMatrixTBN = t2eGetMatrixTBN(gl_NormalMatrix, v_Normal, tangent, (doubleSided != 0.0));

	///////////////////////////////////////////////////////////////////////

	vec2 newTexCoord;
	if(g_iParallaxMode == e_ParallaxMode_OcclusionVariable || g_iParallaxMode == e_ParallaxMode_OcclusionFixed)
	{
		// Compute view vector (in view space):
		vec3 viewDir = vec3(t2eMatrixTBN[0][2], -t2eMatrixTBN[1][2], t2eMatrixTBN[2][2]);

		// Compute initial parallax displacement direction:
		vec2 vParallaxDirection= normalize(viewDir.xy);
   
		// Compute the length of parallax displacement vector
		// i.e. the amount of parallax displacement.
		float fParallaxLength = -sqrt( 1.0 - viewDir.z * viewDir.z ) / viewDir.z; 
   
		// Compute parallax bias parameter which allows
		// the artists control over the parallax perspective
		// bias via an artist editable parameter 'fPerspectiveBias'
		// Use view vector length in tangent space as an offset 
		// limiting technique as well:
		float parallaxPerspectiveBias = 0.5;
		float fParallaxBias = parallaxPerspectiveBias + (1.0 - parallaxPerspectiveBias) * ( 2.0 * viewDir.z - 1.0 );
   
		// Compute the actual reverse parallax displacement vector:
		vec2 parallax = -vParallaxDirection * fParallaxLength * fParallaxBias * parallaxOffset; 

		float x;
		float y;
		float xh;
		float yh;
		if(g_iParallaxMode == e_ParallaxMode_OcclusionVariable)
		{
			// Compute height map profile for 8 samples:
			// Note that the height map is contained in the alpha channel
			// of the input normal map:
			vec2 tc = texCoords;
			float h1  = texture2D( normalMap2D, tc ).w;
			float h3 = h1;
			float inc = 1.0/(8.0 + (1.0 - abs(dot(t2eMatrixTBN[2],viewDir)))*(24.0 - 8.0)*(parallaxOffset*parallaxPrecision));
			float h = 0.0;
			tc -= vec2(parallax*inc);
			float h2 = texture2D( normalMap2D, tc).w;
			while(h <= 1.0)
			{
				if(h2 >= 1.0 - (h + inc))
					break;
				h += inc;
				h1 = h2;
				tc -= vec2(parallax * inc);
				h2 = texture2D( normalMap2D, tc ).w;
			}
			y = 1.0 - h;
			x =  y - inc;
			xh = h2;
			yh = h1;
		}
		else if(g_iParallaxMode == e_ParallaxMode_OcclusionFixed)
		{
			float h0 = texture2D( normalMap2D, texCoords - parallax * 1.000 ).w;
			float h1 = texture2D( normalMap2D, texCoords - parallax * 0.875 ).w;
			float h2 = texture2D( normalMap2D, texCoords - parallax * 0.750 ).w;
			float h3 = texture2D( normalMap2D, texCoords - parallax * 0.625 ).w;
			float h4 = texture2D( normalMap2D, texCoords - parallax * 0.500 ).w;
			float h5 = texture2D( normalMap2D, texCoords - parallax * 0.375 ).w;
			float h6 = texture2D( normalMap2D, texCoords - parallax * 0.250 ).w;
			float h7 = texture2D( normalMap2D, texCoords - parallax * 0.125 ).w;

			// Determine the section in the height profile that contains
			// a sample that is higher than the view vector. This determines
			// the correct displacement point:    
 
			if      (h7 > 0.875) { x = 0.937; y = 0.938; xh = h7; yh = h7; }
			else if (h6 > 0.750) { x = 0.750; y = 0.875; xh = h6; yh = h7; }
			else if (h5 > 0.625) { x = 0.625; y = 0.750; xh = h5; yh = h6; }
			else if (h4 > 0.500) { x = 0.500; y = 0.625; xh = h4; yh = h5; }
			else if (h3 > 0.375) { x = 0.375; y = 0.500; xh = h3; yh = h4; }
			else if (h2 > 0.250) { x = 0.250; y = 0.375; xh = h2; yh = h3; }
			else if (h1 > 0.125) { x = 0.125; y = 0.250; xh = h1; yh = h2; }
			else                 { x = 0.000; y = 0.125; xh = h0; yh = h1; }
 
		}

		// Compute the intersection between the view vector and the highest
		// sample point in the height profile:
		float fParallaxEffect = (x * (y - yh) - y * (x - xh))/((y - yh) - (x - xh));

		newTexCoord = texCoords - parallax * (1.0 - fParallaxEffect );
	}
	else if(g_iParallaxMode == e_ParallaxMode_Offset)
	{
		vec3 viewDir = vec3(t2eMatrixTBN[0][2], -t2eMatrixTBN[1][2], t2eMatrixTBN[2][2]);
		newTexCoord = tGetDispTexCoords(normalMap2D, texCoords, viewDir, parallaxOffset);
	}
	else
	{
		newTexCoord = texCoords;
	}



	// Fetch bump normal
	vec3 tBumpNormal;
	vec3 tMicroFlakeNormal;
	float alpha = 1.0f;
    if(g_bUseNormalMap)
	{
		tBumpNormal = tGetBumpNormal(normalMap2D, newTexCoord, g_bNormalHack);
		if(g_bUseBumpMapAsStencil)
			alpha = tGetBumpMapStencil(normalMap2D, newTexCoord);

		if(g_bMicroFlakeNormalMap)
		{
			tMicroFlakeNormal = tBumpNormal;
		}
	}
	else
	{
		tBumpNormal = t2eMatrixTBN[2];
	}	

	vec4 mixColor;
//	vec3 viewDir;
	if(g_bMixColor2)
	{
/*
		if(g_bUseNormalMap)
		{
			viewDir = vec3(t2eMatrixTBN[0][2], -t2eMatrixTBN[1][2], t2eMatrixTBN[2][2]);
		}
		else
		{
			viewDir = vec3(0.0,0.0,1.0);
		}
*/
		mixColor = getMixColor(matColor, matColor2, colorMixFactor, t2eMatrixTBN[2], vec3(0.0,0.0,1.0));
	}
	else
	{
		mixColor = matColor;
	}
	float fresnelReflect;
	float fresnelColor = 0.0;
	float l_reflectModifier;

	vec3 lightNormal;
	if(g_bMicroFlakeNormalMap)
	{
		lightNormal = t2eMatrixTBN[2];
	}
	else
	{
		lightNormal = tBumpNormal;
	}
	float fresnel;
	if(g_iFresnelType == e_FresnelType_CarPaint || g_iFresnelType == e_FresnelType_RevCarPaint)
	{
		if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
		{
			vec3 fresnelNormal = t2eMatrixTBN*lightNormal;
			fresnelReflect = GetFresnel_New(fresnelNormal, fresnelCoef1);
			fresnelColor = GetFresnel_New(fresnelNormal, fresnelCoef2);
		}
		else
		{
			fresnelReflect = GetFresnel_New(t2eMatrixTBN[2], fresnelCoef1);
			fresnelColor = GetFresnel_New(t2eMatrixTBN[2], fresnelCoef2);
		}
		if(g_iFresnelType == e_FresnelType_RevCarPaint)
		{
			l_reflectModifier = clamp(reflectModifier - fresnelReflect,0.0,1.0);
		}
		else if(g_iFresnelType == e_FresnelType_CarPaint)
		{
			l_reflectModifier = clamp(reflectModifier + fresnelReflect,0.0,1.0);
		}
		else
		{
			l_reflectModifier = reflectModifier;
		}
		mixColor = mixColor*(1.0 - fresnelColor) + fresnelColor;
	}
	else
	{
		if(g_iFresnelType!=e_FresnelType_None)
		{
			if(g_bUseNormalMap)
			{
				vec3 fresnelNormal = t2eMatrixTBN*lightNormal;
				fresnel = GetFresnelTerm(fresnelNormal, transparency, vec2(fresnelCoef1,fresnelCoef2), g_iFresnelType);
			}
			else
			{
				fresnel = GetFresnelTerm(t2eMatrixTBN[2], transparency, vec2(fresnelCoef1,fresnelCoef2), g_iFresnelType);
			}
		}
		l_reflectModifier = reflectModifier;
	}

		
	///////////////////////////////////////////////////////////////////////
	// light computation
	
	// init light value to zero	
	vec3 diffuseLight = vec3(0.0);
	vec4 specularLight = vec4(0.0);
	vec4 microFlakeSpec = vec4(0.0);
		
	// add light influence, this could be done in a loop really
	// for each light it's same story: standard diffuse and specular term are computed in eye space
	// light 0
	vec3 tLightDir = vec3(0,0,1);
	vec3 tHalfVector = vec3(0,0,-1);
	///////////////////////////////////////////////////////////////////////
	// space related computaion for lights
	if(g_bUseLight0)
	{
		if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
		{
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir0), v_PositionEyeSpace, lightType.x);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
		}
		else
		{
			tLightDir = eGetLightDir_DP(vec3(lightDir0), v_PositionEyeSpace, lightType.x);
			tHalfVector = eGetHalfVector(tLightDir, v_EyeDirection);
		}
		AddSpecularAndDiffuse(lightNormal,
			tLightDir, vec3(lightColor0), g_iAlphaType,
			tHalfVector, vec3(lightSpec0), specularMaterial.w, ga_bUseMaxSpec, g_noSpecular,
			diffuseLight, specularLight, transparency);
		if(g_bUseNormalMap && g_bMicroFlakeNormalMap)
		{
			// add in the specular from the micro flake
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir0), v_PositionEyeSpace, lightType.x);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
			microFlakeSpec += GetSpecularFromOneLight( tMicroFlakeNormal, tHalfVector, microFlakeShininess, vec3(lightSpec0*microFlakeColor), false);
		}
	}

	// ShadowMap for light 0
	if(g_bDynamicShadows)
	{
		// dynamic test with uniform here so that we dont have to regenerate a different shader just to temporarily hide shadow
		if (isDynamicShadowVisible > 0.0)
		{
			float shadowColor = GetShadowColor(shadowMap2D, v_projCoordSM.xyz, shadowMapScale);

	//		float dpLight = clamp(dot(normalize(lightDir0.xyz), t2eMatrixTBN[2])*10.0, 0.0, 1.0);
	//		shadowColor = mix(1.0, shadowColor, dpLight);

			// modulate diffuse and specular term
			diffuseLight.xyz *= shadowColor;
			specularLight.xyz *= shadowColor;
		}
	}


	float parallaxShadow;
	if((g_iParallaxMode == e_ParallaxMode_OcclusionFixed || g_iParallaxMode == e_ParallaxMode_OcclusionVariable) && g_bDynamicShadows)
	{
		vec2 light = tLightDir.xy * parallaxOffset;
		// Compute the blurry shadows (note that this computation takes into 
		// account self-occlusion for shadow computation):
		float shadowSoftening = 0.4;
		float h0 =  texture2D( normalMap2D, newTexCoord).w;
		float hA = (texture2D( normalMap2D, newTexCoord + light * 0.88 ).w - h0 - 0.88 ) *  1.0 * shadowSoftening;
		float h9 = (texture2D( normalMap2D, newTexCoord + light * 0.77 ).w - h0 - 0.77 ) *  2.0 * shadowSoftening;
		float h8 = (texture2D( normalMap2D, newTexCoord + light * 0.66 ).w - h0 - 0.66 ) *  4.0 * shadowSoftening;
		float h7 = (texture2D( normalMap2D, newTexCoord + light * 0.55 ).w - h0 - 0.55 ) *  6.0 * shadowSoftening;
		float h6 = (texture2D( normalMap2D, newTexCoord + light * 0.44 ).w - h0 - 0.44 ) *  8.0 * shadowSoftening;
		float h5 = (texture2D( normalMap2D, newTexCoord + light * 0.33 ).w - h0 - 0.33 ) * 10.0 * shadowSoftening;
		float h4 = (texture2D( normalMap2D, newTexCoord + light * 0.22 ).w - h0 - 0.22 ) * 12.0 * shadowSoftening;
		
		// Compute the actual shadow strength:
		parallaxShadow = 1.0 - max(max(max(max(max(max(hA, h9), h8), h7), h6), h5), h4);

		diffuseLight.xyz *= parallaxShadow;
		specularLight.xyz *= parallaxShadow;
   
	}



	if(g_bUseLight1)
	{
		if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
		{
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir1), v_PositionEyeSpace, lightType.y);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
		}
		else
		{
			tLightDir = eGetLightDir_DP(vec3(lightDir1), v_PositionEyeSpace, lightType.y);
			tHalfVector = eGetHalfVector(tLightDir, v_EyeDirection);
		}
		AddSpecularAndDiffuse(lightNormal,
			tLightDir, vec3(lightColor1), g_iAlphaType,
			tHalfVector, vec3(lightSpec1), specularMaterial.w, ga_bUseMaxSpec, g_noSpecular,
			diffuseLight, specularLight, transparency);
		if(g_bUseNormalMap && g_bMicroFlakeNormalMap)
		{
			// add in the specular from the micro flake
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir1), v_PositionEyeSpace, lightType.y);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
			microFlakeSpec += GetSpecularFromOneLight( tMicroFlakeNormal, tHalfVector, microFlakeShininess, vec3(lightSpec1*microFlakeColor), false);
		}
	}
	if(g_bUseLight2)
	{
		if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
		{
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir2), v_PositionEyeSpace, lightType.z);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
		}
		else
		{
			tLightDir = eGetLightDir_DP(vec3(lightDir2), v_PositionEyeSpace, lightType.z);
			tHalfVector = eGetHalfVector(tLightDir, v_EyeDirection);
		}
		AddSpecularAndDiffuse(lightNormal,
			tLightDir, vec3(lightColor2), g_iAlphaType,
			tHalfVector, vec3(lightSpec2), specularMaterial.w, ga_bUseMaxSpec, g_noSpecular,
			diffuseLight, specularLight, transparency);
		if(g_bUseNormalMap && g_bMicroFlakeNormalMap)
		{
			// add in the specular from the micro flake
			tLightDir = tGetLightDir_DP(t2eMatrixTBN, vec3(lightDir2), v_PositionEyeSpace, lightType.z);
			tHalfVector = tGetHalfVector(tLightDir, v_EyeDirection, t2eMatrixTBN);
			microFlakeSpec += GetSpecularFromOneLight( tMicroFlakeNormal, tHalfVector, microFlakeShininess, vec3(lightSpec2*microFlakeColor), ga_bUseMaxSpec);
		}
	}
	
	// diffuse and specular term
	vec4 specularTerm;
	specularTerm.xyz = GetSpecularTerm(vec3(specularLight), vec3(specularMaterial)*specularLightModifier);
	///////////////////////////////////////////////////////////////////////

	// add rim light (FKP to understand!)
	if(g_iFresnelType==e_FresnelType_Rimlight)
	{
		specularTerm.xyz += GetRimLight(t2eMatrixTBN[2],fresnelCoef1,fresnelCoef2,
			g_rimlightColorIsSpecularColor?vec3(specularMaterial):RimlightColor);
	}

	// max specular value to control specular treshold, store in the w component of specularTerm
	if(ga_bUseMaxSpec)
	{
		specularTerm.w = specularLight.w * specularMaterial.x;
	}
	

	///////////////////////////////////////////////////////////////////////
	// alpha
	{
		if (g_iAlphaType ==e_AlphaType_Glass)
		{
			float t = GetMaxSpecTerm(specularTerm, g_bMaxSpecW);
			alpha *= GetAlphaFrom_Glass(t, fresnel, transparency);
		}
		else
		{
			alpha *= transparency;
		}		
	}
	// alpha value
	///////////////////////////////////////////////////////////////////////


	///////////////////////////////////////////////////////////////////////
	// ambient and texture modifier
	// To make stuff looks look, we need more that just a standard vertex lighing,
	// we need to add some high-frequency per-pixel
	// this is the light equation:
	// 
	// color = ambient + diffuse + specular
	// 
	// adding some noise is quite often done using this equatin:
	//
	// color = modifier1 * (ambient + diffuse) + modifier2 * specular
	//
	// this bits of code compute the value of modifier1
	vec3 ambientAndDiffuseModifier = vec3(1.0);
	if(g_bUseEnvMap || g_bUseDiffMap)
	{
		ambientAndDiffuseModifier = vec3(0.0);
		if(g_bUseDiffMap)
		{
			if(g_iDiffuseAlpha == e_DiffuseAlpha_Specularity)
			{
				vec4 diff = GetAmbiantAndDiffuseModifierDiffAlpha(diffMap2D, newTexCoord, diffuseAverage, diffuseMapModifier);
				ambientAndDiffuseModifier += (1.0-l_reflectModifier)*diff.xyz;
				specularTerm *= diff.a;
			}
			else if(g_iDiffuseAlpha == e_DiffuseAlpha_Transparency)
			{
				vec4 diff = GetAmbiantAndDiffuseModifierDiffAlpha(diffMap2D, newTexCoord, diffuseAverage, diffuseMapModifier);
				ambientAndDiffuseModifier += (1.0-l_reflectModifier)*diff.xyz;
				alpha *= (diff.a > 0.5) ? 1.0 : 0.0;//diff.a;
			}
			else if(g_iDiffuseAlpha == e_DiffuseAlpha_Reflectivity)
			{
				vec4 diff = GetAmbiantAndDiffuseModifierDiffAlpha(diffMap2D, newTexCoord, diffuseAverage, diffuseMapModifier);
				l_reflectModifier *= (diff.a > 0.5) ? 1.0 : 0.0;//diff.a;
				ambientAndDiffuseModifier += (1.0 - l_reflectModifier)*diff.xyz;
			}
			else if(g_iDiffuseAlpha == e_DiffuseAlpha_SpecularAndReflect)
			{
				vec4 diff = GetAmbiantAndDiffuseModifierDiffAlpha(diffMap2D, newTexCoord, diffuseAverage, diffuseMapModifier);
				l_reflectModifier *= (diff.a > 0.5) ? 1.0 : 0.0;//diff.a;
				ambientAndDiffuseModifier += (1.0 - l_reflectModifier)*diff.xyz;
				specularTerm *= (diff.a > 0.5) ? 1.0 : 0.0;//diff.a;
			}
			else
			{
				ambientAndDiffuseModifier += (1.0-l_reflectModifier)*GetAmbiantAndDiffuseModifierDiff(diffMap2D, newTexCoord, diffuseAverage, diffuseMapModifier);
			}
		}
		else
		{
			ambientAndDiffuseModifier += (1.0-l_reflectModifier);
		}
		
		if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
			ambientAndDiffuseModifier *= mixColor.rgb;
	
		if(g_bUseEnvMap && !g_bModifyFresnelAmbientWithEnv)
		{

			//Position in Eye space = getSwizzleInW(v_tLightDir0,v_tHalfVector0,v_tLightDir1);
			vec3 eReflection;
			if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
			{
				eReflection = eGetReflection(t2eMatrixTBN*lightNormal, envEyePosition, v_PositionEyeSpace);
			}
			else
			{
				eReflection = eGetReflection(t2eMatrixTBN[2],envEyePosition,v_PositionEyeSpace);
			}
			eReflection = e2lDirectionMat*eReflection;
			if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
			{
				vec3 env = GetEnv(eReflection).xyz;
				float envgs = env.r*0.3 + env.g*0.59* env.r*0.11;
				l_reflectModifier = clamp(l_reflectModifier * envgs*10.0,l_reflectModifier,1.0);
				ambientAndDiffuseModifier += l_reflectModifier*env;
			}
			else
				ambientAndDiffuseModifier += l_reflectModifier*GetAmbiantAndDiffuseModifierEnv(eReflection, envColorWash, envBrightness, envContrast, envColor, g_bUseHdr);
		}
	}
	else if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
			ambientAndDiffuseModifier *= mixColor.rgb;
	
	///////////////////////////////////////////////////////////////////////
	if(g_bMicroFlakeNormalMap)
	{
		specularTerm.xyz += vec3(getMixColor(microFlakeSpec, vec4(0.0,0.0,0.0,1.0), microFlakeCoef, t2eMatrixTBN[2], vec3(0.0,0.0,1.0)));
	}

	vec3 diffuseTerm;
	if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
		diffuseTerm = diffuseLight*matDiffuse;
	else
		diffuseTerm = GetDiffuseTerm(diffuseLight*matDiffuse, vec3(mixColor));

	///////////////////////////////////////////////////////////////////////
	// ambient term
	// this is currently computed per-pixel
	// this could be moved at the vertex level, but interpolator are precious... Perhaps...
	
	// basic ambient term
	vec3 ambientTerm;
	if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
		ambientTerm = (vec3(ambientLight)* matAmbient + vec3(matEmissivity));
	else
		ambientTerm = (vec3(ambientLight)* matAmbient + vec3(matEmissivity)) * mixColor.xyz;

	// add fresnel ambiant
	if(g_bAddFresnelAmbient)
	{
		vec3 fresnelAmbient = GetFresnelAmbient(fresnel*g_fAmbientFresnelCoef);
		if(g_bModifyFresnelAmbientWithEnv)
		{
			vec3 eReflection;
			if(g_bUseNormalMap && !g_bMicroFlakeNormalMap)
			{
				eReflection = eGetReflection(t2eMatrixTBN*lightNormal, envEyePosition, v_PositionEyeSpace);
			}
			else
			{
				eReflection = eGetReflection(t2eMatrixTBN[2],envEyePosition,v_PositionEyeSpace);
			}
			// this is not working on ATI:
			if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
				fresnelAmbient*= GetEnv(eReflection).xyz;
			else
				fresnelAmbient*=GetAmbiantAndDiffuseModifierEnv(eReflection, envColorWash, envBrightness, envContrast, envColor, g_bUseHdr);
		}
		ambientTerm += fresnelAmbient;
	}
	// hard coded modifier (should be removed)
	ambientTerm *= g_fAmbientTermCoef;

	// Ambient Occlusion on ambient term
	if (g_bAOShadow)
	{
		// dynamic test with uniform here so that we dont have to regenerate a different shader just to hide AO
		if (isAOShadowVisible > 0.0)
		{
			vec2 AOcoord = ((v_fragCoord.xy/v_fragCoord.w) + 1.0)/2.0;
			ambientTerm *= (texture2D(AOTermMap2D, AOcoord).a*2.0);
		}
	}

//	float alpha = GetAlpha(matColor, 0.0, transparency, diffuseTerm, vec4(specularTerm.xyz,0.0), g_iMaxSpecMode, g_iAlphaType);
	///////////////////////////////////////////////////////////////////////

	///////////////////////////////////////////////////////////////////////
	// Combine all
	// basically just a way to put ambient, specular and diffuse together

	vec4 finalColor;

	if(g_iFinalColorMode == e_FinalColorMode_SaturateAD_ModifyAD)
	{
		finalColor = GetFinalColor_SaturateAD_ModifyAD(
			ambientTerm,
			diffuseTerm,
			specularTerm.xyz,
			ambientAndDiffuseModifier,
			alpha );
	}
	else if(g_iFinalColorMode == e_FinalColorMode_ModifyADS)
	{
		finalColor = GetFinalColor_ModifyADS(
			ambientTerm,
			diffuseTerm,
			specularTerm.xyz,
			ambientAndDiffuseModifier,
			alpha );
	}
	else if(g_iFinalColorMode == e_FinalColorMode_Fac)
	{
		finalColor = GetFinalColor_Fac(
			ambientTerm,
			diffuseTerm,
			specularColor.xyz,
			GetSpecFac(GetMaxSpecTerm(specularTerm, g_bMaxSpecW), g_specFacCoef),
			alpha );
	}
	else if(g_iFinalColorMode == e_FinalColorMode_Fac_SaturateAD_ModifyAD)
	{
		finalColor = GetFinalColor_Fac_SaturateAD_ModifyAD(
			ambientTerm,
			diffuseTerm,
			specularColor.xyz,
			GetSpecFac(GetMaxSpecTerm(specularTerm, g_bMaxSpecW), g_specFacCoef),
			ambientAndDiffuseModifier,
			alpha );
	}
//	else if(g_iFinalColorMode == e_FinalColorMode_LeadPure)
//	{
//		vec3 envColor = vec3(textureCube(envCubicSpecular, v_eReflexion));
//		vec3 shineColor = vec3(texture2D(normalMap2D, vec2(v_TexCoord)));
//		vec3 diffuseColor = vec3(texture2D(diffMap2D, vec2(v_TexCoord)));
//		vec3 ambiantPlusDiffuse = saturate(ambientTerm+v_diffuseTerm);
//		finalColor = vec4(ambiantPlusDiffuse*diffuseColor + ambiantPlusDiffuse*vec3(specularTerm)*envColor*shineColor,alpha );
//	}
	else if(g_iFinalColorMode == e_FinalColorMode_PVColorEnvBlend)
	{
		finalColor = GetFinalColor(
			ambientTerm * ambientAndDiffuseModifier,
			diffuseTerm * ambientAndDiffuseModifier, 
			specularTerm.xyz,
			alpha );		
	}
	else // if(g_iFinalColorMode == e_FinalColorMode_Default)
	{
		finalColor = GetFinalColor(
			ambientTerm * ambientAndDiffuseModifier,
			diffuseTerm * ambientAndDiffuseModifier, 
			specularTerm.xyz,
			alpha );		
	}
	///////////////////////////////////////////////////////////////////////
	// Reflective Floor
	if(g_bPlanarReflection)
	{
		vec2 reflUV  = v_projCoordRF.xy / v_projCoordRF.w;
		if (g_bUseNormalMap)
		{
			reflUV += tBumpNormal.xy*0.02;
		}
		vec4 refl = texture2D(reflectionMap2D, reflUV.xy);

		refl.a = refl.a * finalColor.a * 0.8;
		finalColor.rgb = refl.a*refl.rgb + (1.0-refl.a)*finalColor.rgb;
	}

	if(g_bPlanarFadeEdge)
	{
		// radial blur to avoid brutal clipping of shadow and/or reflection
		finalColor.a = 1.0 - distance(v_PositionEyeSpace, v_FloorPoint.xyz)/floorPoint.w;
	}

	if(g_bReflectionFading)
	{
		if (finalColor.a == 1.0)
		{
			finalColor.a = clamp(1.0 - v_TexCoord.w, 0.0, 1.0);
		}
		else
		{
			finalColor.a *= clamp((1.0 - v_TexCoord.w*v_TexCoord.w*v_TexCoord.w), 0.0, 1.0);
		}
	}
	
	///////////////////////////////////////////////////////////////////////
	// big hack
	if(g_fFinalColorMul!=1.0)
	{
		finalColor.xyz*=g_fFinalColorMul;
	}
	if(g_fFinalColorAdd>0.0)
	{
		finalColor.xyz+=vec3(g_fFinalColorAdd);
	}
	///////////////////////////////////////////////////////////////////////

	gl_FragColor = finalColor;

	// cosmetic multiplier
	if(g_cosmeticAlpha)
	{
		gl_FragColor.a *= getCosmeticAlpha(v_patternCoord,alphaMap);
	}
}
#endif //_FRAGMENT_PROGRAM


