From c72124e44976fea6ca02f0ddf94404539db8cb5d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 14 May 2009 13:05:32 +0000 Subject: [PATCH] Added support for read the various animaton materials/paths from http --- src/osgPlugins/p3d/CMakeLists.txt | 1 + src/osgPlugins/p3d/ReaderWriterP3D.cpp | 16 +- src/osgPlugins/p3d/ReaderWriterPaths.cpp | 254 ++++++++++++++++++++ src/osgPlugins/p3d/SlideShowConstructor.cpp | 217 +++-------------- src/osgPlugins/p3d/SlideShowConstructor.h | 3 - 5 files changed, 298 insertions(+), 193 deletions(-) create mode 100644 src/osgPlugins/p3d/ReaderWriterPaths.cpp diff --git a/src/osgPlugins/p3d/CMakeLists.txt b/src/osgPlugins/p3d/CMakeLists.txt index 1e4005aff..ccaee008f 100644 --- a/src/osgPlugins/p3d/CMakeLists.txt +++ b/src/osgPlugins/p3d/CMakeLists.txt @@ -1,6 +1,7 @@ SET(TARGET_SRC SlideShowConstructor.cpp ReaderWriterP3D.cpp + ReaderWriterPaths.cpp PickEventHandler.cpp AnimationMaterial.cpp SlideEventHandler.cpp diff --git a/src/osgPlugins/p3d/ReaderWriterP3D.cpp b/src/osgPlugins/p3d/ReaderWriterP3D.cpp index e6ca23782..3fb1529ec 100644 --- a/src/osgPlugins/p3d/ReaderWriterP3D.cpp +++ b/src/osgPlugins/p3d/ReaderWriterP3D.cpp @@ -94,7 +94,7 @@ public: _stringKeyMap["F12"]=osgGA::GUIEventAdapter::KEY_F12; - _notifyLevel = osg::INFO; + _notifyLevel = osg::NOTICE; } virtual const char* className() const @@ -1444,7 +1444,7 @@ class OSGDB_EXPORT MyReadFileCallback : public virtual osgDB::ReadFileCallback osgDB::ReaderWriter::ReadResult readLocal(ObjectType type, const std::string& filename, const osgDB::Options* options) { - osg::notify(osg::INFO)<<"Trying local file "<getFileCache(); if (!fileCache) return osgDB::ReaderWriter::ReadResult::FILE_NOT_FOUND; - osg::notify(osg::INFO)<<"Trying fileCache "<isFileAppropriateForFileCache(filename)) @@ -1511,7 +1511,7 @@ class OSGDB_EXPORT MyReadFileCallback : public virtual osgDB::ReadFileCallback osgDB::ReaderWriter::ReadResult readServer(ObjectType type, const std::string& filename, const osgDB::Options* options) { - osg::notify(osg::INFO)<<"Trying server file "<getReaderWriterForExtension("curl"); @@ -1549,6 +1549,7 @@ class OSGDB_EXPORT MyReadFileCallback : public virtual osgDB::ReadFileCallback fileCache->writeObject(*result.getObject(),filename,options); break; case(IMAGE): + result.getImage()->setFileName(filename); fileCache->writeImage(*result.getImage(),filename,options); break; case(HEIGHT_FIELD): @@ -1607,7 +1608,7 @@ class OSGDB_EXPORT MyReadFileCallback : public virtual osgDB::ReadFileCallback if (!fileCache) fileCache = osgDB::Registry::instance()->getFileCache(); if (fileCache && !fileCache->isFileAppropriateForFileCache(filename)) fileCache = 0; - osg::notify(osg::INFO)<<"reading file "< +#include + +#include +#include +#include +#include + +#include + +#include "SlideShowConstructor.h" +#include "AnimationMaterial.h" + +#include +#include +#include + +#include + +#include +#include + + +/** + * OpenSceneGraph plugin wrapper/converter. + */ +class ReaderWriterPaths : public osgDB::ReaderWriter +{ +public: + ReaderWriterPaths() + { + supportsExtension("material","Material animation Ascii file format"); + supportsExtension("path","Animation path Ascii file format"); + supportsExtension("pivot_path","Animation pivot path Ascii file format"); + supportsExtension("rotation_path","Animation rotation path Ascii file format"); + } + + virtual const char* className() const + { + return "Path Reader/Writer"; + } + + virtual bool acceptsExtension(const std::string& extension) const + { + return osgDB::equalCaseInsensitive(extension,"material") || + osgDB::equalCaseInsensitive(extension,"path") || + osgDB::equalCaseInsensitive(extension,"pivot_path") || + osgDB::equalCaseInsensitive(extension,"rotation_path"); + } + + virtual osgDB::ReaderWriter::ReadResult readObject(const std::string& fileName, const osgDB::Options* options) const; + + virtual osgDB::ReaderWriter::ReadResult readObject(std::istream& fin, const osgDB::Options* options) const; + + virtual osgDB::ReaderWriter::ReadResult read_material(std::istream& fin, const osgDB::Options* options) const; + virtual osgDB::ReaderWriter::ReadResult read_path(std::istream& fin, const osgDB::Options* options) const; + virtual osgDB::ReaderWriter::ReadResult read_pivot_path(std::istream& fin, const osgDB::Options* options) const; + virtual osgDB::ReaderWriter::ReadResult read_rotation_path(std::istream& fin, const osgDB::Options* options) const; +}; + +// Register with Registry to instantiate the above reader/writer. +osgDB::RegisterReaderWriterProxy g_readerWriter_PathsL_Proxy; + +osgDB::ReaderWriter::ReadResult ReaderWriterPaths::readObject(const std::string& file, const osgDB::Options* options) const +{ + + + std::string ext = osgDB::getLowerCaseFileExtension(file); + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + + osg::notify(osg::NOTICE)<<"ReaderWriterPaths::readObject("< local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + local_opt->setPluginStringData("filename",fileName); + + std::ifstream input(fileName.c_str()); + + return readObject(input, local_opt); +} + +osgDB::ReaderWriter::ReadResult ReaderWriterPaths::readObject(std::istream& fin, const osgDB::Options* options) const +{ + osg::notify(osg::NOTICE)<<"ReaderWriterPaths::readObject(std::istream& fin"<getPluginStringData("filename"); + + std::string ext = osgDB::getLowerCaseFileExtension(filename); + + osg::notify(osg::NOTICE)<<" filename found in options: "< animationMaterial = new ss3d::AnimationMaterial; + animationMaterial->read(fin); + + return animationMaterial.get(); +} + +osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_path(std::istream& fin, const osgDB::Options* options) const +{ + osg::ref_ptr animation = new osg::AnimationPath; + animation->read(fin); + return animation.get(); +} + +osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_pivot_path(std::istream& fin, const osgDB::Options* options) const +{ + osg::ref_ptr animation = new osg::AnimationPath; + + while (!fin.eof()) + { + double time; + osg::Vec3 pivot; + osg::Vec3 position; + float scale; + osg::Quat rotation; + fin >> time >> pivot.x() >> pivot.y() >> pivot.z() >> position.x() >> position.y() >> position.z() >> rotation.x() >> rotation.y() >> rotation.z() >> rotation.w() >> scale; + if(!fin.eof()) + { + osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)* + osg::Matrixf::rotate(rotation); + + osg::Matrix invSR; + invSR.invert(SR); + + position += (invSR*pivot)*SR; + + animation->insert(time,osg::AnimationPath::ControlPoint(position,rotation,osg::Vec3(scale,scale,scale))); + } + } + + return animation.get(); +} + +struct RotationPathData +{ + RotationPathData(): + time(0.0), + scale(1.0f), + azim(0.0f), + elevation(0.0f) {} + + double time; + osg::Vec3 pivot; + osg::Vec3 position; + float scale; + float azim; + float elevation; + + void addToPath(osg::AnimationPath* animation) const + { + osg::Quat Rx, Rz, rotation; + + Rx.makeRotate(osg::DegreesToRadians(elevation),1.0f,0.0f,0.0f); + Rz.makeRotate(osg::DegreesToRadians(azim),0.0f,0.0f,1.0f); + rotation = Rz * Rx; // note, I believe this is the wrong way round, but I had to put it in this order to fix the Quat properly. + + osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)* + osg::Matrixf::rotate(rotation); + + osg::Matrix invSR; + invSR.invert(SR); + + osg::Vec3 local_position = position + (invSR*pivot)*SR; + + animation->insert(time,osg::AnimationPath::ControlPoint(local_position,rotation,osg::Vec3(scale,scale,scale))); + } + +}; + +osgDB::ReaderWriter::ReadResult ReaderWriterPaths::read_rotation_path(std::istream& fin, const osgDB::Options* options) const +{ + osg::ref_ptr animation = new osg::AnimationPath; + + RotationPathData prevValue; + bool first = true; + while (!fin.eof()) + { + RotationPathData currValue; + fin >> currValue.time >> currValue.pivot.x() >> currValue.pivot.y() >> currValue.pivot.z() >> currValue.position.x() >> currValue.position.y() >> currValue.position.z() >> currValue.azim >> currValue.elevation >> currValue.scale; + + osg::notify(osg::NOTICE)<<"rotation_path "<addChild(subgraph); - osg::notify(osg::INFO)<<"Rotation Matrix "<getMatrix()<getMatrix()<getBound().center(); @@ -1575,6 +1577,7 @@ osg::Node* SlideShowConstructor::attachMaterialAnimation(osg::Node* model, const if (!positionData.animation_material_filename.empty()) { +#if 0 std::string absolute_animation_file_path = osgDB::findDataFile(positionData.animation_material_filename, _options.get()); if (!absolute_animation_file_path.empty()) { @@ -1585,6 +1588,11 @@ osg::Node* SlideShowConstructor::attachMaterialAnimation(osg::Node* model, const animationMaterial->read(animation_filestream); } } +#else + osg::ref_ptr object = osgDB::readObjectFile(positionData.animation_material_filename, _options.get()); + animationMaterial = dynamic_cast(object.get()); +#endif + } else if (!positionData.fade.empty()) { @@ -1630,204 +1638,43 @@ osg::Node* SlideShowConstructor::attachMaterialAnimation(osg::Node* model, const return model; } -osg::AnimationPath* SlideShowConstructor::readPivotPath(const std::string& filename) const -{ - std::ifstream in(filename.c_str()); - if (!in.eof()) - { - osg::AnimationPath* animation = new osg::AnimationPath; - - while (!in.eof()) - { - double time; - osg::Vec3 pivot; - osg::Vec3 position; - float scale; - osg::Quat rotation; - in >> time >> pivot.x() >> pivot.y() >> pivot.z() >> position.x() >> position.y() >> position.z() >> rotation.x() >> rotation.y() >> rotation.z() >> rotation.w() >> scale; - if(!in.eof()) - { - osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)* - osg::Matrixf::rotate(rotation); - - osg::Matrix invSR; - invSR.invert(SR); - - position += (invSR*pivot)*SR; - - animation->insert(time,osg::AnimationPath::ControlPoint(position,rotation,osg::Vec3(scale,scale,scale))); - } - } - - return animation; - } - return 0; -} - -struct RotationPathData -{ - RotationPathData(): - time(0.0), - scale(1.0f), - azim(0.0f), - elevation(0.0f) {} - - double time; - osg::Vec3 pivot; - osg::Vec3 position; - float scale; - float azim; - float elevation; - - void addToPath(osg::AnimationPath* animation) const - { - osg::Quat Rx, Rz, rotation; - - Rx.makeRotate(osg::DegreesToRadians(elevation),1.0f,0.0f,0.0f); - Rz.makeRotate(osg::DegreesToRadians(azim),0.0f,0.0f,1.0f); - rotation = Rz * Rx; // note, I believe this is the wrong way round, but I had to put it in this order to fix the Quat properly. - - osg::Matrix SR = osg::Matrix::scale(scale,scale,scale)* - osg::Matrixf::rotate(rotation); - - osg::Matrix invSR; - invSR.invert(SR); - - osg::Vec3 local_position = position + (invSR*pivot)*SR; - - animation->insert(time,osg::AnimationPath::ControlPoint(local_position,rotation,osg::Vec3(scale,scale,scale))); - } - -}; -osg::AnimationPath* SlideShowConstructor::readRotationPath(const std::string& filename) const -{ - std::ifstream in(filename.c_str()); - if (!in.eof()) - { - osg::AnimationPath* animation = new osg::AnimationPath; - - RotationPathData prevValue; - bool first = true; - while (!in.eof()) - { - RotationPathData currValue; - in >> currValue.time >> currValue.pivot.x() >> currValue.pivot.y() >> currValue.pivot.z() >> currValue.position.x() >> currValue.position.y() >> currValue.position.z() >> currValue.azim >> currValue.elevation >> currValue.scale; - if(!in.eof()) - { - - if (!first) - { - - unsigned int num = 20; - float dr = 1.0f/(float)num; - float r=dr; - for(unsigned int i=0; - i object = osgDB::readObjectFile(positionData.path, _options.get()); + osg::AnimationPath* animation = dynamic_cast(object.get()); + if (animation) + { + if (positionData.frame==SlideShowConstructor::SLIDE) { - animation = readPivotPath(absolute_animation_file_path); - } - else if (osgDB::equalCaseInsensitive(extension,"rotation_path")) - { - animation = readRotationPath(absolute_animation_file_path); - } - else if (osgDB::equalCaseInsensitive(extension,"path")) - { - std::ifstream animation_filestream(absolute_animation_file_path.c_str()); - if (!animation_filestream.eof()) + osg::AnimationPath::TimeControlPointMap& controlPoints = animation->getTimeControlPointMap(); + for(osg::AnimationPath::TimeControlPointMap::iterator itr=controlPoints.begin(); + itr!=controlPoints.end(); + ++itr) { - animation = new osg::AnimationPath; - animation->read(animation_filestream); + osg::AnimationPath::ControlPoint& cp = itr->second; + cp.setPosition(convertSlideToModel(cp.getPosition()+positionData.position)); } } - else - { - std::ifstream animation_filestream(absolute_animation_file_path.c_str()); - osgDB::Input fr; - fr.attach(&animation_filestream); + animation->setLoopMode(positionData.path_loop_mode); - static osg::ref_ptr s_path = new osg::AnimationPath; - osg::ref_ptr object = osgDB::readObjectFile(absolute_animation_file_path, _options.get()); // fr.readObjectOfType(*s_path); - object = fr.readObject(); // fr.readObjectOfType(*s_path); - if (object.valid()) - { - animation = dynamic_cast(object.get()); - } - } - - if (animation) - { - if (positionData.frame==SlideShowConstructor::SLIDE) - { - osg::AnimationPath::TimeControlPointMap& controlPoints = animation->getTimeControlPointMap(); - for(osg::AnimationPath::TimeControlPointMap::iterator itr=controlPoints.begin(); - itr!=controlPoints.end(); - ++itr) - { - osg::AnimationPath::ControlPoint& cp = itr->second; - cp.setPosition(convertSlideToModel(cp.getPosition()+positionData.position)); - } - } + osg::AnimationPathCallback* apc = new osg::AnimationPathCallback(animation); + apc->setTimeOffset(positionData.path_time_offset); + apc->setTimeMultiplier(positionData.path_time_multiplier); + apc->setUseInverseMatrix(positionData.inverse_path); - animation->setLoopMode(positionData.path_loop_mode); + osg::notify(osg::INFO)<<"UseInverseMatrix "<setTimeOffset(positionData.path_time_offset); - apc->setTimeMultiplier(positionData.path_time_multiplier); - apc->setUseInverseMatrix(positionData.inverse_path); - - osg::notify(osg::INFO)<<"UseInverseMatrix "<