diff --git a/include/osg/Camera b/include/osg/Camera index 2bf23ee1d..3a2219a57 100644 --- a/include/osg/Camera +++ b/include/osg/Camera @@ -680,6 +680,19 @@ class OSG_EXPORT Camera : public Transform, public CullSettings ref_ptr _finalDrawCallback; }; + +/** Functor to assist with ordering camers in the order they should be rendered in.*/ +struct CameraRenderOrderSortOp +{ + inline bool operator() (const Camera* lhs,const Camera* rhs) const + { + if (lhs->getRenderOrder()getRenderOrder()) return true; + if (rhs->getRenderOrder()getRenderOrder()) return false; + return lhs->getRenderOrderNum()getRenderOrderNum(); + } +}; + + } #endif diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index 037b459cc..df2e2492f 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -754,18 +754,6 @@ void GraphicsContext::removeAllOperations() _operationsBlock->set(false); } - -struct CameraRenderOrderSortOp -{ - inline bool operator() (const Camera* lhs,const Camera* rhs) const - { - if (lhs->getRenderOrder()getRenderOrder()) return true; - if (rhs->getRenderOrder()getRenderOrder()) return false; - return lhs->getRenderOrderNum()getRenderOrderNum(); - } -}; - - void GraphicsContext::runOperations() { // sort the cameras into order diff --git a/src/osgViewer/CompositeViewer.cpp b/src/osgViewer/CompositeViewer.cpp index 85045e898..3e7e713c3 100644 --- a/src/osgViewer/CompositeViewer.cpp +++ b/src/osgViewer/CompositeViewer.cpp @@ -837,7 +837,6 @@ void CompositeViewer::generateSlavePointerData(osg::Camera* camera, osgGA::GUIEv } } - void CompositeViewer::generatePointerData(osgGA::GUIEventAdapter& event) { osgViewer::GraphicsWindow* gw = dynamic_cast(event.getGraphicsContext()); @@ -852,6 +851,9 @@ void CompositeViewer::generatePointerData(osgGA::GUIEventAdapter& event) event.addPointerData(new osgGA::PointerData(gw, x, 0, gw->getTraits()->width, y, 0, gw->getTraits()->height)); + typedef std::vector CameraVector; + CameraVector activeCameras; + osg::GraphicsContext::Cameras& cameras = gw->getCameras(); for(osg::GraphicsContext::Cameras::iterator citr = cameras.begin(); citr != cameras.end(); @@ -866,20 +868,31 @@ void CompositeViewer::generatePointerData(osgGA::GUIEventAdapter& event) x >= viewport->x() && y >= viewport->y() && x <= (viewport->x()+viewport->width()) && y <= (viewport->y()+viewport->height()) ) { - event.addPointerData(new osgGA::PointerData(camera, (x-viewport->x())/viewport->width()*2.0f-1.0f, -1.0, 1.0, - (y-viewport->y())/viewport->height()*2.0f-1.0f, -1.0, 1.0)); - - osgViewer::View* view = dynamic_cast(camera->getView()); - osg::Camera* view_masterCamera = view ? view->getCamera() : 0; - - // if camera isn't the master it must be a slave and could need reprojecting. - if (view && camera!=view_masterCamera) - { - generateSlavePointerData(camera, event); - } + activeCameras.push_back(camera); } } } + + std::sort(activeCameras.begin(), activeCameras.end(), osg::CameraRenderOrderSortOp()); + + osg::Camera* camera = activeCameras.empty() ? 0 : activeCameras.back(); + + if (camera) + { + osg::Viewport* viewport = camera ? camera->getViewport() : 0; + + event.addPointerData(new osgGA::PointerData(camera, (x-viewport->x())/viewport->width()*2.0f-1.0f, -1.0, 1.0, + (y-viewport->y())/viewport->height()*2.0f-1.0f, -1.0, 1.0)); + + osgViewer::View* view = dynamic_cast(camera->getView()); + osg::Camera* view_masterCamera = view ? view->getCamera() : 0; + + // if camera isn't the master it must be a slave and could need reprojecting. + if (view && camera!=view_masterCamera) + { + generateSlavePointerData(camera, event); + } + } } void CompositeViewer::reprojectPointerData(osgGA::GUIEventAdapter& source_event, osgGA::GUIEventAdapter& dest_event) diff --git a/src/osgViewer/Viewer.cpp b/src/osgViewer/Viewer.cpp index 613e21b9f..177eb95aa 100644 --- a/src/osgViewer/Viewer.cpp +++ b/src/osgViewer/Viewer.cpp @@ -784,7 +784,9 @@ void Viewer::generatePointerData(osgGA::GUIEventAdapter& event) event.addPointerData(new osgGA::PointerData(gw, x, 0, gw->getTraits()->width, y, 0, gw->getTraits()->height)); - // new code for populating the PointerData + typedef std::vector CameraVector; + CameraVector activeCameras; + osgViewer::View* this_view = dynamic_cast(this); osg::GraphicsContext::Cameras& cameras = gw->getCameras(); for(osg::GraphicsContext::Cameras::iterator citr = cameras.begin(); @@ -801,17 +803,28 @@ void Viewer::generatePointerData(osgGA::GUIEventAdapter& event) x >= viewport->x() && y >= viewport->y() && x <= (viewport->x()+viewport->width()) && y <= (viewport->y()+viewport->height()) ) { - event.addPointerData(new osgGA::PointerData(camera, (x-viewport->x())/viewport->width()*2.0f-1.0f, -1.0, 1.0, - (y-viewport->y())/viewport->height()*2.0f-1.0f, -1.0, 1.0)); - - // if camera isn't the master it must be a slave and could need reprojecting. - if (camera!=getCamera()) - { - generateSlavePointerData(camera, event); - } + activeCameras.push_back(camera); } } } + + std::sort(activeCameras.begin(), activeCameras.end(), osg::CameraRenderOrderSortOp()); + + osg::Camera* camera = activeCameras.empty() ? 0 : activeCameras.back(); + + if (camera) + { + osg::Viewport* viewport = camera ? camera->getViewport() : 0; + + event.addPointerData(new osgGA::PointerData(camera, (x-viewport->x())/viewport->width()*2.0f-1.0f, -1.0, 1.0, + (y-viewport->y())/viewport->height()*2.0f-1.0f, -1.0, 1.0)); + + // if camera isn't the master it must be a slave and could need reprojecting. + if (camera!=getCamera()) + { + generateSlavePointerData(camera, event); + } + } } void Viewer::reprojectPointerData(osgGA::GUIEventAdapter& source_event, osgGA::GUIEventAdapter& dest_event)