diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2018-02-12 12:06:13 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2018-02-14 10:34:44 +0000 |
commit | bbb51e976c16894f40b49450a23787f010f197d3 (patch) | |
tree | 930a6b93982e2ba12ffad12376d3a602c512cdaa /examples | |
parent | e97b7cbec628cc2aaeafe207da081f467c5295b6 (diff) |
Add Q3DSSurfaceViewer API
Same disclaimer as for widgets, rendering is not perfect yet.
Offscreen readback is broken for reasons not known. (QT3DS-1042)
When rendering offscreen (and then reading back to generate images for
example), the global animation time cannot be controlled yet.
(to be handled in QT3DS-1041)
As a bonus, Qt 3D apparently does not like when the event loop is never
pupmped (because the application returns right away from main()) and
tends to deadlock upon cleanup. Worked around with a processEvents in
the example, needs a better solution later.
The API is revised somewhat when compared to 3DS1. Fixed frameUpdate
naming, now uses normal Qt patterns for create-destroy, etc.
Change-Id: Iee0c39483831b68fbc1889922d54b3cc5e8726e2
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'examples')
11 files changed, 618 insertions, 0 deletions
diff --git a/examples/3dstudioruntime2/3dstudioruntime2.pro b/examples/3dstudioruntime2/3dstudioruntime2.pro index 267749d..9f1b2c5 100644 --- a/examples/3dstudioruntime2/3dstudioruntime2.pro +++ b/examples/3dstudioruntime2/3dstudioruntime2.pro @@ -1,5 +1,9 @@ TEMPLATE = subdirs +SUBDIRS += \ + simplewindow \ + simpleoffscreen + qtHaveModule(quick) { SUBDIRS += simpleqml } diff --git a/examples/3dstudioruntime2/simpleoffscreen/main.cpp b/examples/3dstudioruntime2/simpleoffscreen/main.cpp new file mode 100644 index 0000000..b7594dc --- /dev/null +++ b/examples/3dstudioruntime2/simpleoffscreen/main.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <q3dsruntimeglobal.h> +#include <Q3DSSurfaceViewer> +#include <Q3DSPresentation> +#include <QGuiApplication> +#include <QOffscreenSurface> +#include <QOpenGLContext> +#include <QOpenGLFramebufferObject> +#include <QThread> + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + QSurfaceFormat::setDefaultFormat(Q3DS::surfaceFormat()); + + QOpenGLContext context; + if (!context.create()) + qFatal("Failed to create OpenGL context"); + + // Must have a "surface". Depending on the platform this may be a small + // window, pbuffer surface, or even nothing (in case surfaceless contexts are + // supported) under the hood. + QOffscreenSurface surface; + surface.create(); + + if (!context.makeCurrent(&surface)) + qFatal("makeCurrent failed"); + + const QSize size(1280, 800); + QOpenGLFramebufferObject fbo(size, QOpenGLFramebufferObject::CombinedDepthStencil); + + Q3DSSurfaceViewer viewer; + viewer.presentation()->setSource(QUrl(QStringLiteral("qrc:/barrel.uip"))); + + // Will call update() manually when a new frame is wanted. + viewer.setUpdateInterval(-1); + + // Automatic sizing must be turned off. + viewer.setAutoSize(false); + + // Provide the render target size instead. + viewer.setSize(size); + + if (!viewer.create(&surface, &context, fbo.handle())) + qFatal("Initialization failed"); + + // Render a few frames, read them back, and save into png files. + for (int frame = 1; frame <= 20; ++frame) { + // Render the next frame. + viewer.update(); + + // Write it to a file. + const QString fn = QString(QLatin1String("output_%1.png")).arg(frame); + fbo.toImage().save(fn); + + qDebug("Rendered and saved frame %d to %s", frame, qPrintable(fn)); + + // ### hack until QT3DS-1041 is in place + QThread::msleep(10); + } + + // Now one could do the following to generate a 60 fps video from these frames: + // ffmpeg -r 60 -f image2 -s 1280x800 -i output_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p output.mp4 + + // ### this should not be needed. Qt3D should be able to shut down without it. + QGuiApplication::processEvents(); + + return 0; +} diff --git a/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.pro b/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.pro new file mode 100644 index 0000000..e4751db --- /dev/null +++ b/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +QT += 3dstudioruntime2 + +SOURCES += \ + main.cpp + +RESOURCES += simpleoffscreen.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/3dstudioruntime2/$$TARGET +INSTALLS += target diff --git a/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.qrc b/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.qrc new file mode 100644 index 0000000..4558e47 --- /dev/null +++ b/examples/3dstudioruntime2/simpleoffscreen/simpleoffscreen.qrc @@ -0,0 +1,11 @@ +<RCC> + <qresource prefix="/"> + <file alias="barrel.uip">../simpleqml/presentation/barrel.uip</file> + <file alias="barrel/meshes/Barrel.mesh">../simpleqml/presentation/barrel/meshes/Barrel.mesh</file> + <file alias="fonts/Arimo-Regular.ttf">../simpleqml/presentation/fonts/Arimo-Regular.ttf</file> + <file alias="maps/barrel_barrel_Diffuse.png">../simpleqml/presentation/maps/barrel_barrel_Diffuse.png</file> + <file alias="maps/barrel_barrel_Emissive.png">../simpleqml/presentation/maps/barrel_barrel_Emissive.png</file> + <file alias="maps/barrel_barrel_Normal.png">../simpleqml/presentation/maps/barrel_barrel_Normal.png</file> + <file alias="maps/barrel_barrel_Specular.png">../simpleqml/presentation/maps/barrel_barrel_Specular.png</file> + </qresource> +</RCC> diff --git a/examples/3dstudioruntime2/simplewindow/main.cpp b/examples/3dstudioruntime2/simplewindow/main.cpp new file mode 100644 index 0000000..c335b96 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/main.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> +#include <QCommandLineParser> +#include <QDebug> + +#include <q3dsruntimeglobal.h> +#include <Q3DSSurfaceViewer> +#include <Q3DSPresentation> + +#include "window_manualupdate.h" +#include "window_autoupdate.h" + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + QSurfaceFormat::setDefaultFormat(Q3DS::surfaceFormat()); + + QCommandLineParser parser; + QCommandLineOption multiWindowOption("multi", "Multiple windows"); + parser.addOption(multiWindowOption); + parser.process(app); + + ManualUpdateWindow w; + w.setTitle(QLatin1String("Qt 3D Studio plain QWindow Example")); + Q3DSSurfaceViewer viewer; + QObject::connect(&viewer, &Q3DSSurfaceViewer::errorChanged, &viewer, [&viewer] { + const QString msg = viewer.error(); + if (!msg.isEmpty()) + qWarning() << msg; + }); + viewer.presentation()->setSource(QUrl(QLatin1String("qrc:/barrel.uip"))); + viewer.create(&w, w.context()); + w.setViewer(&viewer); + w.resize(1024, 768); + w.show(); + + QScopedPointer<AutoUpdateWindow> w2; + QScopedPointer<Q3DSSurfaceViewer> viewer2; + if (parser.isSet(multiWindowOption)) { + w2.reset(new AutoUpdateWindow); + viewer2.reset(new Q3DSSurfaceViewer); + viewer2->presentation()->setSource(QUrl(QLatin1String("qrc:/barrel.uip"))); + // change from the default -1 (auto-update disabled) to an interval of + // 0 since AutoUpdateWindow does not do anything on its own that would + // lead to calling update() + viewer2->setUpdateInterval(0); + viewer2->create(w2.data(), w2->context()); + w2->resize(800, 600); + w2->show(); + } + + return app.exec(); +} diff --git a/examples/3dstudioruntime2/simplewindow/simplewindow.pro b/examples/3dstudioruntime2/simplewindow/simplewindow.pro new file mode 100644 index 0000000..cf4e8ce --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/simplewindow.pro @@ -0,0 +1,17 @@ +TEMPLATE = app + +QT += 3dstudioruntime2 + +SOURCES += \ + main.cpp \ + window_manualupdate.cpp \ + window_autoupdate.cpp + +HEADERS += \ + window_manualupdate.h \ + window_autoupdate.h + +RESOURCES += simplewindow.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/3dstudioruntime2/$$TARGET +INSTALLS += target diff --git a/examples/3dstudioruntime2/simplewindow/simplewindow.qrc b/examples/3dstudioruntime2/simplewindow/simplewindow.qrc new file mode 100644 index 0000000..4558e47 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/simplewindow.qrc @@ -0,0 +1,11 @@ +<RCC> + <qresource prefix="/"> + <file alias="barrel.uip">../simpleqml/presentation/barrel.uip</file> + <file alias="barrel/meshes/Barrel.mesh">../simpleqml/presentation/barrel/meshes/Barrel.mesh</file> + <file alias="fonts/Arimo-Regular.ttf">../simpleqml/presentation/fonts/Arimo-Regular.ttf</file> + <file alias="maps/barrel_barrel_Diffuse.png">../simpleqml/presentation/maps/barrel_barrel_Diffuse.png</file> + <file alias="maps/barrel_barrel_Emissive.png">../simpleqml/presentation/maps/barrel_barrel_Emissive.png</file> + <file alias="maps/barrel_barrel_Normal.png">../simpleqml/presentation/maps/barrel_barrel_Normal.png</file> + <file alias="maps/barrel_barrel_Specular.png">../simpleqml/presentation/maps/barrel_barrel_Specular.png</file> + </qresource> +</RCC> diff --git a/examples/3dstudioruntime2/simplewindow/window_autoupdate.cpp b/examples/3dstudioruntime2/simplewindow/window_autoupdate.cpp new file mode 100644 index 0000000..927f064 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/window_autoupdate.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "window_autoupdate.h" +#include <QOpenGLContext> + +// Here the window implementation relies on rendering happening automatically +// with some interval whenever isExposed()==true. + +// No event handlers in this version, so the 3D Studio scene is non-interactive. + +AutoUpdateWindow::AutoUpdateWindow() +{ + setSurfaceType(QSurface::OpenGLSurface); + m_context = new QOpenGLContext(this); + m_context->create(); +} diff --git a/examples/3dstudioruntime2/simplewindow/window_autoupdate.h b/examples/3dstudioruntime2/simplewindow/window_autoupdate.h new file mode 100644 index 0000000..7820cd8 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/window_autoupdate.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_AUTOUPDATE_H +#define WINDOW_AUTOUPDATE_H + +#include <QWindow> + +QT_BEGIN_NAMESPACE +class QOpenGLContext; +QT_END_NAMESPACE + +class AutoUpdateWindow : public QWindow +{ +public: + AutoUpdateWindow(); + QOpenGLContext *context() const { return m_context; } + +private: + QOpenGLContext *m_context; +}; + +#endif diff --git a/examples/3dstudioruntime2/simplewindow/window_manualupdate.cpp b/examples/3dstudioruntime2/simplewindow/window_manualupdate.cpp new file mode 100644 index 0000000..d796148 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/window_manualupdate.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "window_manualupdate.h" +#include <QOpenGLContext> +#include <Q3DSSurfaceViewer> +#include <Q3DSPresentation> + +// Here the window implementation drives rendering by manually calling +// Q3DSSurfaceViewer::update() as seen fit. + +// It also relies on the default of autoSize==true (meaning the rendering +// is automatically following the QWindow size). + +ManualUpdateWindow::ManualUpdateWindow() +{ + setSurfaceType(QSurface::OpenGLSurface); + m_context = new QOpenGLContext(this); + m_context->create(); +} + +void ManualUpdateWindow::update() +{ + if (isExposed() && m_viewer) { + m_viewer->update(); + // render continuously + requestUpdate(); + } +} + +bool ManualUpdateWindow::event(QEvent *e) +{ + if (e->type() == QEvent::Expose || e->type() == QEvent::UpdateRequest) + update(); + + return QWindow::event(e); +} + +void ManualUpdateWindow::keyPressEvent(QKeyEvent *e) +{ + m_viewer->presentation()->keyPressEvent(e); +} + +void ManualUpdateWindow::keyReleaseEvent(QKeyEvent *e) +{ + m_viewer->presentation()->keyReleaseEvent(e); +} + +void ManualUpdateWindow::mousePressEvent(QMouseEvent *e) +{ + m_viewer->presentation()->mousePressEvent(e); +} + +void ManualUpdateWindow::mouseMoveEvent(QMouseEvent *e) +{ + // Moves must be forwarded even when no button is down; the + // default QWindow behavior is just what we need in this respect. + m_viewer->presentation()->mouseMoveEvent(e); +} + +void ManualUpdateWindow::mouseReleaseEvent(QMouseEvent *e) +{ + m_viewer->presentation()->mouseReleaseEvent(e); +} + +void ManualUpdateWindow::mouseDoubleClickEvent(QMouseEvent *e) +{ + m_viewer->presentation()->mouseDoubleClickEvent(e); +} + +#if QT_CONFIG(wheelevent) +void ManualUpdateWindow::wheelEvent(QWheelEvent *e) +{ + m_viewer->presentation()->wheelEvent(e); +} +#endif diff --git a/examples/3dstudioruntime2/simplewindow/window_manualupdate.h b/examples/3dstudioruntime2/simplewindow/window_manualupdate.h new file mode 100644 index 0000000..7f3cb58 --- /dev/null +++ b/examples/3dstudioruntime2/simplewindow/window_manualupdate.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_MANUALUPDATE_H +#define WINDOW_MANUALUPDATE_H + +#include <QWindow> + +QT_BEGIN_NAMESPACE +class Q3DSSurfaceViewer; +class QOpenGLContext; +QT_END_NAMESPACE + +class ManualUpdateWindow : public QWindow +{ +public: + ManualUpdateWindow(); + void setViewer(Q3DSSurfaceViewer *viewer) { m_viewer = viewer; } + QOpenGLContext *context() const { return m_context; } + +protected: + bool event(QEvent *) override; + void update(); + + void keyPressEvent(QKeyEvent *) override; + void keyReleaseEvent(QKeyEvent *) override; + void mousePressEvent(QMouseEvent *) override; + void mouseMoveEvent(QMouseEvent *) override; + void mouseReleaseEvent(QMouseEvent *) override; + void mouseDoubleClickEvent(QMouseEvent *) override; +#if QT_CONFIG(wheelevent) + void wheelEvent(QWheelEvent *) override; +#endif + +private: + Q3DSSurfaceViewer *m_viewer = nullptr; + QOpenGLContext *m_context; +}; + +#endif |