hx3d  1
2D/3D Simple Game Framework
shader.hpp
1 /*
2  Shader.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #ifndef HX3D_GRAPHICS_SHADER
22 #define HX3D_GRAPHICS_SHADER
23 
24 #include "hx3d/graphics/gl.hpp"
25 
26 #include "hx3d/utils/ptr.hpp"
27 #include "hx3d/utils/resource.hpp"
28 
29 #include <glm/glm.hpp>
30 
31 #include <string>
32 #include <map>
33 
34 namespace hx3d {
35 namespace graphics {
36 
42 class Shader: public Resource {
43 
44 public:
54  explicit Shader(std::string pathToShader);
55 
62  Shader(std::string vert, std::string frag);
63  ~Shader();
64 
70  GLuint getProgramID() const;
71 
78  void setUniform1f(std::string uniform, float value);
85  void setUniform2f(std::string uniform, glm::vec2 vector);
92  void setUniform3f(std::string uniform, glm::vec3 vector);
99  void setUniform4f(std::string uniform, glm::vec4 vector);
106  void setUniformMatrix3f(std::string uniform, glm::mat3 matrix);
113  void setUniformMatrix4f(std::string uniform, glm::mat4 matrix);
114 
122  GLint getAttribute(std::string name);
130  GLint getUniform(std::string name);
131 
133 
139  static void use(Ptr<Shader> shader);
140 
144  static void disable();
145 
151  static void setProgramAnalyzing(bool value);
152 
153 protected:
154  Shader(const Shader& shader) = delete;
155  Shader& operator=(const Shader& shader) = delete;
156 
165  bool compile(const std::string& vert, const std::string& frag);
166 
176  bool compile(GLuint &shaderId, GLenum type, const std::string& content);
177 
183  bool createProgram();
184 
188  void analyzeAttributes();
192  void analyzeUniforms();
200  std::string getParameterType(GLenum type);
201 
203  GLuint _vertexID;
205  GLuint _fragmentID;
207  GLuint _programID;
208 
210  std::map<std::string, GLint> _activeAttributes;
212  std::map<std::string, GLint> _activeUniforms;
214  static bool _programsAnalyzed;
215 };
216 
217 } /* graphics */
218 } /* hx3d */
219 
220 #endif
GLuint _vertexID
Vertex shader ID.
Definition: shader.hpp:203
void setUniform4f(std::string uniform, glm::vec4 vector)
Send a vec4 to the shader.
Definition: shader.cpp:344
GLuint _programID
Shader program ID.
Definition: shader.hpp:207
void setUniform2f(std::string uniform, glm::vec2 vector)
Send a vec2 to the shader.
Definition: shader.cpp:334
static void use(Ptr< Shader > shader)
Use the shader as the current shader.
Definition: shader.cpp:312
void setUniformMatrix3f(std::string uniform, glm::mat3 matrix)
Send a mat3 to the shader.
Definition: shader.cpp:349
static bool _programsAnalyzed
Are the programs analyzed ?
Definition: shader.hpp:214
std::string getParameterType(GLenum type)
Get the parameter type (debug mode).
Definition: shader.cpp:264
hx3d framework namespace
Definition: audio.hpp:26
bool compile(const std::string &vert, const std::string &frag)
Compile the vertex and fragment shaders.
Definition: shader.cpp:77
GLint getUniform(std::string name)
Get the wanted uniform id.
Definition: shader.cpp:180
void setUniform1f(std::string uniform, float value)
Send a single float to the shader.
Definition: shader.cpp:329
GLuint _fragmentID
Fragment shader ID.
Definition: shader.hpp:205
GLuint getProgramID() const
Get the program ID.
Definition: shader.cpp:325
void analyzeAttributes()
Analyze the shader attributes (debug mode).
Definition: shader.cpp:189
std::map< std::string, GLint > _activeAttributes
Active attributes map.
Definition: shader.hpp:210
void analyzeUniforms()
Analyze the shader uniforms (debug mode).
Definition: shader.cpp:226
Resource type: to use in an asset manager.
Definition: resource.hpp:29
void setUniform3f(std::string uniform, glm::vec3 vector)
Send a vec3 to the shader.
Definition: shader.cpp:339
std::map< std::string, GLint > _activeUniforms
Active uniforms map.
Definition: shader.hpp:212
bool createProgram()
Create the shader program.
Definition: shader.cpp:129
Shader definition class.
Definition: shader.hpp:42
static void disable()
Clear the current shader.
Definition: shader.cpp:316
static void setProgramAnalyzing(bool value)
Analyse the shaders (debug mode).
Definition: shader.cpp:320
Shader(std::string pathToShader)
Create a shader from a path. The path must be without extension, e.g. shaders/test.
Definition: shader.cpp:33
GLint getAttribute(std::string name)
Get the wanted attribute id.
Definition: shader.cpp:171
void setUniformMatrix4f(std::string uniform, glm::mat4 matrix)
Send a mat4 to the shader.
Definition: shader.cpp:354
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34