From 8c276a0b5eb26608d04c527dc5c968fcf1ea2bfa Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 25 Sep 2008 14:39:57 +0000 Subject: [PATCH] Created new osg2cpp utility that creates .cpp files from source shaders that can be included directly in application code, see osgvolume for an example. --- examples/osg2cpp/CMakeLists.txt | 6 ++ examples/osg2cpp/osg2cpp.cpp | 102 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 examples/osg2cpp/CMakeLists.txt create mode 100644 examples/osg2cpp/osg2cpp.cpp diff --git a/examples/osg2cpp/CMakeLists.txt b/examples/osg2cpp/CMakeLists.txt new file mode 100644 index 000000000..896e4580e --- /dev/null +++ b/examples/osg2cpp/CMakeLists.txt @@ -0,0 +1,6 @@ +SET(TARGET_SRC + osg2cpp.cpp +) + +#### end var setup ### +SETUP_APPLICATION(osg2cpp) diff --git a/examples/osg2cpp/osg2cpp.cpp b/examples/osg2cpp/osg2cpp.cpp new file mode 100644 index 000000000..b4e9f2750 --- /dev/null +++ b/examples/osg2cpp/osg2cpp.cpp @@ -0,0 +1,102 @@ +#include +#include + +#include +#include + +#include +#include + +void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) +{ + std::ofstream fout(cppFileName.c_str()); + if (!fout) + { + std::cout<<"Error: could not open file `"<getShaderSource(); + + std::string variableString = std::string("char ")+variableName+std::string("[] = "); + + std::string::size_type startOfLine = 0; + std::string::size_type endOfLine = shaderSource.find_first_of('\n', startOfLine); + + if (endOfLine==std::string::npos) + { + fout<setApplicationName(arguments.getApplicationName()); + arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is a utility for converting between various input and output databases formats."); + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); + arguments.getApplicationUsage()->addCommandLineOption("--shader ","Shader file to create a .cpp file for."); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters"); + + // if user request help write it out to cout. + if (arguments.read("-h") || arguments.read("--help")) + { + arguments.getApplicationUsage()->write(std::cout); + return 1; + } + + std::string filename; + if (arguments.read("--shader",filename)) + { + osg::ref_ptr shader = osgDB::readShaderFile(filename); + if (shader.valid()) + { + std::string name = osgDB::getStrippedName(filename); + std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; + std::string numbericCharacters = "0123456789"; + std::string::size_type pos = name.find_first_of(invalidCharacters); + while (pos != std::string::npos) + { + name[pos] = '_'; + pos = name.find_first_of(invalidCharacters); + } + + std::string ext = osgDB::getFileExtension(filename); + std::string cppFileName = name + "_" + ext + ".cpp"; + std::string variableName = name + "_" + ext; + writeShader(shader.get(), cppFileName, variableName); + + return 0; + } + else + { + std::cout<<"Error: could not found file '"<write(std::cout); + return 1; +}