package org.example; import org.joml.*; import org.lwjgl.*; import org.lwjgl.Version; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; import org.lwjgl.system.*; import java.lang.Math; import java.nio.*; import java.io.*; import java.text.MessageFormat; import static org.lwjgl.glfw.Callbacks.*; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL15.*; import static org.lwjgl.opengl.GL20.*; import static org.lwjgl.opengl.GL30.*; import static org.lwjgl.system.MemoryStack.*; import static org.lwjgl.system.MemoryUtil.*; import com.mokiat.data.front.parser.*; import org.lwjgl.system.MemoryStack; public class Game { public static final float Z_FAR = 1000.0f; public static final float Z_NEAR = 0.1f; public static final float FOV = (float) Math.toRadians(90.0f); private long window; private Matrix4f projection_matrix; public void run() { System.out.println("Running LWJGL Version " + Version.getVersion() + "."); init(); loop(); // Free the window callbacks and destroy the window glfwFreeCallbacks(window); glfwDestroyWindow(window); // Terminate GLFW and free the error callback glfwTerminate(); glfwSetErrorCallback(null).free(); } public void init() { GLFWErrorCallback.createPrint(System.err).set(); if (!glfwInit()) throw new IllegalStateException("Unable to Initialize GLFW"); // Configure GLFW glfwDefaultWindowHints(); // optional, the current window hints are already the default glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable // Create the window window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL); if ( window == NULL ) throw new RuntimeException("Failed to create the GLFW window"); // Setup a key callback. It will be called every time a key is pressed, repeated or released. glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop }); // Get the thread stack and push a new frame try ( MemoryStack stack = stackPush() ) { IntBuffer pWidth = stack.mallocInt(1); // int* IntBuffer pHeight = stack.mallocInt(1); // int* // Get the window size passed to glfwCreateWindow glfwGetWindowSize(window, pWidth, pHeight); // Get the resolution of the primary monitor GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // Center the window glfwSetWindowPos( window, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2 ); } // the stack frame is popped automatically // Make the OpenGL context current glfwMakeContextCurrent(window); // Enable v-sync glfwSwapInterval(1); // Make the window visible glfwShowWindow(window); } private void loop() { // Setup world World world = new World(); // Do open gl stuff GL.createCapabilities(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Add stuff to world world.addEntity(new Entity("/objs/cube.obj", "/shaders/shader.frag", "/shaders/shader.vert")); // Calculate Perspective Matrix int w, h; glfwGetWindowSize(window, w, h); float aspect_ratio = (float) ; this.projection_matrix = new Matrix4f().perspective(FOV, ) // Main loop or something while ( !glfwWindowShouldClose(window) ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); for (Entity entity : world.entities) render(entity); glfwPollEvents(); } } public void render(Entity entity) { entity.shader.bind(); glBindVertexArray(entity.mesh.vao_id); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, entity.mesh.vertex_count, GL_UNSIGNED_INT, 0); } public static void main(String[] args) { new Game().run(); } }