From 667ecd91f1200d3a83525ab8e7177fdfee1f2aba Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 18 Mar 2005 10:00:16 +0000 Subject: [PATCH] From Alberto Farre, fixes to ProxyNode. --- src/osg/ProxyNode.cpp | 2 +- src/osgPlugins/flt/flt2osg.cpp | 7 +- src/osgPlugins/ive/ProxyNode.cpp | 165 +++++++++++++++++-------------- src/osgPlugins/osg/ProxyNode.cpp | 98 +++++++++++++----- 4 files changed, 170 insertions(+), 102 deletions(-) diff --git a/src/osg/ProxyNode.cpp b/src/osg/ProxyNode.cpp index 4a352e74f..8979031d0 100644 --- a/src/osg/ProxyNode.cpp +++ b/src/osg/ProxyNode.cpp @@ -76,7 +76,7 @@ bool ProxyNode::addChild( Node *child ) { if (Group::addChild(child)) { - expandFileNameListTo(_children.size()); + expandFileNameListTo(_children.size()-1); return true; } return false; diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index 1004a36c7..15a922e71 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -2437,7 +2437,7 @@ osg::Group* ConvertFromFLT::visitExternal(osg::Group& osgParent, ExternalRecord* { osg::Group *tempParent = visitAncillary(osgParent, *external, rec); tempParent->addChild(external); - return 0; + return external; } #endif pFile->setDesiredUnits( rec->getFltFile()->getDesiredUnits() ); @@ -2467,7 +2467,7 @@ osg::Group* ConvertFromFLT::visitExternal(osg::Group& osgParent, ExternalRecord* proxynode->setCenterMode(osg::ProxyNode::USE_BOUNDING_SPHERE_CENTER); proxynode->addChild(external, rec->getFilename()); tempParent->addChild(proxynode); - osgDB::Registry::instance()->addEntryToObjectCache(rec->getFilename(), external); + osgDB::Registry::instance()->addEntryToObjectCache(rec->getFilename(), proxynode); #else tempParent->addChild(external); #endif @@ -2486,7 +2486,7 @@ osg::Group* ConvertFromFLT::visitExternal(osg::Group& osgParent, ExternalRecord* proxynode->setCenterMode(osg::ProxyNode::USE_BOUNDING_SPHERE_CENTER); proxynode->addChild(model, rec->getFilename()); tempParent->addChild(proxynode); - osgDB::Registry::instance()->addEntryToObjectCache(rec->getFilename(), model); + osgDB::Registry::instance()->addEntryToObjectCache(rec->getFilename(), proxynode); #else //tempParent->addChild(model); #endif @@ -2504,7 +2504,6 @@ osg::Group* ConvertFromFLT::visitExternal(osg::Group& osgParent, ExternalRecord* return external; } - void ConvertFromFLT::visitLightPoint(GeoSetBuilder* pBuilder,osg::Group& osgParent, LightPointRecord* rec) { DynGeoSet* dgset = pBuilder->getDynGeoSet(); diff --git a/src/osgPlugins/ive/ProxyNode.cpp b/src/osgPlugins/ive/ProxyNode.cpp index d9e6bd1d6..d4e08d8dd 100644 --- a/src/osgPlugins/ive/ProxyNode.cpp +++ b/src/osgPlugins/ive/ProxyNode.cpp @@ -14,14 +14,35 @@ #include #include +#include #include #include "Exception.h" #include "ProxyNode.h" -#include "Group.h" +#include "Node.h" using namespace ive; +/* +for(osgDB::FilePathList::iterator itr=osgDB::getDataFilePathList().begin(); itr!=osgDB::getDataFilePathList().end(); ++itr) +printf("#1######%s\n", itr->c_str()); +for(osgDB::FilePathList::const_iterator itrO=in->getOptions()->getDatabasePathList().begin(); itrO!=in->getOptions()->getDatabasePathList().end(); ++itrO) +printf("#2######%s\n", itrO->c_str()); +namespace osgDB { +class PushAndPopDataPath +{ + public: + PushAndPopDataPath(const std::string& path) + { + getDataFilePathList().push_front(path); + } + ~PushAndPopDataPath() + { + getDataFilePathList().pop_front(); + } +}; +} +*/ void ProxyNode::write(DataOutputStream* out) { @@ -29,59 +50,29 @@ void ProxyNode::write(DataOutputStream* out) out->writeInt(IVEPROXYNODE); // If the osg class is inherited by any other class we should also write this to file. - osg::Group* node = dynamic_cast(this); + osg::Node* node = dynamic_cast(this); if(node) { - static_cast(node)->write(out); - } - else throw Exception("PagedLOD::write(): Could not cast this osg::PagedLOD to an osg::LOD."); - - if ( out->getVersion() > VERSION_0006 ) - { - out->writeString(getDatabasePath()); + static_cast(node)->write(out); } + else + throw Exception("ProxyNode::write(): Could not cast this osg::ProxyNode to an osg::Node."); out->writeFloat(getRadius()); out->writeInt(getCenterMode()); out->writeVec3(getCenter()); - if(out->getIncludeExternalReferences()) // inlined mode + out->writeUInt(getNumFileNames()); + unsigned int numChildrenToWriteOut = 0; + unsigned int i; + for(i=0; iwriteUInt(getNumChildren()); - unsigned int i; - for(i=0; iwriteNode(getChild(i)); + out->writeString(""); + ++numChildrenToWriteOut; } - out->writeUInt(getNumFileNames()); - for(i=0; iwriteString(getFileName(i)); - } - } - else //no inlined mode - { - unsigned int numChildrenToWriteOut = 0; - unsigned int i; - for(i=0; iwriteUInt(numChildrenToWriteOut); - for(i=0; iwriteNode(getChild(i)); - } - } - - out->writeUInt(getNumFileNames()); - for(i=0; igetUseOriginalExternalReferences()) { @@ -91,14 +82,37 @@ void ProxyNode::write(DataOutputStream* out) { std::string ivename = osgDB::getFilePath(getFileName(i)) +"/"+ osgDB::getStrippedName(getFileName(i)) +".ive"; out->writeString(ivename); + } + } + } - if(out->getWriteExternalReferenceFiles()) + if(out->getIncludeExternalReferences()) //--------- inlined mode + { + out->writeUInt(getNumChildren()); + for(i=0; iwriteNode(getChild(i)); + } + } + else //----------------------------------------- no inlined mode + { + out->writeUInt(numChildrenToWriteOut); + + for(i=0; iwriteNode(getChild(i)); + } + else if(out->getWriteExternalReferenceFiles()) + { + if(out->getUseOriginalExternalReferences()) { - std::string path = getDatabasePath(); - if (path.empty() && out->getOptions() && !out->getOptions()->getDatabasePathList().empty()) - path = out->getOptions()->getDatabasePathList().front(); - if(!path.empty()) path += "/"; - ivename = path + ivename; + osgDB::writeNodeFile(*getChild(i), getFileName(i)); + } + else + { + std::string ivename = osgDB::getFilePath(getFileName(i)) +"/"+ osgDB::getStrippedName(getFileName(i)) +".ive"; osgDB::writeNodeFile(*getChild(i), ivename); } } @@ -115,21 +129,15 @@ void ProxyNode::read(DataInputStream* in) // Read ProxyNode's identification. id = in->readInt(); // If the osg class is inherited by any other class we should also read this from file. - osg::Group* node = dynamic_cast(this); + osg::Node* node = dynamic_cast(this); if(node) { - ((ive::Group*)(node))->read(in); + ((ive::Node*)(node))->read(in); } else - throw Exception("Group::read(): Could not cast this osg::Group to an osg::Node."); + throw Exception("ProxyNode::read(): Could not cast this osg::ProxyNode to an osg::Node."); - - if ( in->getVersion() > VERSION_0006 ) - { - setDatabasePath(in->readString()); - } - - if (getDatabasePath().empty() && in->getOptions() && !in->getOptions()->getDatabasePathList().empty()) + if (in->getOptions() && !in->getOptions()->getDatabasePathList().empty()) { const std::string& path = in->getOptions()->getDatabasePathList().front(); if (!path.empty()) @@ -142,28 +150,39 @@ void ProxyNode::read(DataInputStream* in) setCenterMode((osg::ProxyNode::CenterMode)in->readInt()); setCenter(in->readVec3()); - // Read groups properties. - // Read number of children. - unsigned int size = in->readUInt(); - // Read children. + unsigned int numFileNames = in->readUInt(); unsigned int i; - for(i=0; ireadNode()); + setFileName(i, in->readString()); } - size = in->readUInt(); - for(i=0;ireadUInt(); + for(i=0; ireadString()); - std::string filename = in->readString(); - osg::Node *node = osgDB::readNodeFile(getDatabasePath() + filename); // If filename is flt, will need getDatabasePath() - if(node) - addChild(node, filename); + osgDB::FilePathList& fpl = ((osgDB::ReaderWriter::Options*)in->getOptions())->getDatabasePathList(); + fpl.push_front( fpl.empty() ? osgDB::getFilePath(getFileName(i)) : fpl.front()+'/'+ osgDB::getFilePath(getFileName(i))); + addChild(in->readNode()); + fpl.pop_front(); + } + + for(i=0; i=numChildren && !getFileName(i).empty()) + { + osgDB::FilePathList& fpl = ((osgDB::ReaderWriter::Options*)in->getOptions())->getDatabasePathList(); + fpl.push_front( fpl.empty() ? osgDB::getFilePath(getFileName(i)) : fpl.front()+'/'+ osgDB::getFilePath(getFileName(i))); + osg::Node *node = osgDB::readNodeFile(getFileName(i), in->getOptions()); + fpl.pop_front(); + if(node) + { + insertChild(i, node); + } + } } } else { throw Exception("ProxyNode::read(): Expected ProxyNode identification."); - } + } } diff --git a/src/osgPlugins/osg/ProxyNode.cpp b/src/osgPlugins/osg/ProxyNode.cpp index 9b6397aa0..a4af311cb 100644 --- a/src/osgPlugins/osg/ProxyNode.cpp +++ b/src/osgPlugins/osg/ProxyNode.cpp @@ -5,6 +5,9 @@ #include "osgDB/Input" #include "osgDB/Output" #include +#include +#include +#include using namespace osg; using namespace osgDB; @@ -40,8 +43,8 @@ bool ProxyNode_readLocalData(Object& obj, Input& fr) iteratorAdvanced = true; fr+=4; } - else - proxyNode.setCenterMode(osg::ProxyNode::USE_BOUNDING_SPHERE_CENTER); + else + proxyNode.setCenterMode(osg::ProxyNode::USE_BOUNDING_SPHERE_CENTER); float radius; if (fr[0].matchWord("Radius") && fr[1].getFloat(radius)) @@ -51,7 +54,7 @@ bool ProxyNode_readLocalData(Object& obj, Input& fr) iteratorAdvanced = true; } - if (proxyNode.getDatabasePath().empty() && fr.getOptions() && !fr.getOptions()->getDatabasePathList().empty()) + if (fr.getOptions() && !fr.getOptions()->getDatabasePathList().empty()) { const std::string& path = fr.getOptions()->getDatabasePathList().front(); if (!path.empty()) @@ -80,16 +83,7 @@ bool ProxyNode_readLocalData(Object& obj, Input& fr) { if (fr[0].isString() || fr[0].isQuotedString()) { - if (fr[0].getStr()) - { - osg::Node *node = osgDB::readNodeFile(proxyNode.getDatabasePath() + fr[0].getStr()); // If filename is flt, will need getDatabasePath() - if(node) - { - printf("cargando: %s\n", fr[0].getStr()); - proxyNode.addChild(node, fr[0].getStr()); - } - } - //if (fr[0].getStr()) proxyNode.setFileName(i,fr[0].getStr()); + if (fr[0].getStr()) proxyNode.setFileName(i,fr[0].getStr()); else proxyNode.setFileName(i,""); ++fr; @@ -115,11 +109,32 @@ bool ProxyNode_readLocalData(Object& obj, Input& fr) iteratorAdvanced = true; } - Node* node = NULL; - while((node=fr.readNode())!=NULL) + for(int i=0; igetDatabasePathList(); + fpl.push_front( fpl.empty() ? osgDB::getFilePath(proxyNode.getFileName(i)) : fpl.front()+'/'+ osgDB::getFilePath(proxyNode.getFileName(i))); + Node* node = NULL; + if((node=fr.readNode())!=NULL) + { + proxyNode.addChild(node); + iteratorAdvanced = true; + } + fpl.pop_front(); + } + + for(unsigned int i=0; i=proxyNode.getNumChildren() && !proxyNode.getFileName(i).empty()) + { + osgDB::FilePathList& fpl = ((osgDB::ReaderWriter::Options*)fr.getOptions())->getDatabasePathList(); + fpl.push_front( fpl.empty() ? osgDB::getFilePath(proxyNode.getFileName(i)) : fpl.front()+'/'+ osgDB::getFilePath(proxyNode.getFileName(i))); + osg::Node *node = osgDB::readNodeFile(proxyNode.getFileName(i), fr.getOptions()); + fpl.pop_front(); + if(node) + { + proxyNode.insertChild(i, node); + } + } } return iteratorAdvanced; @@ -128,13 +143,17 @@ bool ProxyNode_readLocalData(Object& obj, Input& fr) bool ProxyNode_writeLocalData(const Object& obj, Output& fw) { + bool includeExternalReferences = false; + bool useOriginalExternalReferences = false; + bool writeExternalReferenceFiles = true; + const ProxyNode& proxyNode = static_cast(obj); if (proxyNode.getCenterMode()==osg::ProxyNode::USER_DEFINED_CENTER) fw.indent() << "Center "<< proxyNode.getCenter() << std::endl; fw.indent() << "Radius "<getIncludeExternalReferences()) // inlined mode { - if (proxyNode.getFileName(j).empty()) + fw.indent() << "num_children " << proxyNode.getNumChildren() << std::endl; + for(unsigned int i=0; igetWriteExternalReferenceFiles()) + { + if(useOriginalExternalReferences) //out->getUseOriginalExternalReferences()) + { + osgDB::writeNodeFile(*proxyNode.getChild(i), proxyNode.getFileName(i)); + } + else + { + std::string osgname = osgDB::getFilePath(proxyNode.getFileName(i)) +"/"+ osgDB::getStrippedName(proxyNode.getFileName(i)) +".osg"; + osgDB::writeNodeFile(*proxyNode.getChild(i), osgname); + } + } } } - return true; }