Lagrangian Liquid Simulation
Master Thesis project on simulation of liquids using Lagrangian approach and SPH
src/ShaderLibrary.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 #include "boost/foreach.hpp"
00006 
00007 #include "ngl/ShaderManager.h"
00008 
00009 #include "ShaderLibrary.h"
00010 #include "ShaderObject.h"
00011 #include "Configuration.h"
00012 
00013 
00014 ShaderLibrary::ShaderLibrary()
00015 {
00016     //get an instance of the shader manager
00017     m_shaders = ngl::ShaderManager::instance();
00018 
00019     //read values from settings file
00020     std::vector<ShaderObject*>* myShaders = Configuration::initialiseShader();
00021 
00022     //load list of shaders
00023     BOOST_FOREACH(ShaderObject* item, *myShaders)
00024     {
00025         //save shader name
00026         m_names.push_back(item->getShaderName());
00027 
00028         if (!item->getIsSpecial())
00029         {
00030             //normal shader loading
00031             m_shaders->loadShader(item->getShaderName(), item->getVertexFile(), item->getFragementFile());
00032         }
00033         else
00034         {
00035             //special shader loading with attributes
00036 
00037             std::string vertexName = item->getShaderName() + "Vertex";
00038             std::string fragmentName = item->getShaderName() + "Fragment";
00039 
00040             m_shaders->createShaderProgram(item->getShaderName());
00041 
00042             m_shaders->attachShader(vertexName, ngl::VERTEX);
00043             m_shaders->attachShader(fragmentName, ngl::FRAGMENT);
00044             m_shaders->loadShaderSource(vertexName, item->getVertexFile());
00045             m_shaders->loadShaderSource(fragmentName, item->getFragementFile());
00046 
00047             m_shaders->compileShader(vertexName);
00048             m_shaders->compileShader(fragmentName);
00049             m_shaders->attachShaderToProgram(item->getShaderName(), vertexName);
00050             m_shaders->attachShaderToProgram(item->getShaderName(), fragmentName);
00051 
00052             for (int i = 0; i < item->getAttributes().size(); i++)
00053             {
00054                 m_shaders->bindAttribute(item->getShaderName(), item->getAttributeValues()[i], item->getAttributes()[i]);
00055             }
00056 
00057             m_shaders->linkProgramObject(item->getShaderName());
00058         }
00059 
00060 
00061     }
00062 
00063     //deleting shaders pointers
00064     for (int i = 0; i < myShaders->size(); i++) delete (*myShaders)[i];
00065 
00066     myShaders->clear();
00067     delete myShaders;
00068 
00069 }
00070 
00071 void ShaderLibrary::updateViewProjection(ngl::Camera* _cam)
00072 {
00073     //update view and projection matrices from cam info
00074     updateView(_cam);
00075     updateProjection(_cam);
00076 }
00077 
00078 void ShaderLibrary::updateView(ngl::Camera* _cam)
00079 {
00080     for (int i = 0; i < m_names.size(); i++)
00081     {
00082         //update view matrix for all shaders from cam info
00083         m_shaders->useShader(m_names[i]);
00084         m_shaders->setShaderParamFromMatrix(m_names[i],"ViewMatrix", _cam->getModelView());
00085     }
00086 }
00087 
00088 void ShaderLibrary::updateProjection(ngl::Camera* _cam)
00089 {
00090     for (int i = 0; i < m_names.size(); i++)
00091     {
00092         //update projection matrix for all shaders from cam info
00093         m_shaders->useShader(m_names[i]);
00094         m_shaders->setShaderParamFromMatrix(m_names[i],"projectionMatrix", _cam->getProjection());
00095     }
00096 }
00097 
00098 void ShaderLibrary::updateModel(const ngl::Matrix _matrix)
00099 {
00100     for (int i = 0; i < m_names.size(); i++)
00101     {
00102         //update model matrix for all shaders from matrix
00103         m_shaders->useShader(m_names[i]);
00104         m_shaders->setShaderParamFromMatrix(m_names[i],"ModelMatrix", _matrix);
00105     }
00106 }
00107 
00108 void ShaderLibrary::updateModel
00109                         (
00110                             const std::string _name,
00111                             const ngl::Matrix _matrix,
00112                             const bool _exclusive
00113                         )
00114 {
00115     if (_exclusive)
00116     {
00117         //called entirely on its own and not part of a chain of commands
00118 
00119         //enable the shader
00120         m_shaders->useShader(_name);
00121     }
00122 
00123     //update model matrix for the shader from matrix
00124     m_shaders->setShaderParamFromMatrix(_name,"ModelMatrix", _matrix);
00125 }
00126 
00127 void ShaderLibrary::updateColor
00128                             (
00129                                 const std::string _name,
00130                                 const ngl::Colour _colour,
00131                                 const bool _exclusive
00132                             )
00133 {
00134     if (_exclusive)
00135     {
00136         //called entirely on its own and not part of a chain of commands
00137 
00138         //flush the shader states
00139         glFlush();
00140 
00141         //enable the shader
00142         m_shaders->useShader(_name);
00143     }
00144 
00145     //set the colour param to the shader
00146     m_shaders->setShaderParam4f(_name, "inColor", _colour.m_r, _colour.m_g, _colour.m_b, _colour.m_a);
00147 }
00148 
00149 void ShaderLibrary::updateModelColor
00150                         (
00151                             const std::string _name,
00152                             const ngl::Matrix _matrix,
00153                             const ngl::Colour _colour,
00154                             const bool _reset,
00155                             const bool _exclusive
00156                         )
00157 {
00158     if (_reset)
00159     {
00160         //flush the shader states
00161         glFlush();
00162     }
00163 
00164     if (_exclusive)
00165     {
00166         //enable the shader
00167         m_shaders->useShader(_name);
00168     }
00169 
00170     //update model info to shader
00171     updateModel(_name, _matrix, false);
00172 
00173     //update color into to shader
00174     updateColor(_name, _colour, false);
00175 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator