diff --git a/examples/osgQtWidgets/osgQtWidgets.cpp b/examples/osgQtWidgets/osgQtWidgets.cpp index 10518f1c0..3a918c711 100644 --- a/examples/osgQtWidgets/osgQtWidgets.cpp +++ b/examples/osgQtWidgets/osgQtWidgets.cpp @@ -418,7 +418,7 @@ int main(int argc, char **argv) root->addChild(overlay); - osgViewer::InteractiveImageHandler* handler = new osgViewer::InteractiveImageHandler(widgetImage.get(), camera); + osgViewer::InteractiveImageHandler* handler = new osgViewer::InteractiveImageHandler(widgetImage.get(), texture, camera); quad->setEventCallback(handler); quad->setCullCallback(handler); } diff --git a/include/osgViewer/ViewerEventHandlers b/include/osgViewer/ViewerEventHandlers index 526a11ffb..dd38719f2 100644 --- a/include/osgViewer/ViewerEventHandlers +++ b/include/osgViewer/ViewerEventHandlers @@ -407,8 +407,10 @@ class OSGVIEWER_EXPORT InteractiveImageHandler : public osgGA::GUIEventHandler, { public: - InteractiveImageHandler(osg::Image* image, osg::Camera* camera = 0): - _image(image), _fullscreen(camera != 0), _camera(camera) {} + /// Constructor to use when the InteractiveImage is in the 3D scene (i.e. not in a fullscreen HUD overlay). + InteractiveImageHandler(osg::Image* image); + /// Constructor to use when the InteractiveImage is in a fullscreen HUD overlay. + InteractiveImageHandler(osg::Image* image, osg::Texture2D* texture, osg::Camera* camera); META_Object(osgViewer, InteractiveImageHandler); @@ -428,7 +430,10 @@ protected: bool mousePosition(osgViewer::View* view, osg::NodeVisitor* nv, const osgGA::GUIEventAdapter& ea, int& x, int &y) const; bool computeIntersections(osgViewer::View* view, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff) const; + void resize(int width, int height); + osg::observer_ptr _image; + osg::observer_ptr _texture; bool _fullscreen; osg::observer_ptr _camera; diff --git a/src/osgViewer/ViewerEventHandlers.cpp b/src/osgViewer/ViewerEventHandlers.cpp index ef3b006eb..c37939dd3 100644 --- a/src/osgViewer/ViewerEventHandlers.cpp +++ b/src/osgViewer/ViewerEventHandlers.cpp @@ -20,8 +20,10 @@ #include +#include #include #include +#include #include #include @@ -624,6 +626,30 @@ void LODScaleHandler::getUsage(osg::ApplicationUsage& usage) const } } +InteractiveImageHandler::InteractiveImageHandler(osg::Image* image) : + _image(image), + _texture(0), + _fullscreen(false), + _camera(0) +{ +} + +InteractiveImageHandler::InteractiveImageHandler(osg::Image* image, osg::Texture2D* texture, osg::Camera* camera) : + _image(image), + _texture(texture), + _fullscreen(true), + _camera(camera) +{ + if (_camera.valid() && _camera->getViewport()) + { + // Send an initial resize event (with the same size) so the image can + // resize itself initially. + double width = _camera->getViewport()->width(); + double height = _camera->getViewport()->height(); + + resize(width, height); + } +} bool InteractiveImageHandler::computeIntersections(osgViewer::View* view, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask) const { @@ -822,10 +848,11 @@ bool InteractiveImageHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUI } case (osgGA::GUIEventAdapter::RESIZE): { - if (_fullscreen) + if (_fullscreen && _camera.valid()) { _camera->setViewport(0, 0, ea.getWindowWidth(), ea.getWindowHeight()); - _image->scaleImage(ea.getWindowWidth(), ea.getWindowHeight(), 1); + + resize(ea.getWindowWidth(), ea.getWindowHeight()); return true; } } @@ -846,4 +873,17 @@ bool InteractiveImageHandler::cull(osg::NodeVisitor* nv, osg::Drawable*, osg::Re return false; } +void InteractiveImageHandler::resize(int width, int height) +{ + if (_image.valid()) + { + _image->scaleImage(width, height, 1); + } + + // Make sure the texture does not rescale the image because + // it thinks it should still be the previous size... + if (_texture.valid()) + _texture->setTextureSize(width, height); +} + }