Skip to content

Commit

Permalink
refactor(Shader): got rid of code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
ms0g committed Apr 19, 2024
1 parent 8089666 commit 979f7d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
39 changes: 20 additions & 19 deletions src/shader/shader.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#include "shader.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include "glad/glad.h" // include glad to get all the required OpenGL headers

#define CHECK_ERR(ivFunc, status, infoLogFunc, errStr) { \
ivFunc(shader, status, &success); \
if (!success) { \
glDeleteShader(shader); \
infoLogFunc(shader, 1024, nullptr, infoLog); \
auto errMsg = std::string(errStr) + "\n" + infoLog; \
throw std::runtime_error(errMsg); \
} \
}

Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) {
// 1. retrieve the vertex/fragment source code from filePath
Expand Down Expand Up @@ -32,7 +41,8 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) {
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
auto errMsg = std::string("ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: ") + e.what();
throw std::runtime_error(errMsg);
}

const char* vShaderCode = vertexCode.c_str();
Expand All @@ -50,7 +60,8 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) {
glAttachShader(mID, vertex);
glAttachShader(mID, fragment);
glLinkProgram(mID);
checkCompileErrors(mID, 0);

checkErrors(mID, ErrorType::LINKING);

// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
Expand Down Expand Up @@ -127,29 +138,19 @@ unsigned int Shader::createShader(const char** source, unsigned int type) {
glShaderSource(shader, 1, source, nullptr);
glCompileShader(shader);

checkCompileErrors(shader, type);
checkErrors(shader, ErrorType::COMPILE);

return shader;
}

void Shader::checkCompileErrors(unsigned int shader, unsigned int type) {
int success;
void Shader::checkErrors(unsigned int shader, ErrorType type) {
GLint success;
char infoLog[1024];

if (type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog
<< "\n -- --------------------------------------------------- -- " << std::endl;
}
if (type == ErrorType::COMPILE) {
CHECK_ERR(glGetShaderiv, GL_COMPILE_STATUS, glGetShaderInfoLog, "ERROR::SHADER_COMPILATION_ERROR:")
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, nullptr, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog
<< "\n -- --------------------------------------------------- -- " << std::endl;
}
CHECK_ERR(glGetProgramiv, GL_LINK_STATUS, glGetProgramInfoLog, "ERROR::PROGRAM_LINKING_ERROR:")
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/shader/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <string>
#include <glm/glm.hpp>

enum class ErrorType {
COMPILE,
LINKING
};

class Shader {
public:
Expand Down Expand Up @@ -43,7 +47,7 @@ class Shader {
private:
unsigned int createShader(const char** source, unsigned int type);

void checkCompileErrors(unsigned int shader, unsigned int type);
void checkErrors(unsigned int shader, ErrorType type);

// the program ID
unsigned int mID;
Expand Down

0 comments on commit 979f7d5

Please sign in to comment.