mirror of
https://github.com/MistWeatherMedia/WeatherSTAR-3000-Java.git
synced 2026-11-28 14:38:08 -08:00
Add files via upload
This commit is contained in:
1 parent
800569688a
commit
746f9debdd
42 files changed
+7691
No files matched your search
@@ -0,0 +1,183 @@
|
||||
package renderer;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.file.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.BufferUtils;
|
||||
|
||||
public class Shader {
|
||||
private int shaderProgramID;
|
||||
private boolean beingUsed = false;
|
||||
private String vertexSource;
|
||||
private String fragmentSource;
|
||||
private String filepath;
|
||||
|
||||
public Shader(String filepath) {
|
||||
this.filepath = filepath;
|
||||
try {
|
||||
// Make sure the path starts with a leading slash for root JAR loading
|
||||
String resourcePath = filepath.startsWith("/") ? filepath : "/" + filepath;
|
||||
|
||||
String source;
|
||||
try (InputStream is = Shader.class.getResourceAsStream(resourcePath)) {
|
||||
if (is == null) {
|
||||
throw new IOException("Shader file not found inside JAR: " + resourcePath);
|
||||
}
|
||||
// Safely read all text lines into a single String
|
||||
source = new BufferedReader(new InputStreamReader(is))
|
||||
.lines().collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
String[] splitString = source.split("(#type)( )+([a-zA-Z]+)");
|
||||
|
||||
int index = source.indexOf("#type") + 6;
|
||||
int eol = source.indexOf("\n", index);
|
||||
String firstPattern = source.substring(index, eol).trim();
|
||||
|
||||
index = source.indexOf("#type", eol) + 6;
|
||||
eol = source.indexOf("\n", index);
|
||||
String secondPattern = source.substring(index, eol).trim();
|
||||
|
||||
if (firstPattern.equals("vertex")) {
|
||||
vertexSource = splitString[1];
|
||||
} else if (firstPattern.equals("fragment")) {
|
||||
fragmentSource = splitString[1];
|
||||
} else {
|
||||
throw new IOException("Unexpected token '" + firstPattern + "'");
|
||||
}
|
||||
|
||||
if (secondPattern.equals("vertex")) {
|
||||
vertexSource = splitString[2];
|
||||
} else if (secondPattern.equals("fragment")) {
|
||||
fragmentSource = splitString[2];
|
||||
} else {
|
||||
throw new IOException("Unexpected token '" + secondPattern + "'");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
assert false : "ERROR: Could not open file for shader: " + filepath;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void compile() {
|
||||
int vertexID, fragmentID;
|
||||
vertexID = glCreateShader(GL_VERTEX_SHADER);
|
||||
|
||||
glShaderSource(vertexID, vertexSource);
|
||||
glCompileShader(vertexID);
|
||||
|
||||
int success = glGetShaderi(vertexID, GL_COMPILE_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetShaderi(vertexID, GL_INFO_LOG_LENGTH);
|
||||
System.out.println("ERROR: " + filepath + "\n\tVertex shader compilation failed.");
|
||||
System.out.println(glGetShaderInfoLog(vertexID, len));
|
||||
assert false : "";
|
||||
}
|
||||
|
||||
fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
glShaderSource(fragmentID, fragmentSource);
|
||||
glCompileShader(fragmentID);
|
||||
|
||||
success = glGetShaderi(fragmentID, GL_COMPILE_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetShaderi(fragmentID, GL_INFO_LOG_LENGTH);
|
||||
System.out.println("ERROR: " + filepath + "\n\tVertex shader compilation failed.");
|
||||
System.out.println(glGetShaderInfoLog(fragmentID, len));
|
||||
assert false : "";
|
||||
}
|
||||
|
||||
shaderProgramID = glCreateProgram();
|
||||
glAttachShader(shaderProgramID, vertexID);
|
||||
glAttachShader(shaderProgramID, fragmentID);
|
||||
glLinkProgram(shaderProgramID);
|
||||
|
||||
success = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
|
||||
if (success == GL_FALSE) {
|
||||
int len = glGetProgrami(shaderProgramID, GL_LINK_STATUS);
|
||||
System.out.println("ERROR: " + filepath + "\n\tFailed to link shaders.");
|
||||
System.out.println(glGetProgramInfoLog(shaderProgramID, len));
|
||||
assert false : "";
|
||||
}
|
||||
}
|
||||
|
||||
public void use() {
|
||||
if (!beingUsed) {
|
||||
glUseProgram(shaderProgramID);
|
||||
beingUsed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() {
|
||||
glUseProgram(0);
|
||||
beingUsed = false;
|
||||
}
|
||||
|
||||
public void uploadMat4f(String varName, Matrix4f mat4) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(16);
|
||||
mat4.get(matBuffer);
|
||||
glUniformMatrix4fv(varLocation, false, matBuffer);
|
||||
}
|
||||
|
||||
public void uploadMat3f(String varName, Matrix3f mat3) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(9);
|
||||
mat3.get(matBuffer);
|
||||
glUniformMatrix3fv(varLocation, false, matBuffer);
|
||||
}
|
||||
|
||||
public void uploadVec4f(String varName, Vector4f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform4f(varLocation, vec.x, vec.y, vec.z, vec.w);
|
||||
}
|
||||
|
||||
public void uploadVec3f(String varName, Vector3f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform3f(varLocation, vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
public void uploadVec2f(String varName, Vector2f vec) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform2f(varLocation, vec.x, vec.y);
|
||||
}
|
||||
|
||||
public void uploadFloat(String varName, float val) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1f(varLocation, val);
|
||||
}
|
||||
|
||||
public void uploadInt(String varName, int val) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1i(varLocation, val);
|
||||
}
|
||||
|
||||
public void uploadTexture(String varName, int slot) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1i(varLocation, slot);
|
||||
}
|
||||
|
||||
public void uploadIntArray(String varName, int[] array) {
|
||||
int varLocation = glGetUniformLocation(shaderProgramID, varName);
|
||||
use();
|
||||
glUniform1iv(varLocation, array);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user