Advertisement
Guest User

Untitled

a guest
Mar 10th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package space.engine;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7.  
  8. import org.lwjgl.opengl.GL20;
  9. import org.newdawn.slick.SlickException;
  10. import org.newdawn.slick.util.ResourceLoader;
  11.  
  12. public class ShaderProgram {
  13.  
  14.     private String vertSource;
  15.     private String fragSource;
  16.    
  17.     private int vert;
  18.     private int frag;
  19.     private int program;
  20.    
  21.     private String log = "";
  22.     private final int INFO_LENGTH = 1000;
  23.    
  24.     public static ShaderProgram loadProgram(String vertFile, String fragFile) throws SlickException {
  25.         return new ShaderProgram(load(vertFile), load(fragFile));
  26.     }
  27.    
  28.     public static String load(String ref) throws SlickException {
  29.         InputStream in = ResourceLoader.getResourceAsStream(ref);
  30.         try { return load(in); }
  31.         catch (SlickException e) {
  32.             throw new SlickException("could not load source file: "+ref);
  33.         }
  34.     }
  35.    
  36.     public static String load(InputStream in) throws SlickException{
  37.         try {
  38.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  39.             String line = "";
  40.             String txt = "";
  41.             while ((line=br.readLine()) != null)
  42.                 txt += line + "\n";
  43.             br.close();
  44.             return txt.trim();
  45.         } catch (IOException e) {
  46.             throw new SlickException("could not load source file");
  47.         }
  48.     }
  49.    
  50.     public ShaderProgram(String vertexShader, String fragShader) {
  51.         if (vertexShader==null || fragShader==null)
  52.             throw new IllegalArgumentException("shaders must be non-null");
  53.         this.vertSource = vertexShader;
  54.         this.fragSource = fragShader;
  55.         this.program = compile();
  56.     }
  57.    
  58.     public String getLog() {
  59.         return log;
  60.     }
  61.    
  62.     public void unbind() {
  63.         GL20.glUseProgram(0);
  64.     }
  65.    
  66.     public void bind() {
  67.         if (!isCompiled())
  68.             throw new IllegalStateException("trying to enable a program that did not compile");
  69.         GL20.glUseProgram(program);
  70.     }
  71.    
  72.     public boolean isCompiled() {
  73.         return program!=0;
  74.     }
  75.    
  76.     private int createShader(int type, String source) {
  77.         int shader = GL20.glCreateShader(type);
  78.         if (shader==0) return 0;
  79.         GL20.glShaderSource(shader, source);
  80.         GL20.glCompileShader(shader);
  81.         int comp = GL20.glGetShader(shader, GL20.GL_COMPILE_STATUS);
  82.         if (comp==0) {
  83.             log += GL20.glGetShaderInfoLog(shader, INFO_LENGTH);
  84.             return 0;
  85.         }
  86.         return shader;
  87.     }
  88.    
  89.     private int compile() {
  90.         vert = createShader(GL20.GL_VERTEX_SHADER, vertSource);
  91.         frag = createShader(GL20.GL_FRAGMENT_SHADER, fragSource);
  92.         if (vert==0 || frag==0)
  93.             return 0;
  94.         int program = GL20.glCreateProgram();
  95.         GL20.glAttachShader(program, vert);
  96.         GL20.glAttachShader(program, frag);
  97.         GL20.glLinkProgram(program);
  98.        
  99.         //GL20.glValidateProgram(program);
  100.        
  101.         int comp = GL20.glGetProgram(program, GL20.GL_LINK_STATUS);
  102.         if (comp==0) {
  103.             log += GL20.glGetProgramInfoLog(program, INFO_LENGTH);
  104.             return 0;
  105.         }
  106.         return program;
  107.     }
  108.  
  109.    
  110.     public int getUniformID(String name) {
  111.         return GL20.glGetUniformLocation(program, name);
  112.     }
  113.    
  114.     public void setUniform1f(int id, float f) {
  115.         GL20.glUniform1f(id, f);
  116.     }
  117.    
  118.     public void setUniform1i(int id, int i) {
  119.         GL20.glUniform1i(id, i);
  120.     }
  121.    
  122.     public void setUniform2f(int id, float a, float b) {
  123.         GL20.glUniform2f(id, a, b);
  124.     }
  125.    
  126.     public void setUniform3f(int id, float a, float b, float c) {
  127.         GL20.glUniform3f(id, a, b, c);
  128.     }
  129.    
  130.     public void setUniform4f(int id, float a, float b, float c, float d) {
  131.         GL20.glUniform4f(id, a, b, c, d);
  132.     }
  133.  
  134.     // TODO: include more setUniforms/getUniforms
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement