From da886760f66bdcb8045d386dcb3ef12c588a3845 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 19 Jan 2010 19:14:13 +0100 Subject: Print info logs when loading shaders --- src/de/gamezock/metacraft/ui/ShaderLoader.java | 91 ++++++++++++++++---------- 1 file changed, 58 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/de/gamezock/metacraft/ui/ShaderLoader.java b/src/de/gamezock/metacraft/ui/ShaderLoader.java index 25bc4eb..1f696f8 100644 --- a/src/de/gamezock/metacraft/ui/ShaderLoader.java +++ b/src/de/gamezock/metacraft/ui/ShaderLoader.java @@ -4,6 +4,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; @@ -14,6 +16,23 @@ import javax.media.opengl.GL2; public class ShaderLoader { private static Map, Integer> programs = new HashMap, Integer>(); + static private void printInfoLog(GL2 gl, int object) { + IntBuffer lengthBuffer = IntBuffer.allocate(1); + + gl.glGetObjectParameterivARB(object, GL2.GL_OBJECT_INFO_LOG_LENGTH_ARB, lengthBuffer); + + int length = lengthBuffer.get(0); + + if(length > 0) { + ByteBuffer log = ByteBuffer.allocate(length); + + IntBuffer foo = IntBuffer.allocate(1); + gl.glGetInfoLogARB(object, length, foo, log); + System.err.println(new String(log.array())); + } + + } + static private String readStream(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String ret = "", line; @@ -25,48 +44,54 @@ public class ShaderLoader { return ret; } - static boolean load(GL2 gl, String vsName, String fsName) { + static private int loadShader(GL2 gl, String name, int type) { + int shader = gl.glCreateShaderObjectARB(type); + + try { + InputStream stream = ShaderLoader.class.getResourceAsStream("/de/gamezock/metacraft/resources/shaders/" + name); + + String source = readStream(stream); + + gl.glShaderSourceARB(shader, 1, new String[] {source}, null); + gl.glCompileShaderARB(shader); + + printInfoLog(gl, shader); + + return shader; + } catch (IOException e) { + } + + if(shader != 0) + gl.glDeleteObjectARB(shader); + + return 0; + } + + static public boolean load(GL2 gl, String vsName, String fsName) { Integer savedProg = programs.get(new AbstractMap.SimpleEntry(vsName, fsName)); if(savedProg != null) { gl.glUseProgram(savedProg); return true; } - int vs = gl.glCreateShader(GL2.GL_VERTEX_SHADER); - int fs = gl.glCreateShader(GL2.GL_FRAGMENT_SHADER); + int vs = loadShader(gl, vsName, GL2.GL_VERTEX_SHADER); + int fs = loadShader(gl, fsName, GL2.GL_FRAGMENT_SHADER); - InputStream vsStream = ShaderLoader.class.getResourceAsStream("/de/gamezock/metacraft/resources/shaders/" + vsName); - String vsSource; - try { - vsSource = readStream(vsStream); - } catch (IOException e) { - return false; - } - - gl.glShaderSource(vs, 1, new String[] {vsSource}, null); - gl.glCompileShader(vs); + if(vs != 0 && fs != 0) { + int program = gl.glCreateProgramObjectARB(); + gl.glAttachObjectARB(program, vs); + gl.glAttachObjectARB(program, fs); + gl.glLinkProgramARB(program); + + printInfoLog(gl, program); + + gl.glUseProgramObjectARB(program); - InputStream fsStream = ShaderLoader.class.getResourceAsStream("/de/gamezock/metacraft/resources/shaders/" + fsName); - String fsSource; - try { - fsSource = readStream(fsStream); - } catch (IOException e) { - return false; - } - - gl.glShaderSource(fs, 1, new String[] {fsSource}, null); - gl.glCompileShader(fs); + programs.put(new AbstractMap.SimpleEntry(vsName, fsName), program); - int program = gl.glCreateProgram(); - gl.glAttachShader(program, vs); - gl.glAttachShader(program, fs); - gl.glLinkProgram(program); - gl.glValidateProgram(program); + return true; + } - gl.glUseProgram(program); - - programs.put(new AbstractMap.SimpleEntry(vsName, fsName), program); - - return true; + return false; } } -- cgit v1.2.3