Skip to content

Commit fc1dd0f

Browse files
committed
fix scissoring on GLRenderer
1 parent c0f952a commit fc1dd0f

File tree

5 files changed

+53
-32
lines changed

5 files changed

+53
-32
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ kotlinx-coroutines = "1.10.2"
77
kotlinx-atomicfu = "0.29.0"
88
fabric-language-kotlin = "1.13.5+kotlin.2.2.10"
99
google-ksp = "2.2.10-2.0.2"
10-
polyui = "2.0.6"
10+
polyui = "2.0.7"
1111
annotations = "24.1.0"
1212
hypixel-modapi = "1.0.1"
1313
hypixel-data = "0.2.0" # Dep of hypixel-modapi

minecraft/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import dev.deftu.omnicore.api.client.render.OmniRenderingContext;
3030
import net.minecraft.client.Minecraft;
3131
import net.minecraft.client.gui.GuiScreen;
32+
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
34+
import org.lwjgl.BufferUtils;
3335
import org.polyfrost.oneconfig.api.event.v1.EventDelay;
3436
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud;
3537
import org.polyfrost.oneconfig.api.platform.v1.ScreenPlatform;
@@ -38,13 +40,23 @@
3840
import org.polyfrost.polyui.component.Component;
3941
import org.polyfrost.polyui.component.Drawable;
4042

43+
import java.nio.Buffer;
44+
import java.nio.IntBuffer;
4145
import java.util.List;
4246

4347
public class ScreenPlatformImpl implements ScreenPlatform {
4448
// //#if MC > 1.13
4549
// //$$ private final float[] pixelScaleFactor = new float[1];
4650
// //#endif
4751

52+
private final IntBuffer VIEWPORT = BufferUtils.createIntBuffer(
53+
//#if MC >= 1.13
54+
//$$ 4
55+
//#else
56+
16
57+
//#endif
58+
);
59+
4860
@Override
4961
public int viewportWidth() {
5062
//#if MC>=11502
@@ -81,6 +93,19 @@ public int windowHeight() {
8193
//#endif
8294
}
8395

96+
@Override
97+
public int @NotNull [] glViewport(int @Nullable [] in) {
98+
((Buffer) VIEWPORT).clear();
99+
//#if MC >= 1.13
100+
//$$ org.lwjgl.opengl.GL11.glGetIntegerv(org.lwjgl.opengl.GL11.GL_VIEWPORT, VIEWPORT);
101+
//#else
102+
org.lwjgl.opengl.GL11.glGetInteger(org.lwjgl.opengl.GL11.GL_VIEWPORT, VIEWPORT);
103+
//#endif
104+
int[] out = in != null ? in : new int[4];
105+
VIEWPORT.get(out, 0, 4);
106+
return out;
107+
}
108+
84109
public void renderLegacyHuds(OmniRenderingContext ctx) {
85110
PolyUI defaultInstance = UIManager.INSTANCE.getDefaultInstance();
86111
Drawable master = defaultInstance.getMaster();

minecraft/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/PolyUIScreen.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.apache.logging.log4j.Logger;
4040
import org.jetbrains.annotations.MustBeInvokedByOverriders;
4141
import org.jetbrains.annotations.NotNull;
42-
import org.lwjgl.BufferUtils;
4342
import org.polyfrost.oneconfig.api.platform.v1.Platform;
4443
import org.polyfrost.oneconfig.api.ui.v1.Notifications;
4544
import org.polyfrost.oneconfig.api.ui.v1.UIManager;
@@ -48,8 +47,6 @@
4847
import org.polyfrost.polyui.component.Drawable;
4948
import org.polyfrost.polyui.data.Cursor;
5049

51-
import java.nio.Buffer;
52-
import java.nio.IntBuffer;
5350
import java.util.function.Consumer;
5451

5552
import static org.lwjgl.opengl.GL11.*;
@@ -65,13 +62,7 @@ public class PolyUIScreen extends OmniScreen implements BlurScreen {
6562
private final float designedWidth, designedHeight, initialWidth, initialHeight;
6663
private final boolean pauses, blurs;
6764
private final Consumer<PolyUI> close;
68-
private final IntBuffer VIEWPORT = BufferUtils.createIntBuffer(
69-
//#if MC >= 1.13
70-
//$$ 4
71-
//#else
72-
16
73-
//#endif
74-
);
65+
private final int[] viewport = new int[4];
7566

7667
//#if MC < 1.13
7768
private int mx, my;
@@ -130,20 +121,15 @@ public void onRender(@NotNull OmniRenderingContext ctx, int mouseX, int mouseY,
130121
//#if MC >= 1.21.5
131122
//$$ kotlin.jvm.functions.Function0<kotlin.Unit> unbind = dev.deftu.omnicore.api.client.framebuffer.OmniFramebuffers.getMain().bind();
132123
//#endif
133-
((Buffer) VIEWPORT).clear();
134-
//#if MC >= 1.13
135-
//$$ glGetIntegerv(GL_VIEWPORT, VIEWPORT);
136-
//#else
137-
glGetInteger(GL_VIEWPORT, VIEWPORT);
138-
//#endif
124+
Platform.screen().glViewport(viewport);
139125
float factor = Platform.screen().pixelRatio();
140126
int w = (int) (polyUI.getMaster().getWidth() * factor);
141127
int h = (int) (polyUI.getMaster().getHeight() * factor);
142128
int x = Platform.screen().viewportWidth() / 2 - w / 2;
143129
int y = Platform.screen().viewportHeight() / 2 - h / 2;
144130
glViewport(x, y, w, h);
145131
polyUI.render();
146-
glViewport(VIEWPORT.get(), VIEWPORT.get(), VIEWPORT.get(), VIEWPORT.get());
132+
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
147133
//#if MC >= 1.21.5
148134
//$$ unbind.invoke();
149135
//#endif

modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/internal/GLRendererImpl.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.lwjgl.opengl.GL13.*
1111
import org.lwjgl.opengl.GL14.*
1212
import org.lwjgl.opengl.GL15.*
1313
import org.lwjgl.opengl.GL20.*
14+
import org.polyfrost.oneconfig.api.platform.v1.Platform
1415
import org.polyfrost.oneconfig.api.ui.v1.api.NanoSvgApi
1516
import org.polyfrost.oneconfig.api.ui.v1.api.RendererExt
1617
import org.polyfrost.oneconfig.api.ui.v1.api.StbApi
@@ -99,12 +100,11 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
99100

100101

101102
// Current batch state
103+
private val viewport = IntArray(4)
102104
private var count = 0
103105
private var transformDepth = 0
104106
private var scissorDepth = 0
105107
private var transform = IDENTITY.copyOf()
106-
private var viewportWidth = 0f
107-
private var viewportHeight = 0f
108108
private var pixelRatio = 1f
109109
private var alphaCap = 1f
110110
private var popFlushNeeded = false
@@ -420,8 +420,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
420420
glUniformMatrix3fv(uTransform, false, transform)
421421
glUseProgram(prevProg)
422422
glDisable(GL_SCISSOR_TEST)
423-
viewportWidth = width * pixelRatio
424-
viewportHeight = height * pixelRatio
423+
Platform.screen().glViewport(viewport)
425424
this.pixelRatio = pixelRatio
426425
}
427426

@@ -671,15 +670,15 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
671670
override fun pushScissor(x: Float, y: Float, width: Float, height: Float) {
672671
flush()
673672
val nx = (x * pixelRatio).roundToInt()
674-
val ny = (viewportHeight - (y + height) * pixelRatio).roundToInt()
673+
val ny = (viewport[3] - (y + height) * pixelRatio).roundToInt()
675674
val nw = (width * pixelRatio).roundToInt()
676675
val nh = (height * pixelRatio).roundToInt()
677676
scissorStack[scissorDepth++] = nx
678677
scissorStack[scissorDepth++] = ny
679678
scissorStack[scissorDepth++] = nw
680679
scissorStack[scissorDepth++] = nh
681680
glEnable(GL_SCISSOR_TEST)
682-
glScissor(nx, ny, nw, nh)
681+
glScissor(nx + viewport[0], ny + viewport[1], nw, nh)
683682
}
684683

685684
override fun pushScissorIntersecting(x: Float, y: Float, width: Float, height: Float) {
@@ -693,7 +692,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
693692
val pw = scissorStack[scissorDepth - 2]
694693
val ph = scissorStack[scissorDepth - 1]
695694
val nx = (x * pixelRatio).roundToInt()
696-
val ny = (viewportHeight - (y + height) * pixelRatio).roundToInt()
695+
val ny = (viewport[3] - (y + height) * pixelRatio).roundToInt()
697696

698697
val ix = maxOf(nx, px)
699698
val iy = maxOf(ny, py)
@@ -706,7 +705,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
706705
scissorStack[scissorDepth++] = ih
707706

708707
glEnable(GL_SCISSOR_TEST)
709-
glScissor(ix, iy, iw, ih)
708+
glScissor(ix + viewport[0], iy + viewport[1], iw, ih)
710709
}
711710

712711
override fun popScissor() {
@@ -717,12 +716,12 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
717716
return
718717
}
719718
scissorDepth -= 4
720-
val x = scissorStack[scissorDepth - 4]
721-
val y = scissorStack[scissorDepth - 3]
722-
val width = scissorStack[scissorDepth - 2]
723-
val height = scissorStack[scissorDepth - 1]
719+
val nx = scissorStack[scissorDepth - 4]
720+
val ny = scissorStack[scissorDepth - 3]
721+
val nw = scissorStack[scissorDepth - 2]
722+
val nh = scissorStack[scissorDepth - 1]
724723
glEnable(GL_SCISSOR_TEST)
725-
glScissor(x, y, width, height)
724+
glScissor(nx + viewport[0], ny + viewport[1], nw, nh)
726725
}
727726

728727
override fun globalAlpha(alpha: Float) {
@@ -883,7 +882,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
883882
return fonts.getOrPut(font.resourcePath.hashCode() + renderSize.toInt()) {
884883
val data = font.load {
885884
LOGGER.error("Failed to load font: $font", it)
886-
return@getOrPut fonts[PolyUI.defaultFonts.regular.resourcePath.hashCode() + renderSize.toInt()]
885+
return@getOrPut fonts[PolyUI.defaultFonts.regular.resourcePath.hashCode() + 12f.toInt()]
887886
?: throw IllegalStateException("Default font couldn't be loaded")
888887
}.toDirectByteBuffer()
889888
FontAtlas(data, renderSize)

modules/utils/src/main/java/org/polyfrost/oneconfig/api/platform/v1/ScreenPlatform.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package org.polyfrost.oneconfig.api.platform.v1;
2828

2929
import dev.deftu.omnicore.api.client.render.OmniRenderingContext;
30+
import org.jetbrains.annotations.NotNull;
3031
import org.jetbrains.annotations.Nullable;
3132

3233
public interface ScreenPlatform {
@@ -39,6 +40,16 @@ public interface ScreenPlatform {
3940

4041
int windowHeight();
4142

43+
/**
44+
* Return the result of glGetInteger(GL_VIEWPORT).
45+
* If in is supplied, it will be set to the result and returned. If not, a new array will be created.
46+
*/
47+
int @NotNull [] glViewport(int @Nullable [] in);
48+
49+
default int @NotNull [] glViewport() {
50+
return glViewport(null);
51+
}
52+
4253
void renderLegacyHuds(OmniRenderingContext ctx);
4354

4455
default float pixelRatio() {

0 commit comments

Comments
 (0)