From d9a0a3e84a2ff489f70ec88e8a7d9120c05242be Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Sun, 1 Nov 2015 22:18:29 +0100 Subject: [PATCH] LP-29 simplify resize logic (also fixes some resizing issues) --- .../libs/osgearth/osgQtQuick/OSGCamera.cpp | 84 ++++--------------- .../libs/osgearth/osgQtQuick/OSGCamera.hpp | 2 - .../libs/osgearth/osgQtQuick/OSGViewport.cpp | 20 +++-- .../src/libs/osgearth/qtwindowingsystem.cpp | 31 +++---- 4 files changed, 44 insertions(+), 93 deletions(-) diff --git a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.cpp b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.cpp index b0d566aae..7360b2d2b 100644 --- a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.cpp +++ b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.cpp @@ -73,15 +73,10 @@ public: { fieldOfView = 90.0; - first = true; + first = true; - dirty = false; - - sizeDirty = false; - x = 0; - y = 0; - width = 0; - height = 0; + dirty = false; + fovDirty = false; } ~Hidden() @@ -195,9 +190,10 @@ public: logDepthBuffer->install(camera); } - dirty = true; - sizeDirty = true; + dirty = true; + fovDirty = true; updateCamera(); + updateAspectRatio(); } void detachCamera(osg::Camera *camera) @@ -220,9 +216,6 @@ public: delete logDepthBuffer; logDepthBuffer = NULL; } - - // reset viewport - x = y = width = height = 0; } void attachManipulator(osgViewer::View *view) @@ -311,31 +304,19 @@ public: void updateCamera() { - if (first) { - first = false; - updateCameraFOV(); - } - updateCameraSize(); + updateCameraFOV(); if (manipulatorMode == User) { updateCameraPosition(); } } - void updateCameraSize() - { - if (!sizeDirty || !camera.valid()) { - return; - } - sizeDirty = false; - - // qDebug() << "OSGCamera::updateCamera size" << x << y << width << height << fieldOfView; - camera->getGraphicsContext()->resized(x, y, width, height); - camera->setViewport(x, y, width, height); - updateAspectRatio(); - } - void updateCameraFOV() { + if (!fovDirty || !camera.valid()) { + return; + } + fovDirty = false; + // qDebug() << "OSGCamera::updateCameraFOV"; double fovy, ar, zn, zf; @@ -351,7 +332,8 @@ public: camera->getProjectionMatrixAsPerspective(fovy, ar, zn, zf); - ar = static_cast(width) / static_cast(height); + osg::Viewport *viewport = camera->getViewport(); + ar = static_cast(viewport->width()) / static_cast(viewport->height()); camera->setProjectionMatrixAsPerspective(fovy, ar, zn, zf); } @@ -404,6 +386,7 @@ public: } qreal fieldOfView; + bool fovDirty; OSGNode *sceneData; @@ -430,12 +413,6 @@ public: QVector3D attitude; QVector3D position; - bool sizeDirty; - int x; - int y; - int width; - int height; - osg::ref_ptr camera; osg::ref_ptr cameraUpdateCallback; @@ -478,23 +455,13 @@ qreal OSGCamera::fieldOfView() const return h->fieldOfView; } -// ! Camera vertical field of view in degrees +// Camera vertical field of view in degrees void OSGCamera::setFieldOfView(qreal arg) { if (h->fieldOfView != arg) { h->fieldOfView = arg; - h->sizeDirty = true; + h->fovDirty = true; emit fieldOfViewChanged(fieldOfView()); - - // it should be a queued call to OSGCameraRenderer instead - /*if (h->viewer.get()) { - h->viewer->getCamera()->setProjectionMatrixAsPerspective( - h->fieldOfView, - qreal(h->currentSize.width())/h->currentSize.height(), - 1.0f, 10000.0f); - }*/ - - // updateFrame(); } } @@ -619,23 +586,6 @@ void OSGCamera::setLogarithmicDepthBuffer(bool enabled) } } -void OSGCamera::setViewport(int x, int y, int width, int height) -{ - // qDebug() << "OSGCamera::setViewport" << x << y << width << "x" << heigth; - if (width <= 0 || height <= 0) { - qWarning() << "OSGCamera::setViewport - invalid size" << width << "x" << height; - return; - } - if (h->x != x || h->y != y || h->width != width || h->height != height) { - qWarning() << "OSGCamera::setViewport" << width << "x" << height; - h->x = x; - h->y = y; - h->width = width; - h->height = height; - h->sizeDirty = true; - } -} - bool OSGCamera::attach(osgViewer::View *view) { return h->attach(view); diff --git a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.hpp b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.hpp index 6b40f153d..ec6c85a27 100644 --- a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.hpp +++ b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGCamera.hpp @@ -117,8 +117,6 @@ public: bool logarithmicDepthBuffer(); void setLogarithmicDepthBuffer(bool enabled); - void setViewport(int x, int y, int width, int height); - virtual bool attach(osgViewer::View *view); virtual bool detach(osgViewer::View *view); diff --git a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGViewport.cpp b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGViewport.cpp index 726d4fe57..4c43c005c 100644 --- a/ground/gcs/src/libs/osgearth/osgQtQuick/OSGViewport.cpp +++ b/ground/gcs/src/libs/osgearth/osgQtQuick/OSGViewport.cpp @@ -173,7 +173,6 @@ public: return false; } if (camera) { - camera->setViewport(0, 0, self->width(), self->height()); camera->attach(view); } else { qWarning() << "OSGViewport::attach - no camera!"; @@ -384,7 +383,10 @@ public: // add the screen capture handler // view->addEventHandler(new osgViewer::ScreenCaptureHandler); - view->getCamera()->setGraphicsContext(createGraphicsContext()); + osg::GraphicsContext *gc = createGraphicsContext(); + osg::Camera *camera = view->getCamera(); + camera->setGraphicsContext(gc); + camera->setViewport(new osg::Viewport(0, 0, gc->getTraits()->width, gc->getTraits()->height)); return view; } @@ -548,8 +550,9 @@ public: QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) { qDebug() << "ViewportRenderer::createFramebufferObject" << size; - if (h->camera) { - h->camera->setViewport(0, 0, size.width(), size.height()); + if (h->view.valid()) { + h->view->getCamera()->getGraphicsContext()->resized(0, 0, size.width(), size.height()); + h->view->getEventQueue()->windowResize(0, 0, size.width(), size.height()); } QOpenGLFramebufferObjectFormat format; @@ -583,7 +586,6 @@ QtKeyboardMap OSGViewport::Hidden::keyMap = QtKeyboardMap(); /* class OSGViewport */ - OSGViewport::OSGViewport(QQuickItem *parent) : QQuickFramebufferObject(parent), h(new Hidden(this)) { qDebug() << "OSGViewport::OSGViewport"; @@ -719,10 +721,10 @@ QSGNode *OSGViewport::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNode QPointF OSGViewport::mousePoint(QMouseEvent *event) { - // qreal x = 0.01 * (event->x() - self->width() / 2); - // qreal y = 0.01 * (event->y() - self->height() / 2); - qreal x = 2.0 * (event->x() - width() / 2) / width(); - qreal y = 2.0 * (event->y() - height() / 2) / height(); + // qreal x = 2.0 * (event->x() - width() / 2) / width(); + // qreal y = 2.0 * (event->y() - height() / 2) / height(); + qreal x = event->x(); + qreal y = event->y(); return QPointF(x, y); } diff --git a/ground/gcs/src/libs/osgearth/qtwindowingsystem.cpp b/ground/gcs/src/libs/osgearth/qtwindowingsystem.cpp index 6eac26a29..6f81b3dee 100644 --- a/ground/gcs/src/libs/osgearth/qtwindowingsystem.cpp +++ b/ground/gcs/src/libs/osgearth/qtwindowingsystem.cpp @@ -104,20 +104,7 @@ GraphicsWindowQt::GraphicsWindowQt(osg::GraphicsContext::Traits *traits) : { qDebug() << "GraphicsWindowQt::GraphicsWindowQt"; _traits = traits; - init(); - - if (valid()) { - setState(new osg::State); - getState()->setGraphicsContext(this); - - if (_traits.valid() && _traits->sharedContext.valid()) { - getState()->setContextID(_traits->sharedContext->getState()->getContextID()); - incrementContextIDUsageCount(getState()->getContextID()); - } else { - getState()->setContextID(osg::GraphicsContext::createNewContextID()); - } - } } GraphicsWindowQt::~GraphicsWindowQt() @@ -140,6 +127,20 @@ void GraphicsWindowQt::init() // if ( !parent ) // parent = windowData ? windowData->_parent : NULL; + + setState(new osg::State); + getState()->setGraphicsContext(this); + + if (_traits.valid() && _traits->sharedContext.valid()) { + getState()->setContextID(_traits->sharedContext->getState()->getContextID()); + incrementContextIDUsageCount(getState()->getContextID()); + } else { + getState()->setContextID(osg::GraphicsContext::createNewContextID()); + } + + // make sure the event queue has the correct window rectangle size and input range + getEventQueue()->syncWindowRectangleWithGraphicsContext(); + _initialized = true; _valid = _initialized; @@ -244,8 +245,8 @@ bool GraphicsWindowQt::realizeImplementation() _realized = true; -//// make sure the event queue has the correct window rectangle size and input range -// getEventQueue()->syncWindowRectangleWithGraphcisContext(); + // make sure the event queue has the correct window rectangle size and input range + getEventQueue()->syncWindowRectangleWithGraphicsContext(); // make this window's context not current // note: this must be done as we will probably make the context current from another thread