From 2deacf36fa44e045e8e8edf30cf1fbe63f7cef6a Mon Sep 17 00:00:00 2001 From: guezoloic Date: Wed, 8 May 2024 14:21:35 +0200 Subject: [PATCH] changemet de des fonctions --- Makefile | 3 - src/Graphics.java | 111 ------------------------------------- src/Main.java | 20 ++++--- src/graphics/Camera.java | 109 ------------------------------------ src/graphics/EBO.java | 17 ++++-- src/graphics/Forms.java | 28 ---------- src/graphics/Graphics.java | 106 +++++++++++++++++++++++++++++++++++ src/graphics/Shaders.java | 91 ++++++++++++++++++------------ src/graphics/VAO.java | 22 +++++--- src/graphics/VBO.java | 6 +- 10 files changed, 202 insertions(+), 311 deletions(-) delete mode 100644 src/Graphics.java delete mode 100644 src/graphics/Camera.java delete mode 100644 src/graphics/Forms.java create mode 100644 src/graphics/Graphics.java diff --git a/Makefile b/Makefile index f6564ca..49088a4 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ # Paramètres .PHONY: all clean run BIN_DIR -.SILENT: clean run # variables JAVAC = javac @@ -16,8 +15,6 @@ JAR = $(LIB_DIR)/*:$(LIB_DIR)/lwjgl/* JAR_JAVAC = $(JAR):$(SRC_DIR) JAR_JAVA = $(JAR):$(BIN_DIR) -UNAME := $(shell uname) - # main all: $(MAIN_FILE) run diff --git a/src/Graphics.java b/src/Graphics.java deleted file mode 100644 index 1fdf922..0000000 --- a/src/Graphics.java +++ /dev/null @@ -1,111 +0,0 @@ -import item.*; -import personnages.*; -import graphics.*; - -import org.lwjgl.*; -import org.lwjgl.glfw.*; -import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GL11; -import org.lwjgl.system.MemoryStack; -import org.lwjgl.system.MemoryUtil; - -import static org.lwjgl.glfw.Callbacks.*; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.system.MemoryStack.*; -import static org.lwjgl.system.MemoryUtil.*; - -import java.nio.IntBuffer; - -public class Graphics { - // La fenêtre - private long window; - - private GLFWVidMode window_VidMode; - - // Gestion des FPS. - private double deltaTime; - private double currentTime; - private double lastFrame; - - private float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f - }; - - private int indices[] = { - 0, 1, 2 - }; - - private void init() { - GLFWErrorCallback.createPrint(System.err).set(); - - if(!glfwInit()) { - throw new IllegalStateException("Impossible d'initialiser GLFW"); - } - - this.window = glfwCreateWindow(800, 600, "snake", MemoryUtil.NULL, MemoryUtil.NULL); - this.window_VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - - glfwDefaultWindowHints(); - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); - - GLFW.glfwMakeContextCurrent(window); - GL.createCapabilities(); - - try (MemoryStack stack = stackPush() ) { - IntBuffer pWidth = stack.mallocInt(1); - IntBuffer pHeight = stack.mallocInt(1); - - glfwGetWindowSize(window, pWidth, pHeight); - - GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - - glfwSetWindowPos( - window, - (vidmode.width() - pWidth.get(0)) / 2, - (vidmode.height() - pHeight.get(0)) / 2 - ); - } - - glfwMakeContextCurrent(window); - glfwSwapInterval(1); - glfwShowWindow(window); - } - - private void loop() { - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULT); - - Forms form = new Forms(vertices, indices); - - this.lastFrame = 0.0f; - - while (!GLFW.glfwWindowShouldClose(this.window)) { - GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); - - this.currentTime = glfwGetTime(); - this.deltaTime = this.currentTime - this.lastFrame; - - form.loop(); - - GLFW.glfwSwapBuffers(this.window); - GLFW.glfwPollEvents(); - - this.lastFrame = currentTime; - } - } - - public Graphics() { - System.out.println("LWJGL Version : " + Version.getVersion() +"."); - init(); - loop(); - - glfwFreeCallbacks(window); - glfwDestroyWindow(window); - - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } -} diff --git a/src/Main.java b/src/Main.java index 95cc8d4..a8e09a8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,12 +1,14 @@ -import item.*; -import graphics.*; -import personnages.*; +import graphics.Graphics; +import graphics.Shaders; -import org.lwjgl.*; -import org.lwjgl.glfw.GLFW; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; +import java.util.function.IntSupplier; -public class Main { +public class Main { public static void main(String[] args) { - new Graphics(); - } -} + } +} \ No newline at end of file diff --git a/src/graphics/Camera.java b/src/graphics/Camera.java deleted file mode 100644 index af5beba..0000000 --- a/src/graphics/Camera.java +++ /dev/null @@ -1,109 +0,0 @@ -package graphics; - -import static org.lwjgl.glfw.GLFW.*; -import org.joml.Vector3f; -import org.joml.Matrix4f; -import java.lang.Math; - -public class Camera { - private double mousePosX; - private double mousePosY; - - private float cameraYaw; - private float cameraPitch; - - private Vector3f cameraFront; - - private Vector3f cameraUp; - private Vector3f cameraRight; - private Vector3f worldUp; - - private long window; - - private boolean is_pressed(int key) { - return glfwGetKey(this.window, key) == GLFW_PRESS; - } - - public Camera(int width, int height, long window, float sensitivity) { - this.window = window; - - this.cameraYaw = -90.0f; - this.cameraPitch = 10.0f; - - this.speed = 1.0f; - this.cameraSensitivity = sensitivity; - - this.cameraPosition = new Vector3f(0.0f, 0.0f, 0.0f); - this.cameraFront = new Vector3f(0.0f, 0.0f, -1.0f); - this.cameraUp = new Vector3f(0.0f, 1.0f, 0.0f); - - this.worldUp = new Vector3f(0.0f, 1.0f, 0.0f); - this.cameraRight = new Vector3f(0.0f, 0.0f, 0.0f); - - mousePosX = (width / 2); - mousePosY = (height / 2); - - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - } - - - public void inputProcess(float deltaTime) { - float velocity = speed * (deltaTime/2); - this.fov = 45.0f; - - if(is_pressed(GLFW_KEY_W)) { cameraPosition.add(cameraFront.mul(velocity));} - if(is_pressed(GLFW_KEY_S)) { cameraPosition.sub(cameraFront.mul(velocity));} - if(is_pressed(GLFW_KEY_A)) { cameraPosition.add(cameraRight.mul(velocity));} - if(is_pressed(GLFW_KEY_D)) { cameraPosition.sub(cameraRight.mul(velocity));} - if(is_pressed(GLFW_KEY_LEFT_CONTROL)) { cameraPosition.add(worldUp.mul(velocity));} - if(is_pressed(GLFW_KEY_SPACE)) { cameraPosition.sub(worldUp.mul(velocity));} - } - - public void mouseMouvementProcess() { - double[] mouseX = new double[]{}; - double[] mouseY = new double[]{}; - - glfwGetCursorPos(window, mouseX, mouseY); - - float deltaX = (float)(mouseX[0] - this.mousePosX); - float deltaY = (float)(mouseY[0] - this.mousePosY); - - this.mousePosX = mouseX[0]; - this.mousePosY = mouseY[0]; - - deltaX *= (this.cameraSensitivity/2); - deltaY *= (this.cameraSensitivity/2); - - this.cameraYaw += deltaX; - this.cameraPitch += deltaY; - - if (this.cameraPitch > 89.0f) - this.cameraPitch = 89.0f; - if (this.cameraPitch < -89.0f) - this.cameraPitch = -89.0f; - } - - public void updateCameraVectors() { - Vector3f front = new Vector3f(new float[] { - (float)(Math.cos(Math.toRadians(cameraYaw)) * Math.cos(Math.toRadians(cameraPitch))), - (float)(Math.sin(Math.toRadians(cameraPitch))), - (float)(Math.sin(Math.toRadians(cameraYaw)) * Math.cos(Math.toRadians(cameraPitch))) - }); - - cameraFront = front.normalize(); - - cameraRight = cameraRight.cross(cameraFront, worldUp).normalize(); - cameraUp = cameraUp.cross(cameraRight, cameraFront).normalize(); - } - - public Matrix4f getViewMatrix() { - return new Matrix4f().lookAt(cameraPosition, cameraPosition.add(cameraFront), cameraUp); - } - - public float speed; - public float cameraSensitivity; - - public float fov; - - public Vector3f cameraPosition; -} diff --git a/src/graphics/EBO.java b/src/graphics/EBO.java index 8691d88..65d1013 100644 --- a/src/graphics/EBO.java +++ b/src/graphics/EBO.java @@ -1,5 +1,8 @@ package graphics; +import java.nio.IntBuffer; + +import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL15; public class EBO { @@ -7,21 +10,27 @@ public class EBO { public EBO(int[] indices) { this.id = GL15.glGenBuffers(); - GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.id); - GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW); bind(); + + IntBuffer buffer = BufferUtils.createIntBuffer(indices.length); + buffer.put(indices); + buffer.flip(); + + GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); + unbind(); } - public void cleanup() { + public void clean() { unbind(); GL15.glDeleteBuffers(this.id); } public void bind() { - GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, (int)this.id); + GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.id); } public void unbind() { GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); } } + diff --git a/src/graphics/Forms.java b/src/graphics/Forms.java deleted file mode 100644 index c9c07d8..0000000 --- a/src/graphics/Forms.java +++ /dev/null @@ -1,28 +0,0 @@ -package graphics; - -import static org.lwjgl.opengl.GL11.GL_TRIANGLES; - -import org.lwjgl.opengl.GL11; - -public class Forms { - private VAO vao; - private VBO vbo; - private EBO ebo; - - private Shaders shaders; - - public Forms(float[] vertices, int[] indices) { - this.vbo = new VBO(vertices); - this.ebo = new EBO(indices); - - this.shaders = new Shaders("/Users/loic/Documents/Program/java/snake/res/shaders/cube.vert", "/Users/loic/Documents/Program/java/snake/res/shaders/cube.frag"); - - int stride = 3*Float.BYTES; - vao.setAttributePointer(0, 3, GL11.GL_FLOAT, stride, 0); - } - - public void loop() { - shaders.use(); - vao.drawElementIndices(GL_TRIANGLES, 0, 3); - } -} \ No newline at end of file diff --git a/src/graphics/Graphics.java b/src/graphics/Graphics.java new file mode 100644 index 0000000..9ca245e --- /dev/null +++ b/src/graphics/Graphics.java @@ -0,0 +1,106 @@ +package graphics; + +import org.lwjgl.*; +import org.lwjgl.glfw.*; +import org.lwjgl.opengl.*; + +import static org.lwjgl.glfw.Callbacks.*; +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL30.*; +import static org.lwjgl.system.MemoryUtil.*; + + +public class Graphics { + private VAO vao; + private VBO vbo; + private EBO ebo; + private Shaders shaders; + + private float vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + private int indices[] = { + 0, 1, 2 + }; + + private double lastFrame; + private double currentTime; + private double deltaTime; + + private long window; + + public Graphics() { + System.out.println("LWJGL Version : " + Version.getVersion()); + init(); // Initialiser OpenGL et créer les objets VAO, VBO, EBO + loop(); // Entrer dans la boucle principale de rendu + } + + private void init() { + GLFWErrorCallback.createPrint(System.err).set(); + + if(!glfwInit()) { + throw new IllegalStateException("Impossible de charger GLFW"); + } + + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 1); + + window = glfwCreateWindow(800, 600, "snake", NULL, NULL); + + if (window == NULL) { + throw new RuntimeException("Impossible de créer la fenêtre GLFW"); + } + + // Créer les objets VAO, VBO, EBO ici après avoir initialisé OpenGL + this.vbo = new VBO(this.vertices); + this.ebo = new EBO(this.indices); + this.vao = new VAO(); + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULT); + + glfwMakeContextCurrent(window); + GL.createCapabilities(); + } + + private void loop() { + this.shaders = new Shaders("cube.vert", "cube.frag"); + + this.lastFrame = 0.0f; + + while(!glfwWindowShouldClose(window)) { + GL30.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT); + + this.currentTime = glfwGetTime(); + this.deltaTime = currentTime - lastFrame; + + this.vao.drawElements(GL_TRIANGLES, 0, 3); + + lastFrame = currentTime; + GLFW.glfwSwapBuffers(window); + GLFW.glfwPollEvents(); + } + + // Nettoyage des ressources OpenGL après la fin de la boucle de rendu + this.vao.clean(); + this.vbo.clean(); + this.ebo.clean(); + + // Fermeture de GLFW et nettoyage des callbacks + glfwFreeCallbacks(window); + glfwDestroyWindow(window); + glfwTerminate(); + glfwSetErrorCallback(null).set(); + } +} \ No newline at end of file diff --git a/src/graphics/Shaders.java b/src/graphics/Shaders.java index 4320e9c..07e20fa 100644 --- a/src/graphics/Shaders.java +++ b/src/graphics/Shaders.java @@ -1,56 +1,75 @@ package graphics; -import org.lwjgl.opengl.*; +import org.lwjgl.opengl.GL20; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; public class Shaders { private int vertexShader; private int fragmentShader; + public int shaderProgram; - public Shaders(String vertexShaderSource, String fragmentShaderSource) { - vertexShader = addVertShader(vertexShaderSource); - fragmentShader = addFragShader(fragmentShaderSource); - shaderProgram = compileInProgram(); - } - private int addVertShader(String vertexShaderSource) { - int vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER); - GL20.glShaderSource(vertexShader, vertexShaderSource); - GL20.glCompileShader(vertexShader); - if (GL20.glGetShaderi(vertexShader, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) { - throw new RuntimeException("Vertex shader compilation failed: " + GL20.glGetShaderInfoLog(vertexShader)); - } - return vertexShader; + int vertexShaderLocal = GL20.glCreateShader(GL20.GL_VERTEX_SHADER); + + GL20.glShaderSource(vertexShaderLocal, vertexShaderSource); + GL20.glCompileShader(vertexShaderLocal); + + return vertexShaderLocal; } - private int addFragShader(String fragmentShaderSource) { - int fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER); - GL20.glShaderSource(fragmentShader, fragmentShaderSource); - GL20.glCompileShader(fragmentShader); - if (GL20.glGetShaderi(fragmentShader, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) { - throw new RuntimeException("Fragment shader compilation failed: " + GL20.glGetShaderInfoLog(fragmentShader)); - } - return fragmentShader; + private int addFragShader(String fragShaderSource) { + int fragShaderLocal = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER); + + GL20.glShaderSource(fragShaderLocal, fragShaderSource); + GL20.glCompileShader(fragShaderLocal); + + return fragShaderLocal; } - private int compileInProgram() { - int shaderProgram = GL20.glCreateProgram(); - GL20.glAttachShader(shaderProgram, vertexShader); - GL20.glAttachShader(shaderProgram, fragmentShader); - GL20.glLinkProgram(shaderProgram); - if (GL20.glGetProgrami(shaderProgram, GL20.GL_LINK_STATUS) != GL20.GL_TRUE) { - throw new RuntimeException("Shader program linking failed: " + GL20.glGetProgramInfoLog(shaderProgram)); - } - GL20.glDeleteShader(vertexShader); - GL20.glDeleteShader(fragmentShader); - return shaderProgram; + private void compileInProgram() { + GL20.glAttachShader(this.shaderProgram, this.vertexShader); + GL20.glAttachShader(this.shaderProgram, this.fragmentShader); + + GL20.glLinkProgram(this.shaderProgram); + + GL20.glDetachShader(this.shaderProgram, this.vertexShader); + GL20.glDetachShader(this.shaderProgram, this.fragmentShader); + + GL20.glDeleteProgram(vertexShader); + GL20.glDeleteProgram(fragmentShader); + } + + public void clean() { + GL20.glDeleteShader(this.shaderProgram); + } + + public Shaders(String vertexShaderName, String fragShaderName) { + this.vertexShader = addVertShader(getShaderFile(vertexShaderName)); + this.fragmentShader = addFragShader(getShaderFile(fragShaderName)); + + this.compileInProgram(); } public void use() { - GL20.glUseProgram(shaderProgram); + GL20.glUseProgram(this.shaderProgram); } - public void cleanUp() { - GL20.glDeleteProgram(shaderProgram); + public void unbind() { + GL20.glUseProgram(0); + } + + private String getShaderFile(String filename) { + Path path = Paths.get("res/shaders/"+filename); + try { + return Files.readString(path); + } catch (IOException e) { + throw new IllegalStateException("Impossible de trouver " + filename); + } } } diff --git a/src/graphics/VAO.java b/src/graphics/VAO.java index 9d9ee76..e2e58c5 100644 --- a/src/graphics/VAO.java +++ b/src/graphics/VAO.java @@ -1,8 +1,9 @@ package graphics; -import static org.lwjgl.opengl.GL11.*; +import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL30; + public class VAO { private int id; @@ -11,7 +12,7 @@ public class VAO { bind(); } - public void cleanup() { + public void clean() { unbind(); GL30.glDeleteVertexArrays(id); } @@ -26,19 +27,26 @@ public class VAO { public void setAttributePointer(int index, int size, int type, int stride, long offset) { GL30.glVertexAttribPointer(index, size, type, false, stride, offset); + enableVertexAttribArray(index); + } + + public void enableVertexAttribArray(int index) { GL30.glEnableVertexAttribArray(index); } - public void drawElement(int mode, int count, int type, long indices) { - bind(); + public void disableVertexAttribArray(int index) { + GL30.glDisableVertexAttribArray(index); + } - glDrawElements(mode, count, type, indices); + public void drawElements(int mode, int count, int type, long indices) { + bind(); + GL11.glDrawElements(mode, count, type, indices); unbind(); } - public void drawElementIndices(int mode, int first, int count) { + public void drawElements(int mode, int first, int count) { bind(); - glDrawArrays(mode, first, count); + GL11.glDrawArrays(mode, first, count); unbind(); } } diff --git a/src/graphics/VBO.java b/src/graphics/VBO.java index 0b0ef65..60ef85d 100644 --- a/src/graphics/VBO.java +++ b/src/graphics/VBO.java @@ -7,13 +7,11 @@ public class VBO { public VBO(float[] vertices) { this.id = GL15.glGenBuffers(); - GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.id); - GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW); - bind(); + GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW); } - public void cleanup() { + public void clean() { unbind(); GL15.glDeleteBuffers(this.id); }