From ece7b57df2b08efd77a6607b5613ff2182068f0a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 21 Sep 2007 13:30:33 +0000 Subject: [PATCH] Further steps towards reading coniguration files. --- include/osgViewer/CompositeViewer | 5 +- include/osgViewer/Viewer | 7 +++ src/osgPlugins/osg/ReaderWriterOSG.cpp | 61 ++++++++++++++++++- src/osgPlugins/osgViewer/CMakeLists.txt | 2 + src/osgPlugins/osgViewer/CompositeViewer.cpp | 40 ++++++++++++ .../osgViewer/ReaderWriterOsgViewer.cpp | 2 +- src/osgPlugins/osgViewer/View.cpp | 2 + src/osgPlugins/osgViewer/Viewer.cpp | 36 +++++++++++ src/osgViewer/CompositeViewer.cpp | 12 ++++ src/osgViewer/Viewer.cpp | 60 ++++++++++++++++-- 10 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 src/osgPlugins/osgViewer/CompositeViewer.cpp create mode 100644 src/osgPlugins/osgViewer/Viewer.cpp diff --git a/include/osgViewer/CompositeViewer b/include/osgViewer/CompositeViewer index be2d334d1..4c0f5374a 100644 --- a/include/osgViewer/CompositeViewer +++ b/include/osgViewer/CompositeViewer @@ -32,10 +32,13 @@ class OSGVIEWER_EXPORT CompositeViewer : public osg::Object CompositeViewer(osg::ArgumentParser& arguments); - META_Object(osg,CompositeViewer); + META_Object(osgViewer,CompositeViewer); virtual ~CompositeViewer(); + /** read the viewer configuration from a configuration file.*/ + bool readConfiguration(const std::string& filename); + void addView(osgViewer::View* view); void removeView(osgViewer::View* view); diff --git a/include/osgViewer/Viewer b/include/osgViewer/Viewer index 4b2acf809..4cefa02fc 100644 --- a/include/osgViewer/Viewer +++ b/include/osgViewer/Viewer @@ -32,8 +32,15 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View Viewer(osg::ArgumentParser& arguments); + Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + virtual ~Viewer(); + META_Object(osgViewer,Viewer); + + /** read the viewer configuration from a configuration file.*/ + bool readConfiguration(const std::string& filename); + /** Get whether at least of one of this viewers windows are realized.*/ bool isRealized() const; diff --git a/src/osgPlugins/osg/ReaderWriterOSG.cpp b/src/osgPlugins/osg/ReaderWriterOSG.cpp index 2813aade6..4073776ab 100644 --- a/src/osgPlugins/osg/ReaderWriterOSG.cpp +++ b/src/osgPlugins/osg/ReaderWriterOSG.cpp @@ -23,9 +23,66 @@ class OSGReaderWriter : public ReaderWriter return equalCaseInsensitive(extension,"osg"); } - virtual ReadResult readObject(const std::string& fileName, const Options* opt) const { return readNode(fileName, opt); } + virtual ReadResult readObject(const std::string& file, const Options* opt) const + { + std::string ext = osgDB::getLowerCaseFileExtension(file); + + if (equalCaseInsensitive(ext,"osgs")) + { + std::istringstream fin(osgDB::getNameLessExtension(file)); + if (fin) return readNode(fin,opt); + return ReadResult::ERROR_IN_READING_FILE; + } + + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; - virtual ReadResult readObject(std::istream& fin, const Options* opt) const { return readNode(fin, opt); } + std::string fileName = osgDB::findDataFile( file, opt ); + if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + + // code for setting up the database path so that internally referenced file are searched for on relative paths. + osg::ref_ptr local_opt = opt ? static_cast(opt->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + local_opt->setDatabasePath(osgDB::getFilePath(fileName)); + + std::ifstream fin(fileName.c_str()); + if (fin) + { + return readObject(fin, local_opt.get()); + } + return 0L; + } + + virtual ReadResult readObject(std::istream& fin, const Options* options) const + { + fin.imbue(std::locale::classic()); + + Input fr; + fr.attach(&fin); + fr.setOptions(options); + + typedef std::vector ObjectList; + ObjectList objectList; + + // load all nodes in file, placing them in a group. + while(!fr.eof()) + { + Object *object = fr.readObject(); + if (object) objectList.push_back(object); + else fr.advanceOverCurrentFieldOrBlock(); + } + + if (objectList.empty()) + { + return ReadResult("No data loaded"); + } + else if (objectList.size()==1) + { + return objectList.front(); + } + else + { + return objectList.front(); + } + } virtual ReadResult readNode(const std::string& file, const Options* opt) const { diff --git a/src/osgPlugins/osgViewer/CMakeLists.txt b/src/osgPlugins/osgViewer/CMakeLists.txt index a3c0fe3e1..b45318752 100644 --- a/src/osgPlugins/osgViewer/CMakeLists.txt +++ b/src/osgPlugins/osgViewer/CMakeLists.txt @@ -1,6 +1,8 @@ SET(TARGET_SRC View.cpp + Viewer.cpp + CompositeViewer.cpp ReaderWriterOsgViewer.cpp ) SET(TARGET_ADDED_LIBRARIES osgViewer ) diff --git a/src/osgPlugins/osgViewer/CompositeViewer.cpp b/src/osgPlugins/osgViewer/CompositeViewer.cpp new file mode 100644 index 000000000..67ba321e2 --- /dev/null +++ b/src/osgPlugins/osgViewer/CompositeViewer.cpp @@ -0,0 +1,40 @@ +#include + +#include +#include + +#include +#include +#include +#include + +bool CompositeViewer_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool CompositeViewer_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy CompositeViewer_Proxy +( + new osgViewer::CompositeViewer, + "CompositeViewer", + "Object CompositeViewer", + CompositeViewer_readLocalData, + CompositeViewer_writeLocalData +); + +bool CompositeViewer_readLocalData(osg::Object &obj, osgDB::Input &fr) +{ + osgViewer::CompositeViewer& compositeViewer = static_cast(obj); + bool iteratorAdvanced = false; + + osg::notify(osg::NOTICE)<<"CompositeViewer_readLocalData"<(obj); + + osg::notify(osg::NOTICE)<<"CompositeViewer_writeLocalData"<(obj); + osg::notify(osg::NOTICE)<<"View_writeLocalData"< + +#include +#include + +#include +#include +#include +#include + +bool Viewer_readLocalData(osg::Object &obj, osgDB::Input &fr); +bool Viewer_writeLocalData(const osg::Object &obj, osgDB::Output &fw); + +osgDB::RegisterDotOsgWrapperProxy Viewer_Proxy +( + new osgViewer::Viewer, + "Viewer", + "Object Viewer View", + Viewer_readLocalData, + Viewer_writeLocalData +); + +bool Viewer_readLocalData(osg::Object &obj, osgDB::Input &fr) +{ + osgViewer::Viewer& viewer = static_cast(obj); + bool iteratorAdvanced = false; + + return iteratorAdvanced; +} + +bool Viewer_writeLocalData(const osg::Object &obj, osgDB::Output &fw) +{ + const osgViewer::Viewer& viewer = static_cast(obj); + + return true; +} diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 1515e6041..d200a7626 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -37,6 +37,13 @@ CompositeViewer::CompositeViewer(osg::ArgumentParser& arguments) { constructorInit(); + std::string filename; + bool readConfig = false; + while (arguments.read("-c",filename)) + { + readConfig = readConfiguration(filename) || readConfig; + } + while (arguments.read("--SingleThreaded")) setThreadingModel(SingleThreaded); while (arguments.read("--ThreadPerContext")) setThreadingModel(ThreadPerContext); @@ -109,6 +116,11 @@ CompositeViewer::~CompositeViewer() osg::notify(osg::INFO)<<"finished CompositeViewer::~CompsiteViewer()"< #include +#include #include using namespace osgViewer; +//static osg::ApplicationUsageProxy Viewer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_CONFIG_FILE ","Specify a viewer configuration file to load by default."); +static osg::ApplicationUsageProxy Viewer_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING ","Set the threading model using by Viewer, can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext."); +static osg::ApplicationUsageProxy Viewer_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN ","Set the default screen that windows should open up on."); +static osg::ApplicationUsageProxy Viewer_e3(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WINDOW x y width height","Set the default window dimensions that windows should open up on."); + + Viewer::Viewer() { constructorInit(); @@ -38,6 +45,13 @@ Viewer::Viewer(osg::ArgumentParser& arguments) { constructorInit(); + std::string filename; + bool readConfig = false; + while (arguments.read("-c",filename)) + { + readConfig = readConfiguration(filename) || readConfig; + } + while (arguments.read("--SingleThreaded")) setThreadingModel(SingleThreaded); while (arguments.read("--CullDrawThreadPerContext")) setThreadingModel(CullDrawThreadPerContext); while (arguments.read("--DrawThreadPerContext")) setThreadingModel(DrawThreadPerContext); @@ -100,6 +114,11 @@ Viewer::Viewer(osg::ArgumentParser& arguments) } +Viewer::Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop): + View(viewer,copyop) +{ +} + void Viewer::constructorInit() { _firstFrame = true; @@ -159,6 +178,43 @@ Viewer::~Viewer() } +bool Viewer::readConfiguration(const std::string& filename) +{ + osg::notify(osg::NOTICE)<<"Viewer::readConfiguration("< object = osgDB::readObjectFile(filename); + if (!object) + { + osg::notify(osg::NOTICE)<<"Error: Unable to load configuration file \""<(object.get()); + if (compositeViewer) + { + osg::notify(osg::NOTICE)<<"Error: Config file \""<","Set the threading model using by Viewer, can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext."); -static osg::ApplicationUsageProxy Viewer_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN ","Set the default screen that windows should open up on."); -static osg::ApplicationUsageProxy Viewer_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WINDOW x y width height","Set the default window dimensions that windows should open up on."); - Viewer::ThreadingModel Viewer::suggestBestThreadingModel() { const char* str = getenv("OSG_THREADING");