import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.stream.Collectors;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
public class Shader {
Minecraft mc = Minecraft.getMinecraft();
private final int programId;
private final ScaledResolution sr = new ScaledResolution(this.mc);
public static final String VERTEX_SORUCE = ShaderSuka.getShaderSource("vertex.vsh");
private final long initTime;
public ShaderSuka(String fragmentShaderName) {
int programId = GL20.glCreateProgram();
try {
int vertexShader = GL20.glCreateShader(35633);
GL20.glShaderSource(vertexShader, VERTEX_SORUCE);
GL20.glCompileShader(vertexShader);
int isVertexCompiled = GL20.glGetShaderi(vertexShader, 35713);
if (isVertexCompiled == 0) {
GL20.glDeleteShader(vertexShader);
System.err.println("Vertex shader couldn't compile. It has been deleted.");
}
int fragmentShader = GL20.glCreateShader(35632);
GL20.glShaderSource(fragmentShader, ShaderSuka.getShaderSource(fragmentShaderName));
GL20.glCompileShader(fragmentShader);
int isFragmentCompiled = GL20.glGetShaderi(fragmentShader, 35713);
if (isFragmentCompiled == 0) {
GL20.glDeleteShader(fragmentShader);
System.err.println("Fragment shader couldn't compile. It has been deleted.");
}
GL20.glAttachShader(programId, vertexShader);
GL20.glAttachShader(programId, fragmentShader);
GL20.glLinkProgram(programId);
}
catch (Exception e) {
e.printStackTrace();
}
this.programId = programId;
this.initTime = System.currentTimeMillis();
}
public void draw(float mouseX, float mouseY) {
GL20.glUseProgram(this.programId);
GL20.glUniform2f(GL20.glGetUniformLocation(this.programId, "resolution"), this.sr.getScaledWidth(), this.sr.getScaledHeight());
GL20.glUniform2f(GL20.glGetUniformLocation(this.programId, "mouse"), mouseX, mouseY);
GL20.glUniform1f(GL20.glGetUniformLocation(this.programId, "time"), (float)(System.currentTimeMillis() - this.initTime) / 1000.0f);
GL11.glBegin(7);
GL11.glVertex2d(0.0, 0.0);
GL11.glVertex2d(0.0, this.sr.getScaledHeight());
GL11.glVertex2d(this.sr.getScaledWidth(), this.sr.getScaledHeight());
GL11.glVertex2d(this.sr.getScaledWidth(), 0.0);
GL11.glEnd();
GL20.glUseProgram(0);
}
public static String getShaderSource(String fileName) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(ShaderSuka.class.getResourceAsStream("/assets/minecraft/expensive/shaders/" + fileName))));
String source = bufferedReader.lines().filter(str -> !str.isEmpty()).map(str -> {
str = str.replace("\t", "");
return str;
}).collect(Collectors.joining("\n"));
try {
bufferedReader.close();
}
catch (IOException iOException) {
// empty catch block
}
return source;
}
}