Use VAOs for contraption structures

This commit is contained in:
JozsefA 2021-05-25 12:46:34 -07:00
parent 7f9ff3b7ca
commit de451553dd
3 changed files with 57 additions and 5 deletions

View file

@ -0,0 +1,48 @@
package com.jozufozu.flywheel.backend.core;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import com.jozufozu.flywheel.backend.gl.GlVertexArray;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
import com.jozufozu.flywheel.util.AttribUtil;
public class BufferedArrayModel extends BufferedModel {
protected GlVertexArray vao;
public BufferedArrayModel(VertexFormat format, ByteBuffer data, int vertices) {
super(format, data, vertices);
vao = new GlVertexArray();
vao.bind();
// bind the model's vbo to our vao
vbo.bind();
getFormat().vertexAttribPointers(0);
vbo.unbind();
// enable all the attribute arrays in our vao. we only need to do this once
AttribUtil.enableArrays(getAttributeCount());
vao.unbind();
}
public void render() {
if (vertexCount <= 0 || deleted) return;
vao.bind();
GL20.glDrawArrays(GL11.GL_QUADS, 0, vertexCount);
vao.unbind();
}
@Override
public void delete() {
super.delete();
vao.delete();
}
}

View file

@ -22,7 +22,7 @@ public class BufferedModel {
protected final VertexFormat format; protected final VertexFormat format;
protected final int vertexCount; protected final int vertexCount;
protected GlBuffer vbo; protected GlBuffer vbo;
private boolean removed; protected boolean deleted;
public BufferedModel(VertexFormat format, ByteBuffer data, int vertices) { public BufferedModel(VertexFormat format, ByteBuffer data, int vertices) {
this.data = data; this.data = data;
@ -62,7 +62,7 @@ public class BufferedModel {
* Renders this model, checking first if there is anything to render. * Renders this model, checking first if there is anything to render.
*/ */
public void render() { public void render() {
if (vertexCount <= 0 || removed) return; if (vertexCount <= 0 || deleted) return;
// TODO: minecraft sometimes leaves its state dirty on launch. this is a hack // TODO: minecraft sometimes leaves its state dirty on launch. this is a hack
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
@ -84,9 +84,9 @@ public class BufferedModel {
} }
public void delete() { public void delete() {
if (removed) return; if (deleted) return;
removed = true; deleted = true;
vbo.delete(); vbo.delete();
} }

View file

@ -10,6 +10,7 @@ import java.util.Map;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.core.BufferedArrayModel;
import com.jozufozu.flywheel.backend.core.BufferedModel; import com.jozufozu.flywheel.backend.core.BufferedModel;
import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes; import com.jozufozu.flywheel.backend.gl.attrib.CommonAttributes;
import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat; import com.jozufozu.flywheel.backend.gl.attrib.VertexFormat;
@ -192,6 +193,9 @@ public class RenderedContraption extends ContraptionWorldHolder {
to.rewind(); to.rewind();
return new BufferedModel(format, to, vertexCount); if (Backend.compat.vertexArrayObjectsSupported())
return new BufferedArrayModel(format, to, vertexCount);
else
return new BufferedModel(format, to, vertexCount);
} }
} }