From 5dd07e4d1c8900b0416a1ed650a41db096dc32db Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 10 May 2013 16:06:10 +0000 Subject: [PATCH] Added keystone file handling --- examples/osgkeystone/osgkeystone.cpp | 33 ++++++++++--- include/osgViewer/Keystone | 6 +++ src/osgViewer/Keystone.cpp | 71 +++++++++++++++++++++++----- 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/examples/osgkeystone/osgkeystone.cpp b/examples/osgkeystone/osgkeystone.cpp index fa0f16331..0be915b90 100644 --- a/examples/osgkeystone/osgkeystone.cpp +++ b/examples/osgkeystone/osgkeystone.cpp @@ -66,25 +66,44 @@ int main( int argc, char **argv ) viewer.setCameraManipulator(new osgGA::TrackballManipulator()); - OSG_NOTICE<<"KeystoneFileNames.size()="<getKeystoneFileNames().size()<getKeystoneFileNames().begin(); - itr != osg::DisplaySettings::instance()->getKeystoneFileNames().end(); + OSG_NOTICE<<"KeystoneFileNames.size()="<getKeystoneFileNames().size()<getKeystoneFileNames().begin(); + itr != ds->getKeystoneFileNames().end(); ++itr) { - OSG_NOTICE<<" keystone ="<<*itr<getStereo()) { viewer.setUpViewForStereo(ds); } else - { - viewer.setUpViewForKeystone(new osgViewer::Keystone); + { + osg::ref_ptr keystone = 0; + if (!(ds->getKeystones().empty())) keystone = dynamic_cast(ds->getKeystones().front().get()); + if (!keystone) keystone = new osgViewer::Keystone; + + viewer.setUpViewForKeystone(keystone.get()); } viewer.realize(); diff --git a/include/osgViewer/Keystone b/include/osgViewer/Keystone index bd754e263..d13a64b3b 100644 --- a/include/osgViewer/Keystone +++ b/include/osgViewer/Keystone @@ -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: diff --git a/src/osgViewer/Keystone.cpp b/src/osgViewer/Keystone.cpp index 514e57d16..c3237740c 100644 --- a/src/osgViewer/Keystone.cpp +++ b/src/osgViewer/Keystone.cpp @@ -22,6 +22,7 @@ #include #include +#include #include @@ -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 "<setUserDataContainer(0); - } - OSG_NOTICE<<"Writing file "<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 temp_udc = getUserDataContainer(); + setUserDataContainer(0); + + OSG_NOTICE<<"Writing keystone to: "<getKeystoneFileNames().empty()) + { + for(osg::DisplaySettings::FileNames::iterator itr = ds->getKeystoneFileNames().begin(); + itr != ds->getKeystoneFileNames().end(); + ++itr) + { + const std::string& filename = *itr; + osg::ref_ptr keystone = osgDB::readFile(filename); + if (keystone.valid()) + { + keystone->setUserValue("filename",filename); + ds->getKeystones().push_back(keystone.get()); + keystonesLoaded = true; + } + else + { + OSG_NOTICE<<"Creating Keystone for filename entry: "<setUserValue("filename",filename); + ds->getKeystones().push_back(keystone.get()); + keystonesLoaded = true; + } + } + } + return keystonesLoaded; +} + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////