41 (*this)[m_currentShader]->setUniformMatrix4fv(_paramName.c_str(),1,
GL_FALSE,_p1.openGL());
47 (*this)[m_currentShader]->setRegisteredUniformMatrix4fv(_registeredUniformName,1,
GL_FALSE,_p1.openGL());
53 (*this)[m_currentShader]->setUniformMatrix3fv(_paramName.c_str(),1,
GL_FALSE,_p1.openGL());
59 (*this)[m_currentShader]->setRegisteredUniformMatrix3fv(_paramName,1,
GL_FALSE,_p1.openGL());
65 (*this)[m_currentShader]->setUniform4fv(_paramName.c_str(),1,_p1.openGL());
71 (*this)[m_currentShader]->setRegisteredUniform4f(_paramName,_p1.m_x,_p1.m_y,_p1.m_z,_p1.m_w);
78 (*this)[m_currentShader]->setUniform4fv(_paramName.c_str(),1,_p1.openGL());
84 (*this)[m_currentShader]->setRegisteredUniform4f(_paramName,_p1.m_r,_p1.m_g,_p1.m_b,_p1.m_a);
89 (*this)[m_currentShader]->setRegisteredUniform3f(_paramName,_p1.m_x,_p1.m_y,_p1.m_z);
95 (*this)[m_currentShader]->setRegisteredUniform2f(_paramName,_p1.m_x,_p1.m_y);
104 (*this)[m_currentShader]->setUniform4f(_paramName.c_str(),_p1,_p2,_p3,_p4);
110 (*this)[m_currentShader]->setRegisteredUniform4f(_paramName,_p1,_p2,_p3,_p4);
116 (*this)[m_currentShader]->setUniform3f(_paramName.c_str(),_p1,_p2,_p3);
122 (*this)[m_currentShader]->setRegisteredUniform3f(_paramName,_p1,_p2,_p3);
128 (*this)[m_currentShader]->setUniform2f(_paramName.c_str(),_p1,_p2);
135 (*this)[m_currentShader]->setRegisteredUniform2f(_paramName,_p1,_p2);
140 (*this)[m_currentShader]->setUniform1i(_paramName.c_str(),_p1);
147 (*this)[m_currentShader]->setRegisteredUniform1i(_paramName,_p1);
155 (*this)[m_currentShader]->setUniform1f(_paramName.c_str(),_p1);
162 (*this)[m_currentShader]->setRegisteredUniform1f(_paramName,_p1);
171 createShaderProgram(_shaderName);
175 loadShaderSource(_shaderName+
"Vertex",_vert);
176 loadShaderSource(_shaderName+
"Fragment",_frag);
178 compileShader(_shaderName+
"Vertex");
179 compileShader(_shaderName+
"Fragment");
180 attachShaderToProgram(_shaderName,_shaderName+
"Vertex");
181 attachShaderToProgram(_shaderName,_shaderName+
"Fragment");
185 loadShaderSource(_shaderName+
"Geo",_vert);
186 compileShader(_shaderName+
"Geo");
187 attachShaderToProgram(_shaderName,_shaderName+
"Geo");
190 linkProgramObject(_shaderName);
197 std::cerr<<
"Closing down shader manager\n";
221 std::cerr <<
"Warning trying to get attrib "<<_paramName<<
" from "<<_shaderName<<
" but not found\n";
253 value =
shader->second->getShaderHandle();
257 std::cout<<
"Warning: No shader named "<< _shaderName <<
" in " <<
m_currentShader <<
" shader program \n";
269 shaderPointer =
shader->second;
273 shaderPointer =
nullptr;
274 std::cout<<
"Warning: No shader named "<< _shaderName <<
" in " <<
m_currentShader <<
" shader program \n";
276 return shaderPointer;
283 std::cout<<
"just attached "<<_name<<
" "<<
m_shaders[_name]->getShaderHandle()<<
"\n";
295 shader->second->compile();
297 else {std::cerr<<
"Warning shader not know in compile "<<_name.c_str();}
304 std::cerr<<
"creating empty ShaderProgram "<<_name.c_str()<<
"\n";
321 shader->second->incrementRefCount();
325 std::cerr<<_shader.c_str()<<
" attached to program "<<_program.c_str()<<
"\n";
328 else {std::cerr<<
"Warning cant attach "<<_shader.c_str() <<
" to "<<_program.c_str()<<
"\n";}
338 shader->second->load(_sourceFile);
340 else {std::cerr<<
"Warning shader not know in loadShaderSource "<<_shaderName.c_str();}
350 const static std::unordered_map<std::string,ShaderType> stype=
360 auto value=stype.find(tlower);
361 if(
value !=stype.end())
363 return value->second;
379 std::cerr<<
"error opening json file\n";
382 std::unique_ptr<std::string>
source(
new std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()) );
386 std::unique_ptr<char []>
buffer(
new char[source->size()]);
387 memcpy(
buffer.get(), source->c_str(), source->size());
389 buffer[source->size()]=
'\0';
393 if (doc.ParseInsitu<0>(buffer.get()).HasParseError())
395 std::cerr<<
"Parse Error for file "<<_fname<<
"\n";
400 if(!doc.HasMember(
"ShaderProgram"))
402 std::cerr<<
"This does not seem to be a valid shader json file"<<std::endl;
405 std::cout<<
"***************Loading Shaders from JSON*****************\n";
409 for (rj::Value::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr)
411 if(itr->value.HasMember(
"debug"))
413 debug=itr->value[
"debug"].GetBool();
415 const rj::Value::Ch* progName=itr->value[
"name"].GetString();
416 if(progName ==NULL || strlen(progName)==0 )
418 std::cerr<<
"ShaderProgram must have a name (or could be 0 length) \n";
425 const rj::Value ¤tShader = shaders[i];
426 const rj::Value::Ch *
name=currentShader[
"name"].GetString();
439 std::cout<<
"attempting to load "<<paths[
p].GetString()<<
"\n";
443 std::cerr<<
"error opening shader file\n";
446 std::unique_ptr<std::string>
f(
new std::string((std::istreambuf_iterator<char>(source)), std::istreambuf_iterator<char>()));
449 std::cout<<
"loaded data string \n"<<
const_cast<char *
>(f->c_str())<<
"\n";
458 std::cout<<
"********* Final Shader String ***************\n";
459 std::cout<<shaderSource<<
"\n";
466 std::cout<<
"Linking and registering Uniforms to ShaderLib\n";
470 std::cout<<
"**********************DONE********************\n";
483 shader->second->loadFromString(_string);
486 else {std::cerr<<
"Warning shader not know in loadShaderSource "<<_shaderName.c_str();}
499 std::cerr<<
"Linking "<<_name.c_str()<<
"\n";
502 else {std::cerr<<
"Warning Program not known in link "<<_name.c_str();}
519 std::cerr<<
"Warning Program not know in use "<<_name.c_str()<<
"\n";
534 return program->second->getID();
538 std::cerr<<
"Warning Program not know in use "<<_name.c_str();
550 program->second->autoRegisterUniforms();
554 std::cerr<<
"Warning Program not know in registerUniform "<<_shaderName<<
"\n";
565 program->second->bindAttribute(_index,_attribName);
567 else {std::cerr<<
"Warning Program not know in bindAttribute "<<_programName.c_str();}
578 program->second->bindFragDataLocation(_index,_attribName);
580 else {std::cerr<<
"Warning Program not know in bindAttribute "<<_programName.c_str();}
605 std::cerr<<
"Warning Program not know in [] "<<_name.c_str();
606 std::cerr<<
"returning a null program and hoping for the best\n";
625 std::cerr<<
"Warning Program not know in [] "<<_name;
626 std::cerr<<
"returning a null program and hoping for the best\n";
650 id=
shader->second->getUniformBlockIndex(_uniformBlockName);
654 std::cerr <<
"Can't find id for uniform block " << _uniformBlockName <<
"\n";
682 use(
"nglTextShader");
710 use(
"nglColourShader");
737 use(
"nglDiffuseShader");
765 use(
"nglToonShader");
777 program->second->printRegisteredUniforms();
788 std::cerr<<
"_______________________________________________________________________________________________________________________\n";
789 std::cerr<<
"Printing Properties for ShaderProgram "<<
m_currentShader<<
"\n";
790 std::cerr<<
"_______________________________________________________________________________________________________________________\n";
791 program->second->printProperties();
792 std::cerr<<
"_______________________________________________________________________________________________________________________\n";
794 else {std::cerr<<
"Warning no currently active shader to print properties for "<<
m_currentShader<<
"\n";}
808 (*this)[
m_currentShader]->setRegisteredUniform1f(_paramName.c_str(),_v0);
818 (*this)[
m_currentShader]->setRegisteredUniform2f(_paramName.c_str(),_v0,_v1);
824 (*this)[
m_currentShader]->setUniform3f(_paramName.c_str(),_v0,_v1,_v2);
829 (*this)[
m_currentShader]->setRegisteredUniform3f(_paramName.c_str(),_v0,_v1,_v2);
834 (*this)[
m_currentShader]->setUniform4f(_paramName.c_str(),_v0,_v1,_v2,_v3);
838 (*this)[
m_currentShader]->setRegisteredUniform4f(_paramName.c_str(),_v0,_v1,_v2,_v3);
847 (*this)[
m_currentShader]->setRegisteredUniform1i(_paramName.c_str(),_v0);
856 (*this)[
m_currentShader]->setRegisteredUniform2i(_paramName.c_str(),_v0,_v1);
861 (*this)[
m_currentShader]->setUniform3i(_paramName.c_str(),_v0,_v1,_v2);
865 (*this)[
m_currentShader]->setRegisteredUniform3i(_paramName.c_str(),_v0,_v1,_v2);
870 (*this)[
m_currentShader]->setUniform4i(_paramName.c_str(),_v0,_v1,_v2,_v3);
874 (*this)[
m_currentShader]->setRegisteredUniform4f(_paramName.c_str(),_v0,_v1,_v2,_v3);
879 (*this)[
m_currentShader]->setUniform4f(_paramName.c_str(),_v0.m_r,_v0.m_g,_v0.m_b,_v0.m_a);
883 (*this)[
m_currentShader]->setRegisteredUniform4f(_paramName.c_str(),_v0.m_r,_v0.m_g,_v0.m_b,_v0.m_a);
887 (*this)[
m_currentShader]->setUniform2f(_paramName.c_str(),_v0.m_x,_v0.m_y);
891 (*this)[
m_currentShader]->setRegisteredUniform2f(_paramName.c_str(),_v0.m_x,_v0.m_y);
895 (*this)[
m_currentShader]->setUniform3f(_paramName.c_str(),_v0.m_x,_v0.m_y,_v0.m_z);
899 (*this)[
m_currentShader]->setRegisteredUniform3f(_paramName.c_str(),_v0.m_x,_v0.m_y,_v0.m_z);
903 (*this)[
m_currentShader]->setUniform4f(_paramName.c_str(),_v0.m_x,_v0.m_y,_v0.m_z,_v0.m_w);
907 (*this)[
m_currentShader]->setRegisteredUniform4f(_paramName.c_str(),_v0.m_x,_v0.m_y,_v0.m_z,_v0.m_w);
void printRegisteredUniforms(const std::string &_shader) const noexcept
debug print any registered uniforms
#define NGL_UNUSED(arg)
define unused to quiet Warnings
Mat3 basic 3x3 matrix for certain glsl ops.
void bindFragDataLocation(const std::string &_programName, GLuint _index, const std::string &_attribName) noexcept
fragment shader output location
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
ngl::Shader * getShader(const std::string &_shaderName) noexcept
method to return the specified shader object
void setUniform(const std::string &_paramName, Real _v0) noexcept
overloaded method to set shader Uniforms the shader must be the currently active shader of else this ...
void loadShaderSourceFromString(const std::string &_shaderName, const std::string &_string) noexcept
load shader from a C string, useful for including code in headers etc
void setShaderParam1f(const std::string &_paramName, float _p1) noexcept
set a shader param by name for 1 float param note that the shader must be the currently active shader...
GLuint GLuint GLsizei GLenum type
const std::string colourFragmentShader
const std::string diffuseFragmentShader
#define glGetAttribLocation
GLsizei const GLfloat * value
void loadColourShaders() noexcept
this will load the pre-defined text rendering shaders these are stored in the file src/shaders/Colour...
void loadDiffuseShaders() noexcept
this will load the pre-defined text rendering shaders these are stored in the file src/shaders/Diffus...
void setRegisteredUniform2f(const std::string &_paramName, float _p1, float _p2) noexcept
set the pre-registered uniform
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
void ceckGLError(const std::string &_file, const int _line) noexcept
main shader loader / manager class for GLSL shaders
implementation files for RibExport class
void compileShader(const std::string &_name) noexcept
compile the shader from _name
void bindAttribute(const std::string &_programName, GLuint _index, const std::string &_attribName) noexcept
bind an attribute at index by name
unsigned int m_numShaders
the nunmber of shaders loaded
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
void loadShader(const std::string &_shaderName, const std::string &_vert, const std::string &_frag, const std::string &_geo="", const bool _exitOnError=false) noexcept
method to load shaders
GLsizei GLsizei GLchar * source
GLuint GLenum GLenum transform
GLint getAttribLocation(const std::string &_shaderName, const std::string &_paramName) noexcept
return the index to the shader attribute location
void setRegisteredUniformVec2(const std::string &_paramName, Vec2 _p1) noexcept
set the pre-registered uniform from a Vector
std::string m_currentShader
the name of the currently active shader
ShaderProgram * operator[](const std::string &_name) noexcept
accessor to the shader program using the subscript operatoir
void setRegisteredUniform1f(const std::string &_paramName, float _p1) noexcept
set the pre-registered uniform
bool loadFromJson(const std::string &_fname) noexcept
method to load multiple shaders from a json file
void setRegisteredUniformFromColour(const std::string &_paramName, Colour _p1) noexcept
set the pre-registered uniform from an Colour
const std::string toonVertexShader
GLsizei GLsizei GLuint * shaders
void setRegisteredUniform1i(const std::string &_paramName, int _p1) noexcept
set the pre-registered uniform
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
void setRegisteredUniformVec4(const std::string &_paramName, Vec4 _p1) noexcept
set the pre-registered uniform from an Vec4
std::unordered_map< std::string, ShaderProgram * > m_shaderPrograms
a map of shader Programs using name as key to shader pointer
void use(const std::string &_name) noexcept
set active shader to name (if not found sets glProgramObject(0)
void setShaderParamFromMat4(const std::string &_paramName, Mat4 _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
void loadToonShaders() noexcept
this will load the pre-defined text rendering shaders these are stored in the file src/shaders/ToonSh...
void loadShaderSource(std::string _shaderName, std::string _sourceFile) noexcept
Load shader source from text file the path will be relative from current dir unless a full path is sp...
void printProperties() const noexcept
print the properties of the currently active shader
void setRegisteredUniform4f(const std::string &_paramName, float _p1, float _p2, float _p3, float _p4) noexcept
set the pre-registered uniform
const std::string textFragmentShader
void useNullProgram() noexcept
use the null program (this will turn off any shaders), if using some drivers this will go to the fixe...
void setShaderParam3f(const std::string &_paramName, float _p1, float _p2, float _p3) noexcept
set a shader param by name for 3 float params note that the shader must be the currently active shade...
void autoRegisterUniforms(const std::string &_shaderName) noexcept
will parse the shader source and find any uniforms it can and register them
void attachShaderToProgram(const std::string &_program, const std::string &_shader) noexcept
attatch a Shader to the ShaderProgram referenced by _name
void use() noexcept
use this Shader object as the current Active shader once this is set it is active until unbind us cal...
and encapsulation of an OpenGL Shader object with associations for source code, etc. Used in conjunction with the ShaderProgram class
ShaderType getShaderType(const std::string &type) noexcept
get shader type from string (used for json parsing)
void setRegisteredUniform3f(const std::string &_paramName, float _p1, float _p2, float _p3) noexcept
set the pre-registered uniform
void setShaderParamFromMat3(const std::string &_paramName, Mat3 _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
void setShaderParam1i(const std::string &_paramName, int _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
GLuint const GLchar * name
void setRegisteredUniformFromMat4(const std::string &_registeredUniformName, Mat4 _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
ShaderProgram * m_nullProgram
null ShaderProgram so we can return when shader not know;
bool m_debugState
flag to indicate the debug state
void setShaderParam2f(const std::string &_paramName, float _p1, float _p2) noexcept
set a shader param by name for 2 float params note that the shader must be the currently active shade...
void attachShader(const std::string &_name, ShaderType _type) noexcept
attatch a Shader to the ShaderProgram referenced by _name
void setRegisteredUniform(const std::string &_paramName, Real _v0) noexcept
overloaded method to set shader Uniforms that have been pre-registered using auto-register uniforms m...
const std::string textVertexShader
void loadTextShaders() noexcept
this will load the pre-defined text rendering shaders these are stored in the file src/shaders/TextSh...
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
void setShaderParam4f(const std::string &_paramName, float _p1, float _p2, float _p3, float _p4) noexcept
set a shader param by name for 4 float params note that the shader must be the currently active shade...
const std::string toonFragmentShader
void createShaderProgram(const std::string &_name) noexcept
create an empty ShaderProgram for us to attach shaders etc to
void setShaderParamFromVec4(const std::string &_paramName, Vec4 _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
void setShaderParamFromColour(const std::string &_paramName, Colour _p1) noexcept
set a shader param by name for 1 int param note that the shader must be the currently active shader o...
GLuint getUniformBlockIndex(const std::string &_uniformBlockName) const noexcept
grab the index of the unifrom block, this may not be supported on all GPU's
void setRegisteredUniformVec3(const std::string &_paramName, Vec3 _p1) noexcept
set the pre-registered uniform from an Vector
std::unordered_map< std::string, Shader * > m_shaders
map of shaders using name as key
void setRegisteredUniformFromMat3(const std::string &_paramName, Mat3 _p1) noexcept
set the registered uniform from Max3x3
GLuint getProgramID(const std::string &_name) noexcept
get the Program ID of the GL Program by name
void reset() noexcept
reset the Shader manager which will delete all shader objects
void linkProgramObject(const std::string &_name) noexcept
link the program Object from _name
ShaderLib() noexcept
default ctor private as a singleton
NGL_DLLEXPORT void NGLCheckGLError(const std::string &_file, const int _line) noexcept
check for openGL errors and print out.
GLsizei const GLchar *const * string
GLuint getShaderID(const std::string &_shaderName) noexcept
method to return a shader ID
const std::string colourVertexShader
const std::string diffuseVertexShader
GLenum const void * paths