 	return min(1.0f, f1);
 }
 
-vec3 rescaleXYZ(float X, float Y, float Z, int index){
-	vec4 vMin = scalingFactors[index*2];
-	vec4 vMax = scalingFactors[index*2 + 1];
-	X = X * vMax.x;
-	X = X + vMin.x;
-	
-	Y = Y * vMax.y;
-	Y = Y + vMin.y;
-	
-	Z = Z * vMax.z;
-	Z = Z + vMin.z;
-	return vec3(X, Y, Z); 
-}
-
-float gainF(vec3 K1, vec3 K2){
-	float gF = 1.0 - dot(K1,K2);
-	gF = gF*gF; 
-	float ww = K1.z - K2.z;
+//gain factor as described in the thesis
+//consisting of C = F*G/w^2
+//where F is the fesnel number
+//    G is the geometric term
+//    w is the 3rd component of (u,v,w)
+//for further details I refer to my thesis,
+//in the chapter 'derivations'.
+//@param incident light direction
+//@param viewing direction
+//@return F*G/w^2
+float gainFactor(vec3 k1, vec3 k2){
+	// This side is not visible
+	if (k1.z > 0.0 || k2.z < 0.0){
+		return 0.0;
+	}
+	float ww2 = k1.z - k2.z;
+	ww2 = ww2*ww2;
+	
+	float geometricalTerm = 1.0 - dot(k1,k2);
+	geometricalTerm *= (geometricalTerm/(k2.z*ww2)); 
 	 
-	 
-	if(K1.z > 0.0 || K2.z < 0.0){
-		return 0.0; // This side is not visible
-	}
-	 
-	ww = ww*ww;
-
-	gF = gF / K2.z;
-	gF = gF / ww;
-	 
-	float fFac = getFresnelFactor(K1, K2); 
-	return fFac*gF;
-}
-
+	float fresnelTerm = getFresnelFactor(k1, k2); 
+	return fresnelTerm*geometricalTerm;
+}
+
+//performs gamma correction applied on a vector containing rgb colors
+//see http://en.wikipedia.org/wiki/Gamma_correction
 vec3 gammaCorrect(vec3 inRGB, float gamma){
 	float clLim = 0.0031308;
 	float clScale = 1.055;

		
	vec3 totalXYZ = 0.1*cdiff.xyz + eps;
	totalXYZ = getBRDF_RGB_T_D65(M_Adobe_XRNew, totalXYZ);
	float fac = (getShadowMaskFactor(lightDir, Pos)*gainF(lightDir, Pos));
	fac = (fac < 0.0) ? 0.0 : fac;
	frag_shaded = anis*vec4(gammaCorrect(fac*totalXYZ, 2.3), 1.0);
}


void mainBRDFMap(){

	float thetaR = asin(sqrt(o_org_pos.x*o_org_pos.x + o_org_pos.y*o_org_pos.y ));
	float phiR = atan(o_org_pos.y, o_org_pos.x);
	vec3 k1 = vec3(0.0f);

vec2 getNMMfor(float, float);
vec3 sumContributionAlongDir(vec2, float, float);

//perform a color space transformation:
//from CIE XYZ to RGB using the illuminant D65 
//in order to define the white point.
vec3 getBRDF_RGB_T_D65(mat3 T, vec3 brdf_xyz){
	vec3 D65 = vec3(0.95047, 1.0, 1.08883);
	vec3 output = vec3(0.0);
