From 79e4dce56945053c195d8615040a67d99dd5525f Mon Sep 17 00:00:00 2001 From: TomTheFurry Date: Wed, 8 Dec 2021 14:18:04 +0000 Subject: [PATCH] Fix for WindowOS Render null Error --- .../com/seibel/lod/core/render/GLProxy.java | 74 +++++++++---------- .../lod/core/render/shader/LodShader.java | 24 ++++-- .../core/render/shader/LodShaderProgram.java | 6 +- 3 files changed, 52 insertions(+), 52 deletions(-) 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 3c3109e22..9843d3e4c 100644 --- a/src/main/java/com/seibel/lod/core/render/GLProxy.java +++ b/src/main/java/com/seibel/lod/core/render/GLProxy.java @@ -19,7 +19,6 @@ package com.seibel.lod.core.render; -import java.io.File; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -100,18 +99,16 @@ public class GLProxy - + // May throw IllegalStateException, RuntimeException, FileNotFoundException private GLProxy() { - ClientApi.LOGGER.error("Creating " + GLProxy.class.getSimpleName() + "... If this is the last message you see in the log there must have been a OpenGL error."); + ClientApi.LOGGER.info("Lod: Creating " + GLProxy.class.getSimpleName() + "..."); // getting Minecraft's context has to be done on the render thread, // where the GL context is if (GLFW.glfwGetCurrentContext() == 0L) throw new IllegalStateException(GLProxy.class.getSimpleName() + " was created outside the render thread!"); - - //============================// // create the builder context // //============================// @@ -243,50 +240,45 @@ public class GLProxy // GLProxy creation success - ClientApi.LOGGER.error(GLProxy.class.getSimpleName() + " creation successful. OpenGL smiles upon you this day."); + ClientApi.LOGGER.info(GLProxy.class.getSimpleName() + " creation completed"); } /** Creates all required shaders */ + // May throw RuntimeException, FileNotFoundException public void createShaderProgram() { LodShader vertexShader = null; LodShader fragmentShader = null; - try - { - // get the shaders from the resource folder - vertexShader = LodShader.loadShader(GL20.GL_VERTEX_SHADER, "shaders" + File.separator + "standard.vert", false); - fragmentShader = LodShader.loadShader(GL20.GL_FRAGMENT_SHADER, "shaders" + File.separator + "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 - 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); - } - catch (Exception e) - { - 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)); }