diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 8eba812..667430d 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,10 +1,10 @@ /** * CLASS ENGINE - * + * * Classe principale du moteur de jeu - * + * * @author François Autin - * + * */ package engine; @@ -14,7 +14,6 @@ import org.lwjgl.opengl.GL; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; import static org.lwjgl.system.MemoryUtil.*; public class Engine { @@ -52,6 +51,7 @@ public class Engine { 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); @@ -63,18 +63,10 @@ public class Engine { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); System.out.println("OpenGL: " + glGetString(GL_VERSION)); - this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.triangle); + this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices); } - /* - TODO gérer les input dans une autre classe - */ - private void processInput(){ - if(glfwGetKey(this.window, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(this.window, true); - } - private void update(){ glfwPollEvents(); } @@ -91,7 +83,6 @@ public class Engine { public void run(){ init(); while(running){ - processInput(); update(); render(); if(glfwWindowShouldClose(window)) running = false; @@ -102,7 +93,7 @@ public class Engine { 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 GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){ + private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){ @Override public void invoke(long window, int width, int height){ glViewport(0,0,width,height); diff --git a/src/engine/Input.java b/src/engine/Input.java index c5b1e4e..d93f485 100644 --- a/src/engine/Input.java +++ b/src/engine/Input.java @@ -3,6 +3,9 @@ package engine; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWKeyCallback; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; + public class Input extends GLFWKeyCallback { public static boolean[] keys = new boolean[65536]; @@ -10,5 +13,10 @@ public class Input extends GLFWKeyCallback { @Override public void invoke(long window, int key, int scancode, int action, int mods) { keys[key] = action != GLFW.GLFW_RELEASE; + if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + else if(key == GLFW_KEY_SPACE && action == GLFW_PRESS) //Switch to wireframe + if (glGetInteger(GL_POLYGON_MODE) == GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } } diff --git a/src/engine/Primitive.java b/src/engine/Primitive.java index 887595e..34e1903 100644 --- a/src/engine/Primitive.java +++ b/src/engine/Primitive.java @@ -8,4 +8,20 @@ public class Primitive { -0.5f, -0.5f, 0.0f, }; + public static byte[] triangle_indices = new byte[]{ + 0, 1, 2 + }; + + public static float[] rectangle = new float[] { + 0.5f, 0.5f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f // top left + }; + + public static byte[] rectangle_indices = new byte[] { + 0, 1, 3, + 1, 2, 3 + }; + } diff --git a/src/engine/Scene.java b/src/engine/Scene.java index 301c470..3edf273 100644 --- a/src/engine/Scene.java +++ b/src/engine/Scene.java @@ -3,14 +3,16 @@ package engine; public class Scene { float[] vertices; + byte[] indices; VertexArray vertexArray; Shader shader; - public Scene(String vertPath, String fragPath, float[] vertices){ + public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices){ this.vertices = vertices; + this.indices = indices; this.shader = new Shader(vertPath, fragPath); - this.vertexArray = new VertexArray(this.vertices); + this.vertexArray = new VertexArray(this.vertices, this.indices); } public void render(){ diff --git a/src/engine/VertexArray.java b/src/engine/VertexArray.java index e7c64aa..d14daaf 100644 --- a/src/engine/VertexArray.java +++ b/src/engine/VertexArray.java @@ -6,22 +6,26 @@ import static org.lwjgl.opengl.GL30.*; public class VertexArray { - private int VAO ,VBO; + private int VAO ,VBO, EBO; + private int count; - public VertexArray(float[] vertices){ + public VertexArray(float[] vertices, byte[] indices){ + count = indices.length; // VERTEX ARRAY OBJECT VAO = glGenVertexArrays(); + glBindVertexArray(VAO); // VERTEX BUFFER OBJECT VBO = glGenBuffers(); - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(0); + EBO = glGenBuffers(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -36,7 +40,7 @@ public class VertexArray { } public void draw(){ - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0); } public void render(){