diff --git a/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o b/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o
index 893c5e966ca3ac0c6aefabbcb412c1cc72aae780..7536ca0838313e2c78786a1afa3cf058c11a3603 100644
Binary files a/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o and b/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o differ
diff --git a/apps/specular_estimation/src/Ceres.h b/apps/specular_estimation/src/Ceres.h
index a6d1edbe33b47835da13555eaf67904eed395a3e..cb259dd10e324fa69fc040a84c74c7d425db390d 100644
--- a/apps/specular_estimation/src/Ceres.h
+++ b/apps/specular_estimation/src/Ceres.h
@@ -17,7 +17,7 @@ using ceres::Problem;
 using ceres::Solver;
 using ceres::Solve;*/
 
-void specularMinimisation(double& Roughness, double& Metallic, double& Gain, double& Bias, std::string imageName, double residualValue, cv::Vec3d residual, double totalResidual, cv::Mat residualImage, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, double width, int height, int numberOfLights, GLuint RoughnessID, GLuint MetallicID, GLuint LightDistanceID, GLuint LightIntensityID, GLuint programID, GLuint ModelMatrixID, GLuint ViewMatrixID, GLuint DepthBiasID, GLuint lightInvDirID, GLuint Texture, GLuint TextureID, GLuint depthTexture, GLuint ShadowMapID, GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, GLuint MatrixID, glm::mat4 ModelMatrix, glm::mat4 MVP, glm::mat4 ViewMatrix, glm::mat4 depthBiasMVP, std::vector<glm::vec3> lightInvDirs, std::vector<cv::Mat> textureImages);
+void specularMinimisation(double& Roughness, double& Metallic, double& Gain, double& Bias, double LightDistance, double LightIntensity, std::string imageName, double residualValue, cv::Vec3d residual, double totalResidual, cv::Mat residualImage, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, double width, int height, int numberOfLights, GLuint RoughnessID, GLuint MetallicID, GLuint LightDistanceID, GLuint LightIntensityID, GLuint programID, GLuint ModelMatrixID, GLuint ViewMatrixID, GLuint DepthBiasID, GLuint lightInvDirID, GLuint Texture, GLuint TextureID, GLuint depthTexture, GLuint ShadowMapID, GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, GLuint MatrixID, glm::mat4 ModelMatrix, glm::mat4 MVP, glm::mat4 ViewMatrix, glm::mat4 depthBiasMVP, std::vector<glm::vec3> lightInvDirs, std::vector<cv::Mat> textureImages, bool synthetic, double RoughnessSynthetic, double MetallicSynthetic);
 void outputToFile(std::string outputFileName, double roughness, double metallic, double lightDistance, double lightIntensity, double gain, double bias, double resiudal);
 
 class MyScalarCostFunctor {
@@ -105,7 +105,7 @@ class MyScalarCostFunctor {
 		std::string outputFileName_;
 };
 
-void specularMinimisation(double& Roughness, double& Metallic, double& Gain, double& Bias, double LightDistance, double LightIntensity, std::string imageName, double residualValue, cv::Vec3d residual, double totalResidual, cv::Mat residualImage, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int width, int height, int numberOfLights, GLuint RoughnessID, GLuint MetallicID, GLuint LightDistanceID, GLuint LightIntensityID, GLuint programID, GLuint ModelMatrixID, GLuint ViewMatrixID, GLuint DepthBiasID, GLuint lightInvDirID, GLuint Texture, GLuint TextureID, GLuint depthTexture, GLuint ShadowMapID, GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, GLuint MatrixID, glm::mat4 ModelMatrix, glm::mat4 MVP, glm::mat4 ViewMatrix, glm::mat4 depthBiasMVP, std::vector<glm::vec3> lightInvDirs, std::vector<cv::Mat> textureImages) {
+void specularMinimisation(double& Roughness, double& Metallic, double& Gain, double& Bias, double LightDistance, double LightIntensity, std::string imageName, double residualValue, cv::Vec3d residual, double totalResidual, cv::Mat residualImage, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int width, int height, int numberOfLights, GLuint RoughnessID, GLuint MetallicID, GLuint LightDistanceID, GLuint LightIntensityID, GLuint programID, GLuint ModelMatrixID, GLuint ViewMatrixID, GLuint DepthBiasID, GLuint lightInvDirID, GLuint Texture, GLuint TextureID, GLuint depthTexture, GLuint ShadowMapID, GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, GLuint MatrixID, glm::mat4 ModelMatrix, glm::mat4 MVP, glm::mat4 ViewMatrix, glm::mat4 depthBiasMVP, std::vector<glm::vec3> lightInvDirs, std::vector<cv::Mat> textureImages, bool synthetic, double RoughnessSynthetic, double MetallicSynthetic) {
 	
 	// The variable to solve for with its initial value. It will be mutated in place by the solver.
 	const double initialRoughness = Roughness;
@@ -115,10 +115,12 @@ void specularMinimisation(double& Roughness, double& Metallic, double& Gain, dou
 
 	// Create the file name of the log with the initial parameters
 	std::ostringstream stm;
-	stm << imageName << " " << initialRoughness << " " << initialMetallic << " " << initialGain << " " << initialBias << ".txt";
+	//stm << imageName << " " << Roughness << " " << Metallic << " " << Gain << " " << Bias << ".txt";
+	stm << imageName << " " << Roughness << " " << Metallic << " " << Gain << " " << Bias << ".txt";
 	std::string outputFileName = stm.str();
 	std::ofstream outputFile(outputFileName, std::ios::trunc); // Erase the previous contents of the file
-	outputFile << "Roughness" << "\t" << "Metallic" << "\t" << "Light Distance" << "\t" << "Light Intensity" << "\t" << "Gain" << "\t" << "Bias" << "\t" << "Resiudal" << std::endl;
+	//outputFile << "Roughness" << "\t" << "Metallic" << "\t" << "Light Distance" << "\t" << "Light Intensity" << "\t" << "Gain" << "\t" << "Bias" << "\t" << "Resiudal" << std::endl;
+	outputFile << "Roughness" << "\t" << "Metallic" << "\t" << "Gain" << "\t" << "Bias" << "\t" << "Resiudal" << std::endl;
 
 	// Build the problem.
 	ceres::Problem problem;
@@ -138,8 +140,18 @@ void specularMinimisation(double& Roughness, double& Metallic, double& Gain, dou
 	//problem.SetParameterLowerBound(&Bias, 0, 0);
 	//problem.SetParameterBlockConstant(&Roughness);
 	//problem.SetParameterBlockConstant(&Metallic);
-	//problem.SetParameterBlockConstant(&Gain);
-	//problem.SetParameterBlockConstant(&Bias);
+	problem.SetParameterBlockConstant(&Gain);
+	problem.SetParameterBlockConstant(&Bias);
+
+	/*problem.SetParameterLowerBound(&Roughness, 0, 0);
+	problem.SetParameterUpperBound(&Roughness, 0, 1);
+	problem.SetParameterLowerBound(&Metallic, 0, 0.01);
+	problem.SetParameterLowerBound(&Gain, 0, 0);
+	//problem.SetParameterLowerBound(&Bias, 0, 0);
+	//problem.SetParameterBlockConstant(&Roughness);
+	//problem.SetParameterBlockConstant(&Metallic);
+	problem.SetParameterBlockConstant(&Gain);
+	problem.SetParameterBlockConstant(&Bias);*/
 	
 	// Run the solver
 	Solver::Options options;
@@ -210,12 +222,26 @@ void specularMinimisation(double& Roughness, double& Metallic, double& Gain, dou
 		glfwSwapBuffers(window);
 		glfwPollEvents();
 	}
+
+	//outputFile.close();
+	std::ostringstream stm3;
+	if (synthetic)
+		stm3 << imageName << " " << initialRoughness << " " << initialMetallic << " " << RoughnessSynthetic << " " << MetallicSynthetic << " " << Roughness << " " << Metallic << " " << Gain << " " << Bias << ".txt";
+	else
+		stm3 << imageName << " " << initialRoughness << " " << initialMetallic << " " << Roughness << " " << Metallic << " " << Gain << " " << Bias << ".txt";
+	
+	std::string outputFileName2 = stm3.str();
+	int rename = std::rename(outputFileName.c_str(), outputFileName2.c_str());
+	/*if (std::rename(outputFileName2, outputFileName)) {
+        std::perror("Error renaming");
+    }*/
 }
 
 void outputToFile(std::string outputFileName, double roughness, double metallic, double lightDistance, double lightIntensity, double gain, double bias, double resiudal) {
 	std::ofstream outputFile(outputFileName, std::ios::app);
 	if (outputFile.is_open()) {
-		outputFile << roughness << "\t" << metallic << "\t" << lightDistance << "\t" << lightIntensity << "\t" << gain << "\t" << bias << "\t" << resiudal << std::endl;
+		//outputFile << roughness << "\t" << metallic << "\t" << lightDistance << "\t" << lightIntensity << "\t" << gain << "\t" << bias << "\t" << resiudal << std::endl;
+		outputFile << roughness << "\t" << metallic << "\t" << gain << "\t" << bias << "\t" << resiudal << std::endl;
 		outputFile.close();
 	}
 }
diff --git a/apps/specular_estimation/src/OpenCV.h b/apps/specular_estimation/src/OpenCV.h
index 59b37642f6f506701ec85b0918a8881cb4c00e43..8f791b2d889a1560d1107f4cab09445f860b3ae8 100644
--- a/apps/specular_estimation/src/OpenCV.h
+++ b/apps/specular_estimation/src/OpenCV.h
@@ -752,7 +752,8 @@ cv::Mat convertImageToNormals(cv::Mat normalMap) {
 
 			float nxi = (xi - 128.0) / 256.0;
 			float nyi = (yi - 128.0) / 256.0;
-			float nzi = (zi - 128.0) / 256.0;
+			//float nzinyi = (zi - 128.0) / 256.0;
+			float nzi = sqrt(1.0f - pow(nxi, 2.0f) - pow(nyi, 2.0f));
 
 			result.at<cv::Vec3f>(y, x)[0] = nxi;
 			result.at<cv::Vec3f>(y, x)[1] = nyi;
diff --git a/apps/specular_estimation/src/OpenGL.h b/apps/specular_estimation/src/OpenGL.h
index 8033c0855be2241e7302e8ac7366103e406eaddd..aa86ccfa63d4a5dac1c40118f6032e7bcbc53496 100644
--- a/apps/specular_estimation/src/OpenGL.h
+++ b/apps/specular_estimation/src/OpenGL.h
@@ -890,7 +890,7 @@ void computeMatricesFromLights(int windowWidth, int windowHeight, glm::vec3& pos
 	}
 
 	//lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 0)+0.5, lightDirections.at<float>(lightNumber, 2), 0.5-lightDirections.at<float>(lightNumber, 1));
-	lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 0), -lightDirections.at<float>(lightNumber, 2), lightDirections.at<float>(lightNumber, 1));
+	lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 1), lightDirections.at<float>(lightNumber, 2));
 	
 	
 	// Modify specular power
diff --git a/apps/specular_estimation/src/ShadowMapping.fragmentshader b/apps/specular_estimation/src/ShadowMapping.fragmentshader
index 5df320788d27651e35a7b9eb78586641b71beee1..7b3787b4b93553a13ed06642273b42438d3d9475 100644
--- a/apps/specular_estimation/src/ShadowMapping.fragmentshader
+++ b/apps/specular_estimation/src/ShadowMapping.fragmentshader
@@ -18,7 +18,7 @@ layout(location = 0) out vec3 color;
 // Values that stay constant for the whole mesh.
 uniform sampler2D myTextureSampler;
 //uniform sampler2D mySpecularSampler;
-uniform mat4 MV;
+//uniform mat4 MV;
 uniform sampler2DShadow shadowMap;
 uniform vec3 LightPosition_worldspace;
 //uniform float specularIntensity;
@@ -163,10 +163,10 @@ void main2() {
 	float specularPower = metallic;
 
 	// Light emission properties
-	vec3 LightColor = vec3(1, 1, 1);
+	vec3 LightColor = vec3(lightPower, lightPower, lightPower);
 
 	// The power and colour of each light could be obtained from either the chrome sphere reflection or the Macbeth colour chart
-	float LightPower = 1.0f;
+	//float LightPower = 1.0f;
 	
 	// Material properties
 	float ambientPower = 0.0f;
@@ -180,18 +180,20 @@ void main2() {
 	//vec3 MaterialSpecularColor = texture( myTextureSampler, UV ).rgb;
 
 	// Distance to the light
-	float distance = length( LightPosition_worldspace - Position_worldspace );
+	//float distance = length( LightPosition_worldspace - Position_worldspace );
 
 	// Normal of the computed fragment, in camera space
 	vec3 n = normalize( Normal_cameraspace );
 	// Direction of the light (from the fragment to the light)
 	vec3 l = normalize( LightDirection_cameraspace );
+	//vec3 l = normalize(LightDirection_worldspace - Position_worldspace);
 	// Cosine of the angle between the normal and the light direction, 
 	// clamped above 0
 	//  - light is at the vertical of the triangle -> 1
 	//  - light is perpendiular to the triangle -> 0
 	//  - light is behind the triangle -> 0
 	float cosTheta = clamp( dot( n, l ), 0, 1 );
+	//float cosTheta = max( dot( n, l ), 0.0 );
 	
 	// Eye vector (towards the camera)
 	vec3 E = normalize(EyeDirection_cameraspace);
@@ -201,8 +203,10 @@ void main2() {
 	//  - Looking into the reflection -> 1
 	//  - Looking elsewhere -> <1
 	float cosAlpha = clamp( dot( E, R ), 0, 1 );
+	//float cosAlpha = max( dot( E, R ), 0.0 );
 	
 	float visibility = 1.0f;
+	float attenuation = 1.0 / (distance * distance);
 
 	// Fixed bias
 	//float bias = 0.005;
@@ -234,7 +238,7 @@ void main2() {
 		// Perspective Shadow Projection
 		// texture( shadowMap, ShadowCoord.xy ).z is the distance between the light and the nearest occluder
 		// ShadowCoord.z is the distance between the light and the current fragment
-		visibility -= (darkness/samples)*(1.0-texture( shadowMap, vec3(ShadowCoord.xy/ShadowCoord.w + poissonDisk[i]/700.0,  (ShadowCoord.z-bias)/ShadowCoord.w) ));
+		//visibility -= (darkness/samples)*(1.0-texture( shadowMap, vec3(ShadowCoord.xy/ShadowCoord.w + poissonDisk[i]/700.0,  (ShadowCoord.z-bias)/ShadowCoord.w) ));
 	}
 
 	// For spot lights, use either one of these lines instead.
@@ -245,9 +249,9 @@ void main2() {
 		// Ambient: simulates indirect lighting
 		//MaterialAmbientColor +
 		// Diffuse: "colour" of the object
-		visibility * MaterialDiffuseColor  * LightColor * LightPower * cosTheta +
-		//((visibility * MaterialDiffuseColor * LightColor * LightPower * cosTheta) / 3.1415926535897932384626433832795 ) +
+		attenuation * MaterialDiffuseColor  * LightColor * lightPower * cosTheta +
+		//((visibility * MaterialDiffuseColor * LightColor * lightPower * cosTheta) / 3.1415926535897932384626433832795 ) +
 		// Specular: reflective highlight, like a mirror
-		visibility * MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha, specularPower);
-		//visibility * MaterialSpecularColor * LightColor * LightPower * ((specularPower + 2)/(6.283185307179586476925286766559)) * pow(cosAlpha, specularPower);
+		attenuation * MaterialSpecularColor * LightColor * lightPower * pow(cosAlpha, specularPower);
+		//visibility * MaterialSpecularColor * LightColor * lightPower * ((specularPower + 2)/(6.283185307179586476925286766559)) * pow(cosAlpha, specularPower);
 }
\ No newline at end of file
diff --git a/apps/specular_estimation/src/specular_estimation.cc b/apps/specular_estimation/src/specular_estimation.cc
index c3e8baa2df56c04aa67dc97110933dcf2e76fa98..23a0d700c91235c582490490a4ba2cb41362f9d0 100644
--- a/apps/specular_estimation/src/specular_estimation.cc
+++ b/apps/specular_estimation/src/specular_estimation.cc
@@ -143,22 +143,25 @@ void specularEstimation(std::string imageName, std::string calibration, double R
 	double totalResidual, residualValue;
 
 	//cv::Mat lightDirectionsInverted;
-	cv::Mat lightDirections = (cv::Mat_<float>(6,3) << 0.447712, 0.138562, 0.883377, 0.228758, -0.106536, 0.967636, 0.1, 0.0705882, 0.99248, 0.000653595, -0.0718954, 0.997412, -0.139216, -0.12549, 0.982279, -0.494771, 0.115033, 0.861376);
-	/*cv::Mat lightDirections = (cv::Mat_<float>(6,3) << 0, 0, 1, 
+	//cv::Mat lightDirections = (cv::Mat_<float>(6,3) << 0.447712, 0.138562, 0.883377, 0.228758, -0.106536, 0.967636, 0.1, 0.0705882, 0.99248, 0.000653595, -0.0718954, 0.997412, -0.139216, -0.12549, 0.982279, -0.494771, 0.115033, 0.861376);
+	cv::Mat lightDirections = (cv::Mat_<float>(6,3) << -0.57735, -0.57735, 0.57735, 
 	0, 0.7071, 0.7071,
 	0, -0.7071, 0.7071,
 	0.7071, 0, 0.7071,
 	-0.7071, 0, 0.7071,
 	0.57735, 0.57735, 0.57735
-	);*/
+	);
 	//cv::invert(lightDirections, lightDirectionsInverted, cv::DECOMP_SVD);
 
 
 	std::vector<glm::vec3> lightInvDirs;
-	for (int i = 0; i < numberOfLights; i++)
-		lightInvDirs.push_back(glm::vec3(lightDirections.at<float>(i, 0), -lightDirections.at<float>(i, 2), lightDirections.at<float>(i, 1)));
+	for (int i = 0; i < numberOfLights; i++) {
+		//lightInvDirs.push_back(glm::vec3(lightDirections.at<float>(i, 0), -lightDirections.at<float>(i, 2), lightDirections.at<float>(i, 1)));
+		lightInvDirs.push_back(glm::vec3(lightDirections.at<float>(i, 0), lightDirections.at<float>(i, 1), lightDirections.at<float>(i, 2)));
+	}
 	
-	lightInvDir = glm::vec3(lightDirections.at<float>(0, 0), -lightDirections.at<float>(0, 2), lightDirections.at<float>(0, 1));
+	//lightInvDir = glm::vec3(lightDirections.at<float>(0, 0), -lightDirections.at<float>(0, 2), lightDirections.at<float>(0, 1));
+	lightInvDir = glm::vec3(lightDirections.at<float>(0, 0), lightDirections.at<float>(0, 1), lightDirections.at<float>(0, 2));
 
 	initialiseOpenGL(heightMap, normalMap, albedo, lightDirections, width, height, depthProjectionMatrix, depthViewMatrix, position, horizontalAngle, verticalAngle, FoV, lightInvDir, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, RoughnessID, Roughness, MetallicID, Metallic, LightDistanceID, LightDistance, LightIntensityID, LightIntensity, numberOfLights, calculateResidual, depthMVP, depthModelMatrix, MVP, ProjectionMatrix, ViewMatrix, ModelMatrix, depthBiasMVP, biasMatrix);
 	
@@ -166,7 +169,7 @@ void specularEstimation(std::string imageName, std::string calibration, double R
 		RenderSynthetic(depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, numberOfLights, RoughnessID, RoughnessSynthetic, MetallicID, MetallicSynthetic, LightDistanceID, LightDistance, LightIntensityID, LightIntensity, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);
 
 	if (!denseSample) {
-		specularMinimisation(Roughness, Metallic, Gain, Bias, LightDistance, LightIntensity, imageName, residualValue, residual, totalResidual, residualImage, depthProjectionMatrix, depthViewMatrix, width, height, numberOfLights, RoughnessID, MetallicID, LightDistanceID, LightIntensityID, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, ModelMatrix, MVP, ViewMatrix, depthBiasMVP, lightInvDirs, textureImages);
+		specularMinimisation(Roughness, Metallic, Gain, Bias, LightDistance, LightIntensity, imageName, residualValue, residual, totalResidual, residualImage, depthProjectionMatrix, depthViewMatrix, width, height, numberOfLights, RoughnessID, MetallicID, LightDistanceID, LightIntensityID, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, ModelMatrix, MVP, ViewMatrix, depthBiasMVP, lightInvDirs, textureImages, synthetic, RoughnessSynthetic, MetallicSynthetic);
 	} else {
 		std::string outputFileName = imageName + ".txt";
 		std::ofstream outputFile(outputFileName, std::ios::app);
diff --git a/bin/specular_estimation b/bin/specular_estimation
index 0185dddd4f6b625feb7200c0db016bb858ab2754..03d5c4c6634524aa503b2cd0ca6d46ec6bc651f7 100755
Binary files a/bin/specular_estimation and b/bin/specular_estimation differ