changemet de des fonctions

This commit is contained in:
2024-05-08 14:21:35 +02:00
parent de2448d399
commit 2deacf36fa
10 changed files with 202 additions and 311 deletions

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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 static void main(String[] args) {
new Graphics();
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

106
src/graphics/Graphics.java Normal file
View File

@@ -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();
}
}

View File

@@ -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));
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);
}
GL20.glDeleteShader(vertexShader);
GL20.glDeleteShader(fragmentShader);
return shaderProgram;
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);
}
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}