Blackcomb  prealpha
Engine-ish
Shader.h
Go to the documentation of this file.
1 #ifndef BLACKCOMB_SHADER_H
2 #define BLACKCOMB_SHADER_H
3 
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 #include <glad/glad.h>
8 
13 namespace blackcomb::renderer {
18  const std::unordered_map<GLuint, std::string> ShaderNames = {
19  {GL_VERTEX_SHADER, "vertex shader"},
20  {GL_FRAGMENT_SHADER, "fragment shader"},
21  {GL_GEOMETRY_SHADER, "geometry shader"},
22  {GL_COMPUTE_SHADER, "compute shader"},
23  {GL_TESS_CONTROL_SHADER, "tesselation control shader"},
24  {GL_TESS_EVALUATION_SHADER, "tesselation evaluation shader"},
25  {0, "shader"}
26  };
27 
31  static std::string getShaderName(GLuint shaderType) {
32  auto res = ShaderNames.find(shaderType);
33  if (res != ShaderNames.end()) {
34  return res->second;
35  }
36  return ShaderNames.at(0);
37  }
38 
42  class Shader {
43  public:
44  Shader(const std::string& vertexFilename, const std::string& fragmentFilename);
45  void bind();
46  static void unbind();
47  void setUniformInt(const char* name, int val);
48  void setUniformFloat(const char* name, float val);
49  void setUniformDouble(const char* name, double val);
50  void setUniformVecF(const char* name, std::vector<GLfloat>& val);
51  void setUniformVecF(const char* name, float val[], size_t size);
52  void setUniformMat4F(const char* name, GLfloat* value, GLboolean transpose = GL_FALSE);
53  GLuint program;
54  private:
55  GLuint loadAndCompileShader(unsigned int shaderType, const std::string& filename);
56  void reportCompileStatus(GLuint shaderType, GLuint shader);
57  void reportLinkStatus(GLuint program);
58  };
59 }
60 
61 #endif //BLACKCOMB_SHADER_H
void setUniformMat4F(const char *name, GLfloat *value, GLboolean transpose=GL_FALSE)
Set a 4x4 float matrix shader uniform.
Definition: Shader.cpp:156
void setUniformFloat(const char *name, float val)
Set a float shader uniform.
Definition: Shader.cpp:102
void setUniformInt(const char *name, int val)
Set an integer shader uniform.
Definition: Shader.cpp:92
const std::unordered_map< GLuint, std::string > ShaderNames
A mapping of OpenGL shader type constants to human-readable names.
Definition: Shader.h:18
Definition: Mesh.h:8
static void unbind()
Unbind the shader.
Definition: Shader.cpp:35
Loads and stores shaders.
Definition: Shader.h:42
void setUniformDouble(const char *name, double val)
Set a double-precision float shader uniform.
Definition: Shader.cpp:112
void bind()
Bind the shader for usage.
Definition: Shader.cpp:28