Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include "error.h"
  2. #include "glslprogram.h"
  3. #include "BenGine.h"
  4. #include <vector>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8.  
  9. namespace BenGine {
  10.  
  11. glslprogram::glslprogram() : _numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
  12. {
  13. }
  14.  
  15. glslprogram::~glslprogram()
  16. {
  17. }
  18.  
  19. void glslprogram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) {
  20. _programID = glCreateProgram();
  21.  
  22. _vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  23.  
  24. if (_vertexShaderID == 0) {
  25. fetalError("vertex shader failed to created");
  26. }
  27.  
  28. _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  29. if (_fragmentShaderID == 0) {
  30. fetalError("fragment shader failed to created");
  31. }
  32. compileShader(vertexShaderFilePath, _vertexShaderID);
  33. compileShader(fragmentShaderFilePath, _fragmentShaderID);
  34. }
  35.  
  36.  
  37. void glslprogram::linkshaders(){
  38. glAttachShader(_programID, _vertexShaderID);
  39. glAttachShader(_programID, _fragmentShaderID);
  40. glLinkProgram(_programID);
  41. GLuint isLinked = 0;
  42. glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
  43. if (isLinked == GL_FALSE)
  44. {
  45. GLint maxLength = 0;
  46. glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
  47. std::vector<char> errorLog(maxLength);
  48. glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
  49. glDeleteProgram(_programID);
  50. glDeleteShader(_vertexShaderID);
  51. glDeleteShader(_fragmentShaderID);
  52. std::printf("%sn", &(errorLog[0]));
  53. fetalError("shader failed to link");
  54.  
  55. }
  56. glDetachShader(_programID,_vertexShaderID);
  57. glDetachShader(_programID,_fragmentShaderID);
  58. glDeleteShader(_vertexShaderID);
  59. glDeleteShader(_fragmentShaderID);
  60.  
  61.  
  62. }
  63.  
  64. void glslprogram::addAttribute(const std::string& attributeName) {
  65. glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
  66.  
  67.  
  68. }
  69.  
  70. GLint glslprogram::getUniformLocation(const std::string& uniformName){
  71. GLint location = glGetUniformLocation(_programID, uniformName.c_str());
  72. if (location == GL_INVALID_INDEX) {
  73. fetalError("uniform" + uniformName +"not found in shader");
  74.  
  75.  
  76. }
  77. return location;
  78.  
  79. }
  80.  
  81.  
  82. void glslprogram::use(){
  83. glUseProgram(_programID);
  84. for (int i = 0; i < _numAttributes; i++){
  85. glEnableVertexAttribArray(i);
  86.  
  87. }
  88.  
  89.  
  90. }
  91. void glslprogram::unuse(){
  92. glUseProgram(0);
  93. for (int i = 0; i < _numAttributes; i++){
  94. glDisableVertexAttribArray(i);
  95.  
  96.  
  97.  
  98. }
  99. }
  100.  
  101. void glslprogram::compileShader(const std::string& filePath, GLuint id) {
  102.  
  103. //Open the file
  104. std::ifstream vertexFile(filePath);
  105. if (vertexFile.fail()) {
  106. perror(filePath.c_str());
  107. fetalError("Failed to open " + filePath);
  108. }
  109.  
  110. //File contents stores all the text in the file
  111. std::string fileContents = "";
  112. //line is used to grab each line of the file
  113. std::string line;
  114. //Get all the lines in the file and add it to the contents
  115. while (std::getline(vertexFile, line)) {
  116. fileContents += line + "n";
  117. }
  118. vertexFile.close();
  119.  
  120. //get a pointer to our file contents c string;
  121. const char* contentsPtr = fileContents.c_str();
  122. //tell opengl that we want to use fileContents as the contents of the shader file
  123. glShaderSource(id, 1, &contentsPtr, nullptr);
  124.  
  125. //compile the shader
  126. glCompileShader(id);
  127.  
  128. //check for errors
  129. GLint success = 0;
  130. glGetShaderiv(id, GL_COMPILE_STATUS, &success);
  131.  
  132. if (success == GL_FALSE)
  133. {
  134. GLint maxLength = 0;
  135. glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
  136.  
  137. //The maxLength includes the NULL character
  138. std::vector<char> errorLog(maxLength);
  139. glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
  140.  
  141. //Provide the infolog in whatever manor you deem best.
  142. //Exit with failure.
  143. glDeleteShader(id); //Don't leak the shader.
  144.  
  145. //Print error log and quit
  146. std::printf("%sn", &(errorLog[0]));
  147.  
  148. fetalError("Shader " + filePath + " failed to compile");
  149. }
  150. }
  151. }
  152.  
  153. #pragma once
  154. #include "BenGine.h"
  155. #include <string>
  156. #include <fstream>
  157. #include <iostream>
  158. #include <string>
  159.  
  160. namespace BenGine {
  161. extern void fetalError(std::string errorString);
  162. }
  163.  
  164. if (_vertexShaderID == 0) {>
  165. fetalError("vertex shader failed to created");
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement