From 466b0544e30626904a070fac02f0642bb8de31dd Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Fri, 2 Feb 2024 08:31:52 -0800 Subject: [PATCH] Add a Vector3dStack - Fix nested translations being stored --- .../client/renderer/CapturingTessellator.java | 13 +++---- .../angelica/glsm/stacks/Vector3dStack.java | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Vector3dStack.java diff --git a/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java b/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java index da3c14fb3..a0316b29a 100644 --- a/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java +++ b/src/main/java/com/gtnewhorizons/angelica/client/renderer/CapturingTessellator.java @@ -3,13 +3,13 @@ package com.gtnewhorizons.angelica.client.renderer; import com.gtnewhorizons.angelica.compat.mojang.BlockPos; import com.gtnewhorizons.angelica.compat.mojang.VertexFormat; import com.gtnewhorizons.angelica.compat.nd.Quad; +import com.gtnewhorizons.angelica.glsm.stacks.Vector3dStack; import com.gtnewhorizons.angelica.mixins.interfaces.ITessellatorInstance; import com.gtnewhorizons.angelica.utils.ObjectPooler; import cpw.mods.fml.relauncher.ReflectionHelper; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import me.jellysquid.mods.sodium.client.render.pipeline.BlockRenderer; import net.minecraft.client.renderer.Tessellator; -import org.joml.Vector3d; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; @@ -45,8 +45,7 @@ public class CapturingTessellator extends Tessellator implements ITessellatorIns // Any offset we need to the Tesselator's offset! private final BlockPos offset = new BlockPos(); - private final Vector3d storedTranslation = new Vector3d(); - private boolean translationStored = false; + private final Vector3dStack storedTranslation = new Vector3dStack(); public void setOffset(BlockPos pos) { this.offset.set(pos); @@ -142,19 +141,17 @@ public class CapturingTessellator extends Tessellator implements ITessellatorIns } public void storeTranslation() { - if(translationStored) throw new IllegalStateException("Translation already stored!"); - translationStored = true; + storedTranslation.push(); + this.storedTranslation.set(xOffset, yOffset, zOffset); } public void restoreTranslation() { - if(!translationStored) throw new IllegalStateException("Translation not stored!"); xOffset = storedTranslation.x; yOffset = storedTranslation.y; zOffset = storedTranslation.z; - storedTranslation.zero(); - translationStored = false; + storedTranslation.pop(); } public static int createBrightness(int sky, int block) { diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Vector3dStack.java b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Vector3dStack.java new file mode 100644 index 000000000..1bdef20ae --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Vector3dStack.java @@ -0,0 +1,35 @@ +package com.gtnewhorizons.angelica.glsm.stacks; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.joml.Vector3d; + +public class Vector3dStack extends Vector3d implements IStateStack { + protected final Vector3d[] stack; + + protected int pointer; + + public Vector3dStack() { + stack = new Vector3d[GLStateManager.MAX_ATTRIB_STACK_DEPTH]; + for (int i = 0; i < GLStateManager.MAX_ATTRIB_STACK_DEPTH; i++) { + stack[i] = new Vector3d(); + } + } + + @Override + public Vector3dStack push() { + if(pointer == stack.length) { + throw new IllegalStateException("Stack overflow size " + (pointer + 1) + " reached"); + } + stack[pointer++].set(this); + return this; + } + + @Override + public Vector3dStack pop() { + if(pointer == 0) { + throw new IllegalStateException("Stack underflow"); + } + set(stack[--pointer]); + return this; + } +}