package net.tylermurphy.Minecraft.Render; import java.util.List; import net.tylermurphy.Minecraft.Render.Data.Mesh; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; import org.joml.Matrix4f; import org.joml.Vector2f; import net.tylermurphy.Minecraft.Render.Shaders.GuiShader; import net.tylermurphy.Minecraft.UI.UIComponent; import net.tylermurphy.Minecraft.UI.UIImage; import net.tylermurphy.Minecraft.Util.Maths; public class GuiRenderer { private final Mesh quad; private GuiShader shader; public GuiRenderer(){ float[] positions = {-1, 1, -1, -1, 1, 1, 1, -1}; quad = new Mesh(positions.length/2).store(positions, 2).finish(); shader = new GuiShader(); } public void render(List components){ shader.start(); GL30.glBindVertexArray(quad.getID()); GL20.glEnableVertexAttribArray(0); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glDisable(GL11.GL_DEPTH_TEST); for(UIComponent cp : components) { if(!(cp instanceof UIImage)) continue; UIImage gui = (UIImage)cp; GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture()); Matrix4f matrix = Maths.createTransformationMatrix(new Vector2f(gui.getConvertedPosition().x*2-1,(1-gui.getConvertedPosition().y)*2-1), gui.getConvertedSize()); shader.loadTransformation(matrix); GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount()); } GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glDisable(GL11.GL_BLEND); GL20.glDisableVertexAttribArray(0); GL30.glBindVertexArray(0); shader.stop(); } public void cleanUp(){ shader.cleanUp(); } }