void convertVertexShader(osg::Program& program, osg::Shader& shader) { std::string source = shader.getShaderSource(); // replace ftransform as it only works with built-ins replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex"); #if 1 replaceAndBindAttrib(program, source, "gl_Normal", _normalAlias, "attribute vec3 "); replaceAndBindAttrib(program, source, "gl_Vertex", _vertexAlias, "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_Color", _colorAlias, "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_SecondaryColor", _secondaryColorAlias, "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_FogCoord", _fogCoordAlias, "attribute float "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord0", _texCoordAlias[0], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord1", _texCoordAlias[1], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord2", _texCoordAlias[2], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord3", _texCoordAlias[3], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord4", _texCoordAlias[4], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord5", _texCoordAlias[5], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord6", _texCoordAlias[6], "attribute vec4 "); replaceAndBindAttrib(program, source, "gl_MultiTexCoord7", _texCoordAlias[7], "attribute vec4 "); #endif #if 1 // replace built in uniform replaceBuiltInUniform(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix", "uniform mat4 "); replaceBuiltInUniform(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform mat4 "); replaceBuiltInUniform(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform mat4 "); #endif shader.setShaderSource(source); }
void apply(osg::Program& program, osg::Shader& shader) { if (_visited.count(&shader)!=0) return; _visited.insert(&shader); osg::notify(osg::NOTICE)<<"Shader "<<shader.getTypename()<<" ----before-----------"<<std::endl; osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; if (shader.getType()==osg::Shader::VERTEX) convertVertexShader(program, shader); else if (shader.getType()==osg::Shader::FRAGMENT) convertFragmentShader(program, shader); osg::notify(osg::NOTICE)<<"--after-----------"<<std::endl; osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; osg::notify(osg::NOTICE)<<"---------------------"<<std::endl; }
virtual WriteResult writeShader(const osg::Shader& shader,std::ostream& fout,const Options* = NULL) const { // get shader source std::string source = shader.getShaderSource(); // write source to file fout << source; // return all things went fine return WriteResult::FILE_SAVED; }
static bool readShaderSource( osgDB::InputStream& is, osg::Shader& shader ) { std::string code; unsigned int size = is.readSize(); is >> is.BEGIN_BRACKET; for ( unsigned int i=0; i<size; ++i ) { std::string line; is.readWrappedString( line ); code.append( line ); code.append( 1, '\n' ); } is >> is.END_BRACKET; shader.setShaderSource( code ); return true; }
static bool writeShaderSource( osgDB::OutputStream& os, const osg::Shader& shader ) { std::vector<std::string> lines; std::istringstream iss( shader.getShaderSource() ); std::string line; while ( std::getline(iss, line) ) { lines.push_back( line ); } os.writeSize(lines.size()); os << os.BEGIN_BRACKET << std::endl; for ( std::vector<std::string>::const_iterator itr=lines.begin(); itr!=lines.end(); ++itr ) { os.writeWrappedString( *itr ); os << std::endl; } os << os.END_BRACKET << std::endl; return true; }
static bool checkShaderSource( const osg::Shader& shader ) { return !shader.getShaderSource().empty(); }