mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Better VertexBuffer API (#170)
Co-authored-by: RecursivePineapple <[email protected]>
This commit is contained in:
1 parent
e6a834c624
commit
a51c3719fc
1 file changed
+43
-13
@@ -10,17 +10,14 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat;
|
||||
|
||||
public class VertexBuffer implements AutoCloseable {
|
||||
|
||||
private int id;
|
||||
private int vertexCount;
|
||||
private VertexFormat format;
|
||||
private int drawMode;
|
||||
|
||||
public VertexBuffer() {
|
||||
this.id = GL15.glGenBuffers();
|
||||
}
|
||||
protected int id;
|
||||
protected int vertexCount;
|
||||
protected final VertexFormat format;
|
||||
protected final int drawMode;
|
||||
|
||||
public VertexBuffer(VertexFormat format, int drawMode) {
|
||||
this();
|
||||
if (format == null) throw new IllegalStateException("No format specified for VBO");
|
||||
this.id = GL15.glGenBuffers();
|
||||
this.format = format;
|
||||
this.drawMode = drawMode;
|
||||
}
|
||||
@@ -34,19 +31,45 @@ public class VertexBuffer implements AutoCloseable {
|
||||
}
|
||||
|
||||
public void upload(ByteBuffer buffer, int vertexCount) {
|
||||
upload(buffer, vertexCount, GL15.GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
public void upload(ByteBuffer buffer, int vertexCount, int type) {
|
||||
if (this.id == -1) return;
|
||||
this.vertexCount = vertexCount;
|
||||
this.bind();
|
||||
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
|
||||
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, type);
|
||||
this.unbind();
|
||||
}
|
||||
|
||||
public VertexBuffer upload(ByteBuffer buffer) {
|
||||
if (format == null) throw new IllegalStateException("No format specified for VBO upload");
|
||||
upload(buffer, buffer.remaining() / format.getVertexSize());
|
||||
upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_STATIC_DRAW);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* GL_DYNAMIC_DRAW is more efficient for buffers that have their values constantly updated and read multiple times.
|
||||
*/
|
||||
public void uploadDynamic(ByteBuffer buffer, int vertexCount) {
|
||||
upload(buffer, vertexCount, GL15.GL_DYNAMIC_DRAW);
|
||||
}
|
||||
|
||||
public void uploadDynamic(ByteBuffer buffer) {
|
||||
upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_DYNAMIC_DRAW);
|
||||
}
|
||||
|
||||
/**
|
||||
* GL_STREAM_DRAW is more efficient for buffers that have their values constantly updated and read at most a few
|
||||
* times (example: uploading a set of vertices that only get used a few times at most)
|
||||
*/
|
||||
public void uploadStream(ByteBuffer buffer, int vertexCount) {
|
||||
upload(buffer, vertexCount, GL15.GL_STREAM_DRAW);
|
||||
}
|
||||
|
||||
public void uploadStream(ByteBuffer buffer) {
|
||||
upload(buffer, buffer.remaining() / format.getVertexSize(), GL15.GL_STREAM_DRAW);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (this.id >= 0) {
|
||||
GL15.glDeleteBuffers(this.id);
|
||||
@@ -67,7 +90,6 @@ public class VertexBuffer implements AutoCloseable {
|
||||
}
|
||||
|
||||
public void setupState() {
|
||||
if (format == null) throw new IllegalStateException("No format specified for VBO setup");
|
||||
bind();
|
||||
format.setupBufferState(0L);
|
||||
}
|
||||
@@ -86,4 +108,12 @@ public class VertexBuffer implements AutoCloseable {
|
||||
public VertexFormat getVertexFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public int getDrawMode() {
|
||||
return drawMode;
|
||||
}
|
||||
|
||||
public int getVertexCount() {
|
||||
return vertexCount;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user