Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package space.engine;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.lwjgl.opengl.GL20;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.util.ResourceLoader;
- public class ShaderProgram {
- private String vertSource;
- private String fragSource;
- private int vert;
- private int frag;
- private int program;
- private String log = "";
- private final int INFO_LENGTH = 1000;
- public static ShaderProgram loadProgram(String vertFile, String fragFile) throws SlickException {
- return new ShaderProgram(load(vertFile), load(fragFile));
- }
- public static String load(String ref) throws SlickException {
- InputStream in = ResourceLoader.getResourceAsStream(ref);
- try { return load(in); }
- catch (SlickException e) {
- throw new SlickException("could not load source file: "+ref);
- }
- }
- public static String load(InputStream in) throws SlickException{
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String line = "";
- String txt = "";
- while ((line=br.readLine()) != null)
- txt += line + "\n";
- br.close();
- return txt.trim();
- } catch (IOException e) {
- throw new SlickException("could not load source file");
- }
- }
- public ShaderProgram(String vertexShader, String fragShader) {
- if (vertexShader==null || fragShader==null)
- throw new IllegalArgumentException("shaders must be non-null");
- this.vertSource = vertexShader;
- this.fragSource = fragShader;
- this.program = compile();
- }
- public String getLog() {
- return log;
- }
- public void unbind() {
- GL20.glUseProgram(0);
- }
- public void bind() {
- if (!isCompiled())
- throw new IllegalStateException("trying to enable a program that did not compile");
- GL20.glUseProgram(program);
- }
- public boolean isCompiled() {
- return program!=0;
- }
- private int createShader(int type, String source) {
- int shader = GL20.glCreateShader(type);
- if (shader==0) return 0;
- GL20.glShaderSource(shader, source);
- GL20.glCompileShader(shader);
- int comp = GL20.glGetShader(shader, GL20.GL_COMPILE_STATUS);
- if (comp==0) {
- log += GL20.glGetShaderInfoLog(shader, INFO_LENGTH);
- return 0;
- }
- return shader;
- }
- private int compile() {
- vert = createShader(GL20.GL_VERTEX_SHADER, vertSource);
- frag = createShader(GL20.GL_FRAGMENT_SHADER, fragSource);
- if (vert==0 || frag==0)
- return 0;
- int program = GL20.glCreateProgram();
- GL20.glAttachShader(program, vert);
- GL20.glAttachShader(program, frag);
- GL20.glLinkProgram(program);
- //GL20.glValidateProgram(program);
- int comp = GL20.glGetProgram(program, GL20.GL_LINK_STATUS);
- if (comp==0) {
- log += GL20.glGetProgramInfoLog(program, INFO_LENGTH);
- return 0;
- }
- return program;
- }
- public int getUniformID(String name) {
- return GL20.glGetUniformLocation(program, name);
- }
- public void setUniform1f(int id, float f) {
- GL20.glUniform1f(id, f);
- }
- public void setUniform1i(int id, int i) {
- GL20.glUniform1i(id, i);
- }
- public void setUniform2f(int id, float a, float b) {
- GL20.glUniform2f(id, a, b);
- }
- public void setUniform3f(int id, float a, float b, float c) {
- GL20.glUniform3f(id, a, b, c);
- }
- public void setUniform4f(int id, float a, float b, float c, float d) {
- GL20.glUniform4f(id, a, b, c, d);
- }
- // TODO: include more setUniforms/getUniforms
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement