Fixed MatrixStackRef

This commit is contained in:
2026-03-25 16:10:41 +01:00
parent a004585cba
commit c771764251
2 changed files with 84 additions and 50 deletions
@@ -1,27 +1,79 @@
package org.saturnclient.impl.ref; package org.saturnclient.impl.ref;
import java.util.ArrayDeque;
import java.util.Deque;
import org.joml.Matrix3x2fStack; import org.joml.Matrix3x2fStack;
import org.saturnclient.common.ref.render.MatrixStackRef; import org.saturnclient.common.ref.render.MatrixStackRef;
import org.saturnclient.common.ref.render.QuaternionfRef; import org.saturnclient.common.ref.render.QuaternionfRef;
public class Matrix3x2fStackRef implements MatrixStackRef { public class Matrix3x2fStackRef implements MatrixStackRef {
private final Matrix3x2fStack stack; public final Matrix3x2fStack stack;
// Our custom stack stores the reverse transforms
private final Deque<Transform> customStack = new ArrayDeque<>();
public Matrix3x2fStackRef(Matrix3x2fStack stack) { public Matrix3x2fStackRef(Matrix3x2fStack stack) {
this.stack = stack; this.stack = stack;
} }
@Override
public void push() {
// Push a new empty Transform
customStack.push(new Transform());
}
@Override
public void pop() {
if (customStack.isEmpty())
return;
// Apply inverse transforms in reverse order
Transform t = customStack.pop();
if (t.scaleX != 1 || t.scaleY != 1)
stack.scale(1f / t.scaleX, 1f / t.scaleY);
stack.translate(-t.translateX, -t.translateY);
// rotation inversion can be added here if needed
}
@Override @Override
public void scale(float x, float y, float z) { public void scale(float x, float y, float z) {
stack.scale(x, y); stack.scale(x, y);
if (!customStack.isEmpty()) {
Transform t = customStack.peek();
t.scaleX *= x;
t.scaleY *= y;
}
}
@Override
public void translate(float x, float y, float z) {
stack.translate(x, y);
if (!customStack.isEmpty()) {
Transform t = customStack.peek();
t.translateX += x;
t.translateY += y;
}
}
@Override
public void translate(double x, double y, double z) {
translate((float) x, (float) y, (float) z);
} }
@Override @Override
public void multiply(QuaternionfRef quaternion) { public void multiply(QuaternionfRef quaternion) {
// Not implemented yet
} }
@Override @Override
public void multiply(QuaternionfRef quaternion, float originX, float originY, float originZ) { public void multiply(QuaternionfRef quaternion, float originX, float originY, float originZ) {
// Not implemented yet
}
@Override
public void loadIdentity() {
customStack.clear();
} }
@Override @Override
@@ -31,30 +83,12 @@ public class Matrix3x2fStackRef implements MatrixStackRef {
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return false; return customStack.isEmpty();
} }
@Override private static class Transform {
public void loadIdentity() { float scaleX = 1f, scaleY = 1f;
} float translateX = 0f, translateY = 0f;
// rotation can be added if needed
@Override
public void pop() {
stack.popMatrix();
}
@Override
public void push() {
stack.pushMatrix();
}
@Override
public void translate(float x, float y, float z) {
stack.translate(x, y);
}
@Override
public void translate(double x, double y, double z) {
stack.translate((float) x, (float) y);
} }
} }
@@ -49,7 +49,7 @@ import net.minecraft.util.crash.CrashReportSection;
import net.minecraft.world.World; import net.minecraft.world.World;
public class RenderScopeImpl implements RenderScope { public class RenderScopeImpl implements RenderScope {
private final Matrix3x2fStack matrices; private final Matrix3x2fStackRef matrices;
private final GuiAtlasManager guiAtlasManager; private final GuiAtlasManager guiAtlasManager;
public final GuiRenderState state; public final GuiRenderState state;
@@ -63,7 +63,7 @@ public class RenderScopeImpl implements RenderScope {
public RenderScopeImpl(Matrix3x2fStack matrices, GuiRenderState state) { public RenderScopeImpl(Matrix3x2fStack matrices, GuiRenderState state) {
this.state = state; this.state = state;
this.matrices = matrices; this.matrices = new Matrix3x2fStackRef(matrices);
this.guiAtlasManager = SaturnClient.client.getGuiAtlasManager(); this.guiAtlasManager = SaturnClient.client.getGuiAtlasManager();
this.scissorStack = new ScissorStack(); this.scissorStack = new ScissorStack();
this.itemRenderState = new ItemRenderState(); this.itemRenderState = new ItemRenderState();
@@ -71,7 +71,7 @@ public class RenderScopeImpl implements RenderScope {
@Override @Override
public MatrixStackRef getMatrixStack() { public MatrixStackRef getMatrixStack() {
return new Matrix3x2fStackRef(matrices); return matrices;
} }
@Override @Override
@@ -91,7 +91,7 @@ public class RenderScopeImpl implements RenderScope {
public void fill(int x1, int y1, int x2, int y2, int color) { public void fill(int x1, int y1, int x2, int y2, int color) {
this.state.addSimpleElement( this.state.addSimpleElement(
new ColoredQuadGuiElementRenderState(RenderPipelines.GUI, TextureSetup.empty(), new ColoredQuadGuiElementRenderState(RenderPipelines.GUI, TextureSetup.empty(),
new Matrix3x2f(this.matrices), new Matrix3x2f(this.matrices.stack),
x1, y1, x2, x1, y1, x2,
y2, color, color, this.scissorStack.peekLast())); y2, color, color, this.scissorStack.peekLast()));
} }
@@ -143,12 +143,12 @@ public class RenderScopeImpl implements RenderScope {
for (String line : text.split("\n")) { for (String line : text.split("\n")) {
color = getColor(color); color = getColor(color);
this.state.addText(new TextGuiElementRenderState(SaturnClient.client.textRenderer, this.state.addText(new TextGuiElementRenderState(SaturnClient.client.textRenderer,
((Text) Fonts.setFont(line, font)).asOrderedText(), new Matrix3x2f(this.matrices), x, y, ((Text) Fonts.setFont(line, font)).asOrderedText(), new Matrix3x2f(this.matrices.stack), x, y,
color, 0, false, this.scissorStack.peekLast())); color, 0, false, this.scissorStack.peekLast()));
matrices.pushMatrix(); matrices.push();
matrices.translate(x, y + (i * Fonts.getHeight())); matrices.translate(x, y + (i * Fonts.getHeight()), 0);
matrices.scale(scale, scale); matrices.scale(scale, scale, 0);
i++; i++;
} }
} }
@@ -172,28 +172,28 @@ public class RenderScopeImpl implements RenderScope {
private void drawRoundedSide(int cornerWidth, int cornerHeight, int radius, int color) { private void drawRoundedSide(int cornerWidth, int cornerHeight, int radius, int color) {
// Top // Top
this.matrices.pushMatrix(); this.matrices.push();
this.matrices.scale(0.05f, 0.05f); this.matrices.scale(0.05f, 0.05f, 0);
this.drawRoundedCorner(cornerWidth, cornerHeight, radius * 10, color); this.drawRoundedCorner(cornerWidth, cornerHeight, radius * 10, color);
this.matrices.popMatrix(); this.matrices.pop();
// Bottom // Bottom
this.matrices.pushMatrix(); this.matrices.push();
this.matrices.translate(cornerWidth, cornerHeight * 2); this.matrices.translate(cornerWidth, cornerHeight * 2, 0);
this.matrices.rotate((float) Math.toRadians(90)); this.matrices.stack.rotate((float) Math.toRadians(90));
this.matrices.translate(0, -cornerWidth); this.matrices.translate(0, -cornerWidth, 0);
this.matrices.scale(0.05f, 0.05f); this.matrices.scale(0.05f, 0.05f, 0);
this.drawRoundedCorner(cornerHeight, cornerWidth, radius * 10, color); this.drawRoundedCorner(cornerHeight, cornerWidth, radius * 10, color);
this.matrices.popMatrix(); this.matrices.pop();
} }
@Override @Override
@@ -204,21 +204,21 @@ public class RenderScopeImpl implements RenderScope {
int cornerWidth = width / 2; int cornerWidth = width / 2;
int cornerHeight = height / 2; int cornerHeight = height / 2;
this.matrices.pushMatrix(); this.matrices.push();
this.matrices.translate(x, y); this.matrices.translate(x, y, 0);
this.drawRoundedSide(cornerWidth, cornerHeight, radius, color); this.drawRoundedSide(cornerWidth, cornerHeight, radius, color);
this.matrices.translate(cornerWidth * 2, cornerHeight * 2); this.matrices.translate(cornerWidth * 2, cornerHeight * 2, 0);
this.matrices.rotate((float) Math.toRadians(90)); this.matrices.stack.rotate((float) Math.toRadians(90));
this.matrices.translate(0, 0); this.matrices.translate(0, 0, 0);
this.drawRoundedSide(cornerWidth, cornerHeight, radius, color); this.drawRoundedSide(cornerWidth, cornerHeight, radius, color);
this.matrices.popMatrix(); this.matrices.pop();
} }
@Override @Override
@@ -300,14 +300,14 @@ public class RenderScopeImpl implements RenderScope {
} }
this.state.addSimpleElement(new TexturedQuadGuiElementRenderState(RenderPipelines.GUI, this.state.addSimpleElement(new TexturedQuadGuiElementRenderState(RenderPipelines.GUI,
TextureSetup.withoutGlTexture(gpuTextureView), new Matrix3x2f(this.matrices), x1, y1, x2, y2, u1, u2, TextureSetup.withoutGlTexture(gpuTextureView), new Matrix3x2f(this.matrices.stack), x1, y1, x2, y2, u1, u2,
v1, v2, v1, v2,
color, this.scissorStack.peekLast())); color, this.scissorStack.peekLast()));
} }
@Override @Override
public void enableScissor(int x1, int y1, int x2, int y2) { public void enableScissor(int x1, int y1, int x2, int y2) {
ScreenRect screenRect = (new ScreenRect(x1, y1, x2 - x1, y2 - y1)).transform(this.matrices); ScreenRect screenRect = (new ScreenRect(x1, y1, x2 - x1, y2 - y1)).transform(this.matrices.stack);
this.scissorStack.push(screenRect); this.scissorStack.push(screenRect);
} }
@@ -400,7 +400,7 @@ public class RenderScopeImpl implements RenderScope {
try { try {
this.state.addItem(new ItemGuiElementRenderState(stack.getItem().getName().toString(), this.state.addItem(new ItemGuiElementRenderState(stack.getItem().getName().toString(),
new Matrix3x2f(this.matrices), itemRenderState, x, y, this.scissorStack.peekLast())); new Matrix3x2f(this.matrices.stack), itemRenderState, x, y, this.scissorStack.peekLast()));
} catch (Throwable throwable) { } catch (Throwable throwable) {
CrashReport crashReport = CrashReport.create(throwable, "Rendering item"); CrashReport crashReport = CrashReport.create(throwable, "Rendering item");
CrashReportSection crashReportSection = crashReport.addElement("Item being rendered"); CrashReportSection crashReportSection = crashReport.addElement("Item being rendered");