| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #include <osg/MatrixTransform> |
|---|
| 20 | #include <osg/Billboard> |
|---|
| 21 | #include <osg/Geode> |
|---|
| 22 | #include <osg/Group> |
|---|
| 23 | #include <osg/Notify> |
|---|
| 24 | #include <osg/Texture> |
|---|
| 25 | |
|---|
| 26 | #include <osgDB/Registry> |
|---|
| 27 | #include <osgDB/ReadFile> |
|---|
| 28 | #include <osgDB/WriteFile> |
|---|
| 29 | |
|---|
| [5923] | 30 | #include <osgViewer/Viewer> |
|---|
| [1697] | 31 | |
|---|
| 32 | #include <osgUtil/Optimizer> |
|---|
| 33 | |
|---|
| [5923] | 34 | #include <iostream> |
|---|
| [1734] | 35 | |
|---|
| [1697] | 36 | |
|---|
| 37 | |
|---|
| 38 | class MyCopyOp : public osg::CopyOp |
|---|
| 39 | { |
|---|
| 40 | public: |
|---|
| 41 | |
|---|
| 42 | inline MyCopyOp(CopyFlags flags=SHALLOW_COPY): |
|---|
| 43 | osg::CopyOp(flags), |
|---|
| 44 | _indent(0), |
|---|
| 45 | _step(4) {} |
|---|
| 46 | |
|---|
| 47 | inline void moveIn() const { _indent += _step; } |
|---|
| 48 | inline void moveOut() const { _indent -= _step; } |
|---|
| 49 | inline void writeIndent() const |
|---|
| 50 | { |
|---|
| 51 | for(int i=0;i<_indent;++i) std::cout << " "; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | virtual osg::Referenced* operator() (const osg::Referenced* ref) const |
|---|
| 55 | { |
|---|
| 56 | writeIndent(); std::cout << "copying Referenced "<<ref<<std::endl; |
|---|
| 57 | moveIn(); |
|---|
| [1734] | 58 | osg::Referenced* ret_ref = CopyOp::operator()(ref); |
|---|
| [1697] | 59 | moveOut(); |
|---|
| 60 | return ret_ref; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | virtual osg::Object* operator() (const osg::Object* obj) const |
|---|
| 64 | { |
|---|
| 65 | writeIndent(); std::cout << "copying Object "<<obj; |
|---|
| 66 | if (obj) std::cout<<" "<<obj->className(); |
|---|
| 67 | std::cout<<std::endl; |
|---|
| 68 | moveIn(); |
|---|
| [1734] | 69 | osg::Object* ret_obj = CopyOp::operator()(obj); |
|---|
| [1697] | 70 | moveOut(); |
|---|
| 71 | return ret_obj; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | virtual osg::Node* operator() (const osg::Node* node) const |
|---|
| 75 | { |
|---|
| 76 | writeIndent(); std::cout << "copying Node "<<node; |
|---|
| 77 | if (node) std::cout<<" "<<node->className()<<" '"<<node->getName()<<"'"; |
|---|
| 78 | std::cout<<std::endl; |
|---|
| 79 | moveIn(); |
|---|
| [1734] | 80 | osg::Node* ret_node = CopyOp::operator()(node); |
|---|
| [1697] | 81 | moveOut(); |
|---|
| 82 | return ret_node; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | virtual osg::Drawable* operator() (const osg::Drawable* drawable) const |
|---|
| 86 | { |
|---|
| 87 | writeIndent(); std::cout << "copying Drawable "<<drawable; |
|---|
| 88 | if (drawable) std::cout<<" "<<drawable->className(); |
|---|
| 89 | std::cout<<std::endl; |
|---|
| 90 | moveIn(); |
|---|
| [1734] | 91 | osg::Drawable* ret_drawable = CopyOp::operator()(drawable); |
|---|
| [1697] | 92 | moveOut(); |
|---|
| 93 | return ret_drawable; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | virtual osg::StateSet* operator() (const osg::StateSet* stateset) const |
|---|
| 97 | { |
|---|
| 98 | writeIndent(); std::cout << "copying StateSet "<<stateset; |
|---|
| 99 | if (stateset) std::cout<<" "<<stateset->className(); |
|---|
| 100 | std::cout<<std::endl; |
|---|
| 101 | moveIn(); |
|---|
| [1734] | 102 | osg::StateSet* ret_stateset = CopyOp::operator()(stateset); |
|---|
| [1697] | 103 | moveOut(); |
|---|
| 104 | return ret_stateset; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | virtual osg::StateAttribute* operator() (const osg::StateAttribute* attr) const |
|---|
| 108 | { |
|---|
| 109 | writeIndent(); std::cout << "copying StateAttribute "<<attr; |
|---|
| 110 | if (attr) std::cout<<" "<<attr->className(); |
|---|
| 111 | std::cout<<std::endl; |
|---|
| 112 | moveIn(); |
|---|
| [1734] | 113 | osg::StateAttribute* ret_attr = CopyOp::operator()(attr); |
|---|
| [1697] | 114 | moveOut(); |
|---|
| 115 | return ret_attr; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | virtual osg::Texture* operator() (const osg::Texture* text) const |
|---|
| 119 | { |
|---|
| 120 | writeIndent(); std::cout << "copying Texture "<<text; |
|---|
| 121 | if (text) std::cout<<" "<<text->className(); |
|---|
| 122 | std::cout<<std::endl; |
|---|
| 123 | moveIn(); |
|---|
| [1734] | 124 | osg::Texture* ret_text = CopyOp::operator()(text); |
|---|
| [1697] | 125 | moveOut(); |
|---|
| 126 | return ret_text; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | virtual osg::Image* operator() (const osg::Image* image) const |
|---|
| 130 | { |
|---|
| 131 | writeIndent(); std::cout << "copying Image "<<image; |
|---|
| 132 | if (image) std::cout<<" "<<image->className(); |
|---|
| 133 | std::cout<<std::endl; |
|---|
| 134 | moveIn(); |
|---|
| [1734] | 135 | osg::Image* ret_image = CopyOp::operator()(image); |
|---|
| [1697] | 136 | moveOut(); |
|---|
| 137 | return ret_image; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | protected: |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | mutable int _indent; |
|---|
| 145 | mutable int _step; |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| [6137] | 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | class GraphCopyOp : public osg::CopyOp |
|---|
| 152 | { |
|---|
| 153 | public: |
|---|
| 154 | |
|---|
| 155 | inline GraphCopyOp(CopyFlags flags=SHALLOW_COPY): |
|---|
| 156 | osg::CopyOp(flags) { _nodeCopyMap.clear();} |
|---|
| 157 | |
|---|
| 158 | virtual osg::Node* operator() (const osg::Node* node) const |
|---|
| 159 | { |
|---|
| 160 | if (node && _flags&DEEP_COPY_NODES) |
|---|
| 161 | { |
|---|
| 162 | if ( node->getNumParents() > 1 ) |
|---|
| 163 | { |
|---|
| 164 | if ( _nodeCopyMap.find(node) != _nodeCopyMap.end() ) |
|---|
| 165 | { |
|---|
| 166 | std::cout<<"Copy of node "<<node<<" "<<node->getName()<<", " |
|---|
| 167 | << _nodeCopyMap[node]<<", will be reused"<<std::endl; |
|---|
| 168 | return (osg::Node*)(_nodeCopyMap[node]); |
|---|
| 169 | } |
|---|
| 170 | else |
|---|
| 171 | { |
|---|
| 172 | osg::Node* newNode = dynamic_cast<osg::Node*>( node->clone(*this) ); |
|---|
| 173 | _nodeCopyMap[node] = newNode; |
|---|
| 174 | return newNode; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | else |
|---|
| 178 | return dynamic_cast<osg::Node*>( node->clone(*this) ); |
|---|
| 179 | } |
|---|
| 180 | else |
|---|
| 181 | return const_cast<osg::Node*>(node); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | protected: |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | mutable std::map<const osg::Node*,osg::Node*> _nodeCopyMap; |
|---|
| 189 | |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| [1697] | 192 | int main( int argc, char **argv ) |
|---|
| 193 | { |
|---|
| 194 | |
|---|
| 195 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| [5923] | 198 | osgViewer::Viewer viewer; |
|---|
| [1697] | 199 | |
|---|
| 200 | |
|---|
| 201 | osg::Node* rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 202 | if (!rootnode) |
|---|
| 203 | { |
|---|
| [5923] | 204 | osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl; |
|---|
| [1697] | 205 | return 1; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | osgUtil::Optimizer optimzer; |
|---|
| 210 | optimzer.optimize(rootnode); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| [6137] | 216 | osg::ref_ptr<osg::Node> mycopy = dynamic_cast<osg::Node*>(rootnode->clone(osg::CopyOp::DEEP_COPY_ALL)); |
|---|
| [1697] | 217 | std::cout << "Doing a deep copy of scene graph"<<std::endl; |
|---|
| [6137] | 218 | |
|---|
| [1697] | 219 | |
|---|
| 220 | |
|---|
| [6137] | 221 | osg::ref_ptr<osg::Node> deep_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::DEEP_COPY_ALL))); |
|---|
| [1697] | 222 | |
|---|
| 223 | std::cout << "----------------------------------------------------------------"<<std::endl; |
|---|
| 224 | |
|---|
| [6137] | 225 | |
|---|
| 226 | std::cout << "Doing a graph preserving deep copy of scene graph nodes"<<std::endl; |
|---|
| 227 | osg::ref_ptr<osg::Node> graph_copy = dynamic_cast<osg::Node*>(rootnode->clone(GraphCopyOp(osg::CopyOp::DEEP_COPY_NODES))); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| [1697] | 230 | |
|---|
| 231 | std::cout << "Doing a shallow copy of scene graph"<<std::endl; |
|---|
| [6137] | 232 | osg::ref_ptr<osg::Node> shallow_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::SHALLOW_COPY))); |
|---|
| [1697] | 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| [12529] | 237 | std::cout << std::endl << "Writing out the original scene graph as 'original.osgt'"<<std::endl; |
|---|
| 238 | osgDB::writeNodeFile(*rootnode,"original.osgt"); |
|---|
| [1697] | 239 | |
|---|
| [12529] | 240 | std::cout << std::endl << "Writing out the graph preserving scene graph as 'graph_copy.osgt'"<<std::endl; |
|---|
| 241 | osgDB::writeNodeFile(*graph_copy,"graph_copy.osgt"); |
|---|
| [6137] | 242 | |
|---|
| [12529] | 243 | std::cout << "Writing out the deep copied scene graph as 'deep_copy.osgt'"<<std::endl; |
|---|
| 244 | osgDB::writeNodeFile(*deep_copy,"deep_copy.osgt"); |
|---|
| [1697] | 245 | |
|---|
| [12529] | 246 | std::cout << "Writing out the shallow copied scene graph as 'shallow_copy.osgt'"<<std::endl; |
|---|
| 247 | osgDB::writeNodeFile(*shallow_copy,"shallow_copy.osgt"); |
|---|
| [1697] | 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | viewer.setSceneData(rootnode); |
|---|
| 277 | |
|---|
| [6137] | 278 | |
|---|
| 279 | |
|---|
| [5923] | 280 | return viewer.run(); |
|---|
| [1697] | 281 | } |
|---|