Ramaraunt1

VriesShaderProgram class

May 4th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include "VriesShader.h"
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <iostream>
  6.  
  7. #include <GL/glew.h>
  8.  
  9. // Constructor generates the shader on the fly
  10. VriesShaderProgram::VriesShaderProgram(const GLchar* vertexPath, const GLchar* fragmentPath, const GLchar* geometryPath)
  11. {
  12. // 0. determine if geometry shader is used.
  13. this->geometryShaderYes = false;
  14. if (geometryPath != nullptr)
  15. {
  16. this->geometryShaderYes = true;
  17. }
  18.  
  19.  
  20. // 1. Retrieve the vertex/fragment (and maybe geometry) source code from filePath
  21.  
  22. std::string vertexCode;
  23. std::string fragmentCode;
  24. std::ifstream vShaderFile;
  25. std::ifstream fShaderFile;
  26. std::string geometryCode;
  27. std::ifstream gShaderFile;
  28. //ensures ifstream objects can throw exceptions:
  29. vShaderFile.exceptions(std::ifstream::badbit);
  30. fShaderFile.exceptions(std::ifstream::badbit);
  31. if (geometryShaderYes)
  32. {
  33. gShaderFile.exceptions(std::ifstream::badbit);
  34. }
  35.  
  36.  
  37. try
  38. {
  39. // Open files
  40. vShaderFile.open(vertexPath);
  41. fShaderFile.open(fragmentPath);
  42. std::stringstream vShaderStream, fShaderStream;
  43. // Read file's buffer contents into streams
  44. vShaderStream << vShaderFile.rdbuf();
  45. fShaderStream << fShaderFile.rdbuf();
  46. // close file handlers
  47. vShaderFile.close();
  48. fShaderFile.close();
  49. // Convert stream into string
  50. vertexCode = vShaderStream.str();
  51. fragmentCode = fShaderStream.str();
  52. if (this->geometryShaderYes)
  53. {
  54. gShaderFile.open(geometryPath);
  55. std::stringstream gShaderStream;
  56. gShaderStream << gShaderFile.rdbuf();
  57. gShaderFile.close();
  58. geometryCode = gShaderStream.str();
  59. }
  60. }
  61. catch (std::ifstream::failure e)
  62. {
  63. std::cout << "ERROR_CODE::3::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
  64. }
  65. const GLchar* vShaderCode = vertexCode.c_str();
  66. const GLchar* fShaderCode = fragmentCode.c_str();
  67. const GLchar* gShaderCode;
  68. if (geometryShaderYes)
  69. {
  70. gShaderCode = geometryCode.c_str();
  71. }
  72. // 2. Compile shaders
  73. GLuint vertex, fragment, geometry;
  74. GLint success;
  75. GLchar infoLog[512];
  76. // Vertex Shader
  77. vertex = glCreateShader(GL_VERTEX_SHADER);
  78. glShaderSource(vertex, 1, &vShaderCode, NULL);
  79. glCompileShader(vertex);
  80. // Print compile errors if any
  81. glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
  82. if (!success)
  83. {
  84. glGetShaderInfoLog(vertex, 512, NULL, infoLog);
  85. std::cout << "ERROR_CODE::4::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
  86. }
  87. // Fragment Shader
  88. fragment = glCreateShader(GL_FRAGMENT_SHADER);
  89. glShaderSource(fragment, 1, &fShaderCode, NULL);
  90. glCompileShader(fragment);
  91. // Print compile errors if any
  92. glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
  93. if (!success)
  94. {
  95. glGetShaderInfoLog(fragment, 512, NULL, infoLog);
  96. std::cout << "ERROR_CODE::5::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
  97. }
  98. if (this->geometryShaderYes)
  99. {
  100. // Geometry Shader
  101. geometry = glCreateShader(GL_GEOMETRY_SHADER);
  102. glShaderSource(geometry, 1, &gShaderCode, NULL);
  103. glCompileShader(geometry);
  104. // Print compile errors if any
  105. glGetShaderiv(geometry, GL_COMPILE_STATUS, &success);
  106. if (!success)
  107. {
  108. glGetShaderInfoLog(fragment, 512, NULL, infoLog);
  109. std::cout << "ERROR_CODE::5::SHADER::GEOMETRY::COMPILATION_FAILED\n" << infoLog << std::endl;
  110. }
  111. }
  112. // Shader Program
  113. this->Program = glCreateProgram();
  114. glAttachShader(this->Program, vertex);
  115. glAttachShader(this->Program, fragment);
  116. if (this->geometryShaderYes)
  117. {
  118. glAttachShader(this->Program, geometry);
  119. }
  120. glLinkProgram(this->Program);
  121. // Print linking errors if any
  122. glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
  123. if (!success)
  124. {
  125. glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
  126. std::cout << "ERROR_CODE::6::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  127. }
  128. // Delete the shaders as they're linked into our program now and no longer necessery
  129. glDeleteShader(vertex);
  130. glDeleteShader(fragment);
  131. if (this->geometryShaderYes)
  132. {
  133. glDeleteShader(geometry);
  134. }
  135.  
  136. }
  137. // Uses the current shader
  138. void VriesShaderProgram::Use()
  139. {
  140. glUseProgram(this->Program);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment