| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 27 | struct 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 | |
|---|
| 42 | int main(int argc, char** argv) |
|---|
| 43 | { |
|---|
| 44 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|