Giuseppe499

ShaderProgram.java

Oct 27th, 2015
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.main;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import static org.lwjgl.opengl.GL20.*;
  5.  
  6. public class ShaderProgram {
  7.    
  8.     private int programID;
  9.     private int vertexShaderID;
  10.     private int fragmentShaderID;
  11.    
  12.     public ShaderProgram() {
  13.         programID = glCreateProgram();
  14.     }
  15.    
  16.     public void attachVertexShader(String name)
  17.     {
  18.         // Load the source
  19.         String vertexShaderSource = FileUtil.readFromFile(name);
  20.  
  21.         // Create the shader and set the source
  22.         vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  23.         glShaderSource(vertexShaderID, vertexShaderSource);
  24.  
  25.         // Compile the shader
  26.         glCompileShader(vertexShaderID);
  27.  
  28.         // Check for errors
  29.         if (glGetShaderi(vertexShaderID, GL_COMPILE_STATUS) == GL_FALSE)
  30.             throw new RuntimeException("Error creating vertex shader\n"
  31.                                        + glGetShaderInfoLog(vertexShaderID, glGetShaderi(vertexShaderID, GL_INFO_LOG_LENGTH)));
  32.  
  33.         // Attach the shader
  34.         glAttachShader(programID, vertexShaderID);
  35.     }
  36.    
  37.     public void attachFragmentShader(String name)
  38.     {
  39.         // Load the source
  40.         String fragmentShaderSource = FileUtil.readFromFile(name);
  41.  
  42.         // Create the shader and set the source
  43.         fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  44.         glShaderSource(fragmentShaderID, fragmentShaderSource);
  45.  
  46.         // Compile the shader
  47.         glCompileShader(fragmentShaderID);
  48.  
  49.         // Check for errors
  50.         if (glGetShaderi(fragmentShaderID, GL_COMPILE_STATUS) == GL_FALSE)
  51.             throw new RuntimeException("Error creating vertex shader\n"
  52.                                        + glGetShaderInfoLog(fragmentShaderID, glGetShaderi(fragmentShaderID, GL_INFO_LOG_LENGTH)));
  53.  
  54.         // Attach the shader
  55.         glAttachShader(programID, fragmentShaderID);
  56.     }
  57.    
  58.     public void link()
  59.     {
  60.         // Link this program
  61.         glLinkProgram(programID);
  62.  
  63.         // Check for linking errors
  64.         if (glGetProgrami(programID, GL_LINK_STATUS) == GL_FALSE)
  65.             throw new RuntimeException("Unable to link shader program:");
  66.     }
  67.    
  68.     public void bind()
  69.     {
  70.         glUseProgram(programID);
  71.     }
  72.    
  73.     public void unbind()
  74.     {
  75.         glUseProgram(0);
  76.     }
  77.    
  78.      public void dispose()
  79.         {
  80.             // Unbind the program
  81.             unbind();
  82.  
  83.             // Detach the shaders
  84.             glDetachShader(programID, vertexShaderID);
  85.             glDetachShader(programID, fragmentShaderID);
  86.  
  87.             // Delete the shaders
  88.             glDeleteShader(vertexShaderID);
  89.             glDeleteShader(fragmentShaderID);
  90.  
  91.             // Delete the program
  92.             glDeleteProgram(programID);
  93.         }
  94.      
  95.      public int getID()
  96.         {
  97.             return programID;
  98.         }
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×