From d26aef98066dd595d7c331f5d6cb9a1c438454b1 Mon Sep 17 00:00:00 2001 From: sisyphussy <221229741+sisyphussy@users.noreply.github.com> Date: Mon, 18 May 2026 17:23:49 +0200 Subject: [PATCH] Angelica patch (#353) Co-authored-by: Caedis --- .../client/renderer/CallbackTessellator.java | 143 +++++++++++++++--- .../client/renderer/DirectDrawCallback.java | 4 +- .../client/renderer/DirectTessellator.java | 132 ++++++++++++---- .../client/renderer/ITessellatorInstance.java | 1 + .../gtnhlib/client/renderer/MatrixHelper.java | 50 ++++++ .../client/renderer/TessellatorManager.java | 49 ++++-- .../renderer/VertexCallbackManager.java | 45 ++++++ .../renderer/shader/AutoShaderUpdater.java | 22 +-- .../tessellator/TessellatorCallback.java | 31 ++++ .../tessellator/VertexTransformCallback.java | 95 ++++++++++++ .../renderer/vertex/DefaultVertexFormat.java | 38 +++-- .../client/renderer/vertex/VertexFormat.java | 26 +++- .../gtnhlib/util/font/GlyphReplacements.java | 23 ++- .../renderer/DirectTessellatorTest.java | 24 ++- 14 files changed, 587 insertions(+), 96 deletions(-) create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/MatrixHelper.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/TessellatorCallback.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/VertexTransformCallback.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/CallbackTessellator.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/CallbackTessellator.java index 361f8287..76c05a99 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/CallbackTessellator.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/CallbackTessellator.java @@ -1,22 +1,13 @@ package com.gtnewhorizon.gtnhlib.client.renderer; +import static com.gtnewhorizon.gtnhlib.client.renderer.VertexCallbackManager.callback; + import java.nio.ByteBuffer; -import net.minecraft.client.renderer.Tessellator; - public final class CallbackTessellator extends DirectTessellator { - private DirectDrawCallback drawCallback; - - public CallbackTessellator(DirectDrawCallback callback) { - super(Tessellator.byteBuffer); - this.drawCallback = callback; - } - - public CallbackTessellator(ByteBuffer initial, DirectDrawCallback callback) { - super(initial); - this.drawCallback = callback; - } + // Controls whether the Tessellator will call popCallback() afte it has been removed from the stack. + boolean needsPopCallback; public CallbackTessellator(ByteBuffer initial) { super(initial); @@ -32,14 +23,128 @@ public final class CallbackTessellator extends DirectTessellator { @Override public int draw() { - final int result = super.draw(); - if (drawCallback.onDraw(this)) { - this.reset(); + if (callback.onDraw(this)) { + final int result = super.draw(); + reset(); + return result; } - return result; + + return 0; // onDraw returns false -> no draw happening } - public void setDrawCallback(DirectDrawCallback drawCallback) { - this.drawCallback = drawCallback; + @Override + public void startDrawing(int drawMode) { + if (this.isDrawing) { + throw new IllegalStateException("Already tesselating!"); + } + if (callback.onStartDrawing(this, drawMode)) { + reset(); + this.isDrawing = true; + this.drawMode = drawMode; + } + } + + @Override + public void addVertex(double x, double y, double z) { + if (format == null) { + this.format = getOptimalVertexFormat(); + } + + ensureCapacity(this.format.getVertexSize()); + + callback.onVertex(this, x, y, z); + this.vertexCount++; + } + + @Override + public void setTextureUV(double u, double v) { + if (!hasTexture) { + if (preDefinedFormat != null) return; + + this.hasTexture = true; + + if (format != null) { + fixBufferFormat(); + } + } + + callback.onTextureUV(this, u, v); + } + + @Override + public void setNormal(float nx, float ny, float nz) { + if (!hasNormals) { + if (preDefinedFormat != null) return; + + this.hasNormals = true; + + if (format != null) { + fixBufferFormat(); + } + } + + callback.onNormal(this, nx, ny, nz); + } + + @Override + public void setColorRGBA(int red, int green, int blue, int alpha) { + if (this.isColorDisabled) return; + + if (!this.hasColor) { + if (preDefinedFormat != null) return; + + this.hasColor = true; + + if (format != null) { + fixBufferFormat(); + } + } + + if (red > 255) { + red = 255; + } else if (red < 0) { + red = 0; + } + + if (green > 255) { + green = 255; + } else if (green < 0) { + green = 0; + } + + if (blue > 255) { + blue = 255; + } else if (blue < 0) { + blue = 0; + } + + if (alpha > 255) { + alpha = 255; + } else if (alpha < 0) { + alpha = 0; + } + + callback.onColor(this, red, green, blue, alpha); + } + + @Override + protected void onRemovedFromStack() { + super.onRemovedFromStack(); + if (needsPopCallback) { + VertexCallbackManager.popCallback(); + needsPopCallback = false; + } + } + + public double getXOffset() { + return this.xOffset; + } + + public double getYOffset() { + return this.yOffset; + } + + public double getZOffset() { + return this.zOffset; } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectDrawCallback.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectDrawCallback.java index 6ed0a31b..c2df19c9 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectDrawCallback.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectDrawCallback.java @@ -1,8 +1,6 @@ package com.gtnewhorizon.gtnhlib.client.renderer; -import com.google.common.annotations.Beta; - -@Beta // Not a stable API. May change in the future. +@Deprecated public interface DirectDrawCallback { boolean onDraw(CallbackTessellator tessellator); diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellator.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellator.java index d8486a6a..b512d082 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellator.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellator.java @@ -27,6 +27,7 @@ public class DirectTessellator extends Tessellator { protected VertexFormat format; + @Deprecated // Idk what to do with this, this has caused me too much headache protected VertexFormat preDefinedFormat; protected final ByteBuffer baseBuffer; // never resized, only freed if deleteAfter is true @@ -64,20 +65,71 @@ public class DirectTessellator extends Tessellator { return this.vertexCount * 32; } - protected int interceptDraw(Tessellator tessellator) { - this.vertexCount = tessellator.vertexCount; - this.hasColor = tessellator.hasColor; - this.hasTexture = tessellator.hasTexture; - this.hasBrightness = tessellator.hasBrightness; - this.hasNormals = tessellator.hasNormals; - this.isColorDisabled = tessellator.isColorDisabled; - this.drawMode = tessellator.drawMode; + public final void syncVanillaTessellator() { + syncTessellator(TessellatorManager.getVanillaTessellator()); + } + + public final void syncTessellator(Tessellator other) { + if (!other.isDrawing || other.vertexCount == 0) return; // Not drawing, nothing to sync + + // Cannot merge when both tessellators are drawing with different draw mode + if (this.isDrawing && other.drawMode != this.drawMode) { + TessellatorManager.LOGGER.error( + "Failed draw merging between 2 Tessellators due to mismatching draw mode! Discarding data.", + new IllegalStateException()); + return; + } + + // If this tessellator has emitted a vertex, try to merge their data together + // Really scuffed, but should cover some rare edge-cases + + this.isDrawing = true; + this.drawMode = other.drawMode; + this.isColorDisabled = other.isColorDisabled; + + // If the other Tessellator has offsets set, use those instead + if (other.xOffset != 0 || other.yOffset != 0 || other.zOffset != 0) { + this.xOffset = other.xOffset; + this.yOffset = other.yOffset; + this.zOffset = other.zOffset; + } + + if (other.hasColor) { + this.setColorRGBA_I(other.color, other.color >> 24); + } + if (other.hasTexture) { + this.setTextureUV(other.textureU, other.textureV); + } + if (other.hasBrightness) { + this.setBrightness(other.brightness); + } + if (other.hasNormals) { + final int normal = other.normal; + byte b0 = (byte) (normal & 0xFF); + byte b1 = (byte) ((normal >> 8) & 0xFF); + byte b2 = (byte) ((normal >> 16) & 0xFF); + + float nx = b0 / 127.0f; + float ny = b1 / 127.0f; + float nz = b2 / 127.0f; + this.setNormal(nx, ny, nz); + } + + // Set vertexCount afterward (to not mess with fixBufferFormat()) + this.vertexCount = this.vertexCount + other.vertexCount; + this.format = preDefinedFormat != null ? preDefinedFormat : getOptimalVertexFormat(); - ensureCapacity(tessellator.vertexCount * format.getVertexSize()); + ensureCapacity(other.vertexCount * format.getVertexSize()); - writePtr = writeVertexData(format, tessellator.rawBuffer, tessellator.rawBufferIndex); + writePtr = writeVertexData(format, other.rawBuffer, other.rawBufferIndex); + TessellatorManager.discardTessellator(other); + + } + + protected int interceptDraw(Tessellator tessellator) { + syncTessellator(tessellator); return draw(); } @@ -99,7 +151,6 @@ public class DirectTessellator extends Tessellator { this.isColorDisabled = false; this.format = null; - this.preDefinedFormat = null; if (isResized()) { nmemFree(startPtr); @@ -111,15 +162,16 @@ public class DirectTessellator extends Tessellator { } @Override - public final void startDrawing(int p_78371_1_) { + public void startDrawing(int p_78371_1_) { if (this.isDrawing) { throw new IllegalStateException("Already tesselating!"); } + reset(); this.isDrawing = true; this.drawMode = p_78371_1_; } - private void ensureCapacity(int bytes) { + protected final void ensureCapacity(int bytes) { if (bufferRemaining() >= bytes) { return; } @@ -153,17 +205,20 @@ public class DirectTessellator extends Tessellator { ensureCapacity(this.format.getVertexSize()); - writePtr = format.writeToBuffer0( - writePtr, - this, - (float) (x + this.xOffset), - (float) (y + this.yOffset), - (float) (z + this.zOffset)); + writeVertex((float) (x + xOffset), (float) (y + yOffset), (float) (z + zOffset)); this.vertexCount++; } + public final void writeVertex(float x, float y, float z) { + writePtr = format.writeToBuffer0(writePtr, this, x, y, z); + } + + public final void writeVertex(double x, double y, double z) { + writeVertex((float) x, (float) y, (float) z); + } + @Override - public final void setTextureUV(double p_78385_1_, double p_78385_3_) { + public void setTextureUV(double u, double v) { if (!hasTexture) { if (preDefinedFormat != null) return; @@ -174,12 +229,16 @@ public class DirectTessellator extends Tessellator { } } - this.textureU = p_78385_1_; - this.textureV = p_78385_3_; + writeTextureUV(u, v); + } + + public final void writeTextureUV(double u, double v) { + this.textureU = u; + this.textureV = v; } @Override - public final void setNormal(float nx, float ny, float nz) { + public void setNormal(float nx, float ny, float nz) { if (!hasNormals) { if (preDefinedFormat != null) return; @@ -190,14 +249,18 @@ public class DirectTessellator extends Tessellator { } } + writeNormal(nx, ny, nz); + } + + public final void writeNormal(float nx, float ny, float nz) { byte b0 = (byte) ((int) (nx * 127.0F)); byte b1 = (byte) ((int) (ny * 127.0F)); byte b2 = (byte) ((int) (nz * 127.0F)); - this.normal = b0 & 255 | (b1 & 255) << 8 | (b2 & 255) << 16; + this.normal = (b0 & 255 | (b1 & 255) << 8 | (b2 & 255) << 16); } @Override - public final void setColorRGBA(int red, int green, int blue, int alpha) { + public void setColorRGBA(int red, int green, int blue, int alpha) { if (this.isColorDisabled) return; if (!this.hasColor) { @@ -234,9 +297,17 @@ public class DirectTessellator extends Tessellator { alpha = 0; } + writeColor(red, green, blue, alpha); + } + + public final void writeColor(int red, int green, int blue, int alpha) { this.color = alpha << 24 | blue << 16 | green << 8 | red; } + public final void writeColor(int color) { + this.color = color; + } + @Override public final void setBrightness(int p_78380_1_) { if (!this.hasBrightness) { @@ -346,10 +417,13 @@ public class DirectTessellator extends Tessellator { this.format = newFormat; } - private VertexFormat getOptimalVertexFormat() { + protected final VertexFormat getOptimalVertexFormat() { return VertexFlags.getFormat(this); } + // Method may change/get removed in the future. + // For future-proof use, use TessellatorManager#startCapturingDirect(VertexFormat) + @Deprecated public final void setVertexFormat(VertexFormat format) { if (this.format != null) { throw new IllegalStateException("Cannot call setVertexFormat() after a vertex has already been emitted!"); @@ -421,7 +495,11 @@ public class DirectTessellator extends Tessellator { } protected void onRemovedFromStack() { + this.preDefinedFormat = null; reset(); + this.xOffset = 0; + this.yOffset = 0; + this.zOffset = 0; if (this.deleteAfter) { delete(); } @@ -453,7 +531,7 @@ public class DirectTessellator extends Tessellator { return TessellatorManager.startCapturingDirect(format); } - @Beta // Not a stable API. May change in the future. + @Deprecated public static CallbackTessellator startCapturing(DirectDrawCallback callback) { return TessellatorManager.startCapturingDirect(callback); } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/ITessellatorInstance.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/ITessellatorInstance.java index 835e3387..c172d071 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/ITessellatorInstance.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/ITessellatorInstance.java @@ -3,6 +3,7 @@ package com.gtnewhorizon.gtnhlib.client.renderer; /** * Interface for Tessellator instances to support GTNHLib's capturing/compiling features. */ +@Deprecated public interface ITessellatorInstance { /** diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/MatrixHelper.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/MatrixHelper.java new file mode 100644 index 00000000..bd3d5ea9 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/MatrixHelper.java @@ -0,0 +1,50 @@ +package com.gtnewhorizon.gtnhlib.client.renderer; + +import org.joml.Matrix3f; +import org.joml.Matrix4f; +import org.joml.Matrix4fc; +import org.joml.Vector3f; +import org.joml.Vector4f; + +import com.gtnewhorizon.gtnhlib.client.model.NormalHelper; + +public final class MatrixHelper { + + private static final Matrix4fc IDENTITY = new Matrix4f(); + + public static boolean isIdentity(Matrix4f m) { + return (m.properties() & Matrix4f.PROPERTY_IDENTITY) != 0 || m.equals(IDENTITY, 1e-6f); + } + + public static void transformVertex(Matrix4f modelViewProjection, Vector4f dest) { + modelViewProjection.transform(dest); + + if (dest.w != 1.0f) { + dest.x /= dest.w; + dest.y /= dest.w; + dest.z /= dest.w; + } + } + + public static void transformUV(Matrix4f texture, Vector4f dest) { + texture.transform(dest); + + if (dest.w != 1.0f) { + dest.x /= dest.w; + dest.y /= dest.w; + } + } + + public static void transformColor(Matrix4f color, Vector4f dest) { + color.transform(dest); + } + + public static Matrix3f getNormalMatrix(Matrix4f modelviewMatrix, Matrix3f dest) { + return NormalHelper.getNormalMatrix(modelviewMatrix, dest); + } + + public static void transformNormal(Matrix3f normalMat, Vector3f dest) { + normalMat.transform(dest); + dest.normalize(); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager.java index 880ed73b..268bd4f6 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/TessellatorManager.java @@ -12,11 +12,11 @@ import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import com.github.bsideup.jabel.Desugar; -import com.google.common.annotations.Beta; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.line.ModelLine; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.primitive.ModelPrimitiveView; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.quad.ModelQuadViewMutable; import com.gtnewhorizon.gtnhlib.client.renderer.cel.model.tri.ModelTriangle; +import com.gtnewhorizon.gtnhlib.client.renderer.tessellator.TessellatorCallback; import com.gtnewhorizon.gtnhlib.client.renderer.vao.IVertexArrayObject; import com.gtnewhorizon.gtnhlib.client.renderer.vao.VAOManager; import com.gtnewhorizon.gtnhlib.client.renderer.vao.VertexBufferType; @@ -26,7 +26,7 @@ import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; @SuppressWarnings("unused") public class TessellatorManager { - private static final Logger LOGGER = LogManager.getLogger("TessellatorManager"); + public static final Logger LOGGER = LogManager.getLogger("TessellatorManager"); private enum CaptureMode { CAPTURING, // startCapturing() - batch mode until explicit stop @@ -77,6 +77,7 @@ public class TessellatorManager { private static final Thread mainThread = Thread.currentThread(); // Recursion protection (compiling is main-thread only) + @Deprecated private static boolean isInCompilingCallback = false; public static Tessellator get() { @@ -168,6 +169,7 @@ public class TessellatorManager { /** * Sets the compiling flag on vanilla Tessellator.instance if it implements ITessellatorInstance. */ + @Deprecated private static void setVanillaTessellatorCompiling(boolean compiling) { if (Tessellator.instance instanceof ITessellatorInstance tessInst) { tessInst.gtnhlib$setCompiling(compiling); @@ -281,7 +283,7 @@ public class TessellatorManager { public static final int DEFAULT_BUFFER_SIZE = 0x8000; // 32KB, enough to store 1024 full-sized vertices private static final int BUFFER_CAPACITY = Tessellator.byteBuffer.capacity(); - private static final int DIRECT_TESSELLATOR_STACK_DEPTH = 16; + public static final int DIRECT_TESSELLATOR_STACK_DEPTH = 16; // Instances to use for capturing to vbo's (Cannot be used outside the tessellator stack) private static final DirectTessellator mainInstance = new DirectTessellator(Tessellator.byteBuffer); @@ -304,6 +306,7 @@ public class TessellatorManager { private static void setDirectTessellator(DirectTessellator tessellator) { if (++directTessellatorIndex >= DIRECT_TESSELLATOR_STACK_DEPTH) { + directTessellatorIndex--; throw new IllegalStateException("DirectTessellator stack overflow"); } mainInstanceInStack = mainInstanceInStack || tessellator.baseBuffer == Tessellator.byteBuffer; @@ -336,16 +339,21 @@ public class TessellatorManager { return tessellator; } - @Beta // Not a stable API. May change in the future. + @Deprecated // Temporary method (to make older angelica version with newer gtnhlib versions) public static CallbackTessellator startCapturingDirect(DirectDrawCallback callback) { - if (!mainInstanceInStack) { - final CallbackTessellator tessellator = mainCallbackInstance; - tessellator.setDrawCallback(callback); - setDirectTessellator(tessellator); - return tessellator; - } - final CallbackTessellator tessellator = new CallbackTessellator(DEFAULT_BUFFER_SIZE); - tessellator.setDrawCallback(callback); + return startCapturingDirect(new TessellatorCallback() { + + @Override + public boolean onDraw(CallbackTessellator tessellator) { + return callback.onDraw(tessellator); + } + }); + } + + public static CallbackTessellator startCapturingDirect(TessellatorCallback callback) { + final CallbackTessellator tessellator = mainInstanceInStack ? new CallbackTessellator(DEFAULT_BUFFER_SIZE) + : mainCallbackInstance; + VertexCallbackManager.pushCallback(tessellator, callback); setDirectTessellator(tessellator); return tessellator; } @@ -576,7 +584,7 @@ public class TessellatorManager { final DirectTessellator tessellator = getDirectTessellator(); final int result = tessellator.interceptDraw(tess); - ((ITessellatorInstance) tess).discard(); + discardTessellator(tess); return result; } @@ -749,6 +757,7 @@ public class TessellatorManager { return result; } + @Deprecated public static void cleanup() { // Ensure we've cleaned everything up final CapturingTessellator tessellator = capturingTessellator.get(); @@ -847,4 +856,18 @@ public class TessellatorManager { tess.restoreTranslation(); } + public static void discardTessellator(Tessellator tessellator) { + tessellator.reset(); + tessellator.isDrawing = false; + tessellator.hasNormals = false; + tessellator.hasColor = false; + tessellator.hasTexture = false; + tessellator.hasBrightness = false; + tessellator.isColorDisabled = false; + } + + public static Tessellator getVanillaTessellator() { + return Tessellator.instance; + } + } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.java new file mode 100644 index 00000000..c04ac3e0 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/VertexCallbackManager.java @@ -0,0 +1,45 @@ +package com.gtnewhorizon.gtnhlib.client.renderer; + +import static com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager.DIRECT_TESSELLATOR_STACK_DEPTH; + +import com.gtnewhorizon.gtnhlib.client.renderer.tessellator.TessellatorCallback; + +public final class VertexCallbackManager { + + public static final TessellatorCallback DEFAULT = new TessellatorCallback() {}; + + static TessellatorCallback callback; + + private static final TessellatorCallback[] tessellatorCallbacks; + private static int callbackIndex = 0; // Points to the TessellatorCallback in the stack + + static { + tessellatorCallbacks = new TessellatorCallback[DIRECT_TESSELLATOR_STACK_DEPTH]; + tessellatorCallbacks[0] = DEFAULT; + callback = DEFAULT; + } + + private static void updateCallback() { + callback = tessellatorCallbacks[callbackIndex]; + } + + public static void pushCallback(CallbackTessellator parent, TessellatorCallback callback) { + if (++callbackIndex >= DIRECT_TESSELLATOR_STACK_DEPTH) { + callbackIndex--; + throw new IllegalStateException("VertexCallbackManager stack overflow"); + } + if (parent != null) { + parent.needsPopCallback = true; + } + tessellatorCallbacks[callbackIndex] = callback; + updateCallback(); + } + + public static void popCallback() { + if (callbackIndex == 0) { + throw new IllegalStateException("Cannot call popCallback if there are no callbacks!"); + } + tessellatorCallbacks[callbackIndex--] = null; + updateCallback(); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/shader/AutoShaderUpdater.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/shader/AutoShaderUpdater.java index 759f34f3..1a437f15 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/shader/AutoShaderUpdater.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/shader/AutoShaderUpdater.java @@ -160,17 +160,21 @@ public class AutoShaderUpdater { runnable.run(shader, vertexPath, fragmentPath); ShaderProgram.clear(); } - Minecraft.getMinecraft().thePlayer.addChatMessage( - new ChatComponentText("Updated Shaders " + rawVertexPath + " and " + rawFragmentPath)); + if (Minecraft.getMinecraft().thePlayer != null) { + Minecraft.getMinecraft().thePlayer.addChatMessage( + new ChatComponentText("Updated Shaders " + rawVertexPath + " and " + rawFragmentPath)); + } } catch (Exception e) { e.printStackTrace(); - Minecraft.getMinecraft().thePlayer.addChatMessage( - new ChatComponentText( - EnumChatFormatting.RED + "Failed to update " - + rawVertexPath - + " or " - + rawFragmentPath - + ". See logs for more info.")); + if (Minecraft.getMinecraft().thePlayer != null) { + Minecraft.getMinecraft().thePlayer.addChatMessage( + new ChatComponentText( + EnumChatFormatting.RED + "Failed to update " + + rawVertexPath + + " or " + + rawFragmentPath + + ". See logs for more info.")); + } } } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/TessellatorCallback.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/TessellatorCallback.java new file mode 100644 index 00000000..939ecfcf --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/TessellatorCallback.java @@ -0,0 +1,31 @@ +package com.gtnewhorizon.gtnhlib.client.renderer.tessellator; + +import com.gtnewhorizon.gtnhlib.client.renderer.CallbackTessellator; + +public interface TessellatorCallback { + + default boolean onStartDrawing(CallbackTessellator tessellator, int drawMode) { + return true; + } + + default boolean onDraw(CallbackTessellator tessellator) { + return true; + } + + default void onVertex(CallbackTessellator tessellator, double x, double y, double z) { + tessellator + .writeVertex(x + tessellator.getXOffset(), y + tessellator.getYOffset(), z + tessellator.getZOffset()); + } + + default void onTextureUV(CallbackTessellator tessellator, double u, double v) { + tessellator.writeTextureUV(u, v); + } + + default void onColor(CallbackTessellator tessellator, int red, int green, int blue, int alpha) { + tessellator.writeColor(red, green, blue, alpha); + } + + default void onNormal(CallbackTessellator tessellator, float nx, float ny, float nz) { + tessellator.writeNormal(nx, ny, nz); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/VertexTransformCallback.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/VertexTransformCallback.java new file mode 100644 index 00000000..1fccc64a --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/tessellator/VertexTransformCallback.java @@ -0,0 +1,95 @@ +package com.gtnewhorizon.gtnhlib.client.renderer.tessellator; + +import org.joml.Matrix3f; +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.joml.Vector4f; + +import com.gtnewhorizon.gtnhlib.client.renderer.CallbackTessellator; +import com.gtnewhorizon.gtnhlib.client.renderer.MatrixHelper; + +public abstract class VertexTransformCallback implements TessellatorCallback { + + protected final Vector4f vec4 = new Vector4f(); + protected final Vector3f normalVec = new Vector3f(); + + protected abstract Matrix4f getMVPMatrix(); + + protected abstract Matrix4f getTextureMatrix(); + + protected abstract Matrix4f getColorMatrix(); + + protected abstract Matrix3f getNormalMatrix(); + + @Override + public void onVertex(CallbackTessellator tessellator, double x, double y, double z) { + final Matrix4f mvpMatrix = getMVPMatrix(); + if (mvpMatrix == null) { + TessellatorCallback.super.onVertex(tessellator, x, y, z); + return; + } + vec4.x = (float) (x + tessellator.xOffset); + vec4.y = (float) (y + tessellator.yOffset); + vec4.z = (float) (z + tessellator.zOffset); + vec4.w = 1; + + MatrixHelper.transformVertex(mvpMatrix, vec4); + + tessellator.writeVertex(vec4.x, vec4.y, vec4.z); + } + + @Override + public void onTextureUV(CallbackTessellator tessellator, double u, double v) { + final Matrix4f textureMatrix = getTextureMatrix(); + if (textureMatrix == null) { + TessellatorCallback.super.onTextureUV(tessellator, u, v); + return; + } + vec4.x = (float) u; + vec4.y = (float) v; + vec4.z = 0; + vec4.w = 1; + + MatrixHelper.transformUV(textureMatrix, vec4); + + tessellator.writeTextureUV(vec4.x, vec4.y); + } + + @Override + public void onColor(CallbackTessellator tessellator, int red, int green, int blue, int alpha) { + final Matrix4f colorMatrix = getColorMatrix(); + if (colorMatrix == null) { + TessellatorCallback.super.onColor(tessellator, red, green, blue, alpha); + return; + } + vec4.x = red / 255f; + vec4.y = green / 255f; + vec4.z = blue / 255f; + vec4.w = alpha / 255f; + + MatrixHelper.transformColor(colorMatrix, vec4); + + tessellator.writeColor(convertColor(vec4.x), convertColor(vec4.y), convertColor(vec4.z), convertColor(vec4.w)); + } + + private static int convertColor(float v) { + return Math.max(0, Math.min(255, Math.round(v * 255f))); + } + + @Override + public void onNormal(CallbackTessellator tessellator, float nx, float ny, float nz) { + final Matrix3f normalMatrix = getNormalMatrix(); + if (normalMatrix == null) { + TessellatorCallback.super.onNormal(tessellator, nx, ny, nz); + return; + } + + normalVec.x = nx; + normalVec.y = ny; + normalVec.z = nz; + + MatrixHelper.transformNormal(normalMatrix, normalVec); + + tessellator.writeNormal(normalVec.x, normalVec.y, normalVec.z); + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/DefaultVertexFormat.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/DefaultVertexFormat.java index 3291233c..867e0b36 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/DefaultVertexFormat.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/DefaultVertexFormat.java @@ -59,70 +59,76 @@ public final class DefaultVertexFormat { // --------------- FORMATS --------------- - public static final VertexFormat POSITION = new VertexFormat(POSITION_ELEMENT); + public static final VertexFormat POSITION = registerFormat(POSITION_ELEMENT); // 2 - public static final VertexFormat POSITION_TEXTURE = new VertexFormat(POSITION_ELEMENT, TEXTURE_ELEMENT); - public static final VertexFormat POSITION_COLOR = new VertexFormat(POSITION_ELEMENT, COLOR_ELEMENT); - public static final VertexFormat POSITION_NORMAL = new VertexFormat(POSITION_ELEMENT, NORMAL_ELEMENT); - public static final VertexFormat POSITION_LIGHT = new VertexFormat(POSITION_ELEMENT, LIGHT_ELEMENT); + public static final VertexFormat POSITION_TEXTURE = registerFormat(POSITION_ELEMENT, TEXTURE_ELEMENT); + public static final VertexFormat POSITION_COLOR = registerFormat(POSITION_ELEMENT, COLOR_ELEMENT); + public static final VertexFormat POSITION_NORMAL = registerFormat(POSITION_ELEMENT, NORMAL_ELEMENT); + public static final VertexFormat POSITION_LIGHT = registerFormat(POSITION_ELEMENT, LIGHT_ELEMENT); // 3 - public static final VertexFormat POSITION_TEXTURE_NORMAL = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_NORMAL = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, NORMAL_ELEMENT); - public static final VertexFormat POSITION_TEXTURE_COLOR = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_COLOR = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, COLOR_ELEMENT); - public static final VertexFormat POSITION_TEXTURE_LIGHT = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_LIGHT = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, LIGHT_ELEMENT); - public static final VertexFormat POSITION_COLOR_LIGHT = new VertexFormat( + public static final VertexFormat POSITION_COLOR_LIGHT = registerFormat( POSITION_ELEMENT, COLOR_ELEMENT, LIGHT_ELEMENT); - public static final VertexFormat POSITION_NORMAL_COLOR = new VertexFormat( + public static final VertexFormat POSITION_NORMAL_COLOR = registerFormat( POSITION_ELEMENT, NORMAL_ELEMENT, COLOR_ELEMENT); - public static final VertexFormat POSITION_NORMAL_LIGHT = new VertexFormat( + public static final VertexFormat POSITION_NORMAL_LIGHT = registerFormat( POSITION_ELEMENT, NORMAL_ELEMENT, LIGHT_ELEMENT); // 4 - public static final VertexFormat POSITION_TEXTURE_LIGHT_NORMAL = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_LIGHT_NORMAL = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, LIGHT_ELEMENT, NORMAL_ELEMENT); - public static final VertexFormat POSITION_TEXTURE_COLOR_LIGHT = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_COLOR_LIGHT = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, COLOR_ELEMENT, LIGHT_ELEMENT); - public static final VertexFormat POSITION_TEXTURE_COLOR_NORMAL = new VertexFormat( + public static final VertexFormat POSITION_TEXTURE_COLOR_NORMAL = registerFormat( POSITION_ELEMENT, TEXTURE_ELEMENT, COLOR_ELEMENT, NORMAL_ELEMENT); - public static final VertexFormat POSITION_COLOR_LIGHT_NORMAL = new VertexFormat( + public static final VertexFormat POSITION_COLOR_LIGHT_NORMAL = registerFormat( POSITION_ELEMENT, COLOR_ELEMENT, LIGHT_ELEMENT, NORMAL_ELEMENT); // All - public static final VertexFormat POSITION_COLOR_TEXTURE_LIGHT_NORMAL = new VertexFormat( + public static final VertexFormat POSITION_COLOR_TEXTURE_LIGHT_NORMAL = registerFormat( POSITION_ELEMENT, COLOR_ELEMENT, TEXTURE_ELEMENT, LIGHT_ELEMENT, NORMAL_ELEMENT); + private static VertexFormat registerFormat(VertexFormatElement... elements) { + final VertexFormat format = new VertexFormat(elements); + ALL_FORMATS[format.vertexFlags] = format; + return format; + } + // Duplicate of POSITION_TEXTURE_COLOR @Deprecated public static final VertexFormat POSITION_COLOR_TEXTURE = POSITION_TEXTURE_COLOR; diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/VertexFormat.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/VertexFormat.java index 6b2c50d5..e7462948 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/VertexFormat.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/renderer/vertex/VertexFormat.java @@ -31,10 +31,12 @@ public class VertexFormat { protected final int vertexFlags; + @Deprecated public static void registerSetupBufferStateOverride(SetupBufferState override) { setupBufferStateOverrride.add(override); } + @Deprecated public static void registerClearBufferStateOverride(ClearBufferState override) { clearBufferStateOverrride.add(override); } @@ -56,8 +58,6 @@ public class VertexFormat { this.elements = new ObjectImmutableList<>(elements); this.vertexSize = offset; - - DefaultVertexFormat.ALL_FORMATS[this.vertexFlags] = this; } public void setupBufferState(long l) { @@ -230,6 +230,28 @@ public class VertexFormat { return (vertexFlags & BRIGHTNESS_BIT) != 0; } + public final VertexFormat getSharedFormat(VertexFormat other) { + return VertexFlags.getFormat( + this.hasTexture() || other.hasTexture(), + this.hasColor() || other.hasColor(), + this.hasNormals() || other.hasNormals(), + this.hasBrightness() || other.hasBrightness()); + } + + @Override + public String toString() { + return "VertexFormat[size=" + vertexSize + + " hasTexture=" + + hasTexture() + + " hasColor=" + + hasColor() + + " hasNormals=" + + hasNormals() + + " hasBrightness=" + + hasBrightness() + + "]"; + } + @FunctionalInterface public interface SetupBufferState { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/util/font/GlyphReplacements.java b/src/main/java/com/gtnewhorizon/gtnhlib/util/font/GlyphReplacements.java index 2bdead7d..ea2c8d1f 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/util/font/GlyphReplacements.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/util/font/GlyphReplacements.java @@ -2,21 +2,40 @@ package com.gtnewhorizon.gtnhlib.util.font; import java.util.HashMap; -public class GlyphReplacements { +import it.unimi.dsi.fastutil.chars.Char2CharOpenHashMap; + +public final class GlyphReplacements { /** * A mapping from custom glyphs to the values they are meant to replace (which may be null for novel glyphs or when * the original comes from outside the BMP). */ + @Deprecated public static final HashMap customGlyphs = new HashMap<>(64); + private static final Char2CharOpenHashMap customGlyphsNew = new Char2CharOpenHashMap(); + + @Deprecated // Use registerCustomGlyph(String replacement, char original) instead public static String registerCustomGlyph(String replacement, String original) { customGlyphs.put(replacement, original); + customGlyphsNew.put(replacement.charAt(0), original.charAt(0)); return replacement; } + @Deprecated // Does nothing, can be removed. public static String registerCustomGlyph(String replacement) { - customGlyphs.put(replacement, null); return replacement; } + + public static String registerCustomGlyph(String replacement, char original) { + customGlyphsNew.put(replacement.charAt(0), original); + return replacement; + } + + /** + * Gets a replacement glyph from the Map. If the char does not have a replacement glyph, it'll return 0 instead. + */ + public static char getReplacementGlyph(char ch) { + return customGlyphsNew.get(ch); + } } diff --git a/src/test/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellatorTest.java b/src/test/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellatorTest.java index d1ff7941..25f346c8 100644 --- a/src/test/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellatorTest.java +++ b/src/test/java/com/gtnewhorizon/gtnhlib/client/renderer/DirectTessellatorTest.java @@ -13,6 +13,7 @@ import org.joml.Matrix4fc; import org.joml.Vector3f; import org.junit.jupiter.api.*; +import com.gtnewhorizon.gtnhlib.client.renderer.tessellator.TessellatorCallback; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.DefaultVertexFormat; import com.gtnewhorizon.gtnhlib.client.renderer.vertex.VertexFormat; @@ -186,9 +187,13 @@ public class DirectTessellatorTest { AtomicBoolean callbackCalled = new AtomicBoolean(false); final CallbackTessellator tess = new CallbackTessellator(initialBuffer); - tess.setDrawCallback(t -> { - callbackCalled.set(true); - return true; // request reset + VertexCallbackManager.pushCallback(tess, new TessellatorCallback() { + + @Override + public boolean onDraw(CallbackTessellator tessellator) { + callbackCalled.set(true); + return true; // request reset + } }); tess.startDrawing(0); @@ -200,13 +205,20 @@ public class DirectTessellatorTest { assertEquals(0, tess.vertexCount); assertEquals(tess.startPtr, tess.writePtr); assertTrue(bytes > 0); + VertexCallbackManager.popCallback(); } @Test void testInterceptDrawCopiesVertices() { Tessellator vanilla = Tessellator.instance; final CallbackTessellator tess = new CallbackTessellator(initialBuffer); - tess.setDrawCallback((t) -> false); + VertexCallbackManager.pushCallback(tess, new TessellatorCallback() { + + @Override + public boolean onDraw(CallbackTessellator tessellator) { + return false; + } + }); vanilla.startDrawing(0); vanilla.addVertex(1, 2, 3); vanilla.addVertex(4, 5, 6); @@ -214,7 +226,7 @@ public class DirectTessellatorTest { int bytes = tess.interceptDraw(vanilla); assertEquals(2, tess.vertexCount); - assertTrue(bytes > 0); + assertEquals(0, bytes); long ptr = tess.startPtr; @@ -227,6 +239,7 @@ public class DirectTessellatorTest { assertEquals(4f, memGetFloat(ptr), 0.0001f); assertEquals(5f, memGetFloat(ptr + 4), 0.0001f); assertEquals(6f, memGetFloat(ptr + 8), 0.0001f); + VertexCallbackManager.popCallback(); } @Test @@ -265,6 +278,7 @@ public class DirectTessellatorTest { @Test void testPreDefinedVertexFormat() { + assertFalse(tess.hasColor); tess.setVertexFormat(DefaultVertexFormat.POSITION); tess.startDrawing(0); tess.setColorRGBA(255, 255, 255, 255);