summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Render/GuiRenderer.java
blob: b7ae6f2b2930295e89747a63fe152cf8e749c686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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<UIComponent> 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();
	}
}