generated from calmilamsy/stationapi-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10ae9c8
commit 6c0abcf
Showing
43 changed files
with
2,834 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/net/mine_diver/smoothbeta/client/render/BufferRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.mine_diver.smoothbeta.client.render; | ||
|
||
import net.mine_diver.smoothbeta.client.render.gl.VertexBuffer; | ||
|
||
public class BufferRenderer { | ||
public static void unbindAll() { | ||
VertexBuffer.unbind(); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/net/mine_diver/smoothbeta/client/render/IndexBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package net.mine_diver.smoothbeta.client.render; | ||
|
||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.Environment; | ||
import net.modificationstation.stationapi.api.util.math.MathHelper; | ||
import org.lwjgl.opengl.GL15; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.function.IntConsumer; | ||
|
||
import static net.mine_diver.smoothbeta.SmoothBeta.LOGGER; | ||
|
||
@Environment(EnvType.CLIENT) | ||
public final class IndexBuffer { | ||
private static final IndexBuffer sharedSequential = new IndexBuffer(1, 1, java.util.function.IntConsumer::accept);; | ||
private static final IndexBuffer sharedSequentialQuad = new IndexBuffer(4, 6, (intConsumer, i) -> { | ||
intConsumer.accept(i); | ||
intConsumer.accept(i + 1); | ||
intConsumer.accept(i + 2); | ||
intConsumer.accept(i + 2); | ||
intConsumer.accept(i + 3); | ||
intConsumer.accept(i); | ||
}); | ||
private static final IndexBuffer sharedSequentialLines = new IndexBuffer(4, 6, (intConsumer, i) -> { | ||
intConsumer.accept(i); | ||
intConsumer.accept(i + 1); | ||
intConsumer.accept(i + 2); | ||
intConsumer.accept(i + 3); | ||
intConsumer.accept(i + 2); | ||
intConsumer.accept(i + 1); | ||
}); | ||
|
||
public static IndexBuffer getSequentialBuffer(VertexFormat.DrawMode drawMode) { | ||
return switch (drawMode) { | ||
case QUADS -> sharedSequentialQuad; | ||
case LINES -> sharedSequentialLines; | ||
default -> sharedSequential; | ||
}; | ||
} | ||
|
||
private final int sizeMultiplier; | ||
private final int increment; | ||
private final IndexMapper indexMapper; | ||
private int id; | ||
private VertexFormat.IndexType indexType = VertexFormat.IndexType.BYTE; | ||
private int size; | ||
|
||
IndexBuffer(int sizeMultiplier, int increment, IndexMapper indexMapper) { | ||
this.sizeMultiplier = sizeMultiplier; | ||
this.increment = increment; | ||
this.indexMapper = indexMapper; | ||
} | ||
|
||
public boolean isSizeLessThanOrEqual(int size) { | ||
return size <= this.size; | ||
} | ||
|
||
public void bindAndGrow(int newSize) { | ||
if (this.id == 0) this.id = GL15.glGenBuffers(); | ||
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.id); | ||
this.grow(newSize); | ||
} | ||
|
||
private void grow(int newSize) { | ||
if (this.isSizeLessThanOrEqual(newSize)) return; | ||
newSize = MathHelper.roundUpToMultiple(newSize * 2, this.increment); | ||
LOGGER.debug("Growing IndexBuffer: Old limit {}, new limit {}.", this.size, newSize); | ||
VertexFormat.IndexType indexType = VertexFormat.IndexType.smallestFor(newSize); | ||
int i = MathHelper.roundUpToMultiple(newSize * indexType.size, 4); | ||
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, i, GL15.GL_DYNAMIC_DRAW); | ||
ByteBuffer byteBuffer = GL15.glMapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, GL15.GL_WRITE_ONLY, null); | ||
if (byteBuffer == null) throw new RuntimeException("Failed to map GL buffer"); | ||
this.indexType = indexType; | ||
IntConsumer intConsumer = this.getIndexConsumer(byteBuffer); | ||
for (int j = 0; j < newSize; j += this.increment) | ||
this.indexMapper.accept(intConsumer, j * this.sizeMultiplier / this.increment); | ||
GL15.glUnmapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER); | ||
this.size = newSize; | ||
} | ||
|
||
private IntConsumer getIndexConsumer(ByteBuffer indicesBuffer) { | ||
return switch (this.indexType) { | ||
case BYTE -> index -> indicesBuffer.put((byte) index); | ||
case SHORT -> index -> indicesBuffer.putShort((short) index); | ||
default -> indicesBuffer::putInt; | ||
}; | ||
} | ||
|
||
public VertexFormat.IndexType getIndexType() { | ||
return this.indexType; | ||
} | ||
|
||
@Environment(EnvType.CLIENT) | ||
interface IndexMapper { | ||
void accept(IntConsumer var1, int var2); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/net/mine_diver/smoothbeta/client/render/RenderRegion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package net.mine_diver.smoothbeta.client.render; | ||
|
||
import net.mine_diver.smoothbeta.client.render.gl.GlUniform; | ||
import net.mine_diver.smoothbeta.client.render.gl.VertexBuffer; | ||
import net.mine_diver.smoothbeta.mixin.client.RenderListAccessor; | ||
import net.minecraft.client.render.RenderList; | ||
import net.minecraft.client.render.WorldRenderer; | ||
import net.modificationstation.stationapi.api.util.math.Vec3f; | ||
|
||
import java.nio.IntBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RenderRegion extends RenderList { | ||
|
||
private final RenderListAccessor _super = (RenderListAccessor) this; | ||
private final SmoothWorldRenderer stationWorldRenderer; | ||
private final List<VertexBuffer> buffers = new ArrayList<>(); | ||
|
||
public RenderRegion(WorldRenderer worldRenderer) { | ||
_super.smoothbeta_setField_2486(IntBuffer.allocate(0)); | ||
stationWorldRenderer = ((SmoothWorldRenderer) worldRenderer); | ||
} | ||
|
||
@Override | ||
public void method_1912(int i, int j, int k, double d, double e, double f) { | ||
super.method_1912(i, j, k, d, e, f); | ||
buffers.clear(); | ||
} | ||
|
||
@Override | ||
public void method_1910(int i) { | ||
throw new UnsupportedOperationException("Call lists can't be added to VBO regions!"); | ||
} | ||
|
||
public void addBuffer(VertexBuffer buffer) { | ||
buffers.add(buffer); | ||
} | ||
|
||
public void method_1909() { | ||
if (!_super.smoothbeta_getField_2487()) { | ||
return; | ||
} | ||
if (!buffers.isEmpty()) { | ||
Shader shader = Shaders.getTerrainShader(); | ||
GlUniform chunkOffset = null; | ||
if (shader != null) | ||
chunkOffset = shader.chunkOffset; | ||
if (chunkOffset != null) { | ||
chunkOffset.set(_super.smoothbeta_getField_2480() - _super.smoothbeta_getField_2483(), _super.smoothbeta_getField_2481() - _super.smoothbeta_getField_2484(), _super.smoothbeta_getField_2482() - _super.smoothbeta_getField_2485()); | ||
chunkOffset.upload(); | ||
} | ||
for (VertexBuffer vertexBuffer : buffers) vertexBuffer.uploadToPool(); | ||
stationWorldRenderer.smoothbeta_getTerrainVboPool().drawAll(); | ||
if (chunkOffset != null) | ||
chunkOffset.set(Vec3f.ZERO); | ||
} | ||
} | ||
} |
Oops, something went wrong.