From a2ff25293ae60b7f133d8434363d70081713a8fa Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Sat, 21 Sep 2019 11:52:26 -0700 Subject: [PATCH] Drew a triangle :D --- .../halotroop/litecraft/LiteCraftMain.java | 10 ++--- .../github/halotroop/litecraft/Window.java | 1 - .../halotroop/litecraft/logic/Timer.java | 2 +- .../halotroop/litecraft/render/Renderer.java | 27 ++++++++++- .../litecraft/render/model/Model.java | 45 +++++++++++++++++++ .../litecraft/render/model/Vertex.java | 15 +++++++ 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 src/com/github/halotroop/litecraft/render/model/Model.java create mode 100644 src/com/github/halotroop/litecraft/render/model/Vertex.java diff --git a/src/com/github/halotroop/litecraft/LiteCraftMain.java b/src/com/github/halotroop/litecraft/LiteCraftMain.java index 1718703..e094374 100644 --- a/src/com/github/halotroop/litecraft/LiteCraftMain.java +++ b/src/com/github/halotroop/litecraft/LiteCraftMain.java @@ -11,7 +11,7 @@ import com.github.halotroop.litecraft.render.Renderer; public class LiteCraftMain { protected Timer timer; - private int fps, ups; + private int fps, ups, tps; public static int maxFPS = 100; private long frameTimer; private Renderer renderer; @@ -25,7 +25,7 @@ public class LiteCraftMain @Override public void onTick(float deltaTime) { - + tps++; } }; @@ -73,7 +73,7 @@ public class LiteCraftMain GL.createCapabilities(); // This line is critical for LWJGL's interoperation with GLFW. renderer = new Renderer(); - window.setWindowTitle("LiteCraft " + "INSERT SPLASH TEXT HERE!"); + window.setWindowTitle("LiteCraft - " + "INSERT SPLASH TEXT HERE!"); input(); System.out.println("Initialization complete."); @@ -103,10 +103,10 @@ public class LiteCraftMain if (System.currentTimeMillis() > frameTimer + 1000) // wait for one second { - System.out.println("Frames this second: " + fps); - System.out.println("Updates this second: " + ups); + window.setWindowTitle("LiteCraft | FPS: " + fps + " | TPS: " + tps + " | UPS: " + ups); fps = 0; ups = 0; + tps = 0; frameTimer += 1000; // reset the wait time } } diff --git a/src/com/github/halotroop/litecraft/Window.java b/src/com/github/halotroop/litecraft/Window.java index ae7f648..70845d0 100644 --- a/src/com/github/halotroop/litecraft/Window.java +++ b/src/com/github/halotroop/litecraft/Window.java @@ -103,7 +103,6 @@ public class Window GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); // Get the resolution of the primary monitor GLFW.glfwSetWindowPos(windowLong, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2); // Center the window GLFW.glfwMakeContextCurrent(windowLong); // Make the OpenGL context current - GLFW.glfwSwapInterval(1); // Enable V-Sync GLFW.glfwShowWindow(windowLong); // Make the window visible } } diff --git a/src/com/github/halotroop/litecraft/logic/Timer.java b/src/com/github/halotroop/litecraft/logic/Timer.java index b3bd938..24506e9 100644 --- a/src/com/github/halotroop/litecraft/logic/Timer.java +++ b/src/com/github/halotroop/litecraft/logic/Timer.java @@ -49,7 +49,7 @@ public class Timer public boolean tick() { - long currentTime = System.nanoTime(); + long currentTime = System.currentTimeMillis(); if (currentTime >= nextTick) { long targetTimeDelta = 1000L / tickRate; diff --git a/src/com/github/halotroop/litecraft/render/Renderer.java b/src/com/github/halotroop/litecraft/render/Renderer.java index fc27cbe..e17cf15 100644 --- a/src/com/github/halotroop/litecraft/render/Renderer.java +++ b/src/com/github/halotroop/litecraft/render/Renderer.java @@ -1,17 +1,41 @@ package com.github.halotroop.litecraft.render; import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL20; + +import com.github.halotroop.litecraft.render.model.Model; +import com.github.halotroop.litecraft.render.model.Vertex; public class Renderer { + private Model model; public Renderer() { init(); + + model = new Model(); + + Vertex[] vertices = + { + new Vertex(-1, -1, 0), + new Vertex(1, -1, 0), + new Vertex(0, 1, 0) + }; + + model.bufferVertices(vertices); } public void render() { - GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // clear the framebuffer + + + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, model.getVBO()); + GL20.glEnableVertexAttribArray(0); + GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, Vertex.SIZE * 4, 0); + GL20.glDrawArrays(GL20.GL_TRIANGLES, 0, model.getSize()); + GL20.glDisableVertexAttribArray(0); + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } private void init() @@ -22,6 +46,7 @@ public class Renderer private void prepare() { GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set the background color + GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // clear the framebuffer } diff --git a/src/com/github/halotroop/litecraft/render/model/Model.java b/src/com/github/halotroop/litecraft/render/model/Model.java new file mode 100644 index 0000000..d03e087 --- /dev/null +++ b/src/com/github/halotroop/litecraft/render/model/Model.java @@ -0,0 +1,45 @@ +package com.github.halotroop.litecraft.render.model; + +import java.nio.FloatBuffer; + +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL15; + +public class Model +{ + private int vbo, size; + + public Model() + { + vbo = GL15.glGenBuffers(); + + size = 0; + } + + public void bufferVertices(Vertex[] verts) + { + FloatBuffer buffer = BufferUtils.createFloatBuffer(verts.length * Vertex.SIZE); + + for (Vertex vertex : verts) + { + buffer.put(vertex.x); + buffer.put(vertex.y); + buffer.put(vertex.z); + } + + buffer.flip(); + + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); + + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + + size = verts.length; + } + + public int getVBO() + { return vbo; } + + public int getSize() + { return size; } +} diff --git a/src/com/github/halotroop/litecraft/render/model/Vertex.java b/src/com/github/halotroop/litecraft/render/model/Vertex.java new file mode 100644 index 0000000..1767888 --- /dev/null +++ b/src/com/github/halotroop/litecraft/render/model/Vertex.java @@ -0,0 +1,15 @@ +package com.github.halotroop.litecraft.render.model; + +import org.joml.Vector3i; + +public class Vertex extends Vector3i +{ + public static final int SIZE = 3; + + public Vertex(int x, int y, int z) + { + this.x = x; + this.y = y; + this.z = z; + } +}