Added keystone file handling

This commit is contained in:
Robert Osfield
2013-05-10 16:06:10 +00:00
parent 9eb5465ff5
commit 5dd07e4d1c
3 changed files with 92 additions and 18 deletions

View File

@@ -66,25 +66,44 @@ int main( int argc, char **argv )
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
OSG_NOTICE<<"KeystoneFileNames.size()="<<osg::DisplaySettings::instance()->getKeystoneFileNames().size()<<std::endl;
for(osg::DisplaySettings::FileNames::iterator itr = osg::DisplaySettings::instance()->getKeystoneFileNames().begin();
itr != osg::DisplaySettings::instance()->getKeystoneFileNames().end();
OSG_NOTICE<<"KeystoneFileNames.size()="<<ds->getKeystoneFileNames().size()<<std::endl;
for(osg::DisplaySettings::FileNames::iterator itr = ds->getKeystoneFileNames().begin();
itr != ds->getKeystoneFileNames().end();
++itr)
{
OSG_NOTICE<<" keystone ="<<*itr<<std::endl;
OSG_NOTICE<<" keystone filename = "<<*itr<<std::endl;
}
if (!ds->getKeystoneFileNames().empty())
{
for(osg::DisplaySettings::Objects::iterator itr = ds->getKeystones().begin();
itr != ds->getKeystones().end();
++itr)
{
osgViewer::Keystone* keystone = dynamic_cast<osgViewer::Keystone*>(itr->get());
if (keystone)
{
std::string filename;
keystone->getUserValue("filename",filename);
OSG_NOTICE<<"Loaded keystone "<<filename<<", "<<keystone<<std::endl;
}
}
}
osgViewer::Keystone::loadKeystoneFiles(ds);
if (ds->getStereo())
{
viewer.setUpViewForStereo(ds);
}
else
{
viewer.setUpViewForKeystone(new osgViewer::Keystone);
{
osg::ref_ptr<osgViewer::Keystone> keystone = 0;
if (!(ds->getKeystones().empty())) keystone = dynamic_cast<osgViewer::Keystone*>(ds->getKeystones().front().get());
if (!keystone) keystone = new osgViewer::Keystone;
viewer.setUpViewForKeystone(keystone.get());
}
viewer.realize();

View File

@@ -68,6 +68,12 @@ public:
osg::Geode* createKeystoneDistortionMesh();
osg::Node* createGrid();
/** Write the file specified by the "filename" user value field. Return true if file successfully written. */
bool writeToFile();
/** convinience function that loads and assigns any keystone files specified in the DisplaySettings::KeystoneFileNames list, return true if Keystone's assigned to DisplaySettings.*/
static bool loadKeystoneFiles(osg::DisplaySettings* ds);
protected:

View File

@@ -22,6 +22,7 @@
#include <osg/ValueObject>
#include <osgDB/WriteFile>
#include <osgDB/ReadFile>
#include <osgViewer/Keystone>
@@ -484,23 +485,15 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
}
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='r')
if (ea.getUnmodifiedKey()=='r' && (ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL || ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
{
_selectedRegion = NONE_SELECTED;
_startControlPoints->reset();
_currentControlPoints->reset();
}
else if (ea.getKey()=='s')
else if (ea.getUnmodifiedKey()=='s' && (ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL || ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
{
std::string filename("keystone_new.osgt");
if (_keystone->getUserValue("filename",filename))
{
OSG_NOTICE<<"Got filename field "<<filename<<std::endl;
// remove the user data container so that it isn't written out to file.
_keystone->setUserDataContainer(0);
}
OSG_NOTICE<<"Writing file "<<filename<<std::endl;
osgDB::writeObjectFile(*_keystone, filename);
_keystone->writeToFile();
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up)
{
@@ -541,6 +534,62 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
}
}
bool Keystone::writeToFile()
{
std::string filename;
if (getUserDataContainer()!=0 && getUserValue("filename", filename))
{
// we don't want to write the UDC to the keystone file so take a reference to it, and set the pointer to NULL.
osg::ref_ptr<osg::UserDataContainer> temp_udc = getUserDataContainer();
setUserDataContainer(0);
OSG_NOTICE<<"Writing keystone to: "<<filename<<std::endl;
// write the keystone out to disk
osgDB::writeObjectFile(*this, filename);
// reassign the UDC
setUserDataContainer(temp_udc);
return true;
}
else
{
return false;
}
}
bool Keystone::loadKeystoneFiles(osg::DisplaySettings* ds)
{
bool keystonesLoaded = false;
if (!ds->getKeystoneFileNames().empty())
{
for(osg::DisplaySettings::FileNames::iterator itr = ds->getKeystoneFileNames().begin();
itr != ds->getKeystoneFileNames().end();
++itr)
{
const std::string& filename = *itr;
osg::ref_ptr<osgViewer::Keystone> keystone = osgDB::readFile<osgViewer::Keystone>(filename);
if (keystone.valid())
{
keystone->setUserValue("filename",filename);
ds->getKeystones().push_back(keystone.get());
keystonesLoaded = true;
}
else
{
OSG_NOTICE<<"Creating Keystone for filename entry: "<<filename<<std::endl;
keystone = new Keystone;
keystone->setUserValue("filename",filename);
ds->getKeystones().push_back(keystone.get());
keystonesLoaded = true;
}
}
}
return keystonesLoaded;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////