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

Revision 14015, 4.7 kB (checked in by robert, 13 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

  • 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        pos += rpat.length();
18    }
19}
20
21void 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
62int main( int argc, char **argv )
63{
64    // use an ArgumentParser object to manage the program arguments.
65    osg::ArgumentParser arguments(&argc,argv);
66
67    // set up the usage document, in case we need to print out how to use this program.
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("--write-to-source-file-directory","Use the path to the source filename as the directory to write to.");
73    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
74
75    // if user request help write it out to cout.
76    if (arguments.read("-h") || arguments.read("--help"))
77    {
78        arguments.getApplicationUsage()->write(std::cout);
79        return 1;
80    }
81
82    bool useSamePathAsSourceFile = false;
83    if (arguments.read("--write-to-source-file-directory")) useSamePathAsSourceFile = true;
84
85    std::string filename;
86    if (arguments.read("--shader",filename))
87    {
88        osg::ref_ptr<osg::Shader> shader = osgDB::readShaderFile(filename);
89        if (shader.valid())
90        {
91            std::string name = osgDB::getStrippedName(filename);
92            std::string path = osgDB::getFilePath(filename);
93            std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\"";
94            std::string numbericCharacters = "0123456789";
95            std::string::size_type pos = name.find_first_of(invalidCharacters);
96            while (pos != std::string::npos)
97            {
98                name[pos] = '_';
99                pos = name.find_first_of(invalidCharacters);
100            }
101
102            std::string ext = osgDB::getFileExtension(filename);
103            std::string cppFileName = name + "_" + ext + ".cpp";
104            if (useSamePathAsSourceFile) cppFileName = osgDB::concatPaths(path, cppFileName);
105
106            std::string variableName = name + "_" + ext;
107            writeShader(shader.get(), cppFileName, variableName);
108
109            return 0;
110        }
111        else
112        {
113            std::cout<<"Error: could not find file '"<<filename<<"'"<<std::endl;
114            return 1;
115        }
116
117    }
118
119    std::cout<<"No appropriate command line options used."<<std::endl;
120
121    arguments.getApplicationUsage()->write(std::cout);
122    return 1;
123}
Note: See TracBrowser for help on using the browser.