root/OpenSceneGraph/trunk/examples/osganalysis/osganalysis.cpp @ 11843

Revision 11843, 4.2 kB (checked in by robert, 4 years ago)

Added new osganalysis example as a testbed for profiling peformance of various aspects of OSG/OpenGL and scene graphs.

Line 
1/* OpenSceneGraph example, osgterrain.
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
20#include <osgViewer/Viewer>
21#include <osgViewer/ViewerEventHandlers>
22#include <osgDB/ReadFile>
23#include <osgGA/TrackballManipulator>
24#include <osgUtil/IncrementalCompileOperation>
25
26
27struct CustomCompileCompletedCallback : public osgUtil::IncrementalCompileOperation::CompileCompletedCallback
28{
29    CustomCompileCompletedCallback():
30        completed(false) {}
31
32    virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet)
33    {
34        OSG_NOTICE<<"compileCompleted"<<std::endl;
35        completed = true;
36        return true;
37    }
38
39    bool completed;
40};
41
42int main(int argc, char** argv)
43{
44    osg::ArgumentParser arguments(&argc, argv);
45
46    // construct the viewer.
47    osgViewer::Viewer viewer(arguments);
48
49    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
50    viewer.addEventHandler(new osgViewer::StatsHandler());
51    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
52
53    osg::ref_ptr<osgUtil::IncrementalCompileOperation> incrementalCompile = new osgUtil::IncrementalCompileOperation;
54    viewer.setIncrementalCompileOperation(incrementalCompile.get());
55
56    double timeBetweenMerges = 2.0;
57    while(arguments.read("--interval",timeBetweenMerges)) {}
58
59    typedef std::vector< osg::ref_ptr<osg::Node> > Models;
60    Models models;
61
62    for(int pos=1;pos<arguments.argc();++pos)
63    {
64        if (!arguments.isOption(pos))
65        {
66            // not an option so assume string is a filename.
67            osg::Node *node = osgDB::readNodeFile( arguments[pos]);
68            if(node)
69            {
70                if (node->getName().empty()) node->setName( arguments[pos] );
71                models.push_back(node);
72            }
73        }
74    }
75
76    OSG_NOTICE<<"models.size()="<<models.size()<<std::endl;
77
78    osg::ref_ptr<osg::Group> group = new osg::Group;
79
80    unsigned int modelIndex = 0;
81
82    group->addChild(models[modelIndex++].get());
83
84    viewer.setSceneData(group);
85
86    viewer.realize();
87
88    double timeOfLastMerge = viewer.getFrameStamp()->getReferenceTime();
89
90    osg::ref_ptr<CustomCompileCompletedCallback> compileCompletedCallback;
91
92    while(!viewer.done())
93    {
94        viewer.frame();
95
96        double currentTime = viewer.getFrameStamp()->getReferenceTime();
97
98        if (!compileCompletedCallback &&
99            modelIndex<models.size() &&
100            (currentTime-timeOfLastMerge)>timeBetweenMerges)
101        {
102            OSG_NOTICE<<"Compiling model "<<modelIndex<<" at "<<currentTime<<std::endl;
103
104            osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet =
105                new osgUtil::IncrementalCompileOperation::CompileSet(models[modelIndex].get());
106
107            compileCompletedCallback = new CustomCompileCompletedCallback;
108
109            compileSet->_compileCompletedCallback = compileCompletedCallback;
110
111            incrementalCompile->add(compileSet.get());
112        }
113
114        if (compileCompletedCallback.valid() && compileCompletedCallback->completed)
115        {
116            OSG_NOTICE<<"Merging model "<<modelIndex<<" at "<<currentTime<<std::endl;
117
118            timeOfLastMerge = currentTime;
119
120            compileCompletedCallback = 0;
121
122            group->removeChildren(0,group->getNumChildren());
123
124            group->addChild(models[modelIndex++].get());
125
126            viewer.home();
127        }
128
129    }
130
131    return 0;
132}
Note: See TracBrowser for help on using the browser.