Guest User

My Shader Class

a guest
Dec 25th, 2015
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Shader
  2. {
  3. private:
  4.     GLuint program;
  5.  
  6. public:
  7.     Shader(const char*);
  8.     void bind();
  9.     void release();
  10.     void destroy();
  11.  
  12.     GLuint getUniform(const char*);
  13.     GLuint getAttrib(const char*);
  14.  
  15.     GLuint* getProgram();
  16. };
  17.  
  18. Shader::Shader(const char* name)
  19. {
  20.     std::string basePath = std::string("shaders/");
  21.     std::string vertPath = basePath + name + ".vsh";
  22.     std::string fragPath = basePath + name + ".fsh";
  23.  
  24.     const char* vertSrc = loadFile(vertPath.c_str());
  25.     const char* fragSrc = loadFile(fragPath.c_str());
  26.  
  27.     GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
  28.     glShaderSource(vertShader, 1, &vertSrc, NULL);
  29.     glCompileShader(vertShader);
  30.  
  31.     GLint vertCompStatus;
  32.     glGetShaderiv(vertShader, GL_COMPILE_STATUS, &vertCompStatus);
  33.  
  34.     if (vertCompStatus == GL_FALSE)
  35.     {
  36.         GLint vertLogLength;
  37.         glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &vertLogLength);
  38.         GLchar result[vertLogLength];
  39.         glGetShaderInfoLog(vertShader, vertLogLength, &vertLogLength, result);
  40.         std::cout << "Vertex Shader " << result << std::endl;
  41.     }
  42.  
  43.     GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
  44.     glShaderSource(fragShader, 1, &fragSrc, NULL);
  45.     glCompileShader(fragShader);
  46.  
  47.     GLint fragCompStatus;
  48.     glGetShaderiv(fragShader, GL_COMPILE_STATUS, &fragCompStatus);
  49.  
  50.     if (fragCompStatus == GL_FALSE)
  51.     {
  52.         GLint fragLogLength;
  53.         glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &fragLogLength);
  54.         GLchar result[fragLogLength];
  55.         glGetShaderInfoLog(fragShader, fragLogLength, &fragLogLength, result);
  56.         std::cout << "Fragment Shader " << result << std::endl;
  57.     }
  58.  
  59.     program = glCreateProgram();
  60.     glAttachShader(program, vertShader);
  61.     glAttachShader(program, fragShader);
  62.     glLinkProgram(program);
  63.  
  64.     glDeleteShader(vertShader);
  65.     glDeleteShader(fragShader);
  66. }
  67.  
  68. void Shader::bind()
  69. {
  70.     glUseProgram(program);
  71. }
  72.  
  73. void Shader::release()
  74. {
  75.     glUseProgram(0);
  76. }
  77.  
  78. void Shader::destroy()
  79. {
  80.     glDeleteProgram(program);
  81. }
  82.  
  83. GLuint Shader::getUniform(const char* name)
  84. {
  85.     return glGetUniformLocation(program, name);
  86. }
  87.  
  88. GLuint Shader::getAttrib(const char* name)
  89. {
  90.     return glGetAttribLocation(program, name);
  91. }
  92.  
  93. GLuint* Shader::getProgram()
  94. {
  95.     return &program;
  96. }
RAW Paste Data