root/OpenSceneGraph/trunk/examples/osg2cpp/osg2cpp.cpp @ 12398

Revision 12398, 4.3 kB (checked in by robert, 4 years ago)

From Wang Rui, "A Chinese engineer (named Beilei Geng) reports a possible bug in the
osg2cpp application to me today. The conversion result may become
incorrect if there are quotation marks ( " ) in the shader file, which
will mostly appear in comment lines.

Replace all " into \" before writing to cpp files will solve the
problem, as the attached file shows."

  • Property svn:eol-style set to native
Line 
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// Search in str for all occurences of spat and replace them with rpat.
11void 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    }
18}
19
20void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName)
21{
22    osgDB::ofstream fout(cppFileName.c_str());
23    if (!fout)
24    {
25        std::cout<<"Error: could not open file `"<<cppFileName<<"` for writing."<<std::endl;
26    }
27
28    std::string shaderSource = shader->getShaderSource();
29    searchAndReplace(shaderSource, "\r\n", "\n");
30    searchAndReplace(shaderSource, "\r", "\n");
31    searchAndReplace(shaderSource, "\"", "\\\"");
32 
33    std::string variableString = std::string("char ")+variableName+std::string("[] = ");
34   
35    std::string::size_type startOfLine = 0;
36    std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine);
37   
38    if (endOfLine==std::string::npos)
39    {
40        fout<<variableString<<shaderSource<<"\\n\";"<<std::endl;
41    }
42    else
43    {
44        std::string padding(variableString.size(),' ');
45
46        fout<<variableString<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl;
47        startOfLine = endOfLine+1;
48        endOfLine = shaderSource.find_first_of('\n', startOfLine);
49
50        while (endOfLine != std::string::npos)
51        {
52            fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\""<<std::endl;
53            startOfLine = endOfLine + 1;
54            endOfLine = shaderSource.find_first_of('\n', startOfLine);
55        }
56        fout<<padding<<"\""<<shaderSource.substr(startOfLine,endOfLine-startOfLine)<<"\\n\";"<<std::endl;
57    }
58    std::cout<<"Written shader to `"<<cppFileName<<"`"<<std::endl;
59}
60
61int main( int argc, char **argv )
62{
63    // use an ArgumentParser object to manage the program arguments.
64    osg::ArgumentParser arguments(&argc,argv);
65   
66    // set up the usage document, in case we need to print out how to use this program.
67    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
68    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting between various input and output databases formats.");
69    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
70    arguments.getApplicationUsage()->addCommandLineOption("--shader <filename>","Shader file to create a .cpp file for.");
71    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
72
73    // if user request help write it out to cout.
74    if (arguments.read("-h") || arguments.read("--help"))
75    {
76        arguments.getApplicationUsage()->write(std::cout);
77        return 1;
78    }
79   
80    std::string filename;
81    if (arguments.read("--shader",filename))
82    {
83        osg::ref_ptr<osg::Shader> shader = osgDB::readShaderFile(filename);
84        if (shader.valid())
85        {
86            std::string name = osgDB::getStrippedName(filename);
87            std::string path = osgDB::getFilePath(filename);
88            std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\"";
89            std::string numbericCharacters = "0123456789";
90            std::string::size_type pos = name.find_first_of(invalidCharacters);
91            while (pos != std::string::npos)
92            {
93                name[pos] = '_';
94                pos = name.find_first_of(invalidCharacters);
95            }
96           
97            std::string ext = osgDB::getFileExtension(filename);
98            std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp");
99            std::string variableName = name + "_" + ext;
100            writeShader(shader.get(), cppFileName, variableName);
101
102            return 0;
103        }
104        else
105        {
106            std::cout<<"Error: could not find file '"<<filename<<"'"<<std::endl;
107            return 1;
108        }
109       
110    }
111
112    std::cout<<"No appropriate command line options used."<<std::endl;
113
114    arguments.getApplicationUsage()->write(std::cout);
115    return 1;
116}
Note: See TracBrowser for help on using the browser.