| | 49 | std::string convertShader(std::string source) |
| | 50 | { |
| | 51 | replace(source, "gl_Vertex", "osg_Vertex"); |
| | 52 | replace(source, "gl_Normal", "osg_Normal"); |
| | 53 | replace(source, "gl_Color", "osg_Color"); |
| | 54 | replace(source, "gl_SecondaryColor", "osg_SecondaryColor"); |
| | 55 | replace(source, "gl_FogCoord", "osg_FogCoord"); |
| | 56 | replace(source, "gl_MultiTexCoord0", "osg_MultiTexCoord0"); |
| | 57 | replace(source, "gl_MultiTexCoord1", "osg_MultiTexCoord1"); |
| | 58 | replace(source, "gl_MultiTexCoord2", "osg_MultiTexCoord2"); |
| | 59 | replace(source, "gl_MultiTexCoord3", "osg_MultiTexCoord3"); |
| | 60 | replace(source, "gl_MultiTexCoord4", "osg_MultiTexCoord4"); |
| | 61 | replace(source, "gl_MultiTexCoord5", "osg_MultiTexCoord5"); |
| | 62 | replace(source, "gl_MultiTexCoord6", "osg_MultiTexCoord6"); |
| | 63 | replace(source, "gl_MultiTexCoord7", "osg_MultiTexCoord7"); |
| | 64 | return source; |
| | 65 | } |
| | 66 | |
| | 67 | virtual void reset() |
| | 68 | { |
| | 69 | _visited.clear(); |
| | 70 | } |
| | 71 | |
| | 72 | void apply(osg::Node& node) |
| | 73 | { |
| | 74 | if (_visited.count(&node)!=0) return; |
| | 75 | _visited.insert(&node); |
| | 76 | |
| | 77 | if (node.getStateSet()) apply(*(node.getStateSet())); |
| | 78 | traverse(node); |
| | 79 | } |
| | 96 | |
| | 97 | void replace(std::string& str, const std::string& original_phrase, const std::string& new_phrase) |
| | 98 | { |
| | 99 | std::string::size_type pos = 0; |
| | 100 | while((pos=str.find(original_phrase, pos))!=std::string::npos) |
| | 101 | { |
| | 102 | std::string::size_type endOfPhrasePos = pos+original_phrase.size(); |
| | 103 | if (endOfPhrasePos<str.size()) |
| | 104 | { |
| | 105 | char c = str[endOfPhrasePos]; |
| | 106 | if ((c>='0' && c<='9') || |
| | 107 | (c>='a' && c<='z') || |
| | 108 | (c>='A' && c<='Z')) |
| | 109 | { |
| | 110 | pos = endOfPhrasePos; |
| | 111 | continue; |
| | 112 | } |
| | 113 | } |
| | 114 | |
| | 115 | str.replace(pos, original_phrase.size(), new_phrase); |
| | 116 | } |
| | 117 | } |
| | 118 | |
| | 119 | void apply(osg::Shader& shader) |
| | 120 | { |
| | 121 | if (_visited.count(&shader)!=0) return; |
| | 122 | _visited.insert(&shader); |
| | 123 | |
| | 124 | osg::notify(osg::NOTICE)<<"Shader "<<shader.getTypename()<<" ----before-----------"<<std::endl; |
| | 125 | osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; |
| | 126 | shader.setShaderSource(convertShader(shader.getShaderSource())); |
| | 127 | osg::notify(osg::NOTICE)<<"--after-----------"<<std::endl; |
| | 128 | osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; |
| | 129 | osg::notify(osg::NOTICE)<<"---------------------"<<std::endl; |
| | 130 | } |
| | 131 | |
| | 132 | void apply(osg::StateSet& stateset) |
| | 133 | { |
| | 134 | if (_visited.count(&stateset)!=0) return; |
| | 135 | _visited.insert(&stateset); |
| | 136 | |
| | 137 | osg::notify(osg::NOTICE)<<"Found stateset "<<&stateset<<std::endl; |
| | 138 | osg::Program* program = dynamic_cast<osg::Program*>(stateset.getAttribute(osg::StateAttribute::PROGRAM)); |
| | 139 | if (program) |
| | 140 | { |
| | 141 | osg::notify(osg::NOTICE)<<" Found Program "<<program<<std::endl; |
| | 142 | for(unsigned int i=0; i<program->getNumShaders(); ++i) |
| | 143 | { |
| | 144 | apply(*(program->getShader(i))); |
| | 145 | } |
| | 146 | } |
| | 147 | } |