30 #include <boost/algorithm/string.hpp>
32 #ifndef BOOST_FILESYSTEM_VERSION
33 #define BOOST_FILESYSTEM_VERSION 2
35 #include <boost/filesystem.hpp>
36 #include <boost/function.hpp>
37 #include <boost/lexical_cast.hpp>
38 #include <boost/regex.hpp>
39 #include <boost/signals2/signal.hpp>
40 #include <boost/tokenizer.hpp>
43 #include <osg/StateSet>
45 #include "../../common/WLogger.h"
46 #include "../../common/WPathHelper.h"
47 #include "../../common/WPredicateHelper.h"
48 #include "../WGraphicsEngine.h"
49 #include "WGEShader.h"
50 #include "WGEShaderPreprocessor.h"
51 #include "WGEShaderVersionPreprocessor.h"
55 m_shaderPath( search ),
58 m_shaderLoaded( false ),
59 m_deactivated( false )
62 m_vertexShader = osg::ref_ptr< osg::Shader >(
new osg::Shader( osg::Shader::VERTEX ) );
63 m_fragmentShader = osg::ref_ptr< osg::Shader >(
new osg::Shader( osg::Shader::FRAGMENT ) );
64 m_geometryShader = osg::ref_ptr< osg::Shader >(
new osg::Shader( osg::Shader::GEOMETRY ) );
68 addShader( m_fragmentShader );
69 addShader( m_geometryShader );
87 osg::StateSet* rootState = node->getOrCreateStateSet();
88 rootState->setAttributeAndModes(
this, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED );
93 node->addUpdateCallback( osg::ref_ptr< SafeUpdaterCallback >(
new SafeUpdaterCallback(
this ) ) );
111 osg::StateSet* rootState = node->getOrCreateStateSet();
112 rootState->setAttributeAndModes(
this, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED );
118 node->addUpdateCallback( osg::ref_ptr< SafeUpdaterCallback >(
new SafeUpdaterCallback(
this ) ) );
165 catch(
const std::exception& e )
204 m_shader->updatePrograms();
207 traverse( node, nv );
212 std::stringstream output;
226 "WGEShader (" + filename +
")", LL_ERROR
234 static const boost::regex includeRegexp(
"^[ ]*#[ ]*include[ ]+[\"<](.*)[\">].*" );
243 std::string fn = filename;
244 std::string fnLocal = (
m_shaderPath / filename ).file_string();
245 std::string fnLocalShaders = (
m_shaderPath /
"shaders" / filename ).file_string();
248 if( boost::filesystem::exists(
m_shaderPath / filename ) )
252 else if( boost::filesystem::exists(
m_shaderPath /
"shaders" / filename ) )
274 std::ifstream input( fn.c_str() );
275 if( !input.is_open() )
286 "WGEShader (" + filename +
")", LL_ERROR
292 "WGEShader (" + filename +
")", LL_ERROR
301 boost::smatch matches;
303 while( std::getline( input, line ) )
305 if( boost::regex_search( line, matches, includeRegexp ) )
347 code = ( *pp ).first->process( filename, code );
358 if( !w->get().count( preproc ) )
361 boost::signals2::connection con = preproc->getChangeCondition()->subscribeSignal( boost::bind( &
WGEShader::reload,
this ) );
362 w->get().insert( std::make_pair( preproc, con ) );
371 if( w->get().count( preproc ) )
373 w->get().operator[]( preproc ).disconnect();
374 w->get().erase( preproc );
387 ( *pp ).second.disconnect();
396 return this->setDefine< bool >( key, true );