Masterchoc

Untitled

Jan 4th, 2018
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "shader.h"
  2.  
  3. Shader::Shader(const std::string& filename)
  4. {
  5.     m_program = glCreateProgram();
  6.     m_shaders[0] = createShader(loadShader(filename + "_VS.glsl"), GL_VERTEX_SHADER);
  7.     m_shaders[1] = createShader(loadShader(filename + "_FS.glsl"), GL_FRAGMENT_SHADER);
  8.  
  9.     for (unsigned int i = 0; i < NUM_SHADERS; ++i)
  10.         glAttachShader(m_program, m_shaders[i]);
  11.  
  12.     glLinkProgram(m_program);
  13.     checkShaderError(m_program, GL_LINK_STATUS, true, "Shader Error: program linking failed.");
  14.  
  15.     glValidateProgram(m_program);
  16.     checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Shader Error: the program is invalid.");
  17. }
  18.  
  19. GLuint Shader::createShader(const std::string& text, GLenum type)
  20. {
  21.     GLuint shader = glCreateShader(type);
  22.  
  23.     if (shader == 0)
  24.         std::cerr << "Shader Error: shader creation failed." << std::endl;
  25.  
  26.     const GLchar* sourceStrings[1];
  27.     GLint sourceStringLengths[1];
  28.     sourceStrings[0] = text.c_str();
  29.     sourceStringLengths[0] = text.length();
  30.  
  31.     glShaderSource(shader, 1, sourceStrings, sourceStringLengths);
  32.     glCompileShader(shader);
  33.  
  34.     checkShaderError(shader, GL_COMPILE_STATUS, false, "Shader Error: shader compilation failed.");
  35.  
  36.     return shader;
  37. }
  38.  
  39. void Shader::addUniform(const std::string& name)
  40. {
  41.     m_uniforms[name] = glGetUniformLocation(m_program, name.c_str());
  42. }
  43.  
  44. void Shader::setUniformMat4fv(const std::string& name, const glm::mat4& mat)
  45. {
  46.     glUniformMatrix4fv(m_uniforms[name], 1, GL_FALSE, &mat[0][0]);
  47. }
  48.  
  49. void Shader::setUniform3fv(const std::string& name, const glm::vec3& vec)
  50. {
  51.     glUniform3fv(m_uniforms[name], 1, &vec[0]);
  52. }
  53.  
  54. void Shader::bind()
  55. {
  56.     glUseProgram(m_program);
  57. }  
  58. void Shader::unbind()
  59. {
  60.     glUseProgram(0);
  61. }
  62.  
  63. void Shader::bindAttribute(GLuint location, const std::string& name)
  64. {
  65.     glBindAttribLocation(m_program, location, name.c_str());
  66. }
  67.  
  68. std::string Shader::loadShader(const std::string& filename)
  69. {
  70.     std::ifstream file;
  71.     file.open(filename.c_str());
  72.  
  73.     std::string output;
  74.     std::string line;
  75.  
  76.     if (file.is_open()) {
  77.         while (file.good()) {
  78.             getline(file, line);
  79.             output.append(line + "\n");
  80.         }
  81.     }
  82.     else {
  83.         std::cerr << "Unable to load shader: " << filename << std::endl;
  84.     }
  85.  
  86.     return output;
  87. }
  88.  
  89. void Shader::checkShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& message)
  90. {
  91.     GLint success = 0;
  92.     GLchar error[1024] = { 0 };
  93.  
  94.     if (isProgram)
  95.         glGetProgramiv(shader, flag, &success);
  96.     else
  97.         glGetShaderiv(shader, flag, &success);
  98.  
  99.     if (success == GL_FALSE) {
  100.         if (isProgram)
  101.             glGetProgramInfoLog(shader, sizeof(error), NULL, error);
  102.         else
  103.             glGetShaderInfoLog(shader, sizeof(error), NULL, error);
  104.  
  105.         std::cerr << message << ": '" << error << "'" << std::endl;
  106.     }
  107. }
  108.  
  109. Shader::~Shader()
  110. {
  111.     for (unsigned int i = 0; i < NUM_SHADERS; ++i) {
  112.         glDetachShader(m_program, m_shaders[i]);
  113.         glDeleteShader(m_shaders[i]);
  114.     }
  115.  
  116.     glDeleteProgram(m_program);
  117. }
RAW Paste Data