Skip to content

Commit

Permalink
refactor: add compileShaders in Shader constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhus22 committed Jun 3, 2024
1 parent 2dedc45 commit f1fca16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
67 changes: 26 additions & 41 deletions pyaccell/pyaccell/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,14 @@ pyaccell::Shader::Shader(const char* vertexPath, const char* fragmentPath)
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
// compile shaders
unsigned int vertex, fragment;

vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");

fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");

ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
compileShaders(vShaderCode, fShaderCode);
}

pyaccell::Shader::Shader(const std::string &vs, const std::string &fs)
{
const char* vShaderCode = vs.c_str();
const char* vShaderCode = vs.c_str();
const char* fShaderCode = fs.c_str();
// compile shaders
unsigned int vertex, fragment;

vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");

fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");

ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
compileShaders(vShaderCode, fShaderCode);
}
// activate the shader
void pyaccell::Shader::use()
Expand Down Expand Up @@ -137,6 +99,29 @@ void pyaccell::Shader::setMat4(const std::string &name, const glm::mat4 &mat) co
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}

void pyaccell::Shader::compileShaders(const char *vShaderCode, const char *fShaderCode)
{
unsigned int vertex, fragment;

vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");

fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");

ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
}

// utility function for checking shader compilation/linking errors.
void pyaccell::Shader::checkCompileErrors(unsigned int shader, std::string type)
{
Expand Down
1 change: 1 addition & 0 deletions pyaccell/pyaccell/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace pyaccell {
void setMat3(const std::string &name, const glm::mat3 &mat) const;
void setMat4(const std::string &name, const glm::mat4 &mat) const;
private:
void compileShaders(const char *vShaderCode, const char *fShaderCode);
void checkCompileErrors(unsigned int shader, std::string type);
};
}
Expand Down

0 comments on commit f1fca16

Please sign in to comment.