Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. shaders::shaders()
  2. {
  3. std::cout << "Shaders constructed." << std::endl;
  4. load l;
  5. std::string str;
  6. l.fileToString("config/shaders.txt", &str);
  7. str.erase (std::remove(str.begin(), str.end(), '\n'), str.end());
  8.  
  9. int p = 0;
  10. int iterations = str[p] - '0';
  11. for(int i = 0; i < iterations; i++)
  12. {
  13. std::string type;
  14. for(p++; str[p] != ':'; p++) type += str[p];
  15.  
  16. std::string directory;
  17. for(p++; str[p] != ';'; p++) directory += str[p];
  18.  
  19. if(type == "vertex")
  20. {
  21. std::string source;
  22. l.fileToString(directory.c_str(), &source);
  23. createProgram(GL_VERTEX_SHADER, source, &program);
  24. std::cout << "Vertex shader attached to program." << std::endl;
  25. }
  26. else if(type == "fragment")
  27. {
  28. std::string source;
  29. l.fileToString(directory.c_str(), &source);
  30. createProgram(GL_FRAGMENT_SHADER, source, &program);
  31. std::cout << "Fragment shader attached to program." << std::endl;
  32. }
  33. else
  34. {
  35. std::cout << "Invalid shader type." << std::endl;
  36. }
  37. }
  38. }
  39.  
  40. void shaders::createProgram(GLuint shaderType, std::string data, GLuint *program)
  41. {
  42. GLuint shader = glCreateShader(shaderType);
  43. const char *source[1] = { data.c_str() };
  44. glShaderSource(shader, 1, source, 0);
  45. glCompileShader(shader);
  46.  
  47. GLint success = 0;
  48. glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  49. if(success) std::cout << "Successful shader compilation." << std::endl;
  50. else std::cout << "Unsuccessful shader compilation." << std::endl;
  51.  
  52. *program = glCreateProgram();
  53. glAttachShader(*program, shader);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement