root/OpenSceneGraph/trunk/examples/osgcopy/osgcopy.cpp @ 13342

Revision 12529, 10.5 kB (checked in by robert, 4 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgcopy.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
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
30#include <osgViewer/Viewer>
31
32#include <osgUtil/Optimizer>
33
34#include <iostream>
35
36// Customize the CopyOp so that we add our own verbose
37// output of what's being copied.
38class 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();
58            osg::Referenced* ret_ref = CopyOp::operator()(ref);
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();
69            osg::Object* ret_obj = CopyOp::operator()(obj);
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();
80            osg::Node* ret_node = CopyOp::operator()(node);
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();
91            osg::Drawable* ret_drawable = CopyOp::operator()(drawable);
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();
102            osg::StateSet* ret_stateset = CopyOp::operator()(stateset);
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();
113            osg::StateAttribute* ret_attr = CopyOp::operator()(attr);
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();
124            osg::Texture* ret_text = CopyOp::operator()(text);
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();
135            osg::Image* ret_image = CopyOp::operator()(image);
136            moveOut();
137            return ret_image;
138        }
139       
140    protected:
141   
142        // must be mutable since CopyOp is passed around as const to
143        // the various clone/copy constructors.
144        mutable int _indent;
145        mutable int _step;
146};
147
148// this CopyOp class will preserve the multi-parent structure of the copied
149// object, instead of expanding it into a tree. Works with the
150// DEEP_COPY_NODES flag.
151class 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        // must be mutable since CopyOp is passed around as const to
187        // the various clone/copy constructors.
188        mutable std::map<const osg::Node*,osg::Node*> _nodeCopyMap;
189
190};
191
192int main( int argc, char **argv )
193{
194    // use an ArgumentParser object to manage the program arguments.
195    osg::ArgumentParser arguments(&argc,argv);
196
197    // initialize the viewer.
198    osgViewer::Viewer viewer;
199
200    // load the nodes from the commandline arguments.
201    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
202    if (!rootnode)
203    {
204        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
205        return 1;
206    }
207   
208    // run optimization over the scene graph
209    osgUtil::Optimizer optimzer;
210    optimzer.optimize(rootnode);
211   
212// -------------    Start of copy specific code -------------------------------------------------------
213   
214    // do a deep copy, using MyCopyOp to reveal whats going on under the good,
215    // in your own code you'd typically just use the basic osg::CopyOp something like
216    osg::ref_ptr<osg::Node> mycopy = dynamic_cast<osg::Node*>(rootnode->clone(osg::CopyOp::DEEP_COPY_ALL));
217    std::cout << "Doing a deep copy of scene graph"<<std::endl;
218
219    // note, we need the dyanmic_cast because MS Visual Studio can't handle covarient
220    // return types, so that clone has return just Object*.  bahh hum bug
221    osg::ref_ptr<osg::Node> deep_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::DEEP_COPY_ALL)));
222   
223    std::cout << "----------------------------------------------------------------"<<std::endl;
224
225    // do a graph preserving deep copy.
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
230    // do a shallow copy.
231    std::cout << "Doing a shallow copy of scene graph"<<std::endl;
232    osg::ref_ptr<osg::Node> shallow_copy = dynamic_cast<osg::Node*>(rootnode->clone(MyCopyOp(osg::CopyOp::SHALLOW_COPY)));
233
234
235    // write out the various scene graphs so that they can be browsed, either
236    // in an editor or using a graphics diff tool gdiff/xdiff/xxdiff.
237    std::cout << std::endl << "Writing out the original scene graph as 'original.osgt'"<<std::endl;
238    osgDB::writeNodeFile(*rootnode,"original.osgt");
239
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");
242
243    std::cout << "Writing out the deep copied scene graph as 'deep_copy.osgt'"<<std::endl;
244    osgDB::writeNodeFile(*deep_copy,"deep_copy.osgt");
245
246    std::cout << "Writing out the shallow copied scene graph as 'shallow_copy.osgt'"<<std::endl;
247    osgDB::writeNodeFile(*shallow_copy,"shallow_copy.osgt");
248
249
250    // You can use a bit mask to control which parts of the scene graph are shallow copied
251    // vs deep copied.  The options are (from include/osg/CopyOp) :
252    // enum Options {
253    //        SHALLOW_COPY = 0,
254    //        DEEP_COPY_OBJECTS = 1,
255    //        DEEP_COPY_NODES = 2,
256    //        DEEP_COPY_DRAWABLES = 4,
257    //        DEEP_COPY_STATESETS = 8,
258    //        DEEP_COPY_STATEATTRIBUTES = 16,
259    //        DEEP_COPY_TEXTURES = 32,
260    //        DEEP_COPY_IMAGES = 64,
261    //        DEEP_COPY_ALL = 0xffffffff
262    // };
263    //
264    // These options you can use together such as :
265    //    osg::Node* mycopy = dynamic_cast<osg::Node*>(rootnode->clone(osg::CopyOp::DEEP_COPY_NODES | DEEP_COPY_DRAWABLES));
266    // Which shares state but creates copies of all nodes and drawables (which contain the geometry).
267    //
268    // You may also want to subclass from CopyOp to provide finer grained control of what gets shared (shallow copy) vs
269    // cloned (deep copy).
270   
271
272
273// -------------    End of copy specific code -------------------------------------------------------
274     
275    // set the scene to render
276    viewer.setSceneData(rootnode);
277
278    // viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
279
280    return viewer.run();
281}
Note: See TracBrowser for help on using the browser.