From 58e94314cce92739b8b121b8aa4233b14354ce67 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 19 May 2021 03:35:23 +0200 Subject: [PATCH] cleanup Engine.java --- shaders/ObjectGl/vert.glsl | 4 +- src/engine/Engine.java | 65 +++++++++++++++++---------------- src/engine/object/ObjectGl.java | 1 + 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/shaders/ObjectGl/vert.glsl b/shaders/ObjectGl/vert.glsl index 2565d38..f6c1771 100644 --- a/shaders/ObjectGl/vert.glsl +++ b/shaders/ObjectGl/vert.glsl @@ -2,7 +2,9 @@ layout (location = 0) in vec3 aPos; +uniform mat4 transform; + void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = transform * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 36a0998..8500905 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,13 +1,6 @@ -/** - * CLASS ENGINE - * - * Classe principale du moteur de jeu - * - * @author François Autin - * - */ package engine; +import engine.math.Vector3f; import engine.object.ObjectGl; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWVidMode; @@ -15,17 +8,16 @@ import org.lwjgl.opengl.GL; import java.util.ArrayList; import java.util.List; -import java.util.ListIterator; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.system.MemoryUtil.NULL; public class Engine { private long window; - private List objectsGl; + private final List objectsGl; private boolean running; @@ -34,14 +26,14 @@ public class Engine { */ public Engine() { this.running = false; - this.objectsGl = new ArrayList(); + this.objectsGl = new ArrayList<>(); } /** * Start the engine * Create the window */ - private void init() { + public void init() { glfwInit(); this.running = true; @@ -82,14 +74,14 @@ public class Engine { /** * */ - private void update(){ + public void update(){ glfwPollEvents(); } /** * */ - private void render(){ + public void render(){ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -103,17 +95,6 @@ public class Engine { glfwSwapBuffers(window); //Envoie le buffer vers le moniteur } - /** - * TODO la boucle de rendu doit être inscrite dans la boucle de gameplay: cette methode doit disparaitre - */ - public void run(){ - while(running){ - update(); - render(); - if(glfwWindowShouldClose(window)) running = false; - } - } - public void add_objectGl(ObjectGl obj){ objectsGl.add(obj); } @@ -122,6 +103,18 @@ public class Engine { 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 @@ -136,12 +129,22 @@ public class Engine { public static void main(String[] args) { Engine engine = new Engine(); engine.init(); - // Add objects to render - ObjectGl cube = new ObjectGl(-0.2f,-0.2f,0.0f,1.0f,1.0f); - engine.add_objectGl(cube); -// engine.remove_objectGl(cube); - engine.run(); + // Add objects to render + ObjectGl cube = new ObjectGl(-0.5f,0.5f,0.0f,1.0f,1.0f); + 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); + } } } diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index 6563ad7..7729df9 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -51,6 +51,7 @@ public class ObjectGl { public void render(){ this.shader.enable(); + this.shader.setUniformMat4f("transform", this.transform); this.vertexArray.render(); this.shader.disable(); }