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 ccc2589e3f190e4970c902262ee802c020ef211f..3273a97207e4569bb6e22b3d76378e7bb4294a07 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 77a157a0d0bb93318cf09fa0a39a0d1bdaf06e2b..b46a8b9edcfab779ca63ac994d2485d1576d9d57 100644
--- a/apps/specular_estimation/src/Ceres.h
+++ b/apps/specular_estimation/src/Ceres.h
@@ -32,39 +32,12 @@ class MyScalarCostFunctor {
 				// Render the polygons
 				renderPolygons(width_, height_, programID_, lightInvDirs_[i], MatrixID_, ModelMatrixID_, ViewMatrixID_, DepthBiasID_, lightInvDirID_, Texture_, TextureID_, depthTexture_, ShadowMapID_, vertexbuffer_, uvbuffer_, normalbuffer_, elementbuffer_, indices_, MVP_, ModelMatrix_, ViewMatrix_, depthBiasMVP_, SpecularIntensityID_, specularIntensity_, SpecularPowerID_, specularPower_);
 				
-				
-				
-				// Create an empty Mat the same size as the image
-				cv::Mat temp = cv::Mat(height_, width_, CV_8UC3);
-				cv::Mat sumOfSquaredDifferences = cv::Mat(height_, width_, CV_16UC3);
-				
-				// Read the image into the Mat as an 8-bit colour image
-				glReadPixels(0, 0, width_, height_, GL_BGR, GL_UNSIGNED_BYTE, temp.data);
-				
-				// 3D models have the origin in the bottom-left corner, while images have the origin in the top-left corner. The image is flipped to convert between the two.
-				cv::flip(temp, temp, 0);
-				
-				// Calculate the absolute differences between the model and the photograph
-				cv::absdiff(temp, textureImages_[i], sumOfSquaredDifferences);
-				
-				// Square each element
-				multiply(sumOfSquaredDifferences, sumOfSquaredDifferences, sumOfSquaredDifferences);
-				
-				// Crop a 1-pixel border around the residual
-				sumOfSquaredDifferences = sumOfSquaredDifferences(cv::Rect(1, 1, width_-2, height_-2));
-				
-				//sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2])/(windowHeight-2 * windowWidth-2);
-				sum += (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
-				//residuals = cv::Vec3d(cv::sum(sumOfSquaredDifferences)[0], cv::sum(sumOfSquaredDifferences)[1], cv::sum(sumOfSquaredDifferences)[2]);
-				
-				
-				//std::cout << "Average residual = " << totalResidual << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
+				computeResidual(i, height_, width_, textureImages_, sum);
 				
 				// Swap buffers
 				glfwSwapBuffers(window);
 				glfwPollEvents();
 			}
-			//} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed
 			
 			residualValue[0] = sum;
 			//residualValue[0] = function(SpecularIntensity, SpecularPower);
diff --git a/apps/specular_estimation/src/OpenGL.h b/apps/specular_estimation/src/OpenGL.h
index 5c549d084c42461af685add6edcc01344598a9db..e584df01284c11a1f37291a84912fd16808909a4 100644
--- a/apps/specular_estimation/src/OpenGL.h
+++ b/apps/specular_estimation/src/OpenGL.h
@@ -44,19 +44,20 @@ void computeMatricesFromInputs(int windowWidth, int windowHeight, glm::vec3& pos
 void computeMatricesFromLights(int windowWidth, int windowHeight, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, glm::vec3& lightInvDir, glm::mat4& depthProjectionMatrix, glm::mat4& depthViewMatrix, bool& perspectiveProjection, cv::Mat lightDirections, std::vector<cv::Mat> textureImages, int& lightNumber, int numberOfLights, bool& calculateResidual, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower);
 void createMesh(cv::Mat Z, cv::Mat normals, std::vector<glm::vec3> & out_vertices, std::vector<glm::vec2> & out_uvs, std::vector<glm::vec3> & out_normals, std::vector<unsigned int> & out_indices);
 GLuint loadMat(cv::Mat cv_texture);
-void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture, std::vector< cv::Mat > textureImages, cv::Mat lightDirections, int& windowWidth, int& windowHeight, glm::mat4& depthProjectionMatrix, glm::mat4& depthViewMatrix, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, GLuint& programID, GLuint& MatrixID, 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& depthProgramID, GLuint& quad_programID, GLuint& FramebufferName, GLuint& quad_vertexbuffer, GLuint& VertexArrayID, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, int numberOfLights, bool calculateResidual);
+void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture, std::vector<cv::Mat> textureImages, cv::Mat lightDirections, int& windowWidth, int& windowHeight, glm::mat4& depthProjectionMatrix, glm::mat4& depthViewMatrix, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, GLuint& programID, GLuint& MatrixID, 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& depthProgramID, GLuint& quad_programID, GLuint& FramebufferName, GLuint& quad_vertexbuffer, GLuint& VertexArrayID, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, int numberOfLights, bool calculateResidual, glm::mat4& depthMVP, glm::mat4& depthModelMatrix, glm::mat4& MVP, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, glm::mat4& ModelMatrix, glm::mat4& depthBiasMVP, glm::mat4& biasMatrix);
 //void openGL(int& lightNumber, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, cv::Mat lightDirections, std::vector< cv::Mat > textureImages, GLuint programID, GLuint MatrixID, 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 depthProgramID, GLuint quad_programID, GLuint FramebufferName, GLuint quad_vertexbuffer, GLuint VertexArrayID, int numberOfLights, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, bool& calculateResidual);
 void terminateOpenGL(GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, GLuint programID, GLuint depthProgramID, GLuint quad_programID, GLuint Texture, GLuint FramebufferName, GLuint depthTexture, GLuint quad_vertexbuffer, GLuint VertexArrayID);
 void renderPolygons(int windowWidth, int windowHeight, GLuint programID, glm::vec3 lightInvDir, GLuint MatrixID, 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, glm::mat4 MVP, glm::mat4 ModelMatrix, glm::mat4 ViewMatrix, glm::mat4 depthBiasMVP, GLuint SpecularIntensityID, double SpecularIntensity, GLuint SpecularPowerID, double SpecularPower);
 void renderShadows(GLuint FramebufferName, int shadowResolution, GLuint depthProgramID, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, GLuint depthMatrixID, GLuint vertexbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, glm::mat4 depthModelMatrix, glm::mat4 depthMVP);
 void renderShadowMap(GLuint quad_programID, GLuint depthTexture, GLuint texID, GLuint quad_vertexbuffer);
 void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::string modelPath, std::vector < cv::Mat > textureImages, cv::Mat& residual, cv::Vec3d& residuals, double& sum, double SpecularIntensity, double SpecularPower);
+void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::vector <cv::Mat> textureImages, double& sum);
 double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::vector<cv::Mat> textureImages, cv::Mat& residual, cv::Vec3d& residuals, double SpecularIntensity, double SpecularPower);
 //void computeResiduals(cv::Vec3d& residual, std::vector< cv::Vec3d >& residuals, double& totalResidual, cv::Mat& residualImage, bool calculateResidual, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int width, int height, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, cv::Mat lightDirections, std::vector< cv::Mat > textureImages, int lightNumber, int numberOfLights, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, 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, std::string modelPath);
 void viewModel(cv::Vec3d& residual, std::vector< cv::Vec3d >& residuals, double& totalResidual, cv::Mat& residualImage, bool calculateResidual, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int width, int height, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, cv::Mat lightDirections, std::vector< cv::Mat > textureImages, int lightNumber, int numberOfLights, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, 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, std::string modelPath);
 void checkForErrors(std::string stage, bool printIfOK);
 
-void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture, std::vector<cv::Mat> textureImages, cv::Mat lightDirections, int& windowWidth, int& windowHeight, glm::mat4& depthProjectionMatrix, glm::mat4& depthViewMatrix, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, GLuint& programID, GLuint& MatrixID, 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& depthProgramID, GLuint& quad_programID, GLuint& FramebufferName, GLuint& quad_vertexbuffer, GLuint& VertexArrayID, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, int numberOfLights, bool calculateResidual) {
+void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture, std::vector<cv::Mat> textureImages, cv::Mat lightDirections, int& windowWidth, int& windowHeight, glm::mat4& depthProjectionMatrix, glm::mat4& depthViewMatrix, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, GLuint& programID, GLuint& MatrixID, 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& depthProgramID, GLuint& quad_programID, GLuint& FramebufferName, GLuint& quad_vertexbuffer, GLuint& VertexArrayID, GLuint& SpecularIntensityID, double& SpecularIntensity, GLuint& SpecularPowerID, double& SpecularPower, int numberOfLights, bool calculateResidual, glm::mat4& depthMVP, glm::mat4& depthModelMatrix, glm::mat4& MVP, glm::mat4& ProjectionMatrix, glm::mat4& ViewMatrix, glm::mat4& ModelMatrix, glm::mat4& depthBiasMVP, glm::mat4& biasMatrix) {
 	// Initialise the GLFW library first
 	if (!glfwInit()) {
 		std::cerr << "Failed to initialise GLFW.\n";
@@ -255,26 +256,14 @@ void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture,
 	// Mouse Speed
 	float mouseSpeed = 0.005f;
 	
-	// Render the shadows
-	glm::mat4 depthModelMatrix = glm::mat4(1.0);
-	glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
-	//renderShadows(FramebufferName, shadowResolution, depthProgramID, depthProjectionMatrix, depthViewMatrix, depthMatrixID, vertexbuffer, elementbuffer, indices, depthModelMatrix, depthMVP);
-
-	// Compute the MVP matrix from keyboard and mouse input
-	glm::mat4 ModelMatrix = glm::mat4(1.0);
-	glm::mat4 MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix;
-	bool perspectiveProjection, shadowControl;
-	//computeMatricesFromInputs(windowWidth, windowHeight, position, horizontalAngle, verticalAngle, FoV, mouseSpeed, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, shadowControl, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-	//computeMatricesFromLights(windowWidth, windowHeight, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, 0, numberOfLights, calculateResidual, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-	MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
-
-	glm::mat4 biasMatrix(
-		0.5, 0.0, 0.0, 0.0,
-		0.0, 0.5, 0.0, 0.0,
-		0.0, 0.0, 0.5, 0.0,
-		0.5, 0.5, 0.5, 1.0
-	);
+	depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
+	bool perspectiveProjection = false;
+	int lightNumber = 0;
 
+	// Compute the MVP matrix
+	computeMatricesFromLights(windowWidth, windowHeight, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, lightDirections, textureImages, lightNumber, numberOfLights, calculateResidual, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
+	
+	MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
 	depthBiasMVP = biasMatrix * depthMVP;
 }
 
@@ -551,7 +540,32 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::st
 	//std::cout << "Image " << imageNumber << ", average sum of squared differences: R = " << sum << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
 }
 
-double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::vector < cv::Mat > textureImages, cv::Mat& residual, cv::Vec3d& residuals, double SpecularIntensity, double SpecularPower) {
+void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::vector <cv::Mat> textureImages, double& sum) {
+	
+	// Create an empty Mat the same size as the image
+	cv::Mat temp = cv::Mat(windowHeight, windowWidth, CV_8UC3);
+	cv::Mat sumOfSquaredDifferences = cv::Mat(windowHeight, windowWidth, CV_16UC3);
+	
+	// Read the image into the Mat as an 8-bit colour image
+	glReadPixels(0, 0, windowWidth, windowHeight, GL_BGR, GL_UNSIGNED_BYTE, temp.data);
+	
+	// 3D models have the origin in the bottom-left corner, while images have the origin in the top-left corner. The image is flipped to convert between the two.
+	cv::flip(temp, temp, 0);
+	
+	// Calculate the absolute differences between the model and the photograph
+	cv::absdiff(temp, textureImages[imageNumber], sumOfSquaredDifferences);
+	
+	// Square each element
+	multiply(sumOfSquaredDifferences, sumOfSquaredDifferences, sumOfSquaredDifferences);
+	
+	// Crop a 1-pixel border around the residual
+	sumOfSquaredDifferences = sumOfSquaredDifferences(cv::Rect(1, 1, windowWidth-2, windowHeight-2));
+	
+	//sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2])/(windowHeight-2 * windowWidth-2);
+	sum += (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
+}
+
+double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::vector <cv::Mat> textureImages, cv::Mat& residual, cv::Vec3d& residuals, double SpecularIntensity, double SpecularPower) {
 		
 	// Create an empty Mat the same size as the image
 	cv::Mat temp = cv::Mat(windowHeight, windowWidth, CV_8UC3);
@@ -1035,7 +1049,7 @@ void createMesh(cv::Mat cv_depth, cv::Mat cv_normals, std::vector<glm::vec3> & o
 		*/
 	}
 
-	std::cout << "Vertices indexed.\n\n";
+	std::cout << "Vertices indexed.\n";
 }
 
 GLuint loadMat(cv::Mat cv_texture) {
diff --git a/apps/specular_estimation/src/specular_estimation.cc b/apps/specular_estimation/src/specular_estimation.cc
index a91dbd996998e2dd9591b5e33dc770ead4ce95f0..7eaf008a0bf4edd9a8822dda2849d2b9b041aec8 100644
--- a/apps/specular_estimation/src/specular_estimation.cc
+++ b/apps/specular_estimation/src/specular_estimation.cc
@@ -89,400 +89,37 @@ int main(int argc, char** argv) {
 */
 
 	photometricStereo(imageName, calibration, modelPath, imageScale, heightMap, normalMap, texture, lightDirections, lightDirectionsPerspective, textureImages, modelImages, calibrationImages, calibrationBoundingBox, width, height, rvecs, tvecs);
-	//photometricStereo(imageName, calibration, modelPath, imageScale, heightMap, normalMap, texture, lightDirections, lightDirectionsPerspective, charucoImages, modelImages, calibrationImages, calibrationBoundingBox, width, height, rvecs, tvecs);
-
-
-
-
 
 	glm::vec3 position, lightInvDir;
-	glm::mat4 depthProjectionMatrix, depthViewMatrix;
+	glm::mat4 depthProjectionMatrix, depthViewMatrix, depthModelMatrix = glm::mat4(1.0), depthMVP, ModelMatrix = glm::mat4(1.0), MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix;
+	glm::mat4 biasMatrix(
+		0.5, 0.0, 0.0, 0.0,
+		0.0, 0.5, 0.0, 0.0,
+		0.0, 0.0, 0.5, 0.0,
+		0.5, 0.5, 0.5, 1.0
+	);
 	float horizontalAngle, verticalAngle, FoV;
 	GLuint programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, SpecularIntensityID, SpecularPowerID;
 	std::vector<unsigned int> indices;
 	cv::Vec3d residual;
 	std::vector<cv::Vec3d> residuals;
 	int lightNumber = 0;
-	bool calculateResidual = false;
+	bool calculateResidual = false, perspectiveProjection, shadowControl;
 	double totalResidual, residualValue, SpecularIntensity = 0.5d, SpecularPower = 2.0d;
 	cv::Mat residualImage;
 	
 	std::vector<glm::vec3> lightInvDirs;
 	for (int i = 0; i < numberOfLights; i++)
 		lightInvDirs.push_back(glm::vec3(lightDirections.at<float>(i, 1), -lightDirections.at<float>(i, 0), lightDirections.at<float>(i, 2)));
-
-	/*
-	// Initialise the GLFW library first
-	if (!glfwInit()) {
-		std::cerr << "Failed to initialise GLFW.\n";
-		getchar();
-		return 0;
-	}
-	std::cout << "Initialised GLFW.\n";
-
-	glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
-	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
-	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3.3
-	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
-	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Only needed for macOS
-	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Disable old OpenGL
-
-	// Set the shadow resolution to 4096x4096
-	int shadowResolution = 4096;
-
-	// Open a window and create its OpenGL context
-	window = glfwCreateWindow(width, height, "Window", NULL, NULL);
-	if (window == NULL) {
-		std::cerr << "Failed to open GLFW window. Older Intel GPUs are not OpenGL 3.3 compatible.\n";
-		getchar();
-		glfwTerminate();
-		return 0;
-	}
-	glfwMakeContextCurrent(window);
-	std::cout << "Opened GLFW window.\n";
-
-	// On macOS with a retina screen it will be width*2 and height*2, so the actual framebuffer size is:
-	glfwGetFramebufferSize(window, &width, &height);
-
-	// Initialize GLEW
-	glewExperimental = true; // Needed for core profile
-	if (glewInit() != GLEW_OK) {
-		std::cerr << "Failed to initialise GLEW.\n";
-		getchar();
-		glfwTerminate();
-		return 0;
-	}
-	std::cout << "Initialised GLEW.\n";
-
-	// Ensure that the escape key being pressed can be captured
-	glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
-	// Hide the mouse and enable unlimited mouvement
-	//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
-
-	// Set the mouse at the center of the screen
-	glfwPollEvents();
-	//glfwSetCursorPos(window, width / 2, height / 2);
-
-	// Dark blue background
-	glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
-
-	// Enable depth test, which stores fragments in the Z-buffer
-	glEnable(GL_DEPTH_TEST);
-
-	// Accept fragment if it is closer to the camera than the former one
-	glDepthFunc(GL_LESS);
-
-	// Cull triangles which do not have a normal facing towards the camera
-	glEnable(GL_CULL_FACE);
-
-	// Create a Vertex Array Object (VAO) and set it as the current one
-	//GLuint VertexArrayID;
-	glGenVertexArrays(1, &VertexArrayID);
-	glBindVertexArray(VertexArrayID);
-
-	// Create and compile the GLSL program from the shaders
-	//depthProgramID = LoadShaders("/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/DepthRTT.vertexshader", "/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/DepthRTT.fragmentshader");
-	depthProgramID = LoadShaders("/home/thomas/Documents/Minimisation/apps/specular_estimation/src/DepthRTT.vertexshader", "/home/thomas/Documents/Minimisation/apps/specular_estimation/src/DepthRTT.fragmentshader");
-
-	// Get a handle for the "MVP" uniform
-	GLuint depthMatrixID = glGetUniformLocation(depthProgramID, "depthMVP");
-
-	// Load the texture
-	//GLuint Texture = loadDDS("C:/Users/tdwal/Documents/repos/OpenGLTutorials/tutorial16_shadowmaps/uvmap.DDS");
-	Texture = loadMat(texture);
-	std::cout << "Texture loaded.\n\n";
-
-	// Read the .obj file
-	std::vector<glm::vec3> vertices;
-	std::vector<glm::vec2> uvs;
-	std::vector<glm::vec3> normals;
-	//std::vector<unsigned int> indices;
-	//bool res = loadOBJ("C:/Users/tdwal/Documents/repos/OpenGLTutorials/tutorial16_shadowmaps/room_thickwalls.obj", vertices, uvs, normals);
-	createMesh(heightMap, normalMap, vertices, uvs, normals, indices);
-	std::cout << "\nModel created.\n\n";
-
-	// Load it into a VBO
-	//GLuint vertexbuffer; // This will identify the vertex buffer
-	glGenBuffers(1, &vertexbuffer); // Generate 1 buffer, and put the resulting identifier in vertexbuffer
-	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); // The following commands will talk about our 'vertexbuffer' buffer
-	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); // Give the vertices to OpenGL
-
-	//GLuint uvbuffer;
-	glGenBuffers(1, &uvbuffer);
-	glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
-	glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
-
-	//GLuint normalbuffer;
-	glGenBuffers(1, &normalbuffer);
-	glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
-	glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
-
-	// Generate a buffer for the indices as well
-	//GLuint elementbuffer;
-	glGenBuffers(1, &elementbuffer);
-	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
-	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
-
-
-	// ---------------------------------------------
-	// Render to Texture - specific code begins here
-	// ---------------------------------------------
-
-	// The framebuffer, which regroups 0, 1 or more textures, and 0 or 1 depth buffers
-	FramebufferName = 0;
-	glGenFramebuffers(1, &FramebufferName);
-	glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
-
-	// Depth texture. Slower than a depth buffer, but can be sampled later in the shader
-	//GLuint depthTexture;
-	glGenTextures(1, &depthTexture);
-	glBindTexture(GL_TEXTURE_2D, depthTexture);
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, shadowResolution, shadowResolution, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
-
-	glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
-
-	// No colour output in the bound framebuffer, only depth.
-	glDrawBuffer(GL_NONE);
-
-	// Always check if there are any errors in the framebuffer
-	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
-		return 0;
-
-	// The quad's FBO. Used only for visualising the shadowmap.
-	static const GLfloat g_quad_vertex_buffer_data[] = {
-		-1.0f, -1.0f, 0.0f,
-		 1.0f, -1.0f, 0.0f,
-		-1.0f,  1.0f, 0.0f,
-		-1.0f,  1.0f, 0.0f,
-		 1.0f, -1.0f, 0.0f,
-		 1.0f,  1.0f, 0.0f,
-	};
-
-	//GLuint quad_vertexbuffer;
-	glGenBuffers(1, &quad_vertexbuffer);
-	glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
-	glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
-
-	// Create and compile the GLSL program from the shaders
-	//quad_programID = LoadShaders("/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/Passthrough.vertexshader", "/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/SimpleTexture.fragmentshader");
-	quad_programID = LoadShaders("/home/thomas/Documents/Minimisation/apps/specular_estimation/src/Passthrough.vertexshader", "/home/thomas/Documents/Minimisation/apps/specular_estimation/src/SimpleTexture.fragmentshader");
-	std::cout << "Shaders loaded.\n";
-	GLuint texID = glGetUniformLocation(quad_programID, "texture");
-	std::cout << "Uniform Location of texture found.\n";
-
-	// Create and compile the GLSL program from the shaders
-	//programID = LoadShaders("/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/ShadowMapping.vertexshader", "/user/HS222/tw00275/PhD/Minimisation/apps/specular_estimation/src/ShadowMapping.fragmentshader");
-	programID = LoadShaders("/home/thomas/Documents/Minimisation/apps/specular_estimation/src/ShadowMapping.vertexshader", "/home/thomas/Documents/Minimisation/apps/specular_estimation/src/ShadowMapping.fragmentshader");
-
-	// Get a handle for the "myTextureSampler" uniform
-	TextureID = glGetUniformLocation(programID, "myTextureSampler");
-	//SpecularID = glGetUniformLocation(programID, "mySpecularSampler");
-	
-	SpecularIntensityID = glGetUniformLocation(programID, "specularIntensity");
-	SpecularPowerID = glGetUniformLocation(programID, "specularPower");
-
-	// Get a handle for the "MVP" uniform
-	MatrixID = glGetUniformLocation(programID, "MVP");
-	ViewMatrixID = glGetUniformLocation(programID, "V");
-	ModelMatrixID = glGetUniformLocation(programID, "M");
-	DepthBiasID = glGetUniformLocation(programID, "DepthBiasMVP");
-	ShadowMapID = glGetUniformLocation(programID, "shadowMap");
-
-	// Get a handle for the "LightPosition" uniform
-	lightInvDirID = glGetUniformLocation(programID, "LightInvDirection_worldspace");
-
-	// Compute the MVP matrix from the light's point of view
-	lightInvDir = glm::vec3(0, 0, 1);
-	depthProjectionMatrix = glm::perspective<float>(45.0f, 1.0f, 1.0f, 100.0f);
-	depthViewMatrix = glm::lookAt(lightInvDir, glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
-
-	// Initial position: on +Z
-	position = glm::vec3(0, 0, 1);
-	// Initial horizontal angle: toward -Z
-	horizontalAngle = M_PI;
-	// Initial vertical angle: none
-	verticalAngle = 0.0f;
-	// Initial Field of View
-	FoV = 60.0f;
-	// Mouse Speed
-	float mouseSpeed = 0.005f;
-	
-	// Render the shadows
-	glm::mat4 depthModelMatrix = glm::mat4(1.0);
-	glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
-	//renderShadows(FramebufferName, shadowResolution, depthProgramID, depthProjectionMatrix, depthViewMatrix, depthMatrixID, vertexbuffer, elementbuffer, indices, depthModelMatrix, depthMVP);
-
-	// Compute the MVP matrix from keyboard and mouse input
-	glm::mat4 ModelMatrix = glm::mat4(1.0);
-	glm::mat4 MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix;
-	bool perspectiveProjection, shadowControl;
-	//computeMatricesFromInputs(width, height, position, horizontalAngle, verticalAngle, FoV, mouseSpeed, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, shadowControl, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-	computeMatricesFromLights(width, height, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, 0, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-	MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
-
-	glm::mat4 biasMatrix(
-		0.5, 0.0, 0.0, 0.0,
-		0.0, 0.5, 0.0, 0.0,
-		0.0, 0.0, 0.5, 0.0,
-		0.5, 0.5, 0.5, 1.0
-	);
-
-	depthBiasMVP = biasMatrix * depthMVP;
 	
-	specularMinimisation(SpecularIntensity, SpecularPower, residualValue, residual, residuals, totalResidual, residualImage, depthProjectionMatrix, depthViewMatrix, width, height, numberOfLights, SpecularIntensityID, SpecularPowerID, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, ModelMatrix, MVP, ViewMatrix, depthBiasMVP, lightInvDirs);
-
-	do {
-			//calculateResidual = false;
-			//computeMatricesFromInputs(width, height, position, horizontalAngle, verticalAngle, FoV, mouseSpeed, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, shadowControl, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-			//computeMatricesFromLights(width, height, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-			lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-			
-			// Change light direction
-			if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
-				if (glfwGetKey(window, GLFW_KEY_1) == GLFW_RELEASE) {
-					if (lightNumber > 0) {
-						lightNumber--;
-					}
-					else {
-						lightNumber = numberOfLights - 1;
-					}
-					lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-					//calculateResidual = true;
-				}
-			}
-			else if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
-				if (glfwGetKey(window, GLFW_KEY_2) == GLFW_RELEASE) {
-					if (lightNumber < numberOfLights - 1) {
-						lightNumber++;
-					}
-					else {
-						lightNumber = 0;
-					}
-					lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-					//calculateResidual = true;
-				}
-			}
-			
-			// Render the polygons
-			renderPolygons(width, height, programID, lightInvDir, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MVP, ModelMatrix, ViewMatrix, depthBiasMVP, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-					
-			//computeResiduals(residual, residuals, totalResidual, residualImage, calculateResidual, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);
-			//std::cout << "Average residual = " << totalResidual << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
-			
-			// Swap buffers
-			glfwSwapBuffers(window);
-			glfwPollEvents();
-		//} //while (lightNumber < numberOfLights);
-	} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed	
-	
-	// Cleanup VBO and shader
-	glDeleteBuffers(1, &vertexbuffer);
-	glDeleteBuffers(1, &uvbuffer);
-	glDeleteBuffers(1, &normalbuffer);
-	glDeleteBuffers(1, &elementbuffer);
-	glDeleteProgram(programID);
-	glDeleteProgram(depthProgramID);
-	glDeleteProgram(quad_programID);
-	glDeleteTextures(1, &Texture);
-
-	glDeleteFramebuffers(1, &FramebufferName);
-	glDeleteTextures(1, &depthTexture);
-	glDeleteBuffers(1, &quad_vertexbuffer);
-	glDeleteVertexArrays(1, &VertexArrayID);
-
-	// Close OpenGL window and terminate GLFW
-	glfwTerminate();
-	*/
-
-
-	
-	initialiseOpenGL(heightMap, normalMap, texture, textureImages, 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, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, numberOfLights, calculateResidual);
-	
-	//computeResiduals(residual, residuals, totalResidual, residualImage, calculateResidual, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);	
-	//viewModel(residual, residuals, totalResidual, residualImage, calculateResidual, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);
-	
-	// Render the shadows
-	glm::mat4 depthModelMatrix = glm::mat4(1.0);
-	glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
-	//renderShadows(FramebufferName, shadowResolution, depthProgramID, depthProjectionMatrix, depthViewMatrix, depthMatrixID, vertexbuffer, elementbuffer, indices, depthModelMatrix, depthMVP);
-
-	// Compute the MVP matrix from keyboard and mouse input
-	glm::mat4 ModelMatrix = glm::mat4(1.0);
-	glm::mat4 MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix;
-	bool perspectiveProjection, shadowControl;
-	//computeMatricesFromInputs(width, height, position, horizontalAngle, verticalAngle, FoV, mouseSpeed, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, shadowControl, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-	computeMatricesFromLights(width, height, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, lightNumber, numberOfLights, calculateResidual, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-	MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
-
-	glm::mat4 biasMatrix(
-		0.5, 0.0, 0.0, 0.0,
-		0.0, 0.5, 0.0, 0.0,
-		0.0, 0.0, 0.5, 0.0,
-		0.5, 0.5, 0.5, 1.0
-	);
-
-	depthBiasMVP = biasMatrix * depthMVP;
+	initialiseOpenGL(heightMap, normalMap, texture, textureImages, 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, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, numberOfLights, calculateResidual, depthMVP, depthModelMatrix, MVP, ProjectionMatrix, ViewMatrix, ModelMatrix, depthBiasMVP, biasMatrix);
 	
 	specularMinimisation(SpecularIntensity, SpecularPower, residualValue, residual, residuals, totalResidual, residualImage, depthProjectionMatrix, depthViewMatrix, width, height, numberOfLights, SpecularIntensityID, SpecularPowerID, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, ModelMatrix, MVP, ViewMatrix, depthBiasMVP, lightInvDirs, textureImages);
 
-	/*
-	do {
-			//calculateResidual = false;
-			//computeMatricesFromInputs(width, height, position, horizontalAngle, verticalAngle, FoV, mouseSpeed, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection, shadowControl, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-			//computeMatricesFromLights(width, height, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-			lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-			
-			// Change light direction
-			if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
-				if (glfwGetKey(window, GLFW_KEY_1) == GLFW_RELEASE) {
-					if (lightNumber > 0) {
-						lightNumber--;
-					}
-					else {
-						lightNumber = numberOfLights - 1;
-					}
-					lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-					//calculateResidual = true;
-				}
-			}
-			else if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) {
-				if (glfwGetKey(window, GLFW_KEY_2) == GLFW_RELEASE) {
-					if (lightNumber < numberOfLights - 1) {
-						lightNumber++;
-					}
-					else {
-						lightNumber = 0;
-					}
-					lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
-					//calculateResidual = true;
-				}
-			}
-			
-			// Render the polygons
-			renderPolygons(width, height, programID, lightInvDir, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MVP, ModelMatrix, ViewMatrix, depthBiasMVP, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-					
-			//computeResiduals(residual, residuals, totalResidual, residualImage, calculateResidual, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);
-			//std::cout << "Average residual = " << totalResidual << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
-			
-			// Swap buffers
-			glfwSwapBuffers(window);
-			glfwPollEvents();
-		//} //while (lightNumber < numberOfLights);
-	} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed	
-	*/
-	
 	viewModel(residual, residuals, totalResidual, residualImage, calculateResidual, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, programID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MatrixID, modelPath);
 	
 	terminateOpenGL(vertexbuffer, uvbuffer, normalbuffer, elementbuffer, programID, depthProgramID, quad_programID, Texture, FramebufferName, depthTexture, quad_vertexbuffer, VertexArrayID);
 	
-
-
 	/*
 	// Initialise the values of specular intensity and power exponent
 	double SpecularIntensity = 0.5d, SpecularPower = 2.0d;
diff --git a/bin/specular_estimation b/bin/specular_estimation
index 0b6b64ee1b36989b90012a8cfdf7877560704c9f..8776e9adcc6672f9e56738805e253acf44e835b3 100755
Binary files a/bin/specular_estimation and b/bin/specular_estimation differ