Further steps towards reading coniguration files.

This commit is contained in:
Robert Osfield 2007-09-21 13:30:33 +00:00
parent 609315caa5
commit ece7b57df2
10 changed files with 219 additions and 8 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
virtual ReadResult readObject(std::istream& fin, const Options* opt) const { return readNode(fin, opt); }
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;
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<Options> local_opt = opt ? static_cast<Options*>(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<osg::Object*> 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
{

View File

@ -1,6 +1,8 @@
SET(TARGET_SRC
View.cpp
Viewer.cpp
CompositeViewer.cpp
ReaderWriterOsgViewer.cpp
)
SET(TARGET_ADDED_LIBRARIES osgViewer )

View File

@ -0,0 +1,40 @@
#include <osgViewer/CompositeViewer>
#include <iostream>
#include <string>
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include <osgDB/ParameterOutput>
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<osgViewer::CompositeViewer&>(obj);
bool iteratorAdvanced = false;
osg::notify(osg::NOTICE)<<"CompositeViewer_readLocalData"<<std::endl;
return iteratorAdvanced;
}
bool CompositeViewer_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgViewer::CompositeViewer& compositeViewer = static_cast<const osgViewer::CompositeViewer&>(obj);
osg::notify(osg::NOTICE)<<"CompositeViewer_writeLocalData"<<std::endl;
return true;
}

View File

@ -29,7 +29,7 @@ public:
virtual bool acceptsExtension(const std::string& extension) const
{
return osgDB::equalCaseInsensitive( extension, "osgViewer" ) || osgDB::equalCaseInsensitive( extension, "view" ) ;
return osgDB::equalCaseInsensitive( extension, "osgviewer" ) || osgDB::equalCaseInsensitive( extension, "view" ) ;
}

View File

@ -60,6 +60,8 @@ bool View_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgViewer::View& view = static_cast<const osgViewer::View&>(obj);
osg::notify(osg::NOTICE)<<"View_writeLocalData"<<std::endl;
if (view.getCamera())
{
fw.writeObject(*view.getCamera());

View File

@ -0,0 +1,36 @@
#include <osgViewer/Viewer>
#include <iostream>
#include <string>
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include <osgDB/ParameterOutput>
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<osgViewer::Viewer&>(obj);
bool iteratorAdvanced = false;
return iteratorAdvanced;
}
bool Viewer_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
{
const osgViewer::Viewer& viewer = static_cast<const osgViewer::Viewer&>(obj);
return true;
}

View File

@ -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()"<<std::endl;
}
bool CompositeViewer::readConfiguration(const std::string& filename)
{
osg::notify(osg::NOTICE)<<"CompositeViewer::readConfiguration("<<filename<<")"<<std::endl;
}
void CompositeViewer::addView(osgViewer::View* view)
{

View File

@ -23,12 +23,19 @@
#include <osgViewer/Viewer>
#include <osgViewer/Renderer>
#include <osgViewer/CompositeViewer>
#include <sstream>
using namespace osgViewer;
//static osg::ApplicationUsageProxy Viewer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_CONFIG_FILE <filename>","Specify a viewer configuration file to load by default.");
static osg::ApplicationUsageProxy Viewer_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING <value>","Set the threading model using by Viewer, <value> can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext.");
static osg::ApplicationUsageProxy Viewer_e2(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN <value>","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("<<filename<<")"<<std::endl;
osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(filename);
if (!object)
{
osg::notify(osg::NOTICE)<<"Error: Unable to load configuration file \""<<filename<<"\""<<std::endl;
return false;
}
CompositeViewer* compositeViewer = dynamic_cast<CompositeViewer*>(object.get());
if (compositeViewer)
{
osg::notify(osg::NOTICE)<<"Error: Config file \""<<filename<<"\" containing CompositeViewer cannot be loaded by Viewer."<<std::endl;
return false;
}
osg::notify(osg::NOTICE)<<"Loaded object = "<<object->className()<<std::endl;
View* view = dynamic_cast<osgViewer::View*>(object.get());
if (view)
{
Viewer* viewer = dynamic_cast<Viewer*>(object.get());
osg::notify(osg::NOTICE)<<" ViewerPtr = "<<viewer<<std::endl;
osg::notify(osg::NOTICE)<<" ViewPtr = "<<view<<std::endl;
return true;
}
else
{
osg::notify(osg::NOTICE)<<"Error: Config file \""<<filename<<"\" does not contain a valid Viewer configuration."<<std::endl;
return false;
}
return false;
}
bool Viewer::isRealized() const
{
@ -416,10 +472,6 @@ void Viewer::stopThreading()
osg::notify(osg::INFO)<<"Viewer::stopThreading() - stopped threading."<<std::endl;
}
static osg::ApplicationUsageProxy Viewer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING <value>","Set the threading model using by Viewer, <value> can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext.");
static osg::ApplicationUsageProxy Viewer_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_SCREEN <value>","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");