NGL  6.5
The NCCA Graphics Library
ShaderLib.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SHADERLIB_
18 #define SHADERLIB_
19 // must include types.h first for Real and GLEW if required
22 
23 #include "Colour.h"
24 #include "Shader.h"
25 #include "ShaderProgram.h"
26 #include "Singleton.h"
27 #include "Types.h"
28 #include "Mat3.h"
29 #include "Mat4.h"
30 #include "Vec4.h"
31 
32 #include <string>
33 #include <unordered_map>
34 
35 //----------------------------------------------------------------------------------------------------------------------
36 
37 namespace ngl
38 {
39 
40 
41 //----------------------------------------------------------------------------------------------------------------------
52 //----------------------------------------------------------------------------------------------------------------------
53 
54 
55 class NGL_DLLEXPORT ShaderLib : public Singleton<ShaderLib>
56 {
57  friend class Singleton<ShaderLib>;
58 
59 public :
60 
61 
62  //----------------------------------------------------------------------------------------------------------------------
65  //----------------------------------------------------------------------------------------------------------------------
66  void createShaderProgram( const std::string &_name ) noexcept;
67  //----------------------------------------------------------------------------------------------------------------------
70  //----------------------------------------------------------------------------------------------------------------------
71  void attachShader( const std::string &_name, ShaderType _type ) noexcept;
72  //----------------------------------------------------------------------------------------------------------------------
76  //----------------------------------------------------------------------------------------------------------------------
77 
78  void attachShaderToProgram( const std::string &_program, const std::string &_shader ) noexcept;
79  //----------------------------------------------------------------------------------------------------------------------
83  //----------------------------------------------------------------------------------------------------------------------
84  GLuint getProgramID( const std::string &_name ) noexcept;
85  //----------------------------------------------------------------------------------------------------------------------
88  //----------------------------------------------------------------------------------------------------------------------
89  void compileShader(const std::string &_name ) noexcept;
90  //----------------------------------------------------------------------------------------------------------------------
93  //----------------------------------------------------------------------------------------------------------------------
94  void linkProgramObject( const std::string &_name ) noexcept;
95  //----------------------------------------------------------------------------------------------------------------------
97  //----------------------------------------------------------------------------------------------------------------------
98  void toggleDebug() noexcept{ m_debugState ^=true;}
99  //----------------------------------------------------------------------------------------------------------------------
102  //----------------------------------------------------------------------------------------------------------------------
103  void use( const std::string &_name ) noexcept;
104 
105  //----------------------------------------------------------------------------------------------------------------------
111  //----------------------------------------------------------------------------------------------------------------------
112  void bindAttribute( const std::string &_programName, GLuint _index, const std::string &_attribName ) noexcept;
113  //----------------------------------------------------------------------------------------------------------------------
119  //----------------------------------------------------------------------------------------------------------------------
120  void bindFragDataLocation( const std::string &_programName, GLuint _index, const std::string &_attribName ) noexcept;
121 
122  //----------------------------------------------------------------------------------------------------------------------
126  //----------------------------------------------------------------------------------------------------------------------
127  ShaderProgram * operator[] (const std::string &_name) noexcept;
128  //----------------------------------------------------------------------------------------------------------------------
132  //----------------------------------------------------------------------------------------------------------------------
133  ShaderProgram * operator[] (const char *_name) noexcept;
134  //----------------------------------------------------------------------------------------------------------------------
141  //----------------------------------------------------------------------------------------------------------------------
142  void loadShader(const std::string &_shaderName, const std::string &_vert, const std::string &_frag,
143  const std::string &_geo="",
144  const bool _exitOnError=false ) noexcept;
145  //----------------------------------------------------------------------------------------------------------------------
148  bool loadFromJson(const std::string &_fname) noexcept;
149  //----------------------------------------------------------------------------------------------------------------------
151  //----------------------------------------------------------------------------------------------------------------------
152  void debugOn() noexcept{ m_debugState=true; }
153  //----------------------------------------------------------------------------------------------------------------------
155  //----------------------------------------------------------------------------------------------------------------------
156  void debugOff() noexcept { m_debugState=false; }
157  //----------------------------------------------------------------------------------------------------------------------
160  //----------------------------------------------------------------------------------------------------------------------
161  size_t getNumShaders() const noexcept{ return m_shaders.size();}
162  //----------------------------------------------------------------------------------------------------------------------
167  //----------------------------------------------------------------------------------------------------------------------
168  void setShaderParamFromMat4(const std::string &_paramName, Mat4 _p1 ) noexcept;
169  //----------------------------------------------------------------------------------------------------------------------
174  //----------------------------------------------------------------------------------------------------------------------
175  void setRegisteredUniformFromMat4(const std::string &_registeredUniformName, Mat4 _p1 ) noexcept;
176  //----------------------------------------------------------------------------------------------------------------------
181  //----------------------------------------------------------------------------------------------------------------------
182  void setShaderParamFromMat3( const std::string &_paramName, Mat3 _p1 ) noexcept;
183  //----------------------------------------------------------------------------------------------------------------------
187  //----------------------------------------------------------------------------------------------------------------------
188  void setRegisteredUniformFromMat3( const std::string &_paramName, Mat3 _p1 ) noexcept;
189 
190  //----------------------------------------------------------------------------------------------------------------------
195  //----------------------------------------------------------------------------------------------------------------------
196  void setShaderParamFromVec4( const std::string &_paramName, Vec4 _p1) noexcept;
197 
198  //----------------------------------------------------------------------------------------------------------------------
202  //----------------------------------------------------------------------------------------------------------------------
203  void setRegisteredUniformVec4( const std::string &_paramName, Vec4 _p1) noexcept;
204  //----------------------------------------------------------------------------------------------------------------------
209  //----------------------------------------------------------------------------------------------------------------------
210  void setShaderParamFromColour( const std::string &_paramName, Colour _p1 ) noexcept;
211 
212  //----------------------------------------------------------------------------------------------------------------------
216  //----------------------------------------------------------------------------------------------------------------------
217  void setRegisteredUniformFromColour( const std::string &_paramName, Colour _p1 ) noexcept;
218  //----------------------------------------------------------------------------------------------------------------------
222  //----------------------------------------------------------------------------------------------------------------------
223  void setRegisteredUniformVec3( const std::string &_paramName, Vec3 _p1 ) noexcept;
224  //----------------------------------------------------------------------------------------------------------------------
228  //----------------------------------------------------------------------------------------------------------------------
229  void setRegisteredUniformVec2( const std::string &_paramName, Vec2 _p1 ) noexcept;
230 
231  //----------------------------------------------------------------------------------------------------------------------
236  //----------------------------------------------------------------------------------------------------------------------
237  void setShaderParam1i(const std::string &_paramName, int _p1 ) noexcept;
238  //----------------------------------------------------------------------------------------------------------------------
242  //----------------------------------------------------------------------------------------------------------------------
243  void setRegisteredUniform1i(const std::string &_paramName, int _p1 ) noexcept;
244  //----------------------------------------------------------------------------------------------------------------------
249  //----------------------------------------------------------------------------------------------------------------------
250  void setShaderParam1f(const std::string &_paramName,float _p1 ) noexcept;
251 
252  //----------------------------------------------------------------------------------------------------------------------
256  //----------------------------------------------------------------------------------------------------------------------
257  void setRegisteredUniform1f(const std::string &_paramName, float _p1 ) noexcept;
258  //----------------------------------------------------------------------------------------------------------------------
264  //----------------------------------------------------------------------------------------------------------------------
265  void setShaderParam2f(const std::string &_paramName, float _p1, float _p2 ) noexcept;
266  //----------------------------------------------------------------------------------------------------------------------
271  //----------------------------------------------------------------------------------------------------------------------
272  void setRegisteredUniform2f( const std::string &_paramName, float _p1, float _p2 ) noexcept;
273  //----------------------------------------------------------------------------------------------------------------------
280  //----------------------------------------------------------------------------------------------------------------------
281  void setShaderParam3f(const std::string &_paramName, float _p1, float _p2, float _p3 ) noexcept;
282  //----------------------------------------------------------------------------------------------------------------------
288  //----------------------------------------------------------------------------------------------------------------------
289  void setRegisteredUniform3f(const std::string &_paramName, float _p1,float _p2, float _p3 ) noexcept;
290  //----------------------------------------------------------------------------------------------------------------------
298  //----------------------------------------------------------------------------------------------------------------------
299  void setShaderParam4f(const std::string &_paramName,float _p1, float _p2, float _p3,float _p4 ) noexcept;
300  //----------------------------------------------------------------------------------------------------------------------
306  //----------------------------------------------------------------------------------------------------------------------
307  void setRegisteredUniform4f(const std::string &_paramName,float _p1,float _p2,float _p3,float _p4) noexcept;
308  //----------------------------------------------------------------------------------------------------------------------
310  //----------------------------------------------------------------------------------------------------------------------
311  void printProperties() const noexcept;
312  //----------------------------------------------------------------------------------------------------------------------
314  //----------------------------------------------------------------------------------------------------------------------
315  void reset() noexcept;
316  //----------------------------------------------------------------------------------------------------------------------
321  //----------------------------------------------------------------------------------------------------------------------
322  GLint getAttribLocation(const std::string &_shaderName,const std::string &_paramName ) noexcept;
323  //----------------------------------------------------------------------------------------------------------------------
328  //----------------------------------------------------------------------------------------------------------------------
329  void loadShaderSource(std::string _shaderName, std::string _sourceFile) noexcept;
330 
331  //----------------------------------------------------------------------------------------------------------------------
335  //----------------------------------------------------------------------------------------------------------------------
336  void loadShaderSourceFromString(const std::string &_shaderName, const std::string &_string ) noexcept;
337  //----------------------------------------------------------------------------------------------------------------------
340  //----------------------------------------------------------------------------------------------------------------------
341  void useNullProgram() noexcept;
342 
343  //----------------------------------------------------------------------------------------------------------------------
347  //----------------------------------------------------------------------------------------------------------------------
348  GLuint getUniformBlockIndex(const std::string &_uniformBlockName ) const noexcept;
349  //----------------------------------------------------------------------------------------------------------------------
353  //----------------------------------------------------------------------------------------------------------------------
354  void registerUniform( const std::string &_shaderName, const std::string &_uniformName ) noexcept;
355  //----------------------------------------------------------------------------------------------------------------------
358  //----------------------------------------------------------------------------------------------------------------------
359 
360  void autoRegisterUniforms( const std::string &_shaderName ) noexcept;
361  //----------------------------------------------------------------------------------------------------------------------
363  //----------------------------------------------------------------------------------------------------------------------
364  void printRegisteredUniforms(const std::string &_shader) const noexcept;
365  //----------------------------------------------------------------------------------------------------------------------
370  //----------------------------------------------------------------------------------------------------------------------
371  void setUniform(const std::string &_paramName,Real _v0) noexcept;
372  //----------------------------------------------------------------------------------------------------------------------
378  //----------------------------------------------------------------------------------------------------------------------
379  void setRegisteredUniform(const std::string &_paramName,Real _v0) noexcept;
380  //----------------------------------------------------------------------------------------------------------------------
386  //----------------------------------------------------------------------------------------------------------------------
387  void setUniform(const std::string &_paramName,Real _v0,Real _v1) noexcept;
388  //----------------------------------------------------------------------------------------------------------------------
395  //----------------------------------------------------------------------------------------------------------------------
396  void setRegisteredUniform(const std::string &_paramName,Real _v0,Real _v1) noexcept;
397  //----------------------------------------------------------------------------------------------------------------------
404  //----------------------------------------------------------------------------------------------------------------------
405  void setUniform(const std::string &_paramName,Real _v0,Real _v1,Real _v2) noexcept;
406  //----------------------------------------------------------------------------------------------------------------------
414  //----------------------------------------------------------------------------------------------------------------------
415  void setRegisteredUniform(const std::string &_paramName,Real _v0,Real _v1,Real _v2) noexcept;
416  //----------------------------------------------------------------------------------------------------------------------
424  //----------------------------------------------------------------------------------------------------------------------
425  void setUniform(const std::string &_paramName,Real _v0,Real _v1,Real _v2,Real _v3) noexcept;
426  //----------------------------------------------------------------------------------------------------------------------
435  //----------------------------------------------------------------------------------------------------------------------
436  void setRegisteredUniform(const std::string &_paramName,Real _v0,Real _v1,Real _v2,Real _v3) noexcept;
437  //----------------------------------------------------------------------------------------------------------------------
442  //----------------------------------------------------------------------------------------------------------------------
443  void setUniform(const std::string &_paramName,GLint _v0) noexcept;
444  //----------------------------------------------------------------------------------------------------------------------
450  //----------------------------------------------------------------------------------------------------------------------
451  void setRegisteredUniform(const std::string &_paramName,GLint _v0) noexcept;
452  //----------------------------------------------------------------------------------------------------------------------
458  //----------------------------------------------------------------------------------------------------------------------
459  void setUniform(const std::string &_paramName,GLint _v0,GLint _v1) noexcept;
460  //----------------------------------------------------------------------------------------------------------------------
467  //----------------------------------------------------------------------------------------------------------------------
468  void setRegisteredUniform(const std::string &_paramName,GLint _v0,GLint _v1) noexcept;
469  //----------------------------------------------------------------------------------------------------------------------
476  //----------------------------------------------------------------------------------------------------------------------
477  void setUniform(const std::string &_paramName,GLint _v0,GLint _v1,GLint _v2) noexcept;
478  //----------------------------------------------------------------------------------------------------------------------
486  //----------------------------------------------------------------------------------------------------------------------
487  void setRegisteredUniform(const std::string &_paramName,GLint _v0,GLint _v1,GLint _v2) noexcept;
488  //----------------------------------------------------------------------------------------------------------------------
496  //----------------------------------------------------------------------------------------------------------------------
497  void setUniform(const std::string &_paramName,GLint _v0,GLint _v1,GLint _v2,GLint _v3) noexcept;
498  //----------------------------------------------------------------------------------------------------------------------
507  //----------------------------------------------------------------------------------------------------------------------
508  void setRegisteredUniform(const std::string &_paramName,GLint _v0,GLint _v1,GLint _v2,GLint _v3) noexcept;
509  //----------------------------------------------------------------------------------------------------------------------
514  //----------------------------------------------------------------------------------------------------------------------
515  void setUniform(const std::string &_paramName,Colour _v0) noexcept;
516  //----------------------------------------------------------------------------------------------------------------------
522  //----------------------------------------------------------------------------------------------------------------------
523  void setRegisteredUniform(const std::string &_paramName,Colour _v0) noexcept;
524  //----------------------------------------------------------------------------------------------------------------------
529  //----------------------------------------------------------------------------------------------------------------------
530  void setUniform(const std::string &_paramName,Vec2 _v0) noexcept;
531  //----------------------------------------------------------------------------------------------------------------------
537  //----------------------------------------------------------------------------------------------------------------------
538  void setRegisteredUniform(const std::string &_paramName,Vec2 _v0) noexcept;
539  //----------------------------------------------------------------------------------------------------------------------
544  //----------------------------------------------------------------------------------------------------------------------
545  void setUniform(const std::string &_paramName,Vec3 _v0) noexcept;
546  //----------------------------------------------------------------------------------------------------------------------
552  //----------------------------------------------------------------------------------------------------------------------
553  void setRegisteredUniform(const std::string &_paramName,Vec3 _v0) noexcept;
554  //----------------------------------------------------------------------------------------------------------------------
559  //----------------------------------------------------------------------------------------------------------------------
560  void setUniform(const std::string &_paramName,Vec4 _v0) noexcept;
561  //----------------------------------------------------------------------------------------------------------------------
567  //----------------------------------------------------------------------------------------------------------------------
568  void setRegisteredUniform(const std::string &_paramName,Vec4 _v0) noexcept;
569  //----------------------------------------------------------------------------------------------------------------------
574  //----------------------------------------------------------------------------------------------------------------------
575  void setUniform(const std::string &_paramName,Mat3 _v0) noexcept;
576  //----------------------------------------------------------------------------------------------------------------------
582  //----------------------------------------------------------------------------------------------------------------------
583  void setRegisteredUniform(const std::string &_paramName,Mat3 _v0) noexcept;
584  //----------------------------------------------------------------------------------------------------------------------
589  //----------------------------------------------------------------------------------------------------------------------
590  void setUniform(const std::string &_paramName,Mat4 _v0) noexcept;
591  //----------------------------------------------------------------------------------------------------------------------
597  //----------------------------------------------------------------------------------------------------------------------
598  void setRegisteredUniform(const std::string &_paramName,Mat4 _v0) noexcept;
599  //----------------------------------------------------------------------------------------------------------------------
602  //----------------------------------------------------------------------------------------------------------------------
603  GLuint getShaderID(const std::string &_shaderName) noexcept;
604 
605  //----------------------------------------------------------------------------------------------------------------------
608  //----------------------------------------------------------------------------------------------------------------------
609  ngl::Shader* getShader(const std::string &_shaderName) noexcept;
610 
611 protected:
612 
613  //----------------------------------------------------------------------------------------------------------------------
615  //----------------------------------------------------------------------------------------------------------------------
616  ShaderType getShaderType(const std::string &type) noexcept;
617  //----------------------------------------------------------------------------------------------------------------------
620  //----------------------------------------------------------------------------------------------------------------------
621  void loadTextShaders() noexcept;
622  //----------------------------------------------------------------------------------------------------------------------
625  //----------------------------------------------------------------------------------------------------------------------
626  void loadColourShaders() noexcept;
627  //----------------------------------------------------------------------------------------------------------------------
630  //----------------------------------------------------------------------------------------------------------------------
631  void loadDiffuseShaders() noexcept;
632  //----------------------------------------------------------------------------------------------------------------------
635  //----------------------------------------------------------------------------------------------------------------------
636  void loadToonShaders() noexcept;
637  //----------------------------------------------------------------------------------------------------------------------
639  //----------------------------------------------------------------------------------------------------------------------
640  std::unordered_map <std::string,ShaderProgram *> m_shaderPrograms;
641  //----------------------------------------------------------------------------------------------------------------------
643  //----------------------------------------------------------------------------------------------------------------------
644  std::unordered_map <std::string,Shader *> m_shaders;
645  //----------------------------------------------------------------------------------------------------------------------
647  //----------------------------------------------------------------------------------------------------------------------
649  //----------------------------------------------------------------------------------------------------------------------
651  //----------------------------------------------------------------------------------------------------------------------
653  //----------------------------------------------------------------------------------------------------------------------
655  //----------------------------------------------------------------------------------------------------------------------
657  //----------------------------------------------------------------------------------------------------------------------
659  //----------------------------------------------------------------------------------------------------------------------
660  unsigned int m_numShaders;
661  //----------------------------------------------------------------------------------------------------------------------
663  //----------------------------------------------------------------------------------------------------------------------
664  ShaderLib() noexcept;
665  //----------------------------------------------------------------------------------------------------------------------
667  //----------------------------------------------------------------------------------------------------------------------
668  virtual ~ShaderLib(){ reset();}
669  //----------------------------------------------------------------------------------------------------------------------
671  //----------------------------------------------------------------------------------------------------------------------
672  ShaderLib( const ShaderLib& )=delete;
673 
674 
675 }; // end of class
676 
677 } // end ngl Namespace
678 
679 #endif
680 //----------------------------------------------------------------------------------------------------------------------
681 
void debugOff() noexcept
Toggle debug mode off.
Definition: ShaderLib.h:156
unsigned int GLuint
Definition: glew.h:280
Mat3 basic 3x3 matrix for certain glsl ops.
Definition: Mat3.h:43
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
Definition: Vec2.h:49
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
Definition: Colour.h:40
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
a simple colour class for RGBA colour
main definition of types and namespace
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1256
size_t getNumShaders() const noexcept
get the number of shaders stored
Definition: ShaderLib.h:161
#define NGL_DLLEXPORT
Definition: Types.h:65
Singleton Class to init and Use GLSL Shaders the class stores the shaders as a map of shader objects ...
Definition: ShaderLib.h:55
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
unsigned int m_numShaders
the nunmber of shaders loaded
Definition: ShaderLib.h:660
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
encapsulates a 4d Homogenous Point / Vector object
std::string m_currentShader
the name of the currently active shader
Definition: ShaderLib.h:652
void toggleDebug() noexcept
toggle debug mode
Definition: ShaderLib.h:98
std::unordered_map< std::string, ShaderProgram * > m_shaderPrograms
a map of shader Programs using name as key to shader pointer
Definition: ShaderLib.h:640
void debugOn() noexcept
Toggle debug mode on.
Definition: ShaderLib.h:152
ShaderType
Definition: Shader.h:30
GLboolean reset
Definition: glew.h:4987
a simple 3x3 TX matrix
and encapsulation of an OpenGL Shader object with associations for source code, etc. Used in conjunction with the ShaderProgram class
Definition: Shader.h:40
a simple singleton template inherited by other classes
ShaderProgram * m_nullProgram
null ShaderProgram so we can return when shader not know;
Definition: ShaderLib.h:648
bool m_debugState
flag to indicate the debug state
Definition: ShaderLib.h:656
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
int GLint
Definition: glew.h:281
std::unordered_map< std::string, Shader * > m_shaders
map of shaders using name as key
Definition: ShaderLib.h:644
GLsizei const GLchar *const * string
Definition: glew.h:1847
Singleton template added to NGL framework 20/04/10 by jmacey.
Definition: Singleton.h:40