| 1 | #include <osg/ArgumentParser> |
|---|
| 2 | #include <osg/ApplicationUsage> |
|---|
| 3 | |
|---|
| 4 | #include <osgDB/ReadFile> |
|---|
| 5 | #include <osgDB/FileNameUtils> |
|---|
| 6 | #include <osgDB/fstream> |
|---|
| 7 | |
|---|
| 8 | #include <iostream> |
|---|
| 9 | |
|---|
| 10 | void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) |
|---|
| 11 | { |
|---|
| 12 | osgDB::ofstream fout(cppFileName.c_str()); |
|---|
| 13 | if (!fout) |
|---|
| 14 | { |
|---|
| 15 | std::cout<<"Error: could not open file `"<<cppFileName<<"` for writing."<<std::endl; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | std::string shaderSource = shader->getShaderSource(); |
|---|
| 19 | |
|---|
| 20 | std::string variableString = std::string("char ")+variableName+std::string("[] = "); |
|---|
| 21 | |
|---|
| 22 | std::string::size_type startOfLine = 0; |
|---|
| 23 | std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 24 | |
|---|
| 25 | if (endOfLine==std::string::npos) |
|---|
| 26 | { |
|---|
| 27 | fout<<variableString<<shaderSource<<"\\n\";"<<std::endl; |
|---|
| 28 | } |
|---|
| 29 | else |
|---|
| 30 | { |
|---|
| 31 | std::string padding(variableString.size(),' '); |
|---|
| 32 | |
|---|
| 33 | fout<<variableString<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl; |
|---|
| 34 | startOfLine = endOfLine+1; |
|---|
| 35 | endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 36 | |
|---|
| 37 | while (endOfLine != std::string::npos) |
|---|
| 38 | { |
|---|
| 39 | fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl; |
|---|
| 40 | startOfLine = endOfLine + 1; |
|---|
| 41 | endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 42 | } |
|---|
| 43 | fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\";"<<std::endl; |
|---|
| 44 | } |
|---|
| 45 | std::cout<<"Written shader to `"<<cppFileName<<"`"<<std::endl; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | int main( int argc, char **argv ) |
|---|
| 49 | { |
|---|
| 50 | |
|---|
| 51 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 55 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting between various input and output databases formats."); |
|---|
| 56 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 57 | arguments.getApplicationUsage()->addCommandLineOption("--shader <filename>","Shader file to create a .cpp file for."); |
|---|
| 58 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 62 | { |
|---|
| 63 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 64 | return 1; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | std::string filename; |
|---|
| 68 | if (arguments.read("--shader",filename)) |
|---|
| 69 | { |
|---|
| 70 | osg::ref_ptr<osg::Shader> shader = osgDB::readShaderFile(filename); |
|---|
| 71 | if (shader.valid()) |
|---|
| 72 | { |
|---|
| 73 | std::string name = osgDB::getStrippedName(filename); |
|---|
| 74 | std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; |
|---|
| 75 | std::string numbericCharacters = "0123456789"; |
|---|
| 76 | std::string::size_type pos = name.find_first_of(invalidCharacters); |
|---|
| 77 | while (pos != std::string::npos) |
|---|
| 78 | { |
|---|
| 79 | name[pos] = '_'; |
|---|
| 80 | pos = name.find_first_of(invalidCharacters); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | std::string ext = osgDB::getFileExtension(filename); |
|---|
| 84 | std::string cppFileName = name + "_" + ext + ".cpp"; |
|---|
| 85 | std::string variableName = name + "_" + ext; |
|---|
| 86 | writeShader(shader.get(), cppFileName, variableName); |
|---|
| 87 | |
|---|
| 88 | return 0; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | std::cout<<"Error: could not found file '"<<filename<<"'"<<std::endl; |
|---|
| 93 | return 1; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | std::cout<<"No appropriate command line options used."<<std::endl; |
|---|
| 99 | |
|---|
| 100 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 101 | return 1; |
|---|
| 102 | } |
|---|