diff --git a/Makefile b/Makefile index 4b38a36..f6564ca 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,11 @@ LIB_DIR = lib JAR = $(LIB_DIR)/*:$(LIB_DIR)/lwjgl/* +JAR_JAVAC = $(JAR):$(SRC_DIR) +JAR_JAVA = $(JAR):$(BIN_DIR) + +UNAME := $(shell uname) + # main all: $(MAIN_FILE) run @@ -20,10 +25,10 @@ $(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class $(BIN_DIR)/$(MAIN_FILE).class : $(SRC_DIR)/$(MAIN_FILE).java @mkdir -p $(BIN_DIR) - $(JAVAC) -XstartOnFirstThread -d $(BIN_DIR) -sourcepath $(SRC_DIR) -classpath $(JAR) $< + $(JAVAC) -d $(BIN_DIR) -sourcepath $(SRC_DIR) -classpath $(JAR_JAVAC) $< run: - java -cp $(BIN_DIR) $(MAIN_FILE) + java -cp $(BIN_DIR) -XstartOnFirstThread -classpath $(JAR_JAVA) $(MAIN_FILE) clean: @rm -rf $(BIN_DIR) \ No newline at end of file diff --git a/res/shaders/cube.frag b/res/shaders/cube.frag index 8ec2a19..370c251 100644 --- a/res/shaders/cube.frag +++ b/res/shaders/cube.frag @@ -1,3 +1,4 @@ + #version 330 core out vec4 FragColor; diff --git a/res/shaders/cube.vert b/res/shaders/cube.vert index 14da693..cdd6dd3 100644 --- a/res/shaders/cube.vert +++ b/res/shaders/cube.vert @@ -1,13 +1,7 @@ - #version 330 core +#version 330 core +layout (location = 0) in vec3 aPos; - layout (location = 0) in vec3 aPos; - layout (location = 1) in vec3 aNormal; - layout (location = 2) in vec2 aTexCoords; - - uniform mat4 projection; - uniform mat4 model; - uniform mat4 view; - - void main() { - FragPos = vec4(aPos, 1.0); - } \ No newline at end of file +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); +} \ No newline at end of file diff --git a/src/Graphics.java b/src/Graphics.java index 3e24a73..1fdf922 100644 --- a/src/Graphics.java +++ b/src/Graphics.java @@ -6,6 +6,7 @@ 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.*; @@ -14,67 +15,97 @@ 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 GLFWVidMode window_VidMode; + 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("unable to initialize GLFW"); + if(!glfwInit()) { + throw new IllegalStateException("Impossible d'initialiser GLFW"); } - window = GLFW.glfwCreateWindow(800, 600, "snake", MemoryUtil.NULL, MemoryUtil.NULL); - window_VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); + this.window = glfwCreateWindow(800, 600, "snake", MemoryUtil.NULL, MemoryUtil.NULL); + this.window_VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - GLFW.glfwWindowHint(GLFW_VERSION_MAJOR, 4); - GLFW.glfwWindowHint(GLFW_VERSION_MINOR, 6); - - GLFW.glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - GLFW.glfwWindowHint(GLFW_DEPTH_BITS, 24); - GLFW.glfwWindowHint(GLFW_STENCIL_BITS, 8); - - GLFW.glfwWindowHint(GLFW_SAMPLES, 4); + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); GLFW.glfwMakeContextCurrent(window); + GL.createCapabilities(); - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULT); + 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() { - GL.createCapabilities(); + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULT); - lastFrame = 0.0f; + Forms form = new Forms(vertices, indices); - while(!glfwWindowShouldClose(window)) { + this.lastFrame = 0.0f; + + while (!GLFW.glfwWindowShouldClose(this.window)) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); - currentTime = glfwGetTime(); - deltaTime = currentTime - lastFrame; + this.currentTime = glfwGetTime(); + this.deltaTime = this.currentTime - this.lastFrame; + + form.loop(); - - lastFrame = currentTime; - GLFW.glfwSwapBuffers(window); + GLFW.glfwSwapBuffers(this.window); GLFW.glfwPollEvents(); + + this.lastFrame = currentTime; } } - public void run() { + public Graphics() { + System.out.println("LWJGL Version : " + Version.getVersion() +"."); init(); loop(); glfwFreeCallbacks(window); glfwDestroyWindow(window); - glfwTerminate(); - glfwSetErrorCallback(null).free(); + glfwTerminate(); + glfwSetErrorCallback(null).free(); } } diff --git a/src/Main.java b/src/Main.java index 647f27b..95cc8d4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,6 +7,6 @@ import org.lwjgl.glfw.GLFW; public class Main { public static void main(String[] args) { - System.out.println(GLFW.glfwInit()); + new Graphics(); } } diff --git a/src/graphics/Forms.java b/src/graphics/Forms.java index 6b03464..c9c07d8 100644 --- a/src/graphics/Forms.java +++ b/src/graphics/Forms.java @@ -1,5 +1,28 @@ 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