diff --git a/src/main/java/com/seibel/lod/core/render/GLProxy.java b/src/main/java/com/seibel/lod/core/render/GLProxy.java index 819d8a15c..2abd7caf9 100644 --- a/src/main/java/com/seibel/lod/core/render/GLProxy.java +++ b/src/main/java/com/seibel/lod/core/render/GLProxy.java @@ -19,6 +19,7 @@ package com.seibel.lod.core.render; +import java.io.FileNotFoundException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -101,7 +102,11 @@ public class GLProxy - + /** + * @throws IllegalStateException + * @throws RuntimeException + * @throws FileNotFoundException + */ private GLProxy() { ClientApi.LOGGER.info("Creating " + GLProxy.class.getSimpleName() + "... If this is the last message you see in the log there must have been a OpenGL error."); @@ -111,8 +116,6 @@ public class GLProxy if (GLFW.glfwGetCurrentContext() == 0L) throw new IllegalStateException(GLProxy.class.getSimpleName() + " was created outside the render thread!"); - - //============================// // create the builder context // //============================// @@ -248,7 +251,11 @@ public class GLProxy ClientApi.LOGGER.info(GLProxy.class.getSimpleName() + " creation successful. OpenGL smiles upon you this day."); } - /** Creates all required shaders */ + /** + * Creates all required shaders + * @throws RuntimeException + * @throws FileNotFoundException + */ public void createShaderProgram() { LodShader vertexShader = null; @@ -289,6 +296,35 @@ public class GLProxy { ClientApi.LOGGER.error("Unable to compile shaders. Error: " + e.getMessage()); } + // get the shaders from the resource folder + // Use File.separator ONLY if the file is external (not in the resourse), otherwise use "/" + vertexShader = LodShader.loadShader(GL20.GL_VERTEX_SHADER, "shaders/standard.vert", false); + fragmentShader = LodShader.loadShader(GL20.GL_FRAGMENT_SHADER, "shaders/flat_shaded.frag", false); + + // this can be used when testing shaders, + // since we can't hot swap the files in the resource folder + // vertexShader = LodShader.loadShader(GL20.GL_VERTEX_SHADER, "C:/Users/James Seibel/Desktop/shaders/standard.vert", true); + // fragmentShader = LodShader.loadShader(GL20.GL_FRAGMENT_SHADER, "C:/Users/James Seibel/Desktop/shaders/flat_shaded.frag", true); + + + // create the shaders + lodShaderProgram = new LodShaderProgram(); + + // Attach the compiled shaders to the program, throws RuntimeException on link error + lodShaderProgram.attachShader(vertexShader); + lodShaderProgram.attachShader(fragmentShader); + + // activate the fragment shader output + GL30.glBindFragDataLocation(lodShaderProgram.id, 0, "fragColor"); + + // attach the shader program to the OpenGL context + lodShaderProgram.link(); + + // after the shaders have been attached to the program + // we don't need their OpenGL references anymore + GL20.glDeleteShader(vertexShader.id); + GL20.glDeleteShader(fragmentShader.id); + } diff --git a/src/main/java/com/seibel/lod/core/render/shader/LodShader.java b/src/main/java/com/seibel/lod/core/render/shader/LodShader.java index d5689a0a1..91a1ca397 100644 --- a/src/main/java/com/seibel/lod/core/render/shader/LodShader.java +++ b/src/main/java/com/seibel/lod/core/render/shader/LodShader.java @@ -21,14 +21,13 @@ package com.seibel.lod.core.render.shader; import java.io.BufferedReader; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.lwjgl.opengl.GL20; -import com.seibel.lod.core.api.ClientApi; - /** * This object holds a OpenGL reference to a shader * and allows for reading in and compiling a shader file. @@ -59,14 +58,23 @@ public class LodShader * @param absoluteFilePath If false the file path is relative to the resource jar folder. * @throws Exception if the shader fails to compile */ - public static LodShader loadShader(int type, String path, boolean absoluteFilePath) throws Exception + public static LodShader loadShader(int type, String path, boolean absoluteFilePath) { StringBuilder stringBuilder = new StringBuilder(); try { // open the file - InputStream in = absoluteFilePath ? new FileInputStream(path) : LodShader.class.getClassLoader().getResourceAsStream(path); + InputStream in; + if (absoluteFilePath) { + // Throws FileNotFoundException + in = new FileInputStream(path); // Note: this should use OS path seperator + } else { + in = LodShader.class.getClassLoader().getResourceAsStream(path); // Note: path seperator should be '/' + if (in == null) { + throw new FileNotFoundException("Shader file not found in resource: "+path); + } + } BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // read in the file @@ -76,7 +84,7 @@ public class LodShader } catch (IOException e) { - ClientApi.LOGGER.error("Unable to load shader from file [" + path + "]. Error: " + e.getMessage()); + throw new RuntimeException("Unable to load shader from file [" + path + "]. Error: " + e.getMessage()); } CharSequence shaderFileSource = stringBuilder.toString(); @@ -90,7 +98,7 @@ public class LodShader * @param source Source of the shader * @throws Exception if the shader fails to compile */ - public static LodShader createShader(int type, CharSequence source) throws Exception + public static LodShader createShader(int type, CharSequence source) { LodShader shader = new LodShader(type); GL20.glShaderSource(shader.id, source); @@ -103,14 +111,14 @@ public class LodShader * Compiles the shader and checks its status afterwards. * @throws Exception if the shader fails to compile */ - public void compile() throws Exception + public void compile() { GL20.glCompileShader(id); // check if the shader compiled int status = GL20.glGetShaderi(id, GL20.GL_COMPILE_STATUS); if (status != GL20.GL_TRUE) - throw new Exception(GL20.glGetShaderInfoLog(id)); + throw new RuntimeException("Shader compiler error. Details: "+GL20.glGetShaderInfoLog(id)); } } diff --git a/src/main/java/com/seibel/lod/core/render/shader/LodShaderProgram.java b/src/main/java/com/seibel/lod/core/render/shader/LodShaderProgram.java index f5478f607..e8c59b8dd 100644 --- a/src/main/java/com/seibel/lod/core/render/shader/LodShaderProgram.java +++ b/src/main/java/com/seibel/lod/core/render/shader/LodShaderProgram.java @@ -73,7 +73,7 @@ public class LodShaderProgram * Links the shader program to the current OpenGL context. * @throws Exception Exception if the program failed to link */ - public void link() throws Exception + public void link() { GL20.glLinkProgram(this.id); checkLinkStatus(); @@ -83,11 +83,11 @@ public class LodShaderProgram * Checks if the program was linked successfully. * @throws Exception if the program failed to link */ - public void checkLinkStatus() throws Exception + public void checkLinkStatus() { int status = GL20.glGetProgrami(this.id, GL20.GL_LINK_STATUS); if (status != GL20.GL_TRUE) - throw new Exception(GL20.glGetProgramInfoLog(this.id)); + throw new RuntimeException("Shader Link Error. Details: "+GL20.glGetProgramInfoLog(this.id)); }