#include #include #include #include #include #include #include #include #include #include #include #include // Customize the CopyOp so that we add our own verbose // output of what's being copied. class MyCopyOp : public osg::CopyOp { public: inline MyCopyOp(CopyFlags flags=SHALLOW_COPY): osg::CopyOp(flags), _indent(0), _step(4) {} inline void moveIn() const { _indent += _step; } inline void moveOut() const { _indent -= _step; } inline void writeIndent() const { for(int i=0;i<_indent;++i) std::cout << " "; } virtual osg::Referenced* operator() (const osg::Referenced* ref) const { writeIndent(); std::cout << "copying Referenced "<className(); std::cout<className()<<" '"<getName()<<"'"; std::cout<className(); std::cout<className(); std::cout<className(); std::cout<className(); std::cout<className(); std::cout<getNumParents() > 1 ) { if ( _nodeCopyMap.find(node) != _nodeCopyMap.end() ) { std::cout<<"Copy of node "<getName()<<", " << _nodeCopyMap[node]<<", will be reused"<( node->clone(*this) ); _nodeCopyMap[node] = newNode; return newNode; } } else return dynamic_cast( node->clone(*this) ); } else return const_cast(node); } protected: // must be mutable since CopyOp is passed around as const to // the various clone/copy constructors. mutable std::map _nodeCopyMap; }; int main( int argc, char **argv ) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // initialize the viewer. osgViewer::Viewer viewer; // load the nodes from the commandline arguments. osg::Node* rootnode = osgDB::readNodeFiles(arguments); if (!rootnode) { osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."< mycopy = dynamic_cast(rootnode->clone(osg::CopyOp::DEEP_COPY_ALL)); std::cout << "Doing a deep copy of scene graph"< deep_copy = dynamic_cast(rootnode->clone(MyCopyOp(osg::CopyOp::DEEP_COPY_ALL))); std::cout << "----------------------------------------------------------------"< graph_copy = dynamic_cast(rootnode->clone(GraphCopyOp(osg::CopyOp::DEEP_COPY_NODES))); // do a shallow copy. std::cout << "Doing a shallow copy of scene graph"< shallow_copy = dynamic_cast(rootnode->clone(MyCopyOp(osg::CopyOp::SHALLOW_COPY))); // write out the various scene graphs so that they can be browsed, either // in an editor or using a graphics diff tool gdiff/xdiff/xxdiff. std::cout << std::endl << "Writing out the original scene graph as 'original.osg'"<(rootnode->clone(osg::CopyOp::DEEP_COPY_NODES | DEEP_COPY_DRAWABLES)); // Which shares state but creates copies of all nodes and drawables (which contain the geometry). // // You may also want to subclass from CopyOp to provide finer grained control of what gets shared (shallow copy) vs // cloned (deep copy). // ------------- End of copy specific code ------------------------------------------------------- // set the scene to render viewer.setSceneData(rootnode); // viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); return viewer.run(); }