#include #include #include #include #include #include // Search in str for all occurences of spat and replace them with rpat. void searchAndReplace(std::string& str, const std::string& spat, const std::string& rpat) { std::string::size_type pos = 0; while ((pos = str.find(spat, pos)) != std::string::npos) { str.replace(pos, spat.length(), rpat); pos += rpat.length(); } } void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) { osgDB::ofstream fout(cppFileName.c_str()); if (!fout) { std::cout<<"Error: could not open file `"<getShaderSource(); searchAndReplace(shaderSource, "\r\n", "\n"); searchAndReplace(shaderSource, "\r", "\n"); searchAndReplace(shaderSource, "\"", "\\\""); std::string variableString = std::string("char ")+variableName+std::string("[] = "); std::string::size_type startOfLine = 0; std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine); if (endOfLine==std::string::npos) { fout<setApplicationName(arguments.getApplicationName()); arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting between various input and output databases formats."); arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("--shader ","Shader file to create a .cpp file for."); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); // if user request help write it out to cout. if (arguments.read("-h") || arguments.read("--help")) { arguments.getApplicationUsage()->write(std::cout); return 1; } std::string filename; if (arguments.read("--shader",filename)) { osg::ref_ptr shader = osgDB::readShaderFile(filename); if (shader.valid()) { std::string name = osgDB::getStrippedName(filename); std::string path = osgDB::getFilePath(filename); std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; std::string numbericCharacters = "0123456789"; std::string::size_type pos = name.find_first_of(invalidCharacters); while (pos != std::string::npos) { name[pos] = '_'; pos = name.find_first_of(invalidCharacters); } std::string ext = osgDB::getFileExtension(filename); std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp"); std::string variableName = name + "_" + ext; writeShader(shader.get(), cppFileName, variableName); return 0; } else { std::cout<<"Error: could not find file '"<write(std::cout); return 1; }