package engine; import engine.math.Vector3f; import engine.object.ObjectGl; import engine.object.ObjectGlColor; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; import java.util.ArrayList; import java.util.List; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.NULL; public class Engine { private long window; private final List objectsGl; private boolean running; /** * Create the engine and initial attributes use .init() to start the engine */ public Engine() { this.running = false; this.objectsGl = new ArrayList<>(); } /** * Start the engine * Create the window */ public void init() { glfwInit(); this.running = true; glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //On utilise la version 3.3 d'openGL glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Compatible MAC glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Le core profile est l'interface 'avancé' d'openGL int width = 1280; int height = 720; this.window = glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL); assert this.window != NULL; // On récupère les informations du moniteur principal GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); assert vidmode != null; // On met la fenêtre au centre de l'écran principale glfwSetWindowPos(this.window, (vidmode.width() - width)/2, (vidmode.height() - height)/2); glfwSetKeyCallback(window, new Input()); glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE); // Contexte = zone cible des rendus glfwMakeContextCurrent(this.window); glfwShowWindow(this.window); GL.createCapabilities(); glfwSetFramebufferSizeCallback(window, resizeWindow); glEnable(GL_DEPTH_TEST); // Z-Buffer glClearColor(0.2f, 0.3f, 0.3f, 1.0f); System.out.println("OpenGL: " + glGetString(GL_VERSION)); } /** * */ public void update(){ glfwPollEvents(); } /** * */ public void render(){ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (ObjectGl objectGl : objectsGl) { objectGl.render(); } int error = glGetError(); if (error != GL_NO_ERROR) System.out.println(error); glfwSwapBuffers(window); //Envoie le buffer vers le moniteur } public void add_objectGl(ObjectGl obj){ objectsGl.add(obj); } public void remove_objectGl(ObjectGl obj){ objectsGl.remove(obj); } public boolean isRunning(){ return running; } public void setRunning(boolean b){ running = b; } public boolean shouldClose(){ return glfwWindowShouldClose(window); } /** * Est appelé à chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu * pour quelle corresponde à la taille de la fenêtre */ private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){ @Override public void invoke(long window, int width, int height){ glViewport(0,0,width,height); } }; public static void main(String[] args) { Engine engine = new Engine(); engine.init(); // Add objects to render ObjectGl cube = new ObjectGlColor(-0.5f,0.5f,0.0f,1.0f,1.0f, new float[] {0.2f, 0.2f, 0.8f}); engine.add_objectGl(cube); while(engine.isRunning()){ // Game logic should fit here cube.rotateZ(1.0f); cube.translate(new Vector3f(0.01f, 0.0f, 0.0f)); //essential part v engine.update(); engine.render(); if(engine.shouldClose()) engine.setRunning(false); } } }