Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - package ecumene.opengl;
 - import java.io.BufferedReader;
 - import java.io.File;
 - import java.io.FileReader;
 - import java.nio.FloatBuffer;
 - import java.nio.IntBuffer;
 - import java.util.HashMap;
 - import org.lwjgl.BufferUtils;
 - import org.lwjgl.opengl.GL11;
 - import org.lwjgl.opengl.GL20;
 - import org.lwjgl.opengl.Util;
 - import ecumene.EcuException;
 - /**
 - * Used for loading GLSL shader program sources from
 - * files then loading them into OpenGL with their
 - * correct custom, and primitive GLSL syntax.
 - *
 - * @author Ecumene
 - */
 - public class Shader {
 - /**Shader program pointer*/
 - private int programID;
 - /** Holds the shader sources*/
 - private HashMap<File, Integer> shaderSources;
 - /**Initializes a shader with empty parameters*/
 - public Shader(){
 - programID = GL20.glCreateProgram();
 - shaderSources = new HashMap<>();
 - }
 - /**
 - * Attach a shader source for linking
 - *
 - * @param file The file name of the shader source
 - * @param type The shader type
 - * @see Shader#link();
 - */
 - public void attachShaderSource(File file, int type){
 - String shaderSource = sourceFromFile(file);
 - int shader;
 - shader = GL20.glCreateShader(type);
 - GL20.glShaderSource(shader, shaderSource);
 - GL20.glCompileShader(shader);
 - if (GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE)
 - {
 - System.err.println("Unable to compile shader:");
 - System.err.println(GL20.glGetShaderInfoLog(shader, GL20.glGetShaderi(shader, GL20.GL_INFO_LOG_LENGTH)));
 - dispose();
 - throw new EcuException("Unable to compile shader, GLSL error");
 - }
 - GL20.glAttachShader(programID, shader);
 - Util.checkGLError();
 - shaderSources.put(file, shader);
 - }
 - /**
 - * Links the shader program
 - * @see Shader#attachShaderSource(File file, int type);
 - */
 - public void link(){
 - GL20.glLinkProgram(programID);
 - GL20.glValidateProgram(programID);
 - if (GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS) == GL11.GL_FALSE)
 - {
 - dispose();
 - throw new EcuException("Unable to link shader program.");
 - }
 - Util.checkGLError();
 - }
 - /**
 - * Binds the shader program
 - */
 - public void bind(){
 - GL20.glUseProgram(programID);
 - }
 - /**
 - * Unbinds the shader program
 - */
 - public static void unbind(){
 - GL20.glUseProgram(0);
 - }
 - /**
 - * Removes the shader program from the ram, and unbinds it
 - */
 - public void dispose()
 - {
 - unbind();
 - for(Integer i : shaderSources.values()){
 - GL20.glDetachShader(programID, i);
 - GL20.glDeleteShader(i);
 - }
 - GL20.glDeleteProgram(programID);
 - }
 - /**
 - * Sets a uniform float
 - * @param name The uniform's name
 - * @param values The uniforms values
 - */
 - public void setUniformf(String name, float ... values){
 - if(values.length > 4){
 - throw new EcuException("Too many variables! (x>4)");
 - }
 - FloatBuffer floats = BufferUtils.createFloatBuffer(values.length);
 - floats.put(values);
 - floats.flip();
 - int location = GL20.glGetUniformLocation(programID, name);
 - switch(values.length){
 - case 1: GL20.glUniform1(location, floats);
 - case 2: GL20.glUniform2(location, floats);
 - case 3: GL20.glUniform3(location, floats);
 - case 4: GL20.glUniform4(location, floats);
 - }
 - }
 - /**
 - * Sets a uniform integer
 - * @param name The uniform's name
 - * @param values The uniforms values
 - */
 - public void setUniformi(String name, int ... values){
 - if(values.length > 4){
 - throw new EcuException("Too many variables! (x>4)");
 - }
 - IntBuffer ints = BufferUtils.createIntBuffer(values.length);
 - ints.put(values);
 - ints.flip();
 - int location = GL20.glGetUniformLocation(programID, name);
 - switch(values.length){
 - case 1: GL20.glUniform1(location, ints);
 - case 2: GL20.glUniform2(location, ints);
 - case 3: GL20.glUniform3(location, ints);
 - case 4: GL20.glUniform4(location, ints);
 - }
 - }
 - /**
 - * Binds a vertex attribute to a ID
 - * @param name The attribute's name
 - * @param id The attributre's ID
 - */
 - public void bindAttribute(int id, String name){
 - GL20.glBindAttribLocation(programID, id, name);
 - }
 - /**
 - * <a href="https://github.com/BennyQBD/3DGameEngine/blob/master/src/com/base/engine/rendering/Shader.java">Special thanks to TheBennyBox (BennyQBD)</a>
 - * Loads text from a file and parses for a '#include', then matches it with the correct included file
 - */
 - private static String sourceFromFile(File file)
 - {
 - StringBuilder shaderSource = new StringBuilder();
 - BufferedReader shaderReader = null;
 - final String INCLUDE_DIRECTIVE = "#include";
 - try
 - {
 - shaderReader = new BufferedReader(new FileReader(file));
 - String line;
 - while((line = shaderReader.readLine()) != null)
 - {
 - if(line.startsWith(INCLUDE_DIRECTIVE))
 - {
 - shaderSource.append(sourceFromFile(new File(line.substring(INCLUDE_DIRECTIVE.length() + 2, line.length() - 1))));
 - }
 - else
 - shaderSource.append(line).append("\n");
 - }
 - shaderReader.close();
 - }
 - catch(Exception e)
 - {
 - e.printStackTrace();
 - System.exit(1);
 - }
 - return shaderSource.toString();
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment