Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. std::string readFromFile(const std::string &filename) {
  2.     std::string res;
  3.     std::string line;
  4.     std::ifstream file(filename);
  5.     if (file.is_open()) {
  6.         while (std::getline(file, line)) {
  7.             res += line + "\n";
  8.         }
  9.     }
  10.     else {
  11.         throw std::logic_error("Can't open file: " + filename);
  12.     }
  13.     return res;
  14. }
  15.  
  16. Shader::Shader(const std::string &vertexPath, const std::string &fragPath) {
  17.     unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
  18.     std::string vShader = readFromFile(vertexPath);
  19.     GLchar const* vSh[] = { vShader.c_str() };
  20.     glShaderSource(vertexShader, 1, vSh, NULL);
  21.     glCompileShader(vertexShader);
  22.     checkShaderCompilation(vertexShader);
  23.  
  24.     unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  25.     std::string fShader = readFromFile(fragPath);
  26.     GLchar const* fSh[] = { fShader.c_str() };
  27.     glShaderSource(fragmentShader, 1, fSh, NULL);
  28.     glCompileShader(fragmentShader);
  29.     checkShaderCompilation(fragmentShader);
  30.  
  31.     programID = glCreateProgram();
  32.     glAttachShader(programID, vertexShader);
  33.     glAttachShader(programID, fragmentShader);
  34.     glLinkProgram(programID);
  35.     checkProgramLinking();
  36.  
  37.     glDeleteShader(fragmentShader);
  38.     glDeleteShader(vertexShader);
  39. }
  40.  
  41. void Shader::Use() {
  42.     glUseProgram(programID);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement