Changed to using IBO drawElements for quads

This commit is contained in:
mcrcortex
2022-04-13 23:07:02 +10:00
parent 743e504ccc
commit a7e174cf74
3 changed files with 68 additions and 21 deletions
@@ -45,7 +45,7 @@ import static com.seibel.lod.core.render.LodRenderer.EVENT_LOGGER;
public class LodQuadBuilder
{
static final int MAX_BUFFER_SIZE = (1024 * 1024);
static final int QUAD_BYTE_SIZE = (12 * 6);
static final int QUAD_BYTE_SIZE = (12 * 4);
static final int MAX_QUADS_PER_BUFFER = MAX_BUFFER_SIZE / QUAD_BYTE_SIZE;
static final ILodConfigWrapperSingleton CONFIG = SingletonHandler.get(ILodConfigWrapperSingleton.class);
@@ -62,18 +62,12 @@ public class LodQuadBuilder
{ 1, 0 }, // 0
{ 1, 1 }, // 1
{ 0, 1 }, // 2
{ 1, 0 }, // 0
{ 0, 1 }, // 2
{ 0, 0 }, // 3
},
{ // DOWN
{ 0, 0 }, // 0
{ 0, 1 }, // 1
{ 1, 1 }, // 2
{ 0, 0 }, // 0
{ 1, 1 }, // 2
{ 1, 0 }, // 3
},
@@ -82,18 +76,14 @@ public class LodQuadBuilder
{ 0, 0 }, // 0
{ 0, 1 }, // 1
{ 1, 1 }, // 2
{ 0, 0 }, // 0
{ 1, 1 }, // 2
{ 1, 0 }, // 3
},
{ // SOUTH
{ 1, 0 }, // 0
{ 1, 1 }, // 1
{ 0, 1 }, // 2
{ 1, 0 }, // 0
{ 0, 1 }, // 2
{ 0, 0 }, // 3
},
@@ -102,18 +92,14 @@ public class LodQuadBuilder
{ 0, 0 }, // 0
{ 1, 0 }, // 1
{ 1, 1 }, // 2
{ 0, 0 }, // 0
{ 1, 1 }, // 2
{ 0, 1 }, // 3
},
{ // EAST
{ 0, 1 }, // 0
{ 1, 1 }, // 1
{ 1, 0 }, // 2
{ 0, 1 }, // 0
{ 1, 0 }, // 2
{ 0, 0 }, // 3
},
};
@@ -406,7 +392,7 @@ public class LodQuadBuilder
}
bb.rewind();
vbo.unmapBuffer(method);
vbo.vertexCount = numOfQuads * 6;
vbo.vertexCount = numOfQuads * 4;
return dir < 6;
}
@@ -0,0 +1,58 @@
package com.seibel.lod.core.objects.opengl;
import org.lwjgl.system.MemoryUtil;
import static org.lwjgl.opengl.GL11C.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL45.*;
public class QuadIBO {
//Datatype of the stored indices (can be GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, GL_UNSIGNED_BYTE)
public int type;
//IBO object
int id;
//Current capacity (in quads)
int ccap;
//Global object, used for sharing the IBO for any draw calls
public static QuadIBO GLOBAL = new QuadIBO();
public QuadIBO() {
id = glCreateBuffers();
}
public void resize(int cap) {
//If requested capacity is less than or equal to current capacity, ignore
if (cap <= ccap)
return;
//Not really necessary, just to stop constant resizes
cap *= 1.5;
System.out.println("Resizing from "+ccap+" to " + cap);
ccap = cap;
//TODO: DO DYNAMIC TYPES, just makes things more efficent
type = GL_UNSIGNED_INT;
int DT_SIZE = 4;//Datatype size (int: 4, short: 2, byte: 1)
//Resize the buffer
glNamedBufferData(id, (long) DT_SIZE * 6 * cap, GL_STATIC_DRAW);// 4L is datatype
//Map and write the index data to the buffer
long ptr = nglMapNamedBuffer(id, GL_WRITE_ONLY);
for (int base = 0; base < cap; base++) {
//Write index's
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*0),(int)(base*4 + 0));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*1),(int)(base*4 + 1));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*2),(int)(base*4 + 2));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*3),(int)(base*4 + 2));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*4),(int)(base*4 + 3));
MemoryUtil.memPutInt(ptr+(base*6*DT_SIZE+DT_SIZE*5),(int)(base*4 + 0));
}
glUnmapNamedBuffer(id);
}
public void bind(int capacity) {
resize(capacity);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
}
@@ -23,7 +23,9 @@ import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import com.mojang.authlib.minecraft.client.MinecraftClient;
import com.seibel.lod.core.builders.lodBuilding.bufferBuilding.LodQuadBuilder;
import net.minecraft.client.Minecraft;
import org.lwjgl.opengl.GL32;
import com.seibel.lod.core.api.ApiShared;
@@ -80,7 +82,8 @@ public class SimpleRenderBuffer extends RenderBuffer
hasRendered = true;
GL32.glBindBuffer(GL32.GL_ARRAY_BUFFER, vbo.id);
shaderProgram.bindVertexBuffer(vbo.id);
GL32.glDrawArrays(GL32.GL_TRIANGLES, 0, vbo.vertexCount);
QuadIBO.GLOBAL.bind(vbo.vertexCount/4);
GL32.glDrawElements(GL32.GL_TRIANGLES, (vbo.vertexCount/4)*6, QuadIBO.GLOBAL.type, 0);
//LodRenderer.tickLogger.info("Vertex buffer: {}", vbo);
}
return hasRendered;