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

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

From Alberto Luaces,"the current code uses the preprocessor for generating the plugin path in
a way that when CMAKE_INSTALL_PREFIX contains something along the lines
of

/usr/x86_64-linux-gnu/

it gets substituted as

/usr/x86_64-1-gnu/

that is, the string is preprocessed again, thereby making changes to
anything that matches any defined symbol, as "linux" in this example
(https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=763816).

Quoting that path directly in CMake scripts solves that problem.
"

  • Property svn:eol-style set to native
RevLine 
[8937]1#include <osg/ArgumentParser>
2#include <osg/ApplicationUsage>
3
4#include <osgDB/ReadFile>
5#include <osgDB/FileNameUtils>
[9124]6#include <osgDB/fstream>
[8937]7
8#include <iostream>
9
[10840]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);
[12460]17        pos += rpat.length();
[10840]18    }
19}
20
[8937]21void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName)
22{
[9124]23    osgDB::ofstream fout(cppFileName.c_str());
[8937]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();
[10840]30    searchAndReplace(shaderSource, "\r\n", "\n");
31    searchAndReplace(shaderSource, "\r", "\n");
[12398]32    searchAndReplace(shaderSource, "\"", "\\\"");
[14015]33
[8937]34    std::string variableString = std::string("char ")+variableName+std::string("[] = ");
[14015]35
[8937]36    std::string::size_type startOfLine = 0;
37    std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine);
[14015]38
39    if (endOfLine==std::string::npos)
[8937]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);
[14015]66
[8937]67    // set up the usage document, in case we need to print out how to use this program.
68    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
[12985]69    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting glsl shader files into char arrays that can be compiled into applications.");
[8937]70    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
71    arguments.getApplicationUsage()->addCommandLineOption("--shader <filename>","Shader file to create a .cpp file for.");
[14015]72    arguments.getApplicationUsage()->addCommandLineOption("--write-to-source-file-directory","Use the path to the source filename as the directory to write to.");
[8937]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"))
[14015]77    {
[8937]78        arguments.getApplicationUsage()->write(std::cout);
79        return 1;
80    }
[14015]81
82    bool useSamePathAsSourceFile = false;
83    if (arguments.read("--write-to-source-file-directory")) useSamePathAsSourceFile = true;
84
[8937]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);
[10840]92            std::string path = osgDB::getFilePath(filename);
[8937]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            }
[14015]101
[8937]102            std::string ext = osgDB::getFileExtension(filename);
[14015]103            std::string cppFileName = name + "_" + ext + ".cpp";
104            if (useSamePathAsSourceFile) cppFileName = osgDB::concatPaths(path, cppFileName);
105
[8937]106            std::string variableName = name + "_" + ext;
107            writeShader(shader.get(), cppFileName, variableName);
108
109            return 0;
110        }
111        else
112        {
[9692]113            std::cout<<"Error: could not find file '"<<filename<<"'"<<std::endl;
[8937]114            return 1;
115        }
[14015]116
[8937]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.