Advertisement
Nightdra

Shader.h

Jul 14th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. #ifndef SHADER_H
  2. #define SHADER_H
  3.  
  4. #include <glad/glad.h>
  5.  
  6. #include <string>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <iostream>
  10.  
  11. class Shader
  12. {
  13. public:
  14.     unsigned int ID;
  15.     // constructor generates the shader on the fly
  16.     // ------------------------------------------------------------------------
  17.     Shader(const char* vertexPath, const char* fragmentPath)
  18.     {
  19.         // 1. retrieve the vertex/fragment source code from filePath
  20.         std::string vertexCode;
  21.         std::string fragmentCode;
  22.         std::ifstream vShaderFile;
  23.         std::ifstream fShaderFile;
  24.         // ensure ifstream objects can throw exceptions:
  25.         vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
  26.         fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
  27.         try
  28.         {
  29.             // open files
  30.             vShaderFile.open(vertexPath);
  31.             fShaderFile.open(fragmentPath);
  32.             std::stringstream vShaderStream, fShaderStream;
  33.             // read file's buffer contents into streams
  34.             vShaderStream << vShaderFile.rdbuf();
  35.             fShaderStream << fShaderFile.rdbuf();
  36.             // close file handlers
  37.             vShaderFile.close();
  38.             fShaderFile.close();
  39.             // convert stream into string
  40.             vertexCode   = vShaderStream.str();
  41.             fragmentCode = fShaderStream.str();
  42.         }
  43.         catch (std::ifstream::failure e)
  44.         {
  45.             std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
  46.         }
  47.         const char* vShaderCode = vertexCode.c_str();
  48.         const char * fShaderCode = fragmentCode.c_str();
  49.         // 2. compile shaders
  50.         unsigned int vertex, fragment;
  51.         int success;
  52.         char infoLog[512];
  53.         // vertex shader
  54.         vertex = glCreateShader(GL_VERTEX_SHADER);
  55.         glShaderSource(vertex, 1, &vShaderCode, NULL);
  56.         glCompileShader(vertex);
  57.         checkCompileErrors(vertex, "VERTEX");
  58.         // fragment Shader
  59.         fragment = glCreateShader(GL_FRAGMENT_SHADER);
  60.         glShaderSource(fragment, 1, &fShaderCode, NULL);
  61.         glCompileShader(fragment);
  62.         checkCompileErrors(fragment, "FRAGMENT");
  63.         // shader Program
  64.         ID = glCreateProgram();
  65.         glAttachShader(ID, vertex);
  66.         glAttachShader(ID, fragment);
  67.         glLinkProgram(ID);
  68.         checkCompileErrors(ID, "PROGRAM");
  69.         // delete the shaders as they're linked into our program now and no longer necessary
  70.         glDeleteShader(vertex);
  71.         glDeleteShader(fragment);
  72.     }
  73.     // activate the shader
  74.     // ------------------------------------------------------------------------
  75.     void use()
  76.     {
  77.         glUseProgram(ID);
  78.     }
  79.     // utility uniform functions
  80.     // ------------------------------------------------------------------------
  81.     void setBool(const std::string &name, bool value) const
  82.     {
  83.         glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
  84.     }
  85.     // ------------------------------------------------------------------------
  86.     void setInt(const std::string &name, int value) const
  87.     {
  88.         glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
  89.     }
  90.     // ------------------------------------------------------------------------
  91.     void setFloat(const std::string &name, float value) const
  92.     {
  93.         glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
  94.     }
  95.     void setVec2(const std::string &name, const glm::vec2 &value) const
  96.     {
  97.         glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
  98.     }
  99.     void setVec2(const std::string &name, float x, float y) const
  100.     {
  101.         glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
  102.     }
  103.     // ------------------------------------------------------------------------
  104.     void setVec3(const std::string &name, const glm::vec3 &value) const
  105.     {
  106.         glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
  107.     }
  108.     void setVec3(const std::string &name, float x, float y, float z) const
  109.     {
  110.         glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
  111.     }
  112.     // ------------------------------------------------------------------------
  113.     void setVec4(const std::string &name, const glm::vec4 &value) const
  114.     {
  115.         glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
  116.     }
  117.     void setVec4(const std::string &name, float x, float y, float z, float w)
  118.     {
  119.         glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
  120.     }
  121.     // ------------------------------------------------------------------------
  122.     void setMat2(const std::string &name, const glm::mat2 &mat) const
  123.     {
  124.         glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
  125.     }
  126.     // ------------------------------------------------------------------------
  127.     void setMat3(const std::string &name, const glm::mat3 &mat) const
  128.     {
  129.         glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
  130.     }
  131.     // ------------------------------------------------------------------------
  132.     void setMat4(const std::string &name, const glm::mat4 &mat) const
  133.     {
  134.         glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
  135.     }
  136.  
  137. private:
  138.     // utility function for checking shader compilation/linking errors.
  139.     // ------------------------------------------------------------------------
  140.     void checkCompileErrors(unsigned int shader, std::string type)
  141.     {
  142.         int success;
  143.         char infoLog[1024];
  144.         if (type != "PROGRAM")
  145.         {
  146.             glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  147.             if (!success)
  148.             {
  149.                 glGetShaderInfoLog(shader, 1024, NULL, infoLog);
  150.                 std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
  151.             }
  152.         }
  153.         else
  154.         {
  155.             glGetProgramiv(shader, GL_LINK_STATUS, &success);
  156.             if (!success)
  157.             {
  158.                 glGetProgramInfoLog(shader, 1024, NULL, infoLog);
  159.                 std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
  160.             }
  161.         }
  162.     }
  163. };
  164. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement