SHARE
TWEET

Untitled

a guest Mar 21st, 2012 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package space.engine;
  2.  
  3. import org.lwjgl.opengl.ARBShaderObjects;
  4. import org.lwjgl.opengl.GL11;
  5. import org.lwjgl.opengl.GL20;
  6. import org.lwjgl.opengl.GL32;
  7. import org.newdawn.slick.SlickException;
  8. import org.newdawn.slick.util.Log;
  9.  
  10. public class Shader {
  11.        
  12.         public static final int VERTEX_SHADER = GL20.GL_VERTEX_SHADER;
  13.         public static final int FRAGMENT_SHADER = GL20.GL_FRAGMENT_SHADER;
  14.         public static final int GEOMETRY_SHADER = GL32.GL_GEOMETRY_SHADER;
  15.        
  16.         protected String source;
  17.         protected int type;
  18.         protected int id;
  19.         protected String log = "";
  20.        
  21.         public Shader(int type, String source) throws SlickException {
  22.                 if (source==null)
  23.                         throw new IllegalArgumentException("shader source code must not be null");
  24.                 this.source = source;
  25.                 this.type = type;
  26.                 id = compileShader(type, source);
  27.         }
  28.        
  29.         /**
  30.          * Implementations may wish to override Shader for specific functionality; e.g. creating
  31.          * a tesselation shader or creating a single vertex shader made up of multiple
  32.          * mix-and-matched Shader objects.
  33.          */
  34.         protected Shader() { }
  35.  
  36.         public boolean valid() {
  37.                 return id!=0;
  38.         }
  39.        
  40.         void attach(ShaderProgram program) {
  41.         ARBShaderObjects.glAttachObjectARB(program.getID(), getID());
  42.         }
  43.        
  44.         void detach(ShaderProgram program) {
  45.                 ARBShaderObjects.glDetachObjectARB(program.getID(), getID());
  46.         }
  47.        
  48.         private String shaderTypeString(int type) {
  49.                 if (type==FRAGMENT_SHADER) return "FRAGMENT_SHADER";
  50.                 if (type==GEOMETRY_SHADER) return "GEOMETRY_SHADER";
  51.                 else if (type==VERTEX_SHADER) return "VERTEX_SHADER";
  52.                 else return "shader";
  53.         }
  54.        
  55.         protected int compileShader(int type, String source) throws SlickException {
  56.                 int shader = ARBShaderObjects.glCreateShaderObjectARB(type);
  57.                 if (shader==0)
  58.                         throw new SlickException("could not create shader object; check ShaderProgram.isSupported()");
  59.                 ARBShaderObjects.glShaderSourceARB(shader, source);
  60.                 ARBShaderObjects.glCompileShaderARB(shader);
  61.                 int comp = ARBShaderObjects.glGetObjectParameteriARB(shader, GL20.GL_COMPILE_STATUS);
  62.                 int len = ARBShaderObjects.glGetObjectParameteriARB(shader, GL20.GL_INFO_LOG_LENGTH);
  63.                 log = ARBShaderObjects.glGetInfoLogARB(shader, len);
  64.                 if (comp==GL11.GL_FALSE)
  65.                         throw new SlickException("ERROR: Compiler error in "+shaderTypeString(type)+"\n"+log);
  66.                 else if (log!=null&&log.length()!=0)
  67.                         Log.warn("GLSL shader compile warning: "+log);
  68.                 return shader;
  69.         }
  70.        
  71.         public String getCompileLog() {
  72.                 return log;
  73.         }
  74.        
  75.         public void release() {
  76.                 if (id!=0) {
  77.                         ARBShaderObjects.glDeleteObjectARB(id);
  78.                         id = 0;
  79.                 }
  80.         }
  81.        
  82.         public int getID() {
  83.                 return id;
  84.         }
  85.        
  86.         public int getType() {
  87.                 return type;
  88.         }
  89.        
  90.         public String getSource() {
  91.                 return source;
  92.         }
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top