1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package de.gamezock.metacraft.ui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.media.opengl.GL2;
public class ShaderLoader {
private static Map<Entry<String, String>, Integer> programs = new HashMap<Entry<String, String>, Integer>();
static private String readStream(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String ret = "", line;
while ((line = reader.readLine()) != null) {
ret += line + "\n";
}
return ret;
}
static 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);
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);
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);
int program = gl.glCreateProgram();
gl.glAttachShader(program, vs);
gl.glAttachShader(program, fs);
gl.glLinkProgram(program);
gl.glValidateProgram(program);
gl.glUseProgram(program);
programs.put(new AbstractMap.SimpleEntry<String, String>(vsName, fsName), program);
return true;
}
}
|