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 f7e0c1a2835a6ff32b653f2fef6fb189365f249b..0390374e21e042c01e22239397232e2786f98725 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 1e5b3a631d7d40ab41633cf6197659211ccdf629..a7820e751e96fde01182852caea08c356165bbda 100644
--- a/apps/specular_estimation/src/Ceres.h
+++ b/apps/specular_estimation/src/Ceres.h
@@ -11,6 +11,8 @@ using ceres::Problem;
 using ceres::Solver;
 using ceres::Solve;
 
+void specularMinimisation(float& SpecularIntensity, float& SpecularPower, double& residual);
+
 // A templated cost functor that implements the residual r = 10 - x.
 // The method operator() is templated so that we can then use an automatic differentiation wrapper around it to generate its derivatives.
 struct CostFunctor {
@@ -20,4 +22,57 @@ struct CostFunctor {
 	}
 };
 
-#endif
\ No newline at end of file
+void specularMinimisation(float& SpecularIntensity, float& SpecularPower, double& residual) {
+	
+  google::InitGoogleLogging(argv[0]);
+  
+  // The variable to solve for with its initial value. It will be mutated in place by the solver.
+  double x = 0.5;
+  const double initial_x = SpecularIntensity;
+
+  // Build the problem.
+  Problem problem;
+
+  // Set up the only cost function (also known as residual). This uses auto-differentiation to obtain the derivative (jacobian).
+  // AutoDiffCostFunction<CostFunctor, (Dimensions of Residual), (Dimensions of Variables)>
+  CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
+  problem.AddResidualBlock(cost_function, NULL, &x);
+
+  // Run the solver!
+  Solver::Options options;
+  options.minimizer_progress_to_stdout = true;
+  Solver::Summary summary;
+  Solve(options, &problem, &summary);
+
+  std::cout << summary.BriefReport() << "\n";
+  std::cout << "Specular Intensity: " << initial_x << " -> " << x << "\n";
+}
+
+/*void specularMinimisation(char** argv) {
+	
+  google::InitGoogleLogging(argv[0]);
+  
+  // The variable to solve for with its initial value. It will be
+  // mutated in place by the solver.
+  double x = 0.5;
+  const double initial_x = x;
+
+  // Build the problem.
+  Problem problem;
+
+  // Set up the only cost function (also known as residual). This uses
+  // auto-differentiation to obtain the derivative (jacobian).
+  CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
+  problem.AddResidualBlock(cost_function, NULL, &x);
+
+  // Run the solver!
+  Solver::Options options;
+  options.minimizer_progress_to_stdout = true;
+  Solver::Summary summary;
+  Solve(options, &problem, &summary);
+
+  std::cout << summary.BriefReport() << "\n";
+  std::cout << "x : " << initial_x << " -> " << x << "\n";
+}*/
+
+#endif
diff --git a/apps/specular_estimation/src/OpenGL.h b/apps/specular_estimation/src/OpenGL.h
index 3a283fe971cea07cee9983726fae336a55bccc19..2a277244c9e73e7af018751904a7b994459a06fb 100644
--- a/apps/specular_estimation/src/OpenGL.h
+++ b/apps/specular_estimation/src/OpenGL.h
@@ -31,7 +31,8 @@ void computeMatricesFromLights(int windowWidth, int windowHeight, glm::vec3& pos
 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, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower);
-void openGL(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, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower);
+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, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower);
+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, float& SpecularIntensity, GLuint& SpecularPowerID, float& 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);
@@ -238,61 +239,50 @@ void initialiseOpenGL(cv::Mat cv_depth, cv::Mat cv_normals, cv::Mat cv_texture,
 	float mouseSpeed = 0.005f;
 }
 
-void openGL(glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int windowWidth, int windowHeight, 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, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower) {
+void openGL(int& lightNumber, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int windowWidth, int windowHeight, 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, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower, double& residual) {
 	
-	int lightNumber = 0;
+	// 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 = glm::ortho(-0.5f * float(windowWidth) / float(windowHeight), 0.5f * float(windowWidth) / float(windowHeight), -0.5f, 0.5f, 0.0f, 10.0f);
-	bool perspectiveProjection, shadowControl;
-	computeResiduals(windowWidth, windowHeight, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensity, SpecularPower);
-	*/
-	
-	do {
-		// 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, calculateResidual;
-		//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, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
-		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;
-
-		// Render the polygons
-		renderPolygons(windowWidth, windowHeight, programID, lightInvDir, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MVP, ModelMatrix, ViewMatrix, depthBiasMVP, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-		
-		
-		if (calculateResidual) {
-			// Compute the residual image and sum of differences
-			cv::Mat residual;
-			double sum;
-			computeResidual(lightNumber, windowHeight, windowWidth, textureImages, residual, sum, SpecularIntensity, SpecularPower);
-		}
+	glm::mat4 MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix;
+	bool perspectiveProjection, shadowControl, calculateResidual;
+	//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, lightNumber, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, calculateResidual);
+	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
+	);
 
-		// Optionally render the shadowmap (for debug only)
-		//void renderShadowMap(GLuint quad_programID, GLuint depthTexture, GLuint texID, GLuint quad_vertexbuffer);
-		
-		// Swap buffers
-		glfwSwapBuffers(window);
-		glfwPollEvents();
+	depthBiasMVP = biasMatrix * depthMVP;
 
-	} // Check if the ESC key was pressed or the window was closed
-	while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&	glfwWindowShouldClose(window) == 0);
+	// Render the polygons
+	renderPolygons(windowWidth, windowHeight, programID, lightInvDir, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, MVP, ModelMatrix, ViewMatrix, depthBiasMVP, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
+	
+	
+	if (calculateResidual) {
+		// Compute the residual image and sum of differences
+		cv::Mat residualImage;
+		computeResidual(lightNumber, windowHeight, windowWidth, textureImages, residualImage, residual, SpecularIntensity, SpecularPower);
+	}
 
+	// Optionally render the shadowmap (for debug only)
+	//void renderShadowMap(GLuint quad_programID, GLuint depthTexture, GLuint texID, GLuint quad_vertexbuffer);
+	
+	// Swap buffers
+	glfwSwapBuffers(window);
+	glfwPollEvents();
+}
+
+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) {
+	
 	// Cleanup VBO and shader
 	glDeleteBuffers(1, &vertexbuffer);
 	glDeleteBuffers(1, &uvbuffer);
diff --git a/apps/specular_estimation/src/specular_estimation.cc b/apps/specular_estimation/src/specular_estimation.cc
index fce4ff6633f04677e417bc4a875af422ac476191..3a99030b5ac8996b57bac0278655b5ab826cac86 100644
--- a/apps/specular_estimation/src/specular_estimation.cc
+++ b/apps/specular_estimation/src/specular_estimation.cc
@@ -1,34 +1,10 @@
 #include "stdafx.h"
 
 int main(int argc, char** argv) {
-  google::InitGoogleLogging(argv[0]);
-
-  // The variable to solve for with its initial value. It will be
-  // mutated in place by the solver.
-  double x = 0.5;
-  const double initial_x = x;
-
-  // Build the problem.
-  Problem problem;
-
-  // Set up the only cost function (also known as residual). This uses
-  // auto-differentiation to obtain the derivative (jacobian).
-  CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
-  problem.AddResidualBlock(cost_function, NULL, &x);
-
-  // Run the solver!
-  Solver::Options options;
-  options.minimizer_progress_to_stdout = true;
-  Solver::Summary summary;
-  Solve(options, &problem, &summary);
-
-  std::cout << summary.BriefReport() << "\n";
-  std::cout << "x : " << initial_x << " -> " << x << "\n";
-            
-            
-            
-            
-  std::string imageName, calibration = "chrome";
+	
+	google::InitGoogleLogging(argv[0]);
+	
+	std::string imageName, calibration = "chrome";
 	int imageScale = 1, cropping = 0;
 	bool showTexture = true;
 
@@ -129,14 +105,38 @@ int main(int argc, char** argv) {
 	float horizontalAngle, verticalAngle, FoV, SpecularIntensity = 0.5f, SpecularPower = 5.0f;
 	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;
+	int lightNumber = 0;
+	double residual;
 
 	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);
 	//initialiseOpenGL(heightMap, normalMap, texture, textureImages, lightDirectionsPerspective, 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);
 	//initialiseOpenGL(heightMap, normalMap, texture, charucoImages, lightDirectionsPerspective, 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);
 
-	openGL(depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-	//openGL(depthProjectionMatrix, depthViewMatrix, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirectionsPerspective, textureImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
-	//openGL(depthProjectionMatrix, depthViewMatrix, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirectionsPerspective, charucoImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower);
+	/*
+	glm::mat4 ModelMatrix = glm::mat4(1.0);
+	glm::mat4 MVP, ViewMatrix, depthBiasMVP, ProjectionMatrix = glm::ortho(-0.5f * float(windowWidth) / float(windowHeight), 0.5f * float(windowWidth) / float(windowHeight), -0.5f, 0.5f, 0.0f, 10.0f);
+	bool perspectiveProjection, shadowControl;
+	computeResiduals(windowWidth, windowHeight, position, horizontalAngle, verticalAngle, FoV, ProjectionMatrix, ViewMatrix, lightInvDir, depthProjectionMatrix, depthViewMatrix, perspectiveProjection = false, lightDirections, textureImages, lightNumber, numberOfLights, SpecularIntensity, SpecularPower);
+	*/
+	
+	
+	
+	do {
+		openGL(lightNumber, depthProjectionMatrix, depthViewMatrix, width, height, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirections, textureImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, residual);
+		//openGL(lightNumber, depthProjectionMatrix, depthViewMatrix, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirectionsPerspective, textureImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, residual);
+		//openGL(lightNumber, depthProjectionMatrix, depthViewMatrix, position, horizontalAngle, verticalAngle, FoV, lightInvDir, lightDirectionsPerspective, charucoImages, programID, MatrixID, ModelMatrixID, ViewMatrixID, DepthBiasID, lightInvDirID, Texture, TextureID, depthTexture, ShadowMapID, vertexbuffer, uvbuffer, normalbuffer, elementbuffer, indices, depthProgramID, quad_programID, FramebufferName, quad_vertexbuffer, VertexArrayID, numberOfLights, SpecularIntensityID, SpecularIntensity, SpecularPowerID, SpecularPower, residual);
+		
+		// TODO: Check if the viewpoint has been changed
+		
+		specularMinimisation(SpecularIntensity, SpecularPower, residual);
+	} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&	glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed
+	
+	
+	
+	
+	
+	terminateOpenGL(vertexbuffer, uvbuffer, normalbuffer, elementbuffer, programID, depthProgramID, quad_programID, Texture, FramebufferName, depthTexture, quad_vertexbuffer, VertexArrayID);
+	
 	
 	return 0;
 }
diff --git a/bin/specular_estimation b/bin/specular_estimation
index fd0469691b05592cdc186b59e7c1cb6f1e2d5768..028a9b19d99d666e00620c8b7ba3bbf2bb4f013e 100755
Binary files a/bin/specular_estimation and b/bin/specular_estimation differ