From 035f49c0b35c60f853fe2a3c6fe29df7e34e5477 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 18 Jul 2007 16:17:06 +0000 Subject: [PATCH] Refactored the viewer setup code to use a CustomViewer subclass of Viewer which wraps up the set up of compile contexts and merging of changes. --- examples/osgterrain/osgterrain.cpp | 113 ++++++++++++++++++----------- 1 file changed, 71 insertions(+), 42 deletions(-) diff --git a/examples/osgterrain/osgterrain.cpp b/examples/osgterrain/osgterrain.cpp index 84ec45f46..5943c4b32 100644 --- a/examples/osgterrain/osgterrain.cpp +++ b/examples/osgterrain/osgterrain.cpp @@ -237,10 +237,22 @@ public: } // merge the changes with the main scene graph. - void update(osg::Group* scene) + void update(osg::Node* scene) { + osg::Group* group = dynamic_cast(scene); + if (!group) + { + osg::notify(osg::NOTICE)<<"Error, MasterOperation::update(Node*) can only work with a Group as Viewer::getSceneData()."< lock(_mutex); + if (!_nodesToRemove.empty() || !_nodesToAdd.empty()) + { + osg::notify(osg::NOTICE)<<"update().................. "<removeChild(fnmItr->second.get()); + group->removeChild(fnmItr->second.get()); _existingFilenameNodeMap.erase(fnmItr); } } @@ -266,7 +278,8 @@ public: itr != _nodesToAdd.end(); ++itr) { - scene->addChild(itr->second.get()); + osg::notify(osg::NOTICE)<<" update():inserting "<first<addChild(itr->second.get()); _existingFilenameNodeMap[itr->first] = itr->second; } @@ -411,12 +424,60 @@ protected: osg::observer_ptr _layer; }; +class CustomViewer : public osgViewer::Viewer +{ +public: + CustomViewer(osg::ArgumentParser& arguments): + Viewer(arguments) {} + + + void setMasterOperation(MasterOperation* masterOp) { _masterOperation = masterOp; } + MasterOperation* getMasterOperation() { return _masterOperation.get(); } + + // override the realize to create the compile graphics contexts + threads for us. + virtual void realize() + { + Viewer::realize(); + + + int numProcessors = OpenThreads::GetNumberOfProcessors(); + int processNum = (getThreadingModel()==osgViewer::Viewer::SingleThreaded) ? 1 : 0; + + for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) + { + osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); + + if (gc) + { + gc->createGraphicsThread(); + gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); + gc->getGraphicsThread()->startThread(); + + ++processNum; + } + } + } + + // override the updateTraversal to add the merging of data from the MasterOperation. + virtual void updateTraversal() + { + Viewer::updateTraversal(); + + if (_masterOperation.valid()) _masterOperation->update(getSceneData()); + } + +protected: + + osg::ref_ptr _masterOperation; +}; + + int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc, argv); // construct the viewer. - osgViewer::Viewer viewer(arguments); + CustomViewer viewer(arguments); // set up the camera manipulators. { @@ -472,6 +533,7 @@ int main(int argc, char** argv) while(arguments.read("-m",masterFilename)) { masterOperation = new MasterOperation(masterFilename); + viewer.setMasterOperation(masterOperation.get()); } @@ -717,46 +779,13 @@ int main(int argc, char** argv) operationThread->startThread(); operationThread->add(masterOperation.get()); } + + viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); // realize the graphics windows. viewer.realize(); - - // set up any compile contexts that are required. - if (createBackgroundContextForCompiling) - { - - int numProcessors = OpenThreads::GetNumberOfProcessors(); - int processNum = 0; - for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i) - { - osg::GraphicsContext* gc = osg::GraphicsContext::getOrCreateCompileContext(i); - - if (gc && createBackgroundThreadsForCompiling) - { - gc->createGraphicsThread(); - gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); - gc->getGraphicsThread()->startThread(); - - ++processNum; - } - } - } - - - // run main loop, with syncing with masterOperation - while (!viewer.done()) - { - viewer.advance(); - viewer.eventTraversal(); - viewer.updateTraversal(); - - if (masterOperation.valid()) masterOperation->update(scene.get()); - - viewer.frame(); - } - - // kill the operation thread - operationThread = 0; + // run the viewers main loop + return viewer.run(); }