diff --git a/include/osgViewer/Scene b/include/osgViewer/Scene index 7279ad1e3..3d908e2e5 100644 --- a/include/osgViewer/Scene +++ b/include/osgViewer/Scene @@ -39,7 +39,9 @@ class OSGVIEWER_EXPORT Scene : public virtual osg::Referenced osg::Node* getSceneData(); const osg::Node* getSceneData() const; + void setFrameStamp(osg::FrameStamp* frameStamp); osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); } + const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); } void setDatabasePager(osgDB::DatabasePager* dp); osgDB::DatabasePager* getDatabasePager() { return _databasePager.get(); } diff --git a/include/osgViewer/View b/include/osgViewer/View index 6eaf0115f..4efc523fc 100644 --- a/include/osgViewer/View +++ b/include/osgViewer/View @@ -32,7 +32,7 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter View(); - void setSceneData(osg::Node* node); + virtual void setSceneData(osg::Node* node); osg::Node* getSceneData() { return _scene.valid() ? _scene->getSceneData() : 0; } const osg::Node* getSceneData() const { return _scene.valid() ? _scene->getSceneData() : 0; } diff --git a/include/osgViewer/Viewer b/include/osgViewer/Viewer index fee7dd9e6..af421d028 100644 --- a/include/osgViewer/Viewer +++ b/include/osgViewer/Viewer @@ -20,8 +20,6 @@ namespace osgViewer { -// WARNING ** Under development do not use, yet :-) - /** Viewer holds a single view on to a single scene..*/ class OSGVIEWER_EXPORT Viewer : public osgViewer::View { @@ -40,9 +38,18 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View bool done() const { return _done; } - osg::FrameStamp* getFrameStamp(); - - const osg::FrameStamp* getFrameStamp() const; + void Viewer::setStartTick(osg::Timer_t tick); + osg::Timer_t getStartTick() const { return _startTick; } + + void setReferenceTime(double time=0.0); + + void setFrameStamp(osg::FrameStamp* frameStamp); + osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); } + const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); } + + + + virtual void setSceneData(osg::Node* node); enum ThreadingModel { @@ -124,15 +131,16 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View typedef std::vector Windows; void getWindows(Windows& windows, bool onlyValid=true); + void stopThreading(); + void startThreading(); + void setUpRenderingSupport(); + protected: void init(); - void stopThreading(); - void startThreading(); void checkWindowStatus(); - void setUpRenderingSupport(); bool _firstFrame; @@ -150,6 +158,9 @@ class OSGVIEWER_EXPORT Viewer : public osgViewer::View unsigned int _numThreadsOnBarrier; + osg::Timer_t _startTick; + osg::ref_ptr _frameStamp; + osg::observer_ptr _cameraWithFocus; osg::ref_ptr _eventVisitor; diff --git a/src/osgViewer/Scene.cpp b/src/osgViewer/Scene.cpp index 32bcdd30b..2bfd23a24 100644 --- a/src/osgViewer/Scene.cpp +++ b/src/osgViewer/Scene.cpp @@ -33,6 +33,12 @@ Scene::~Scene() { } +void Scene::setFrameStamp(osg::FrameStamp* frameStamp) +{ + _frameStamp = frameStamp; + _updateVisitor->setFrameStamp(_frameStamp.get()); +} + void Scene::init() { osg::notify(osg::NOTICE)<<"Scene::init() not implementated yet."<setFrameNumber(0); + _frameStamp->setReferenceTime(0); + _eventVisitor = new osgGA::EventVisitor; _eventVisitor->setActionAdapter(this); } @@ -109,14 +113,50 @@ int Viewer::run() return 0; } -osg::FrameStamp* Viewer::getFrameStamp() +void Viewer::setStartTick(osg::Timer_t tick) { - return _scene.valid() ? _scene->getFrameStamp() : 0; + _startTick = tick; + + Contexts contexts; + getContexts(contexts,false); + + getEventQueue()->setStartTick(osg::Timer::instance()->getStartTick());; + for(Contexts::iterator citr = contexts.begin(); + citr != contexts.end(); + ++citr) + { + osgViewer::GraphicsWindow* gw = dynamic_cast(*citr); + if (gw) + { + gw->getEventQueue()->setStartTick(osg::Timer::instance()->getStartTick()); + } + } } -const osg::FrameStamp* Viewer::getFrameStamp() const + +void Viewer::setReferenceTime(double time) { - return _scene.valid() ? _scene->getFrameStamp() : 0; + osg::Timer_t tick = osg::Timer::instance()->tick(); + double currentTime = osg::Timer::instance()->delta_s(_startTick, tick); + double delta_ticks = (time-currentTime)*(osg::Timer::instance()->getSecondsPerTick()); + if (delta_ticks>=0) tick += osg::Timer_t(delta_ticks); + else tick -= osg::Timer_t(-delta_ticks); + + // assign the new start tick + setStartTick(tick); +} + + +void Viewer::setSceneData(osg::Node* node) +{ + _scene = new osgViewer::Scene; + _scene->setSceneData(node); + _scene->setFrameStamp(_frameStamp.get()); + + setReferenceTime(0.0); + + assignSceneDataToCameras(); + setUpRenderingSupport(); } void Viewer::setThreadingModel(ThreadingModel threadingModel) @@ -184,7 +224,7 @@ struct CompileOperation : public osg::GraphicsOperation compileVisitor.setState(context->getState()); // do the compile traversal - _scene->accept(compileVisitor); + if (_scene.valid()) _scene->accept(compileVisitor); // osg::notify(osg::NOTICE)<<"Done Compile "<getFrameStamp(); + osg::FrameStamp* frameStamp = getFrameStamp(); // what should we do with the old sceneViews? _cameraSceneViewMap.clear(); @@ -470,6 +511,7 @@ void Viewer::setUpRenderingSupport() } osg::DisplaySettings* ds = _displaySettings.valid() ? _displaySettings.get() : osg::DisplaySettings::instance(); + osgDB::DatabasePager* dp = _scene.valid() ? _scene->getDatabasePager() : 0; if (_camera.valid() && _camera->getGraphicsContext()) { @@ -484,7 +526,7 @@ void Viewer::setUpRenderingSupport() sceneView->setSceneData(getSceneData()); sceneView->setFrameStamp(frameStamp); - _camera->getGraphicsContext()->add(new RenderingOperation(sceneView, _scene->getDatabasePager())); + _camera->getGraphicsContext()->add(new RenderingOperation(sceneView, dp)); } for(unsigned i=0; isetSceneData(getSceneData()); sceneView->setFrameStamp(frameStamp); - slave._camera->getGraphicsContext()->add(new RenderingOperation(sceneView, _scene->getDatabasePager())); + slave._camera->getGraphicsContext()->add(new RenderingOperation(sceneView, dp)); } } } @@ -563,19 +605,9 @@ void Viewer::realize() // initialize the global timer to be relative to the current time. osg::Timer::instance()->setStartTick(); - - getEventQueue()->setStartTick(osg::Timer::instance()->getStartTick());; - for(Contexts::iterator citr = contexts.begin(); - citr != contexts.end(); - ++citr) - { - osgViewer::GraphicsWindow* gw = dynamic_cast(*citr); - if (gw) - { - gw->getEventQueue()->setStartTick(osg::Timer::instance()->getStartTick()); - } - } + // pass on the start tick to all the associated eventqueues + setStartTick(osg::Timer::instance()->getStartTick()); } @@ -607,9 +639,8 @@ void Viewer::advance() { if (_done) return; - // osg::notify(osg::NOTICE)<<"Viewer::frameAdvance()."<frameAdvance(); + _frameStamp->setReferenceTime( osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()) ); + _frameStamp->setFrameNumber(_frameStamp->getFrameNumber()+1); } void Viewer::eventTraversal() @@ -663,7 +694,7 @@ void Viewer::eventTraversal() float y = event->getY(); bool invert_y = event->getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS; - if (invert_y) y = gw->getTraits()->height - y; + if (invert_y && gw->getTraits()) y = gw->getTraits()->height - y; switch(event->getEventType()) { @@ -769,7 +800,7 @@ void Viewer::eventTraversal() // osg::notify(osg::NOTICE)<<"mouseEventState Xmin = "<getXmin()<<" Ymin="<getYmin()<<" xMax="<getXmax()<<" Ymax="<getYmax()<frame( _scene->getFrameStamp()->getReferenceTime() ); + if (_scene.valid()) _eventQueue->frame( getFrameStamp()->getReferenceTime() ); _eventQueue->takeEvents(events); @@ -869,10 +900,10 @@ void Viewer::eventTraversal() } } - if (_eventVisitor.valid()) + if (_eventVisitor.valid() && _scene.valid()) { - _eventVisitor->setFrameStamp(_scene->getFrameStamp()); - _eventVisitor->setTraversalNumber(_scene->getFrameStamp()->getFrameNumber()); + _eventVisitor->setFrameStamp(getFrameStamp()); + _eventVisitor->setTraversalNumber(getFrameStamp()->getFrameNumber()); for(osgGA::EventQueue::Events::iterator itr = events.begin(); itr != events.end(); @@ -914,10 +945,10 @@ void Viewer::renderingTraversals() if (_done) return; - osgDB::DatabasePager* dp = _scene->getDatabasePager(); + osgDB::DatabasePager* dp = _scene.valid() ? _scene->getDatabasePager() : 0; if (dp) { - dp->signalBeginFrame(_scene->getFrameStamp()); + dp->signalBeginFrame(getFrameStamp()); } // osg::notify(osg::NOTICE)<