Skip to content

Instantly share code, notes, and snippets.

@ar-android
Created May 19, 2019 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ar-android/1179b0c9aed0de055fa407611bc4d962 to your computer and use it in GitHub Desktop.
Save ar-android/1179b0c9aed0de055fa407611bc4d962 to your computer and use it in GitHub Desktop.
package engine.graphic;
import static org.lwjgl.opengl.GL20.*;
public class ShaderProgram {
private int programId;
private int vertexId;
private int fragmentId;
public ShaderProgram() throws Exception{
programId = glCreateProgram();
if (programId == 0) {
throw new Exception("Could not create shader.");
}
}
public void createVertexShader(String shaderCode) throws Exception {
vertexId = createShader(shaderCode, GL_VERTEX_SHADER);
}
public void createFragmentShader(String shaderCode) throws Exception {
fragmentId = createShader(shaderCode, GL_FRAGMENT_SHADER);
}
private int createShader(String shaderCode, int shaderType) throws Exception{
int shaderId = glCreateShader(shaderType);
if (shaderId == 0)
throw new Exception("Error creating shader. Type: " + shaderType);
glShaderSource(shaderId, shaderCode);
glCompileShader(shaderId);
if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0)
throw new Exception("Error compiling shader code :" + glGetShaderInfoLog(shaderId, 1024));
glAttachShader(programId, shaderId);
return shaderId;
}
public void link() throws Exception {
glLinkProgram(programId);
if (glGetProgrami(programId, GL_LINK_STATUS) == 0)
throw new Exception("Error linking Shader Code: " + glGetProgramInfoLog(programId, 1024));
if (vertexId != 0)
glDetachShader(programId, vertexId);
if (fragmentId != 0)
glDetachShader(programId, fragmentId);
glValidateProgram(programId);
if (glGetProgrami(programId, GL_VALIDATE_STATUS) == 0)
System.err.println("Warning validating shader code: "+ glGetProgramInfoLog(programId, 1024));
}
public void bind() {
glUseProgram(programId);
}
public void unbind() {
glUseProgram(0);
}
public void cleanup() {
unbind();
if (programId != 0)
glDeleteProgram(programId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment