Shader.h
Go to the documentation of this file.
1 #ifndef B_SHADER_H
2 #define B_SHADER_H
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 #include "vmmlib/matrix.hpp"
8 #include "Renderer_GL.h"
9 #include "Texture.h"
10 
11 
12 class IShaderData;
13 
17 class Shader
18 {
19 public:
20 
21  /* Structs */
22 
23  struct Attrib
24  {
25  GLint loc;
26  GLint size;
27  GLenum type;
28  GLsizei stride; // size of vertex data structure
29  size_t offset;
30  };
31 
32  /* Typedefs */
33 
34  typedef std::unordered_map< std::string, GLint > LocationMap;
35  typedef std::unordered_map< std::string, Attrib > AttribMap;
36 
37  /* Functions */
38 
42  Shader(const IShaderData &shaderData);
43 
46  virtual ~Shader()
47  {
48  deleteShader();
49  }
50 
53  virtual void bind();
54 
59  virtual void setUniform(const std::string &name, const vmml::Matrix4f &arg);
60 
65  virtual void setUniform(const std::string &name, const vmml::Matrix3f &arg);
66 
71  virtual void setUniform(const std::string &name, const vmml::Vector4f &arg);
72 
77  virtual void setUniform(const std::string &name, const vmml::Vector3f &arg);
78 
83  virtual void setUniform(const std::string &name, GLfloat arg);
84 
89  virtual void setUniform(const std::string &name, TexturePtr texture);
90 
94  virtual GLint registerUniform(const std::string &name);
95 
103  virtual GLint registerAttrib(const std::string &name, GLint size, GLenum type, GLsizei stride, size_t offset);
104 
107  GLuint getProgramID() { return _programID; }
108 
112  virtual GLint findUniformLocation(const std::string &name);
113 
121  virtual GLint findAttribLocation(const std::string &name, GLint size, GLenum type, GLsizei stride, size_t offset);
122 
126  virtual GLint getUniformLocation(const std::string &name);
127 
131  virtual GLint getAttribLocation(const std::string &name);
132 
135  GLuint getMaxLights() { return _shaderMaxLights; }
136 
139  bool supportsVariableNumberOfLights() const { return _variableNumberOfLights; }
140 
143  bool supportsAmbientLighting() const { return _ambientLighting; }
144 
147  bool supportsDiffuseLighting() const { return _diffuseLighting; }
148 
151  bool supportsSpecularLighting() const { return _specularLighting; }
152 
155  bool supportsCubicReflectionMap() const { return _cubicReflectionMap; }
156 
157  template< typename T >
161  void setUniforms(const T &arg)
162  {
163  for (auto i = arg.cbegin(); i != arg.cend(); ++i)
164  {
165  setUniform(i->first, i->second);
166  }
167  }
168 
171  GLint getCurrentTexUnit() { return GL_TEXTURE0 + _cTexUnit; }
172 
175  virtual void deleteShader()
176  {
177  if (_programID) {
178  glDeleteProgram(_programID);
179  }
180  }
181 
182 protected:
183 
184  /* Functions */
185 
186  virtual void resetTexUnit();
187  virtual bool compile(GLuint* shader, GLenum type, const std::string &src);
188  virtual bool link();
189  virtual bool validate();
190 
191 private:
192 
193  /* Variables */
194 
195  GLuint _programID = 0;
196 
197  GLint _cTexUnit = 0;
198  GLint _maxTexUnits = 0;
199 
200  LocationMap _uniformLocations;
201  AttribMap _attribs;
202 
203  GLuint _shaderMaxLights;
204  bool _variableNumberOfLights;
205  bool _ambientLighting;
206  bool _diffuseLighting;
207  bool _specularLighting;
208  bool _cubicReflectionMap;
209 };
210 
211 typedef std::shared_ptr< Shader > ShaderPtr;
212 
213 #endif /* defined(B_SHADER_H) */
bool supportsSpecularLighting() const
Returns true if the shader supports specular lighting.
Definition: Shader.h:151
virtual GLint findAttribLocation(const std::string &name, GLint size, GLenum type, GLsizei stride, size_t offset)
Returns attribute location (tries to register attribute if not already available) ...
Definition: Shader.cpp:168
std::shared_ptr< Texture > TexturePtr
Definition: Texture.h:67
std::shared_ptr< Shader > ShaderPtr
Definition: Shader.h:211
std::unordered_map< std::string, Attrib > AttribMap
Definition: Shader.h:35
Definition: Shader.h:23
virtual void bind()
Binds the shader and its attributes.
Definition: Shader.cpp:197
bool supportsDiffuseLighting() const
Returns true if the shader supports diffuse lighting.
Definition: Shader.h:147
GLuint getProgramID()
Returns the shader ID.
Definition: Shader.h:107
An interface for the underlying data of a shader.
Definition: IShaderData.h:10
virtual GLint registerAttrib(const std::string &name, GLint size, GLenum type, GLsizei stride, size_t offset)
Register an attribute.
Definition: Shader.cpp:137
void setUniforms(const T &arg)
Sets multiple uniforms.
Definition: Shader.h:161
virtual void resetTexUnit()
Definition: Shader.cpp:192
size_t offset
Definition: Shader.h:29
bool supportsVariableNumberOfLights() const
Returns true if the number of lights is variable in the shader.
Definition: Shader.h:139
virtual bool validate()
Definition: Shader.cpp:272
virtual bool link()
Definition: Shader.cpp:251
virtual GLint getUniformLocation(const std::string &name)
Returns uniform location.
Definition: Shader.cpp:182
virtual ~Shader()
Virtual destructor.
Definition: Shader.h:46
virtual GLint getAttribLocation(const std::string &name)
Returns attribute location.
Definition: Shader.cpp:187
GLenum type
Definition: Shader.h:27
GLint getCurrentTexUnit()
Returns the current texture unit used in the shader.
Definition: Shader.h:171
GLint loc
Definition: Shader.h:25
GLuint getMaxLights()
Get the maximum number of lights used.
Definition: Shader.h:135
bool supportsCubicReflectionMap() const
Returns true if the shader supports a cubic reflection map.
Definition: Shader.h:155
GLint size
Definition: Shader.h:26
virtual bool compile(GLuint *shader, GLenum type, const std::string &src)
Definition: Shader.cpp:218
A shader class that represents a program to be run on the GPU.
Definition: Shader.h:17
GLsizei stride
Definition: Shader.h:28
virtual GLint findUniformLocation(const std::string &name)
Returns uniform location (tries to register uniform if not already available)
Definition: Shader.cpp:154
std::unordered_map< std::string, GLint > LocationMap
Definition: Shader.h:34
Shader(const IShaderData &shaderData)
Constructor.
Definition: Shader.cpp:6
virtual void setUniform(const std::string &name, const vmml::Matrix4f &arg)
Pass a 4 dimensional matrix to the shader.
Definition: Shader.cpp:79
virtual void deleteShader()
Deletes the shader.
Definition: Shader.h:175
virtual GLint registerUniform(const std::string &name)
Register a uniform.
Definition: Shader.cpp:126
bool supportsAmbientLighting() const
Returns true if the shader supports ambient lighting.
Definition: Shader.h:143