virtual WriteResult writeShader(const osg::Shader &shader,const std::string& fileName, const osgDB::ReaderWriter::Options*) const { std::string ext = osgDB::getFileExtension(fileName); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; return writeShader(shader, fout); }
int main( int argc, char **argv ) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting glsl shader files into char arrays that can be compiled into applications."); arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("--shader <filename>","Shader file to create a .cpp file for."); arguments.getApplicationUsage()->addCommandLineOption("--write-to-source-file-directory","Use the path to the source filename as the directory to write to."); 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; } bool useSamePathAsSourceFile = false; if (arguments.read("--write-to-source-file-directory")) useSamePathAsSourceFile = true; std::string filename; if (arguments.read("--shader",filename)) { osg::ref_ptr<osg::Shader> 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 = name + "_" + ext + ".cpp"; if (useSamePathAsSourceFile) cppFileName = osgDB::concatPaths(path, cppFileName); std::string variableName = name + "_" + ext; writeShader(shader.get(), cppFileName, variableName); return 0; } else { std::cout<<"Error: could not find file '"<<filename<<"'"<<std::endl; return 1; } } std::cout<<"No appropriate command line options used."<<std::endl; arguments.getApplicationUsage()->write(std::cout); return 1; }