ajout de 3D avec lwjgl

This commit is contained in:
2024-05-07 21:30:36 +02:00
parent 690be28982
commit de2448d399
6 changed files with 97 additions and 43 deletions

View File

@@ -13,6 +13,11 @@ LIB_DIR = lib
JAR = $(LIB_DIR)/*:$(LIB_DIR)/lwjgl/* JAR = $(LIB_DIR)/*:$(LIB_DIR)/lwjgl/*
JAR_JAVAC = $(JAR):$(SRC_DIR)
JAR_JAVA = $(JAR):$(BIN_DIR)
UNAME := $(shell uname)
# main # main
all: $(MAIN_FILE) run 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 $(BIN_DIR)/$(MAIN_FILE).class : $(SRC_DIR)/$(MAIN_FILE).java
@mkdir -p $(BIN_DIR) @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: run:
java -cp $(BIN_DIR) $(MAIN_FILE) java -cp $(BIN_DIR) -XstartOnFirstThread -classpath $(JAR_JAVA) $(MAIN_FILE)
clean: clean:
@rm -rf $(BIN_DIR) @rm -rf $(BIN_DIR)

View File

@@ -1,3 +1,4 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;

View File

@@ -1,13 +1,7 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 0) in vec3 aPos; void main()
layout (location = 1) in vec3 aNormal; {
layout (location = 2) in vec2 aTexCoords; gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
uniform mat4 projection;
uniform mat4 model;
uniform mat4 view;
void main() {
FragPos = vec4(aPos, 1.0);
}

View File

@@ -6,6 +6,7 @@ import org.lwjgl.*;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.MemoryUtil;
import static org.lwjgl.glfw.Callbacks.*; 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.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*; import static org.lwjgl.system.MemoryUtil.*;
import java.nio.IntBuffer;
public class Graphics { public class Graphics {
// La fenêtre
private long window; private long window;
private GLFWVidMode window_VidMode;
// Gestion des FPS.
private double deltaTime; private double deltaTime;
private double currentTime; private double currentTime;
private double lastFrame; 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() { private void init() {
GLFWErrorCallback.createPrint(System.err).set(); GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) { if(!glfwInit()) {
throw new IllegalStateException("unable to initialize GLFW"); throw new IllegalStateException("Impossible d'initialiser GLFW");
} }
window = GLFW.glfwCreateWindow(800, 600, "snake", MemoryUtil.NULL, MemoryUtil.NULL); this.window = glfwCreateWindow(800, 600, "snake", MemoryUtil.NULL, MemoryUtil.NULL);
window_VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); this.window_VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
GLFW.glfwWindowHint(GLFW_VERSION_MAJOR, 4); glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW_VERSION_MINOR, 6); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
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);
GLFW.glfwMakeContextCurrent(window); GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
glEnable(GL_DEPTH_TEST); try (MemoryStack stack = stackPush() ) {
glEnable(GL_MULT); 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() { 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); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
currentTime = glfwGetTime(); this.currentTime = glfwGetTime();
deltaTime = currentTime - lastFrame; this.deltaTime = this.currentTime - this.lastFrame;
form.loop();
GLFW.glfwSwapBuffers(this.window);
lastFrame = currentTime;
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents(); GLFW.glfwPollEvents();
this.lastFrame = currentTime;
} }
} }
public void run() { public Graphics() {
System.out.println("LWJGL Version : " + Version.getVersion() +".");
init(); init();
loop(); loop();
glfwFreeCallbacks(window); glfwFreeCallbacks(window);
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
glfwSetErrorCallback(null).free(); glfwSetErrorCallback(null).free();
} }
} }

View File

@@ -7,6 +7,6 @@ import org.lwjgl.glfw.GLFW;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(GLFW.glfwInit()); new Graphics();
} }
} }

View File

@@ -1,5 +1,28 @@
package graphics; package graphics;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import org.lwjgl.opengl.GL11;
public class Forms { 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);
}
} }