|
| 1 | +#ifndef SHADER_H |
| 2 | +#define SHADER_H |
| 3 | + |
| 4 | +#include <glad/glad.h> |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <fstream> |
| 8 | +#include <sstream> |
| 9 | +#include <iostream> |
| 10 | + |
| 11 | +class Shader |
| 12 | +{ |
| 13 | +public: |
| 14 | + GLuint ID; |
| 15 | + // constructor generates the shader on the fly |
| 16 | + // ------------------------------------------------------------------------ |
| 17 | + Shader(const char* vertexPath, const char* fragmentPath) |
| 18 | + { |
| 19 | + // 1. retrieve the vertex/fragment source code from filePath |
| 20 | + std::string vertexCode; |
| 21 | + std::string fragmentCode; |
| 22 | + std::ifstream vShaderFile; |
| 23 | + std::ifstream fShaderFile; |
| 24 | + // ensure ifstream objects can throw exceptions: |
| 25 | + vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); |
| 26 | + fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); |
| 27 | + try |
| 28 | + { |
| 29 | + // open files |
| 30 | + vShaderFile.open(vertexPath); |
| 31 | + fShaderFile.open(fragmentPath); |
| 32 | + std::stringstream vShaderStream, fShaderStream; |
| 33 | + // read file's buffer contents into streams |
| 34 | + vShaderStream << vShaderFile.rdbuf(); |
| 35 | + fShaderStream << fShaderFile.rdbuf(); |
| 36 | + // close file handlers |
| 37 | + vShaderFile.close(); |
| 38 | + fShaderFile.close(); |
| 39 | + // convert stream into string |
| 40 | + vertexCode = vShaderStream.str(); |
| 41 | + fragmentCode = fShaderStream.str(); |
| 42 | + } |
| 43 | + catch (std::ifstream::failure e) |
| 44 | + { |
| 45 | + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; |
| 46 | + } |
| 47 | + const char* vShaderCode = vertexCode.c_str(); |
| 48 | + const char * fShaderCode = fragmentCode.c_str(); |
| 49 | + // 2. compile shaders |
| 50 | + unsigned int vertex, fragment; |
| 51 | + // vertex shader |
| 52 | + vertex = glCreateShader(GL_VERTEX_SHADER); |
| 53 | + glShaderSource(vertex, 1, &vShaderCode, NULL); |
| 54 | + glCompileShader(vertex); |
| 55 | + checkCompileErrors(vertex, "VERTEX"); |
| 56 | + // fragment Shader |
| 57 | + fragment = glCreateShader(GL_FRAGMENT_SHADER); |
| 58 | + glShaderSource(fragment, 1, &fShaderCode, NULL); |
| 59 | + glCompileShader(fragment); |
| 60 | + checkCompileErrors(fragment, "FRAGMENT"); |
| 61 | + // shader Program |
| 62 | + ID = glCreateProgram(); |
| 63 | + glAttachShader(ID, vertex); |
| 64 | + glAttachShader(ID, fragment); |
| 65 | + glLinkProgram(ID); |
| 66 | + checkCompileErrors(ID, "PROGRAM"); |
| 67 | + // delete the shaders as they're linked into our program now and no longer necessary |
| 68 | + glDeleteShader(vertex); |
| 69 | + glDeleteShader(fragment); |
| 70 | + } |
| 71 | + // activate the shader |
| 72 | + // ------------------------------------------------------------------------ |
| 73 | + void use() |
| 74 | + { |
| 75 | + glUseProgram(ID); |
| 76 | + } |
| 77 | + |
| 78 | + GLint getUniformLocation(const GLchar *name) |
| 79 | + { |
| 80 | + return glGetUniformLocation(ID, name); |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + // utility function for checking shader compilation/linking errors. |
| 85 | + // ------------------------------------------------------------------------ |
| 86 | + void checkCompileErrors(unsigned int shader, std::string type) |
| 87 | + { |
| 88 | + int success; |
| 89 | + char infoLog[1024]; |
| 90 | + if (type != "PROGRAM") |
| 91 | + { |
| 92 | + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); |
| 93 | + if (!success) |
| 94 | + { |
| 95 | + glGetShaderInfoLog(shader, 1024, NULL, infoLog); |
| 96 | + std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; |
| 97 | + } |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + glGetProgramiv(shader, GL_LINK_STATUS, &success); |
| 102 | + if (!success) |
| 103 | + { |
| 104 | + glGetProgramInfoLog(shader, 1024, NULL, infoLog); |
| 105 | + std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +#endif |
| 112 | + |
0 commit comments