Shader.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  3. * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Shader.hpp"
  19. #include "System.hpp"
  20. #include <fstream>
  21. #include <sstream>
  22. Shader::Shader(const std::string &vShaderPath,
  23. const std::string &fShaderPath)
  24. {
  25. // Load the code from the files.
  26. std::string vShaderCode, fShaderCode;
  27. std::ifstream vShaderFile, fShaderFile;
  28. vShaderFile.exceptions(std::ifstream::failbit bitor
  29. std::ifstream::badbit);
  30. fShaderFile.exceptions(std::ifstream::failbit bitor
  31. std::ifstream::badbit);
  32. try
  33. {
  34. vShaderFile.open(vShaderPath);
  35. fShaderFile.open(fShaderPath);
  36. std::stringstream vShaderStream, fShaderStream;
  37. vShaderStream << vShaderFile.rdbuf();
  38. fShaderStream << fShaderFile.rdbuf();
  39. vShaderFile.close();
  40. fShaderFile.close();
  41. vShaderCode = vShaderStream.str();
  42. fShaderCode = fShaderStream.str();
  43. }
  44. catch(const std::exception &e)
  45. {
  46. System::logger->writeError(
  47. std::string("Failed to read from file: ") +
  48. e.what());
  49. }
  50. // Compile the shaders
  51. unsigned int vertexId, fragmentId;
  52. int res;
  53. char infoLog[512];
  54. vertexId = glCreateShader(GL_VERTEX_SHADER);
  55. {
  56. const char *vShaderCodeChar = vShaderCode.c_str();
  57. glShaderSource(vertexId, 1,
  58. &vShaderCodeChar, nullptr);
  59. }
  60. glCompileShader(vertexId);
  61. glGetShaderiv(vertexId, GL_COMPILE_STATUS, &res);
  62. if(not res)
  63. {
  64. glGetShaderInfoLog(vertexId, 512, nullptr, infoLog);
  65. System::logger->writeError(
  66. std::string(
  67. "Vertex shader compilation failed: ") +
  68. infoLog);
  69. }
  70. fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
  71. {
  72. const char *fShaderCodeChar = fShaderCode.c_str();
  73. glShaderSource(fragmentId, 1,
  74. &fShaderCodeChar, nullptr);
  75. }
  76. glCompileShader(fragmentId);
  77. glGetShaderiv(fragmentId, GL_COMPILE_STATUS, &res);
  78. if(not res)
  79. {
  80. glGetShaderInfoLog(fragmentId, 512, nullptr, infoLog);
  81. System::logger->writeError(
  82. std::string(
  83. "Fragment shader compilation failed: ") +
  84. infoLog);
  85. }
  86. // Link the program
  87. id = glCreateProgram();
  88. glAttachShader(id, vertexId);
  89. glAttachShader(id, fragmentId);
  90. glLinkProgram(id);
  91. glGetProgramiv(id, GL_LINK_STATUS, &res);
  92. if(not res)
  93. {
  94. glGetProgramInfoLog(id, 512, nullptr, infoLog);
  95. System::logger->writeError(
  96. std::string("Failed to link program: ") +
  97. infoLog);
  98. }
  99. // Delete the shaders
  100. glDeleteShader(vertexId);
  101. glDeleteShader(fragmentId);
  102. }