Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "GLSLProgram.h"
  2. #include "Errors.h"
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. GLSLProgram::GLSLProgram() : _numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
  7. {
  8.    
  9. }
  10.  
  11.  
  12. GLSLProgram::~GLSLProgram()
  13. {
  14.    
  15. }
  16.  
  17. void GLSLProgram::compileShaders(const std::string& vertextShaderFilePath, const std::string& fragmentShaderFilepath)
  18. {
  19.     _vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  20.     if (_vertexShaderID == 0)
  21.     {
  22.         fatalError("Vertex shader failed to be created");
  23.     }
  24.  
  25.     _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  26.     if (_fragmentShaderID == 0)
  27.     {
  28.         fatalError("Fragment shader failed to be created");
  29.     }
  30.  
  31.     compileShader(vertextShaderFilePath, _vertexShaderID);
  32.     compileShader(fragmentShaderFilepath, _fragmentShaderID);
  33.  
  34. }
  35.  
  36. void GLSLProgram::linkShaders()
  37. {
  38.     //Attach our shaders to our program
  39.     glAttachShader(_programID, _vertexShaderID);
  40.     glAttachShader(_programID, _fragmentShaderID);
  41.  
  42.     //Link our program
  43.     glLinkProgram(_programID);
  44.  
  45.     //Note the different functions here: glGetProgram* instead of glGetShader*.
  46.     GLint isLinked = 0;
  47.     glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
  48.     if (isLinked == GL_FALSE)
  49.     {
  50.         GLint maxLength = 0;
  51.         glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
  52.  
  53.         //The maxLength includes the NULL character
  54.         std::vector<char> errorLog(maxLength);
  55.         glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
  56.  
  57.         //We don't need the program anymore.
  58.         glDeleteProgram(_programID);
  59.         //Don't leak shaders either.
  60.         glDeleteShader(_vertexShaderID);
  61.         glDeleteShader(_fragmentShaderID);
  62.  
  63.         std::printf("%s\n", &(errorLog[0]));
  64.         fatalError("Shaders failed to link!");
  65.     }
  66.  
  67.     //Always detach shaders after a successful link.
  68.     glDetachShader(_programID, _vertexShaderID);
  69.     glDetachShader(_programID, _fragmentShaderID);
  70.     glDeleteShader(_vertexShaderID);
  71.     glDeleteShader(_fragmentShaderID);
  72. }
  73.  
  74. void GLSLProgram::addAttribute(const std::string& attributeName)
  75. {
  76.     glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
  77. }
  78.  
  79. void GLSLProgram::use()
  80. {
  81.     glUseProgram(_programID);
  82.     for (int i = 0; i < _numAttributes; i++)
  83.     {
  84.         glEnableVertexAttribArray(i);
  85.     }
  86. }
  87.  
  88. void GLSLProgram::unuse()
  89. {
  90.     glUseProgram(0);
  91.     for (int i = 0; i < _numAttributes; i++)
  92.     {
  93.         glDisableVertexAttribArray(i);
  94.     }
  95. }
  96.  
  97. void GLSLProgram::compileShader(const std::string& filePath, GLuint id)
  98. {
  99.     //Vertex and fragment shaders are successfully compiled.
  100.     //Now time to link them together into a program.
  101.     //Get a program object.
  102.     _programID = glCreateProgram();
  103.  
  104.     std::ifstream vertexFile(filePath);
  105.     if (vertexFile.fail())
  106.     {
  107.         perror(filePath.c_str());
  108.         fatalError("Failed to open " + filePath);
  109.     }
  110.  
  111.     std::string fileContents = "";
  112.     std::string line;
  113.  
  114.     while (std::getline(vertexFile, line))
  115.     {
  116.         fileContents += line + "\n";
  117.     }
  118.  
  119.     vertexFile.close();
  120.  
  121.     const char* contentsPtr = fileContents.c_str();
  122.     glShaderSource(id, 1, &contentsPtr, nullptr);
  123.  
  124.     glCompileShader(id);
  125.  
  126.     GLint success = 0;
  127.     glGetShaderiv(id, GL_COMPILE_STATUS, &success);
  128.  
  129.     if (success == GL_FALSE)
  130.     {
  131.         GLint maxLength = 0;
  132.         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
  133.  
  134.         std::vector<char> errorLog(maxLength);
  135.         glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
  136.  
  137.         glDeleteShader(id);
  138.  
  139.         std::printf("%s\n", &(errorLog[0]));
  140.         fatalError("Shader " + filePath + "failed to compile");
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement