diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2010-01-19 19:14:13 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2010-01-19 19:14:13 +0100 |
commit | da886760f66bdcb8045d386dcb3ef12c588a3845 (patch) | |
tree | b55a4e35e2e4fedb97727c9241d7dbba5f974cc7 /src | |
parent | c7030ae2b41fc2e559304737a54d6f927cf3d740 (diff) | |
download | metacraft-da886760f66bdcb8045d386dcb3ef12c588a3845.tar metacraft-da886760f66bdcb8045d386dcb3ef12c588a3845.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/de/gamezock/metacraft/ui/ShaderLoader.java | 91 |
1 files changed, 58 insertions, 33 deletions
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<Entry<String, String>, Integer> programs = new HashMap<Entry<String, String>, 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<String, String>(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<String, String>(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<String, String>(vsName, fsName), program); - - return true; + return false; } } |