11package io.github.notenoughupdates.moulconfig.platform
22
3- import com.mojang.blaze3d.platform.GlStateManager
43import com.mojang.blaze3d.systems.RenderSystem
54import io.github.notenoughupdates.moulconfig.common.DynamicTextureReference
65import io.github.notenoughupdates.moulconfig.common.IFontRenderer
@@ -9,22 +8,56 @@ import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
98import io.github.notenoughupdates.moulconfig.common.RenderContext
109import io.github.notenoughupdates.moulconfig.common.RenderContext.TextureFilter
1110import net.minecraft.client.MinecraftClient
11+ import net.minecraft.client.gl.ShaderProgramKeys
1212import net.minecraft.client.gui.DrawContext
13- import net.minecraft.client.render.BufferRenderer
14- import net.minecraft.client.render.GameRenderer
1513import net.minecraft.client.render.RenderLayer
16- import net.minecraft.client.render.Tessellator
14+ import net.minecraft.client.render.RenderPhase
1715import net.minecraft.client.render.VertexFormat
1816import net.minecraft.client.render.VertexFormats
1917import net.minecraft.client.texture.NativeImageBackedTexture
2018import net.minecraft.client.util.InputUtil
2119import net.minecraft.text.Text
20+ import net.minecraft.util.Identifier
21+ import net.minecraft.util.TriState
22+ import net.minecraft.util.Util
2223import org.joml.Matrix4f
2324import org.lwjgl.glfw.GLFW
2425import org.lwjgl.opengl.GL11
2526import java.awt.image.BufferedImage
2627
2728class ModernRenderContext (val drawContext : DrawContext ) : RenderContext {
29+ companion object {
30+ val RL = RenderLayer .of(
31+ " moulconfig_inverted_rect" ,
32+ VertexFormats .POSITION ,
33+ VertexFormat .DrawMode .QUADS ,
34+ RenderLayer .DEFAULT_BUFFER_SIZE ,
35+ false ,
36+ false ,
37+ RenderLayer .MultiPhaseParameters .builder()
38+ .program(RenderPhase .POSITION_PROGRAM )
39+ .colorLogic(RenderPhase .OR_REVERSE )
40+ .build(false )
41+ )
42+ val COLORED_TRIANGLES = Util .memoize { it: Identifier ->
43+ RenderLayer .of(
44+ " mc_triangles" ,
45+ VertexFormats .POSITION_COLOR ,
46+ VertexFormat .DrawMode .TRIANGLES ,
47+ RenderLayer .DEFAULT_BUFFER_SIZE ,
48+ false ,
49+ false ,
50+ RenderLayer .MultiPhaseParameters .builder()
51+ // TODO: import mipmap settings
52+ .texture(RenderPhase .Texture (it, TriState .DEFAULT , false ))
53+ .program(RenderPhase .ShaderProgram (ShaderProgramKeys .POSITION_TEX_COLOR ))
54+ .transparency(RenderPhase .TRANSLUCENT_TRANSPARENCY )
55+ .depthTest(RenderPhase .LEQUAL_DEPTH_TEST )
56+ .build(false )
57+ )
58+ }
59+ }
60+
2861 val mouse = MinecraftClient .getInstance().mouse
2962 val window = MinecraftClient .getInstance().window
3063 override fun disableDepth () {
@@ -39,11 +72,7 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
3972 for (i in (0 until img.width)) {
4073 for (j in (0 until img.height)) {
4174 val argb = img.getRGB(i, j)
42- val b = (argb and 0xFF ) shl 16
43- val r = (argb and 0xFF0000 ) shr 16
44- val aAndG = argb and 0xFF00FF00 .toInt()
45- // Nice ABGR, nerd
46- image!! .setColor(i, j, b or r or aAndG)
75+ image!! .setColorArgb(i, j, argb)
4776 }
4877 }
4978 }
@@ -94,8 +123,16 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
94123 drawContext.matrices.scale(x, y, z)
95124 }
96125
126+ var tintR = 1F
127+ var tintG = 1F
128+ var tintB = 1F
129+ var tintA = 1F
130+
97131 override fun color (r : Float , g : Float , b : Float , a : Float ) {
98- drawContext.setShaderColor(r, g, b, a)
132+ tintR = r
133+ tintG = g
134+ tintB = b
135+ tintA = a
99136 }
100137
101138 override fun isMouseButtonDown (mouseButton : Int ): Boolean {
@@ -106,19 +143,20 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
106143 return InputUtil .isKeyPressed(window.handle, keyboardKey)
107144 }
108145
146+
109147 override fun drawTriangles (vararg coordinates : Float ) {
110- RenderSystem .setShader(GameRenderer ::getPositionTexProgram)
111- val tess = Tessellator .getInstance()
112- val buffer = tess.begin(VertexFormat .DrawMode .TRIANGLES , VertexFormats .POSITION )
113- val matrix = drawContext.matrices.peek().positionMatrix
148+ require(coordinates.size % 6 == 0 )
114149 RenderSystem .enableBlend()
150+ drawContext.draw {
151+ val buf = it.getBuffer(COLORED_TRIANGLES .apply (ModernMinecraft .boundTexture))
152+ val matrix = drawContext.matrices.peek().positionMatrix
115153
116- require(coordinates.size % 6 == 0 )
117- for (i in 0 until (coordinates.size / 2 )) {
118- buffer.vertex(matrix, coordinates[i * 2 ], coordinates[i * 2 + 1 ], 0.0F ).next()
154+ for (i in 0 until (coordinates.size / 2 )) {
155+ buf.vertex(matrix, coordinates[i * 2 ], coordinates[i * 2 + 1 ], 0.0F )
156+ .color(tintA, tintR, tintG, tintB).next()
157+ }
119158 }
120-
121- BufferRenderer .drawWithGlobalProgram(buffer.end())
159+ RenderSystem .disableBlend()
122160 }
123161
124162 override fun drawString (
@@ -137,20 +175,14 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
137175 }
138176
139177 override fun invertedRect (left : Float , top : Float , right : Float , bottom : Float ) {
140- val tess = Tessellator .getInstance()
141- val buffer = tess.begin(VertexFormat .DrawMode .QUADS , VertexFormats .POSITION )
142- val matrix = drawContext.matrices.peek().positionMatrix
143- RenderSystem .setShaderColor(1F , 1F , 1f , 1f )
144- RenderSystem .setShader(GameRenderer ::getPositionProgram)
145- RenderSystem .enableColorLogicOp()
146- RenderSystem .logicOp(GlStateManager .LogicOp .OR_REVERSE )
147- buffer.vertex(matrix, left, bottom, 0F ).next()
148- buffer.vertex(matrix, right, bottom, 0F ).next()
149- buffer.vertex(matrix, right, top, 0F ).next()
150- buffer.vertex(matrix, left, top, 0F ).next()
151- BufferRenderer .drawWithGlobalProgram(buffer.end())
152- RenderSystem .disableColorLogicOp()
153- RenderSystem .setShaderColor(1f , 1f , 1f , 1f )
178+ drawContext.draw {
179+ val matrix = drawContext.matrices.peek().positionMatrix
180+ val buffer = it.getBuffer(RL )
181+ buffer.vertex(matrix, left, bottom, 0F ).next()
182+ buffer.vertex(matrix, right, bottom, 0F ).next()
183+ buffer.vertex(matrix, right, top, 0F ).next()
184+ buffer.vertex(matrix, left, top, 0F ).next()
185+ }
154186 }
155187
156188 override fun setTextureMinMagFilter (textureFilter : TextureFilter ) {
@@ -173,16 +205,18 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
173205 u2 : Float ,
174206 v2 : Float
175207 ) {
176- RenderSystem .setShaderTexture(0 , ModernMinecraft .boundTexture!! )
177- RenderSystem .setShader(GameRenderer ::getPositionTexProgram)
178- val matrix4f: Matrix4f = drawContext.matrices.peek().positionMatrix
179- val bufferBuilder = Tessellator .getInstance()
180- .begin(VertexFormat .DrawMode .QUADS , VertexFormats .POSITION_TEXTURE )
181- bufferBuilder.vertex(matrix4f, x, y, 0F ).texture(u1, v1).next()
182- bufferBuilder.vertex(matrix4f, x, y + height, 0f ).texture(u1, v2).next()
183- bufferBuilder.vertex(matrix4f, x + width, y + height, 0f ).texture(u2, v2).next()
184- bufferBuilder.vertex(matrix4f, x + width, y, 0F ).texture(u2, v1).next()
185- BufferRenderer .drawWithGlobalProgram(bufferBuilder.end())
208+ drawContext.draw {
209+ val matrix4f: Matrix4f = drawContext.matrices.peek().positionMatrix
210+ val bufferBuilder = it.getBuffer(RenderLayer .getGuiTextured(ModernMinecraft .boundTexture!! ))
211+ bufferBuilder.vertex(matrix4f, x, y, 0F ).texture(u1, v1)
212+ .color(tintA, tintG, tintB, tintA).next()
213+ bufferBuilder.vertex(matrix4f, x, y + height, 0f ).texture(u1, v2)
214+ .color(tintA, tintG, tintB, tintA).next()
215+ bufferBuilder.vertex(matrix4f, x + width, y + height, 0f ).texture(u2, v2)
216+ .color(tintA, tintG, tintB, tintA).next()
217+ bufferBuilder.vertex(matrix4f, x + width, y, 0F ).texture(u2, v1)
218+ .color(tintA, tintG, tintB, tintA).next()
219+ }
186220 }
187221
188222 override fun drawDarkRect (x : Int , y : Int , width : Int , height : Int , shadow : Boolean ) {
@@ -230,7 +264,7 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
230264 override fun renderItemStack (itemStack : IItemStack , x : Int , y : Int , overlayText : String? ) {
231265 val item = (itemStack as ModernItemStack ).backing
232266 drawContext.drawItem(item, x, y)
233- drawContext.drawItemInSlot (
267+ drawContext.drawStackOverlay (
234268 MinecraftClient .getInstance().textRenderer,
235269 item,
236270 x,
0 commit comments