1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include "shader.h" #include <iostream> #include <fstream>
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage); static std::string LoadShader(const std::string& fileName); static GLuint CreateShader(const std::string& text, GLenum shaderType);
Shader::Shader(const std::string& fileName) { m_program = glCreateProgram(); m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER); m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++) { glAttachShader(m_program, m_shaders[i]); }
glLinkProgram(m_program); CheckShaderError(m_program, GL_LINK_STATUS, true, "Error: Program linking failed!");
glValidateProgram(m_program); CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Program invalid!");
}
Shader::~Shader() { for (unsigned int i = 0; i < NUM_SHADERS; i++) { glDetachShader(m_program, m_shaders[i]); glDeleteShader(m_shaders[i]); }
glDeleteProgram(m_program); }
void Shader::Bind() { glUseProgram(m_program); }
static GLuint CreateShader(const std::string& text, GLenum shaderType) { GLuint shader = glCreateShader(shaderType);
if (shader == 0) std::cerr << "Error: Shader creation failed!" << std::endl;
const GLchar* shaderSourceStrings[1]; GLint shaderSourceStringLengths[1];
shaderSourceStrings[0] = text.c_str();
glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths); glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error: Shader compilation failed!");
return shader; }
static std::string LoadShader(const std::string& fileName) { std::ifstream file; file.open((fileName).c_str());
std::string output; std::string line;
if (file.is_open()) { while (file.good()) { getline(file, line); output.append(line + "\n"); } } else { std::cerr << "Unable to load shader: " << fileName << std::endl; }
return output; }
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage) { GLint success = 0; GLchar error[1024] = { 0 };
if (isProgram) glGetProgramiv(shader, flag, &success); else glGetShaderiv(shader, flag, &success);
if (success == GL_FALSE) { if (isProgram) glGetProgramInfoLog(shader, sizeof(error), NULL, error); else glGetShaderInfoLog(shader, sizeof(error), NULL, error);
std::cerr << errorMessage << ": '" << error << "'" << std::endl; } }
|