Drew a triangle :D

master
Caroline Bell 2019-09-21 11:52:26 -07:00
parent 80d7dbe072
commit a2ff25293a
6 changed files with 92 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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