Skip to content

Instantly share code, notes, and snippets.

@thetooth
Created March 21, 2014 01:46
Show Gist options
  • Save thetooth/9677878 to your computer and use it in GitHub Desktop.
Save thetooth/9677878 to your computer and use it in GitHub Desktop.
class Shader {
public:
GLuint shader;
Shader() = default;
Shader(GLuint &shaderProgram, GLuint shaderType, std::string shaderSource){
compile(shaderProgram, shaderType, shaderSource);
};
~Shader() = default;
void compile(GLuint &shaderProgram, GLuint shaderType, std::string shaderSource){
shader = glCreateShader(shaderType);
const char *c_str = shaderSource.c_str();
glShaderSource(shader, 1, &c_str, 0);
glCompileShader(shader);
/* Error Checking */
glAttachShader(shaderProgram, shader);
}
};
class ShaderProgram {
public:
GLuint program;
std::vector<Shader> shaders;
ShaderProgram(){ program = glCreateProgram(); };
ShaderProgram(std::initializer_list<std::string> s) : ShaderProgram() {
if (s.size() > 3){
/* Error Checking */
}
int i = 0;
std::vector<int> type = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_GEOMETRY_SHADER };
for (auto shader : s){
create(type[i], shader.c_str()); i++;
}
};
~ShaderProgram(){ glDeleteProgram(program); };
void create(GLuint type, const char* shader){
std::ifstream shaderFile(shader, std::ios::in);
if (shaderFile.bad()){
/* Error Checking */
}
std::string shaderSource((std::istreambuf_iterator<char>(shaderFile)),
std::istreambuf_iterator<char>());
shaders.push_back(Shader(program, type, shaderSource));
}
GLuint link(){
glLinkProgram(program);
/* Error Checking */
return program;
}
void bind(){
glUseProgram(program);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment