| 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 | |
|---|
| 11 | void searchAndReplace(std::string& str, const std::string& spat, const std::string& rpat) |
|---|
| 12 | { |
|---|
| 13 | std::string::size_type pos = 0; |
|---|
| 14 | while ((pos = str.find(spat, pos)) != std::string::npos) |
|---|
| 15 | { |
|---|
| 16 | str.replace(pos, spat.length(), rpat); |
|---|
| 17 | pos += rpat.length(); |
|---|
| 18 | } |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) |
|---|
| 22 | { |
|---|
| 23 | osgDB::ofstream fout(cppFileName.c_str()); |
|---|
| 24 | if (!fout) |
|---|
| 25 | { |
|---|
| 26 | std::cout<<"Error: could not open file `"<<cppFileName<<"` for writing."<<std::endl; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | std::string shaderSource = shader->getShaderSource(); |
|---|
| 30 | searchAndReplace(shaderSource, "\r\n", "\n"); |
|---|
| 31 | searchAndReplace(shaderSource, "\r", "\n"); |
|---|
| 32 | searchAndReplace(shaderSource, "\"", "\\\""); |
|---|
| 33 | |
|---|
| 34 | std::string variableString = std::string("char ")+variableName+std::string("[] = "); |
|---|
| 35 | |
|---|
| 36 | std::string::size_type startOfLine = 0; |
|---|
| 37 | std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 38 | |
|---|
| 39 | if (endOfLine==std::string::npos) |
|---|
| 40 | { |
|---|
| 41 | fout<<variableString<<shaderSource<<"\\n\";"<<std::endl; |
|---|
| 42 | } |
|---|
| 43 | else |
|---|
| 44 | { |
|---|
| 45 | std::string padding(variableString.size(),' '); |
|---|
| 46 | |
|---|
| 47 | fout<<variableString<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl; |
|---|
| 48 | startOfLine = endOfLine+1; |
|---|
| 49 | endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 50 | |
|---|
| 51 | while (endOfLine != std::string::npos) |
|---|
| 52 | { |
|---|
| 53 | fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl; |
|---|
| 54 | startOfLine = endOfLine + 1; |
|---|
| 55 | endOfLine = shaderSource.find_first_of('\n', startOfLine); |
|---|
| 56 | } |
|---|
| 57 | fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\";"<<std::endl; |
|---|
| 58 | } |
|---|
| 59 | std::cout<<"Written shader to `"<<cppFileName<<"`"<<std::endl; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | int main( int argc, char **argv ) |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 69 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting glsl shader files into char arrays that can be compiled into applications."); |
|---|
| 70 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 71 | arguments.getApplicationUsage()->addCommandLineOption("--shader <filename>","Shader file to create a .cpp file for."); |
|---|
| 72 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 76 | { |
|---|
| 77 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 78 | return 1; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | std::string filename; |
|---|
| 82 | if (arguments.read("--shader",filename)) |
|---|
| 83 | { |
|---|
| 84 | osg::ref_ptr<osg::Shader> shader = osgDB::readShaderFile(filename); |
|---|
| 85 | if (shader.valid()) |
|---|
| 86 | { |
|---|
| 87 | std::string name = osgDB::getStrippedName(filename); |
|---|
| 88 | std::string path = osgDB::getFilePath(filename); |
|---|
| 89 | std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; |
|---|
| 90 | std::string numbericCharacters = "0123456789"; |
|---|
| 91 | std::string::size_type pos = name.find_first_of(invalidCharacters); |
|---|
| 92 | while (pos != std::string::npos) |
|---|
| 93 | { |
|---|
| 94 | name[pos] = '_'; |
|---|
| 95 | pos = name.find_first_of(invalidCharacters); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | std::string ext = osgDB::getFileExtension(filename); |
|---|
| 99 | std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp"); |
|---|
| 100 | std::string variableName = name + "_" + ext; |
|---|
| 101 | writeShader(shader.get(), cppFileName, variableName); |
|---|
| 102 | |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | else |
|---|
| 106 | { |
|---|
| 107 | std::cout<<"Error: could not find file '"<<filename<<"'"<<std::endl; |
|---|
| 108 | return 1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | std::cout<<"No appropriate command line options used."<<std::endl; |
|---|
| 114 | |
|---|
| 115 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 116 | return 1; |
|---|
| 117 | } |
|---|