BARE2D
ShaderProgram.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <initializer_list>
5 
6 #include <GL/glew.h>
7 
8 namespace BARE2D {
9 
10  /**
11  * @class ShaderProgram
12  * @brief The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment shader - to allow rendering. Each renderer should have its own program.
13  */
15  {
16  public:
17  ShaderProgram();
19 
20  /**
21  * @brief Compiles the shaders. Does not link them.
22  * @param vertexShaderPath The path to the vertex shader
23  * @param fragmentShaderPath The path to the fragment shader.
24  */
25  void compileShaders(const char* vertexShaderPath, const char* fragmentShaderPath);
26 
27  /**
28  * @brief Similar to compileShaders, this just compiles the shaders.
29  * @param vertexSource The source code for the vertex shader
30  * @param fragmentSource The source code for the fragment shader
31  */
32  void compileShadersFromSource(const char* vertexSource, const char* fragmentSource);
33 
34  /**
35  * @brief Links the compiled shaders together to create a coherent shader program
36  * @param attributes An initializer_list of std::string type of attributes to add to the program. These must link up with the renderer used to draw with this program.
37  */
38  void linkShaders(std::initializer_list<std::string> attributes);
39 
40  /**
41  * @brief A generalized wrapper to find and set a uniform for this shader.
42  * @param uniform The name of the uniform in the shader
43  * @param data The variable to actually set the uniform's value to. This can be a glm::{i,ui,f}vec{1,2,3,4}, an unsigned int, an int, or a float
44  */
45  template<class T>
46  void setUniform(const std::string uniform, T* data, unsigned int num = 1);
47 
48  /**
49  * @brief Similar to setUniform.
50  * @param uniform The name of the uniform in the shader
51  * @param transpose Should the matrix be transposed when converted? If false, each matrix is assumed to be supplied in column major order. Else, row major order
52  * @param data A pointer to the matrix, which is a 1D array of floats, ints, or unsigned ints.
53  */
54  template<class T>
55  void setUniformMatrix(const std::string uniform, bool transpose, T* data, unsigned int num = 1);
56 
57  /**
58  * @param uniform The uniform to check for
59  * @return True if the uniform is in the compiled shader program, false otherwise.
60  */
61  bool doesUniformExist(const std::string uniform);
62 
63  /**
64  * @brief A call to glBindFragDataLocation() - Causes an output variable of the fragment shader to output to a specific location. This is roughly equivalent to modern GLSL's layout(location=n) specifier, which is unusable until GLSL 330.
65  * @param outputVariableName The name of the output variable (oftentimes just colour or something)
66  * @param location The output location, corresponding to a colour attachment. Must be less than GL_MAX_DRAW_BUFFERS.
67  */
68  void bindFragOutputLocation(std::string outputVariableName, unsigned int location);
69 
70  /**
71  * @brief Activates this shader program for the renderer to use.
72  */
73  void use();
74 
75  /**
76  * @brief Deactivates this shader program.
77  */
78  void unuse();
79 
80  /**
81  * @brief Releases all bound objects and deletes any allocated stuff.
82  */
83  void destroy();
84 
85  private:
86  unsigned int m_numberAttributes = 0;
87 
88  GLuint m_programID;
91 
92  /**
93  * @brief Compiles a shader from source with OpenGL id
94  * @param source The source to compile from.
95  * @param name The name of the shader. This is normally associated with the key in the cache.
96  * @param id The ID of the shader that has been created with OpenGL calls.
97  */
98  void compileShaderFromSource(const char* source, const std::string& name, GLuint id);
99 
100  /**
101  * @brief Gets the location of a uniform in a program
102  * @param uniform The name of the uniform to find.
103  * @return The location of the uniform.
104  */
105  GLint getUniformLocation(const std::string& uniform);
106  };
107 
108 }
109 
110 #include "ShaderProgram.tcc"
BARE2D::ShaderProgram::setUniformMatrix
void setUniformMatrix(const std::string uniform, bool transpose, T *data, unsigned int num=1)
Similar to setUniform.
BARE2D::ShaderProgram::use
void use()
Activates this shader program for the renderer to use.
Definition: ShaderProgram.cpp:147
BARE2D
Definition: App.cpp:13
BARE2D::ShaderProgram::unuse
void unuse()
Deactivates this shader program.
Definition: ShaderProgram.cpp:160
BARE2D::ShaderProgram::m_programID
GLuint m_programID
Definition: ShaderProgram.hpp:88
BARE2D::ShaderProgram::ShaderProgram
ShaderProgram()
Definition: ShaderProgram.cpp:14
BARE2D::ShaderProgram::compileShaders
void compileShaders(const char *vertexShaderPath, const char *fragmentShaderPath)
Compiles the shaders. Does not link them.
Definition: ShaderProgram.cpp:23
BARE2D::ShaderProgram::bindFragOutputLocation
void bindFragOutputLocation(std::string outputVariableName, unsigned int location)
A call to glBindFragDataLocation() - Causes an output variable of the fragment shader to output to a ...
Definition: ShaderProgram.cpp:141
BARE2D::ShaderProgram::m_fragmentShaderID
GLuint m_fragmentShaderID
Definition: ShaderProgram.hpp:90
BARE2D::ShaderProgram::~ShaderProgram
~ShaderProgram()
Definition: ShaderProgram.cpp:19
ShaderProgram.tcc
BARE2D::ShaderProgram::compileShaderFromSource
void compileShaderFromSource(const char *source, const std::string &name, GLuint id)
Compiles a shader from source with OpenGL id.
Definition: ShaderProgram.cpp:183
BARE2D::ShaderProgram::getUniformLocation
GLint getUniformLocation(const std::string &uniform)
Gets the location of a uniform in a program.
Definition: ShaderProgram.cpp:125
BARE2D::ShaderProgram::destroy
void destroy()
Releases all bound objects and deletes any allocated stuff.
Definition: ShaderProgram.cpp:172
BARE2D::ShaderProgram::setUniform
void setUniform(const std::string uniform, T *data, unsigned int num=1)
A generalized wrapper to find and set a uniform for this shader.
BARE2D::ShaderProgram
The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment s...
Definition: ShaderProgram.hpp:14
BARE2D::ShaderProgram::doesUniformExist
bool doesUniformExist(const std::string uniform)
Definition: ShaderProgram.cpp:213
BARE2D::ShaderProgram::linkShaders
void linkShaders(std::initializer_list< std::string > attributes)
Links the compiled shaders together to create a coherent shader program.
Definition: ShaderProgram.cpp:75
BARE2D::ShaderProgram::m_numberAttributes
unsigned int m_numberAttributes
Definition: ShaderProgram.hpp:86
BARE2D::ShaderProgram::m_vertexShaderID
GLuint m_vertexShaderID
Definition: ShaderProgram.hpp:89
BARE2D::ShaderProgram::compileShadersFromSource
void compileShadersFromSource(const char *vertexSource, const char *fragmentSource)
Similar to compileShaders, this just compiles the shaders.
Definition: ShaderProgram.cpp:38