

////////////////////////////////////////////////////////////////////////
// space name:
// o - >object
// w -> world
// e -> eye
// p -> projected
// t -> tangent
// c -> cosmetic space
// n -> normalized space (or mathematic space)
// x -> texture space




////////////////////////////////////////////////////////////////////////
// debug





////////////////////////////////////////////////////////////////////////
// tool

float nonZeroSign(float x)
{
	return x<0.0?-1.0:1.0;
}

vec2 ApplyMat2D(vec2 v, mat4 m)
{
	return (m*vec4(v,0.0,1.0)).xy;
}

// hdr doesn't need clamping (and material needs re-cailbrating)
vec4 saturateLight(vec4 value)
{
	if(g_bUseHdr) return max(vec4(0.0),value);
	else return saturate(value);
}
vec3 saturateLight(vec3 value)
{
	if(g_bUseHdr) return max(vec3(0.0),value);
	else return saturate(value);
}
vec2 saturateLight(vec2 value)
{
	if(g_bUseHdr) return max(vec2(0.0),value);
	else return saturate(value);
}
float saturateLight(float value)
{
	if(g_bUseHdr) return max(0.0,value);
	else return saturate(value);
}

void SetSwizzleInW(in vec3 value, inout vec4 vx, inout vec4 vy, inout vec4 vz)
{
	vx.w=value.x;
	vy.w=value.y;
	vz.w=value.z;
}
vec3 GetSwizzleInW(in vec4 vx, in vec4 vy, in vec4 vz)
{
	return vec3(vx.w,vy.w,vz.w);
}

float Max3(vec3 lightSpec)
{
	float maxSpec = (lightSpec.r > lightSpec.g) ? lightSpec.r : lightSpec.g;
	if(lightSpec.b > maxSpec) maxSpec = lightSpec.b;
	return maxSpec;
}




////////////////////////////////////////////////////////////////////////
// position, transform from object to projected space
vec4 pGetProjectedPosition(vec4 oPosition, mat4 o2pMatrix)
{
	return o2pMatrix*oPosition;
}







////////////////////////////////////////////////////////////////////////
// normal/tangent space
vec3 eGetNormal(vec3 oNormal, mat3 o2eMatrix, bool bDoubleSided)
{
	vec3 normal = normalize(o2eMatrix * oNormal);
	// if double sided like surface, flip backface normal
	if ((normal.z < 0.0) && bDoubleSided)
	{
		normal = -normal;
	}
	return normal;
}
mat3 t2eGetMatrixTBN(mat3 o2eMatrix, vec3 oNormal, vec3 oTangent, bool bDoubleSided)
{
	mat3 res;
	
	// Set up tangent-space matrix -- btn
	res[2] = normalize(o2eMatrix * oNormal); // normal
	res[0] = normalize(o2eMatrix * oTangent); // tangeant
	res[1] = normalize(cross(res[0], res[2])); // binormal
	res[0] = normalize(cross(res[2], res[1])); // binormal
	
	if ((res[2].z < 0.0) && bDoubleSided)
	{
		res[2] = -res[2];
	}
	return res;
}
mat3 CreateMatrixTBN(vec3 eTangent, vec3 eNormal,  bool bDoubleSided)
{
	mat3 res;
	
	// Set up tangent-space matrix -- btn
	res[2] = eNormal; // normal
	res[0] = eTangent; // tangeant
	res[1] = -normalize(cross(res[2], res[0])); // binormal
	
	if ((res[2].z < 0.0) && bDoubleSided)
	{
		res[2] = -res[2];
	}
	return res;
}
vec3 tGetBumpNormal(sampler2D normalMap, vec2 commonCoord, bool bNormalHack)
{
	vec3 tBumpNormal = 2.0*vec3(texture2D(normalMap, commonCoord))-vec3(1.0);
	if (bNormalHack)
	{
		tBumpNormal*=0.8;
	}
	return normalize(tBumpNormal);
}


float tGetBumpMapStencil(sampler2D normalMap, vec2 commonCoord)
{
	return ((texture2D(normalMap, vec2(commonCoord.x, commonCoord.y))).x > 0.5) ? 1.0 : 0.0;
}


////////////////////////////////////////////////////////////////////////
// get eye info
struct EyeInfo_s
{
	vec4 ePosition;
	vec3 eEyeDirection;
};
EyeInfo_s GetEyeInfo(vec4 oPosition, mat4 o2eMatrix)
{
	EyeInfo_s res;
	res.ePosition = o2eMatrix * oPosition;
	res.eEyeDirection = normalize(res.ePosition.xyz);

	return res;
}




////////////////////////////////////////////////////////////////////////
// reflexion coordinate
// 

vec3 eGetReflection(vec3 eNormal, vec3 eEnvEyePosition, vec3 eEyeCoordPosition)
{
	/// eDrawings Hack!
	/// Hoops has a weird inverted x component in its model view matrix, so we must manually fix the x for reflections.
#if 0 

	vec3 viewVec = eEyeCoordPosition - eEnvEyePosition;
	return viewVec - 2.0 * eNormal*dot(eNormal, viewVec);
//	return reflect(eEyeDirection, eNormal);

#else

	vec3 viewVec = eEyeCoordPosition - eEnvEyePosition;
	viewVec = viewVec - 2.0 * eNormal*dot(eNormal, viewVec);
	viewVec.x = -viewVec.x;
	return viewVec;
	
#endif
	/// End Hack
}

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




////////////////////////////////////////////////////////////////////////
// texcoord


////////////////////////////////////////////////////////////////////////
// caluculate tangents and binormals


/////////////////////////////////////////////////
////////////// AUTOMATIC  ///////////////////////
vec3 oGetAutomaticTangent(vec3 nNormal, mat4 n2oMatrix)
{
	// abs and sign of normal
	vec3 nAbsnormal = abs(nNormal);
	
	// compute cmp vector containing 1 only where needed
	vec3 teststep = step(vec3(0.0),nAbsnormal-nAbsnormal.yzx);
	vec3 teststep2 = vec3(1.0)-teststep.zxy;
	vec3 cmp = teststep*teststep2;

	// get sign
	float nSign = dot(sign(nNormal),cmp);
	
	// return tangenat
	vec3 nTangent = nSign*cmp.yzx;
	
	// back to object space:
	//return normalize((n2oMatrix*vec4(nTangent,1.0)).xyz);
	return nTangent;
}
vec2 xGetAutomaticTexCoords(vec3 nPos, vec3 nNormal, mat4 n2xMatrix)
{
	// abs and sign of normal
	vec3 nAbsnormal = abs(nNormal);
	
	// compute cmp vector containing 1 only where needed
	vec3 teststep = step(vec3(0.0),nAbsnormal-nAbsnormal.yzx);
	vec3 teststep2 = vec3(1.0)-teststep.zxy;
	vec3 cmp = teststep*teststep2;
	
	// get sign
	vec3 nNormalsign = sign(nNormal);
	
	// get result (that could be simpler but we need to match legacy...
	nNormalsign.y*=-1.0;
	cmp.z*=nNormalsign.z;
	vec2 nTexcoord = vec2(dot(nNormalsign*nPos.yxx,cmp),dot(nPos.zzy,cmp));
	
	// rotate
	return ApplyMat2D(nTexcoord,n2xMatrix);
}
/////////////////////////////////////////////////


/////////////////////////////////////////////////
///////////////CYL AND SPHERE   /////////////////
vec3 oGetCylindricalTangent(vec3 nPos, mat4 n2oMatrix)
{
	// compute tangeant in cylinder space
	float l = length(nPos.xz);
	vec2 nCylnormal = nPos.xz / (l+0.001);
	// back to object space
	return (n2oMatrix*vec4(nCylnormal.y,0.0,-nCylnormal.x,0.0)).xyz;
}
vec2 xGetCylindricalTexCoords(vec3 nPos, mat4 n2xMatrix)
{
	vec2 nDir = normalize(nPos.xz);
	float angle = acos(-nDir.x)*nonZeroSign(nDir.y);
	vec2 nTexcoord = vec2(0.5*INVERSE_PI*(PI+angle)*faceWidth,nPos.y);
	return ApplyMat2D(nTexcoord,n2xMatrix);
}
vec2 xGetSphericalTexCoords(vec3 nDir, mat4 n2xMatrix)
{
	nDir = normalize(nDir);
	vec2 nTexcoord = faceWidth*INVERSE_PI*vec2(PI*0.5-atan(nDir.z,-nDir.x)*0.5,acos(nDir.y));
	return ApplyMat2D(nTexcoord,n2xMatrix);
}
/////////////////////////////////////////////////



/////////////////////////////////////////////////
////////////// PLANAR      //////////////////////
vec3 oGetProjectedTangent(mat4 n2oMatrix)
{
	return normalize(n2oMatrix[0].xyz);
}
vec2 xGetPlanarTexCoords(vec4 nPos, mat4 n2xMatrix)
{
	return ApplyMat2D(nPos.xy,n2xMatrix);
}
/////////////////////////////////////////////////

	
/////////////////////////////////////////////////
/////////////////SURFACE  ////////////////////////
vec2 xGetSurfaceTexCoords(vec4 nTexCoord, mat4 n2xMatrix)
{
	return ApplyMat2D(nTexCoord.xy,n2xMatrix);
}
/////////////////////////////////////////////////
	






































// this function return:
// - whether the texture coordinate (in texture space)
// - whether the position in normalized (math) space
vec3 GetCommonTexCoord(vec4 texCoord, vec4 vertex, int iTextureCoordinates, mat4 textureMatrix, mat4 o2nMatrix)
{
	vec3 texCoordOut;
	// per-vertex texcoord, compute here
	if(iTextureCoordinates == e_TextureCoordinates_UV)
	{
		texCoordOut.xy = xGetSurfaceTexCoords(texCoord,textureMatrix).xy;
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Projected)
	{
		texCoordOut.xy = xGetPlanarTexCoords(o2nMatrix*vertex,textureMatrix);
	}
	// per-pixel tex coord, only do some preparation here and pass to pixel
	else if(iTextureCoordinates == e_TextureCoordinates_Automatic)
	{
		texCoordOut = (o2nMatrix*vertex).xyz;
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Cylindrical)
	{
		texCoordOut = (o2nMatrix*vertex).xyz;
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Spherical)
	{
		texCoordOut = (o2nMatrix*vertex).xyz;
	}
	// remove warning
	else texCoordOut = vec3(0.0);
	
	return texCoordOut;
}


// tangeant (vertex satge)
vec3 oGetTangent(vec3 pos, vec3 normal, int iTextureCoordinates, mat4 n2oMatrix)
	{
	vec3 tangentOut;
	if(iTextureCoordinates == e_TextureCoordinates_Automatic)
	{
		tangentOut = oGetAutomaticTangent(normal, n2oMatrix);
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Cylindrical)
	{
		tangentOut = oGetCylindricalTangent(pos, n2oMatrix);
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Spherical)
	{
		tangentOut = oGetCylindricalTangent(pos, n2oMatrix);
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Projected)
	{
		tangentOut = oGetProjectedTangent(n2oMatrix);
	}
	// remove warning
	else tangentOut = vec3(0.0);

	return tangentOut;
}


// pixel stage
vec2 tGetTexCoords(vec3 texCoords, vec3 normal, int iTextureCoordinates, mat4 textureMatrix)
{
	vec2 texCoordsOut;
	// done per-vertex
	if(iTextureCoordinates == e_TextureCoordinates_UV || iTextureCoordinates == e_TextureCoordinates_Projected)
	{
		texCoordsOut.xy = texCoords.xy;
	}
	// per-pixel texcoord computation
	else if(iTextureCoordinates == e_TextureCoordinates_Automatic)
	{
		texCoordsOut = xGetAutomaticTexCoords(texCoords,normal,textureMatrix);
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Cylindrical)
	{
		texCoordsOut = xGetCylindricalTexCoords(texCoords,textureMatrix);
	}
	else if(iTextureCoordinates == e_TextureCoordinates_Spherical)
	{
		texCoordsOut = xGetSphericalTexCoords(texCoords,textureMatrix);		
	}
	
	return texCoordsOut;
}















vec2 tGetDispTexCoords(sampler2D normalMap, vec2 commonCoord, vec3 viewDir, float parallaxOffset)
{
	// Fetch height map and generate new texture coordinates
	float height = 2.0 * (texture2D(normalMap, commonCoord).a - 0.5);
	vec2 offset = normalize(viewDir.xyz).xy * height * parallaxOffset;

	return commonCoord + offset;
}








////////////////////////////////////////////////////////////////////////
// cosmetic texture coordinate
vec2 getCosmeticTexcoord(vec4 ePos,mat4 e2cTransform)
{
	vec4 cPos=e2cTransform*ePos;
	return cPos.xy/cPos.w;
}


////////////////////////////////////////////////////////////////////////
// cosmetic texture coordinate
float getCosmeticAlpha(vec2 texcoord,sampler2D alphaMap)
{
	return texture2D(alphaMap, texcoord.xy).a;
}





////////////////////////////////////////////////////////////////////////
// light direction
// the lighttype is an handy way of simulating directional and poitn light
// with the same arithmetic
// lightype=0 -> directional (and lightDirOrPos is a direction)
// lightype=1 -> positional (and lightDirOrPos is a position)
vec3 GetLightDir_DP(vec3 lightDirOrPos, vec3 position, float lightType)
{
	return normalize(lightDirOrPos - position * lightType );
}
vec3 GetLightDir_D(vec3 lightDir)
{
	// TODO_FKP: do we need the normalize here??? => OZ: Yes, lightDirs come non normalized form SW...
	return normalize(lightDir);
}
vec3 GetLightDir_P(vec3 lightPos, vec3 position)
{
	return normalize(lightPos - position);
}
vec3 tGetLightDir_D(mat3 t2eMatrixTBN, vec3 eLightDir)
{
	return GetLightDir_D(eLightDir) * t2eMatrixTBN;
}
vec3 eGetLightDir_DP(vec3 eLightDirOrPos, vec3 ePosition, float lightType)
{
	return GetLightDir_DP(eLightDirOrPos, ePosition, lightType);
}
vec3 tGetLightDir_DP(mat3 t2eMatrixTBN, vec3 eLightDirOrPos, vec3 ePosition, float lightType)
{
	return eGetLightDir_DP(eLightDirOrPos, ePosition, lightType) * t2eMatrixTBN;
}






////////////////////////////////////////////////////////////////////////
// half vector
vec3 GetHalfVector(vec3 lightDir, vec3 eyeDirection)
{
	return normalize(lightDir + eyeDirection);
}
vec3 eGetHalfVector(vec3 eLightDir, vec3 eEyeDirection)
{
	// this computation is not using the real eye direction
	// that might be a good idea to try it
	
//	return GetHalfVector(eLightDir,eEyeDirection);	// real eye direction
	return GetHalfVector(eLightDir,vec3(0.0,0.0,1.0));	// fake eye direction vec3(0,0,1)
}
vec3 tGetHalfVector(vec3 tLightDir, vec3 eEyeDirection, mat3 t2eMatrixTBN)
{
	// this computation is not using the real eye direction
	// that might be a good idea to try it
	
//	vec3 tEyeDirection = eEyeDirection * t2eMatrixTBN;									// real eye direction
	vec3 tEyeDirection = vec3(t2eMatrixTBN[0].z,t2eMatrixTBN[1].z,t2eMatrixTBN[2].z);	// fake eye direction vec3(0,0,1)
	return GetHalfVector(tLightDir,tEyeDirection);
}
////////////////////////////////////////////////////////////////////////











////////////////////////////////////////////////////////////////////////
// fresnel stuff

// the fresnel power if a value which is high when the normal is almost parallel to the surface
float GetFresnelPower(vec3 eNormal)
{
	// Simplified here, we need abs(dot(eyeInfo.eEyeDirection, normal))
	// - since we know that eyeInfo.eEyeDirection = <0, 0, 1>,
	// we replace the dot with just a swizzle for z
	float oneMCosAngle = max(1.0 - abs(eNormal.z), 0.01);
	return pow(oneMCosAngle, 5.0);
}
// fresnel depending on object transparency
float GetFresnelTerm_Transparency(vec3 eNormal, float transparency, vec2 fresnelCoef)
{
	return (1.0 - transparency) * fresnelCoef.x + fresnelCoef.y * GetFresnelPower(eNormal);
}
// fresnel depending on object transparency
float GetFresnelTerm_Standard(vec3 eNormal, vec2 fresnelCoef)
{
	return fresnelCoef.x + fresnelCoef.y * GetFresnelPower(eNormal);
}
float GetFresnel_New(vec3 eNormal, float fresnelCoef)
{
	return pow(max(1.0 - abs(eNormal.z),0.01),fresnelCoef);
}
// get final fresnel term (which depends on fresnel power
float GetFresnelTerm(vec3 eNormal, float transparency, vec2 fresnelCoef, int iFresnelType)
{
	if(iFresnelType==e_FresnelType_Transparency)
	{
		return GetFresnelTerm_Transparency(eNormal, transparency, fresnelCoef);
	}
	else if(iFresnelType==e_FresnelType_Standard)
	{
		return GetFresnelTerm_Standard(eNormal, fresnelCoef);
	}
	else
	{
		return GetFresnel_New(eNormal, fresnelCoef.x);
	}
}


// a rim light is a fake light which is constructed to light the surface for very low incident angle
vec3 GetRimLight(vec3 tNormal, float rimlightCoef1, float rimlightCoef2, vec3 rimlightColor)
{
	return (rimlightCoef1*GetFresnelPower(tNormal)+rimlightCoef2) * rimlightColor;
}
////////////////////////////////////////////////////////////////////////








////////////////////////////////////////////////////////////////////////
// specfac ????

float GetMaxSpecTerm(vec4 specular, bool iMaxSpecW)
{
	if(iMaxSpecW)
	{
		return specular.w;
	}
	else
	{
		return specular.x;
	}
}

float GetSpecFac(float fMaxSpec, vec2 coef)
{
	return saturate((fMaxSpec-coef.x)*coef.y);
}
////////////////////////////////////////////////////////////////////////








////////////////////////////////////////////////////////////////////////
// standard diffuse and specular

// diffuse
vec3 GetDiffuseFromOneLight(vec3 normal, vec3 lightDir, vec3 lightColor, int iAlphaType, float transparency)
{
	// Calc light0 products
	float LdotB = dot(lightDir, normal);
	if (iAlphaType ==e_AlphaType_Opaque)
	{
		LdotB = saturate(LdotB);
	}
	else
	{
		if(LdotB < 0.0)
		{
			LdotB = abs(LdotB) * (1.0 - transparency);
		}
	}

	// Look up illumination
	return LdotB * lightColor;
}

// specular
vec4 GetSpecularFromOneLight(vec3 normal, vec3 halfVector, float specularPower, vec3 lightSpec, bool bSpecWTerm)
{
	float ndoth = dot(normal, halfVector);
	ndoth=pow(max(ndoth,0.01), specularPower);
	
	vec4 res = vec4(ndoth * lightSpec,0);
	if(bSpecWTerm)
	{
		res.w=ndoth*Max3(lightSpec);
	}
	return res;
}

// add specualr and diffuse
void AddSpecularAndDiffuse(in vec3 normal,
	in vec3 lightDirAux, in vec3 lightColor, in int iAlphaType,
	in vec3 halfVector, in vec3 lightSpec, in float specularPower, in bool bMaxSpec, in bool noSpec,
	inout vec3 diffuseLight, inout vec4 specularLight, in float transparency)
{
	diffuseLight += GetDiffuseFromOneLight( normal, lightDirAux, lightColor, iAlphaType, transparency);
	if(!noSpec)
	{
		specularLight += GetSpecularFromOneLight( normal, halfVector, specularPower, lightSpec, bMaxSpec);
	}
}


// mix diffuse with material color
vec3 GetDiffuseTerm(vec3 diffuseLight, vec3 materialColor)
{
	return diffuseLight * materialColor;
}
// mix diffuse specular material color
vec3 GetSpecularTerm(vec3 specularLight, vec3 materialColor)
{
	return specularLight * materialColor;
}

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
















////////////////////////////////////////////////////////////////////////
// ambient light

// ambient depending on fresnel term
//vec3 GetFresnelAmbient(float fresnel, vec3 ambientAndDiffuseModifier)
//{
//	return fresnel*ambientAndDiffuseModifier;
//}
vec3 GetFresnelAmbient(float fresnel)
{
	return vec3(fresnel);
}

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






vec3 TransformEnvColor(vec3 envColorIn, bool useHdr,
	float envColorWash, float envBrightness, float envContrast, vec3 envColorModifier)
{
	if (useHdr)
	{
		// here we have a big issue
		// most of our metal material are strongly based on the few LDR lines bellow
		// unfortunately those lines doesn't have a nice equivalent in the HDR world so
		// that will make the material calibration quite hard
		envColorIn = (envColorIn-envcolorAverage)*envContrast+envcolorAverage*envBrightness; // contrast and brigness
		envColorIn = mix(envColorIn,vec3(GetLuminance(envColorIn)),envColorWash); // colorwash
		envColorIn *= envColorModifier; // envcolor
		return max(envColorIn,vec3(0.0));
	}
	else
	{
		vec3 color = mix(envColorIn.xyz,vec3(GetLuminance(envColorIn)),envColorWash);
		vec3 res = (color - 0.635 + envBrightness)*envContrast + 0.635 + envColorModifier;
		return res;
	}
}


vec4 GetEnv(vec3 envCoord)
{
	if(g_envmaptype==e_EnvMapType_Cubic)
	{
		return textureCube(envCubicSpecular, envCoord);
	}
	else
	{
		return texture2D(envSphericalSpecular, Flip(SphereMapCoord(envCoord),g_envmapFlip));
	}
}

vec4 GetEnvDiffuse(vec3 envCoord)
{
	if(g_envmaptype==e_EnvMapType_Cubic)
	{
		return textureCube(envCubicDiffuse, envCoord);
	}
	else
	{
		return texture2D(envSphericalDiffuse, Flip(SphereMapCoord(envCoord),g_envmapFlip));
	}
}
vec3 GetAmbiantAndDiffuseModifierEnv(vec3 envCoord,	float envColorWash, float envBrightness, float envContrast, vec3 envColor, bool useHdr)
{
	vec4 env = GetEnv(envCoord);
	return TransformEnvColor(env.xyz, useHdr, envColorWash, envBrightness, envContrast, envColor);
}
vec4 GetDiffuseMap(sampler2D diffMap, vec2 coord, vec3 average, vec3 diffuseMapModifierCWB)
{
	if(g_bUseHdr)
	{
		vec4 res = texture2D(diffMap, coord);
		res.rgb = (res.rgb-average)*diffuseMapModifier.x+average*diffuseMapModifier.z; // contrast and brigness
		res.rgb = mix(res.rgb,vec3(GetLuminance(res.rgb)),diffuseMapModifier.y); // colorwash
		return res;
	}
	else
	{
		return texture2D(diffMap, coord);
	}
}

vec3 GetAmbiantAndDiffuseModifierDiff(sampler2D diffMap, vec2 commonCoord, vec3 average, vec3 diffuseMapModifier)
{
	return GetDiffuseMap(diffMap,commonCoord, average, diffuseMapModifier).rgb;
}

vec4 GetAmbiantAndDiffuseModifierDiffAlpha(sampler2D diffMap, vec2 commonCoord, vec3 average, vec3 diffuseMapModifier)
{
	return GetDiffuseMap(diffMap,commonCoord, average, diffuseMapModifier).rgba;
}
////////////////////////////////////////////////////////////////////////


















////////////////////////////////////////////////////////////////////////
// transparency
float GetAlphaFrom_Material(vec4 matColor)
{
	return matColor.w;
}
float GetAlphaFrom_Fresnel(float fresnel)
{
	return 1.0-fresnel;
}
float GetAlphaFrom_FresnelPlusTransparency(float fresnel, float transparency)
{
	return fresnel + transparency;
}
float GetAlphaFrom_PlasticPerspex(vec3 diffuseTerm)
{
	return 0.7 * (diffuseTerm.x + 0.2);
}
float GetAlphaFrom_Glass(float fMaxSpec, float fresnel, float transparency)
{
	float specfac = saturate((fMaxSpec - 0.48) * 5.0);
	float fresnelGlass = saturate(fresnel*fresnel*0.01);
	return saturate(fresnelGlass + transparency) * (1.0 - specfac) + specfac;
}
float GetAlpha(vec4 matColor, float fresnel, float transparency, vec3 diffuseTerm, vec4 specularTerm, bool iMaxSpecW, int iAlphaType)
{
	if (iAlphaType ==e_AlphaType_Fresnel)
	{
		return GetAlphaFrom_Fresnel(fresnel);
	}
	else if (iAlphaType ==e_AlphaType_FresnelPlusTransparency)
	{
		return GetAlphaFrom_FresnelPlusTransparency(fresnel, transparency);
	}
	else if (iAlphaType ==e_AlphaType_Material)
	{
		return GetAlphaFrom_Material(matColor);
	}
	else if (iAlphaType ==e_AlphaType_PlasticPerspex)
	{
		return GetAlphaFrom_PlasticPerspex(diffuseTerm);
	}
	else if (iAlphaType ==e_AlphaType_Glass)
	{
		float t = GetMaxSpecTerm(specularTerm, iMaxSpecW);
		return GetAlphaFrom_Glass(t, fresnel, transparency);
	}
	else if (iAlphaType ==e_AlphaType_Transparency)
	{
		return transparency;
	}
	else // if (g_iAlphaType ==e_AlphaType_Opaque)
	{
		return 1.0;
	}
}
////////////////////////////////////////////////////////////////////////










////////////////////////////////////////////////////////////////////////
// last step

vec4 GetFinalColor(vec3 ambiant, vec3 diffuse, vec3 specular, float alpha)
{
	return vec4(ambiant+diffuse+specular,alpha);
}
vec4 GetFinalColor_Fac(vec3 ambiant, vec3 diffuse, vec3 specular, float facCoef, float alpha)
{
	return vec4((1.0-facCoef)*(ambiant+diffuse)+facCoef*specular,alpha);
}
vec4 GetFinalColor_Fac_SaturateAD_ModifyAD(vec3 ambiant, vec3 diffuse, vec3 specular, float facCoef, vec3 modify, float alpha)
{
	return vec4((1.0-facCoef)*modify*saturateLight(ambiant+diffuse)+facCoef*specular,alpha);
}
vec4 GetFinalColor_SaturateAD_ModifyAD(vec3 ambiant, vec3 diffuse, vec3 specular, vec3 modify, float alpha)
{
	return vec4(modify*saturateLight(ambiant+diffuse)+specular,alpha);
}
vec4 GetFinalColor_ModifyADS(vec3 ambiant, vec3 diffuse, vec3 specular, vec3 modify, float alpha)
{
	return vec4(modify*(saturateLight(ambiant+diffuse)+specular),alpha);
}
vec4 GetFinalColor_Illum(vec3 ambiant, vec3 diffuseAndSpecular, float alpha)
{
	return vec4(ambiant+diffuseAndSpecular,alpha);
}
////////////////////////////////////////////////////////////////////////
// apply a percentage Close Filtering 4x4 for smoothing shadow edges
float GetShadowColor(sampler2DShadow shadowMap, vec3 shadowUV, float mapScale)
{
	float shadowColor = shadow2D(shadowMap, shadowUV + vec3( -1.5, -1.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -1.5, -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -1.5,  0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -1.5,  1.5, 0)*mapScale).r;

	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -0.5, -1.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -0.5, -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -0.5,  0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -0.5,  1.5, 0)*mapScale).r;

	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 0.5, -1.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 0.5, -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 0.5,  0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 0.5,  1.5, 0)*mapScale).r;

	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 1.5, -1.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 1.5, -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 1.5,  0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( 1.5,  1.5, 0)*mapScale).r;
	return shadowColor/16.0;
}

////////////////////////////////////////////////////////////////////////
// Simpler 2x2 PCF for lower end cards
float GetShadowColorLow(sampler2DShadow shadowMap, vec3 shadowUV, float mapScale)
{
	float shadowColor = shadow2D(shadowMap, shadowUV + vec3( -0.5, -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3( -0.5,  0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3(  0.5,  -0.5, 0)*mapScale).r;
	shadowColor += shadow2D(shadowMap, shadowUV + vec3(  0.5,  0.5, 0)*mapScale).r;
	return shadowColor/4.0;
}

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

vec4 getMixColor(vec4 matColor, vec4 matColor2, float colorMixFactor, vec3 eNormal, vec3 eyeVector)
{
	float cpdot = max(abs(dot(eNormal,eyeVector)), 0.01);
	cpdot = clamp(1.2*pow(cpdot, colorMixFactor)-0.1,0.0,1.0);
	return mix(matColor2,matColor,cpdot);
}
