package gameEngine;

import static org.lwjgl.opengl.GL11.*;
import gameEngine.camera.Camera;
import gameEngine.light.SceneLight;
import gameEngine.skybox.Skybox;

import java.nio.file.Files;
import java.nio.file.Paths;

import org.joml.Matrix4f;

public class Renderer {
	protected ShaderProgram sh;


	public Renderer() throws Exception{

		//Initialization of the shader program
		sh = new ShaderProgram();
		try{
			sh.createVertexShader(new String(Files.readAllBytes(Paths.get("./java/src/gameEngine/vertShader.vs"))));
			sh.createFragmentShader(new String(Files.readAllBytes(Paths.get("./java/src/gameEngine/fragShader.fs"))));
		}catch(Exception e){
			try{
				sh.createVertexShader(new String(Files.readAllBytes(Paths.get("./src/gameEngine/vertShader.vs"))));
				sh.createFragmentShader(new String(Files.readAllBytes(Paths.get("./src/gameEngine/fragShader.fs"))));
			}catch(Exception x){
				sh.createVertexShader(new String(Files.readAllBytes(Paths.get("./yaw/java/src/gameEngine/vertShader.vs"))));
				sh.createFragmentShader(new String(Files.readAllBytes(Paths.get("./yaw/java/src/gameEngine/fragShader.fs"))));
			}

		}
		sh.link();
		//Initialization of the camera's uniform
		sh.createUniform("projectionMatrix");
		//Initialization of the mesh's uniform
		sh.createUniform("modelViewMatrix");

		// Create uniform for material
		sh.createMaterialUniform("material");

		//Initialization of the light's uniform
		sh.createUniform("camera_pos");
		sh.createUniform("specularPower");
		sh.createUniform("ambientLight");
		sh.createPointLightListUniform("pointLights",SceneLight.maxPointlight);
		sh.createSpotLightUniformList("spotLights", SceneLight.maxSpotlight);
		sh.createDirectionalLightUniform("directionalLight");

	}


	public void cleanUp(){
		// The Shader Program is deallocated
		sh.cleanup();
	}

	public void render(SceneVertex sc, SceneLight sl, boolean isResized,Camera cam, Skybox sk){
		sh.bind();
		//Preparation of the camera
		if(isResized || SceneVertex.itemAdded){
			cam.updateCameraMat();
		}

		//Debug
		//		 int err = GL11.GL_NO_ERROR;
		//		if((err = GL11.glGetError()) != GL11.GL_NO_ERROR)
		//		{
		//			
		//		  System.out.println(err);
		//		}

		//Set the camera to render.
		sh.setUniform("projectionMatrix", cam.getCameraMat());
		sh.setUniform("camera_pos", cam.position);
		Matrix4f mvm = cam.setupViewMatrix();

		//Enable the option needed to render.
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);

		glDepthMask(true);
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_GREATER);
		glClearDepth(cam.getzFar()*-1);
		glDisable(GL_BLEND);

		glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

		//Rendering of the light
		sl.render(sh,mvm);

		//Init Objects
		sc.initMesh();

		//Update objects
		sc.update();


		//Rendering of the object
		sc.draw(sh,mvm);

		sh.unbind();
		if(sk!= null){
			if(sk.init==false)
				SceneVertex.itemAdded=true;
				try {
					sk.init();
					sk.init=false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			sk.draw(cam);
		}
	}
}

@@ -2408,19 +2408,23 @@
 atrix4f 
-mvm
+viewMat
  = cam.s
@@ -2773,27 +2773,31 @@
 l.render(sh,
-mvm
+viewMat
 );%0D%0A%0D%0A%09%09//In
@@ -2914,11 +2914,15 @@
 (sh,
-mvm
+viewMat
 );%0D%0A
@@ -2978,16 +2978,48 @@
 ==false)
+%7B%0D%0A%09%09%09%09System.out.println(%22la%22);
 %0D%0A%09%09%09%09Sc
@@ -3077,29 +3077,8 @@
 );%0D%0A
-%09%09%09%09%09sk.init=false;%0D%0A
 %09%09%09%09
@@ -3133,15 +3133,21 @@
 );%0D%0A
-
 %09%09%09%09%7D%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%09s

Fix memory loss.