2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgcopy.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/Billboard>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/Texture>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
#include <iostream>
|
2003-03-22 06:32:01 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// 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 "<<ref<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Referenced* ret_ref = CopyOp::operator()(ref);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::Object* operator() (const osg::Object* obj) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying Object "<<obj;
|
|
|
|
if (obj) std::cout<<" "<<obj->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Object* ret_obj = CopyOp::operator()(obj);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::Node* operator() (const osg::Node* node) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying Node "<<node;
|
|
|
|
if (node) std::cout<<" "<<node->className()<<" '"<<node->getName()<<"'";
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Node* ret_node = CopyOp::operator()(node);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::Drawable* operator() (const osg::Drawable* drawable) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying Drawable "<<drawable;
|
|
|
|
if (drawable) std::cout<<" "<<drawable->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Drawable* ret_drawable = CopyOp::operator()(drawable);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_drawable;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::StateSet* operator() (const osg::StateSet* stateset) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying StateSet "<<stateset;
|
|
|
|
if (stateset) std::cout<<" "<<stateset->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::StateSet* ret_stateset = CopyOp::operator()(stateset);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_stateset;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::StateAttribute* operator() (const osg::StateAttribute* attr) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying StateAttribute "<<attr;
|
|
|
|
if (attr) std::cout<<" "<<attr->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::StateAttribute* ret_attr = CopyOp::operator()(attr);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::Texture* operator() (const osg::Texture* text) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying Texture "<<text;
|
|
|
|
if (text) std::cout<<" "<<text->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Texture* ret_text = CopyOp::operator()(text);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual osg::Image* operator() (const osg::Image* image) const
|
|
|
|
{
|
|
|
|
writeIndent(); std::cout << "copying Image "<<image;
|
|
|
|
if (image) std::cout<<" "<<image->className();
|
|
|
|
std::cout<<std::endl;
|
|
|
|
moveIn();
|
2003-03-22 06:32:01 +08:00
|
|
|
osg::Image* ret_image = CopyOp::operator()(image);
|
2003-03-15 04:35:45 +08:00
|
|
|
moveOut();
|
|
|
|
return ret_image;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// must be mutable since CopyOp is passed around as const to
|
|
|
|
// the various clone/copy constructors.
|
|
|
|
mutable int _indent;
|
|
|
|
mutable int _step;
|
|
|
|
};
|
|
|
|
|
2007-02-09 22:54:54 +08:00
|
|
|
// this CopyOp class will preserve the multi-parent structure of the copied
|
|
|
|
// object, instead of expanding it into a tree. Works with the
|
|
|
|
// DEEP_COPY_NODES flag.
|
|
|
|
class GraphCopyOp : public osg::CopyOp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
inline GraphCopyOp(CopyFlags flags=SHALLOW_COPY):
|
|
|
|
osg::CopyOp(flags) { _nodeCopyMap.clear();}
|
|
|
|
|
|
|
|
virtual osg::Node* operator() (const osg::Node* node) const
|
|
|
|
{
|
|
|
|
if (node && _flags&DEEP_COPY_NODES)
|
|
|
|
{
|
|
|
|
if ( node->getNumParents() > 1 )
|
|
|
|
{
|
|
|
|
if ( _nodeCopyMap.find(node) != _nodeCopyMap.end() )
|
|
|
|
{
|
|
|
|
std::cout<<"Copy of node "<<node<<" "<<node->getName()<<", "
|
|
|
|
<< _nodeCopyMap[node]<<", will be reused"<<std::endl;
|
|
|
|
return (osg::Node*)(_nodeCopyMap[node]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::Node* newNode = dynamic_cast<osg::Node*>( node->clone(*this) );
|
|
|
|
_nodeCopyMap[node] = newNode;
|
|
|
|
return newNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return dynamic_cast<osg::Node*>( node->clone(*this) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return const_cast<osg::Node*>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// must be mutable since CopyOp is passed around as const to
|
|
|
|
// the various clone/copy constructors.
|
|
|
|
mutable std::map<const osg::Node*,osg::Node*> _nodeCopyMap;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// initialize the viewer.
|
2007-01-07 05:35:57 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// load the nodes from the commandline arguments.
|
|
|
|
osg::Node* rootnode = osgDB::readNodeFiles(arguments);
|
|
|
|
if (!rootnode)
|
|
|
|
{
|
2007-01-07 05:35:57 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
|
2003-03-15 04:35:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// run optimization over the scene graph
|
|
|
|
osgUtil::Optimizer optimzer;
|
|
|
|
optimzer.optimize(rootnode);
|
|
|
|
|
|
|
|
// ------------- Start of copy specific code -------------------------------------------------------
|
|
|
|
|
|
|
|
// do a deep copy, using MyCopyOp to reveal whats going on under the good,
|
|
|
|
// in your own code you'd typically just use the basic osg::CopyOp something like
|
2007-02-09 22:54:54 +08:00
|
|
|
osg::ref_ptr<osg::Node> mycopy = dynamic_cast<osg::Node*>(rootnode->clone(osg::CopyOp::DEEP_COPY_ALL));
|
2003-03-15 04:35:45 +08:00
|
|
|
std::cout << "Doing a deep copy of scene graph"<<std::endl;
|
2007-02-09 22:54:54 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// note, we need the dyanmic_cast because MS Visual Studio can't handle covarient
|
|
|
|
// return types, so that clone has return just Object*. bahh hum bug
|
2007-02-09 22:54:54 +08:00
|
|
|
osg::ref_ptr<osg::Node> deep_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::DEEP_COPY_ALL)));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
std::cout << "----------------------------------------------------------------"<<std::endl;
|
|
|
|
|
2007-02-09 22:54:54 +08:00
|
|
|
// do a graph preserving deep copy.
|
|
|
|
std::cout << "Doing a graph preserving deep copy of scene graph nodes"<<std::endl;
|
|
|
|
osg::ref_ptr<osg::Node> graph_copy = dynamic_cast<osg::Node*>(rootnode->clone(GraphCopyOp(osg::CopyOp::DEEP_COPY_NODES)));
|
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// do a shallow copy.
|
|
|
|
std::cout << "Doing a shallow copy of scene graph"<<std::endl;
|
2007-02-09 22:54:54 +08:00
|
|
|
osg::ref_ptr<osg::Node> shallow_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::SHALLOW_COPY)));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
2011-06-15 00:54:20 +08:00
|
|
|
std::cout << std::endl << "Writing out the original scene graph as 'original.osgt'"<<std::endl;
|
|
|
|
osgDB::writeNodeFile(*rootnode,"original.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
std::cout << std::endl << "Writing out the graph preserving scene graph as 'graph_copy.osgt'"<<std::endl;
|
|
|
|
osgDB::writeNodeFile(*graph_copy,"graph_copy.osgt");
|
2007-02-09 22:54:54 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
std::cout << "Writing out the deep copied scene graph as 'deep_copy.osgt'"<<std::endl;
|
|
|
|
osgDB::writeNodeFile(*deep_copy,"deep_copy.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
std::cout << "Writing out the shallow copied scene graph as 'shallow_copy.osgt'"<<std::endl;
|
|
|
|
osgDB::writeNodeFile(*shallow_copy,"shallow_copy.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// You can use a bit mask to control which parts of the scene graph are shallow copied
|
|
|
|
// vs deep copied. The options are (from include/osg/CopyOp) :
|
|
|
|
// enum Options {
|
|
|
|
// SHALLOW_COPY = 0,
|
|
|
|
// DEEP_COPY_OBJECTS = 1,
|
|
|
|
// DEEP_COPY_NODES = 2,
|
|
|
|
// DEEP_COPY_DRAWABLES = 4,
|
|
|
|
// DEEP_COPY_STATESETS = 8,
|
|
|
|
// DEEP_COPY_STATEATTRIBUTES = 16,
|
|
|
|
// DEEP_COPY_TEXTURES = 32,
|
|
|
|
// DEEP_COPY_IMAGES = 64,
|
|
|
|
// DEEP_COPY_ALL = 0xffffffff
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// These options you can use together such as :
|
|
|
|
// osg::Node* mycopy = dynamic_cast<osg::Node*>(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);
|
|
|
|
|
2007-02-09 22:54:54 +08:00
|
|
|
// viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|