Add a Scene3D item to wrap Qt3D scenes in QQ2

Comes with an example to try the embedding and check that
transformations work on the Scene3D element.

Change-Id: I93b4690b7fac0c9c90f8a89c53ad6b83aa2c89a6
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Kevin Ottens 2015-01-18 12:07:58 +01:00 committed by Sean Harmer
parent 971063f7ad
commit 67ffbd60ed
13 changed files with 717 additions and 1 deletions

View File

@ -25,7 +25,8 @@ SUBDIRS += \
loader-qml \
wave \
materials-cpp \
dynamicscene-cpp
dynamicscene-cpp \
scene3d
# TODO Port the old examples to new APIs
#SUBDIRS += qt3d

View File

@ -0,0 +1,139 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
import Qt3D 2.0
import Qt3D.Render 2.0
import QtQuick 2.0 as QQ2
Entity {
id: sceneRoot
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 45
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 0.0, 0.0, -40.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
components: [
FrameGraph {
activeFrameGraph: Viewport {
id: viewport
rect: Qt.rect(0.0, 0.0, 1.0, 1.0) // From Top Left
clearColor: Qt.rgba(0, 0.5, 1, 1)
CameraSelector {
id : cameraSelector
camera: camera
ClearBuffer {
buffers : ClearBuffer.ColorDepthBuffer
}
}
}
}
]
PhongMaterial {
id: material
}
TorusMesh {
id: torusMesh
radius: 5
minorRadius: 1
rings: 100
slices: 20
}
Transform {
id: torusTransform
Scale { scale3D: Qt.vector3d(1.5, 1, 0.5) }
Rotate {
angle: 45
axis: Qt.vector3d(1, 0, 0)
}
}
Entity {
id: torusEntity
components: [ torusMesh, material, torusTransform ]
}
SphereMesh {
id: sphereMesh
radius: 3
}
Transform {
id: sphereTransform
Translate {
translation: Qt.vector3d(20, 0, 0)
}
Rotate {
id: sphereRotation
axis: Qt.vector3d(0, 1, 0)
QQ2.NumberAnimation {
target: sphereRotation
property: "angle"
duration: 10000
from: 0
to: 360
loops: QQ2.Animation.Infinite
running: true
}
}
}
Entity {
id: sphereEntity
components: [ sphereMesh, material, sphereTransform ]
}
}

61
examples/scene3d/main.cpp Normal file
View File

@ -0,0 +1,61 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QQuickView view;
QSurfaceFormat f = view.format();
f.setVersion(4, 5);
f.setProfile(QSurfaceFormat::CoreProfile);
view.setFormat(f);
view.resize(500, 500);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}

117
examples/scene3d/main.qml Normal file
View File

@ -0,0 +1,117 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.0
import QtQuick.Scene3D 2.0
Item {
Text {
text: "Click me!"
anchors.top: parent.top
anchors.topMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
MouseArea {
anchors.fill: parent
onClicked: animation.start()
}
}
Rectangle {
id: scene
width: Math.min(parent.width, parent.height) - 100
height: width
anchors.centerIn: parent
color: "darkRed"
transform: Rotation {
id: sceneRotation
axis.x: 1
axis.y: 0
axis.z: 0
origin.x: scene.width / 2
origin.y: scene.height / 2
}
Scene3D {
anchors.fill: parent
anchors.margins: 10
AnimatedEntity {}
}
}
SequentialAnimation {
id: animation
RotationAnimation {
to: 45
duration: 1000
target: sceneRotation
property: "angle"
easing.type: Easing.InOutQuad
}
PauseAnimation { duration: 500 }
NumberAnimation {
to: 0.5
duration: 1000
target: scene
property: "scale"
easing.type: Easing.OutElastic
}
PauseAnimation { duration: 500 }
NumberAnimation {
to: 1.0
duration: 1000
target: scene
property: "scale"
easing.type: Easing.OutElastic
}
PauseAnimation { duration: 500 }
RotationAnimation {
to: 0
duration: 1000
target: sceneRotation
property: "angle"
easing.type: Easing.InOutQuad
}
}
}

View File

@ -0,0 +1,12 @@
TEMPLATE = app
QT += qml quick
SOURCES += \
main.cpp
OTHER_FILES += \
AnimatedEntity.qml \
main.qml
RESOURCES += \
scene3d.qrc

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>AnimatedEntity.qml</file>
<file>main.qml</file>
</qresource>
</RCC>

View File

@ -0,0 +1,17 @@
CXX_MODULE = qml
TARGET = qtquickscene3dplugin
TARGETPATH = QtQuick/Scene3D
QT += qml quick 3dcore 3drenderer
HEADERS += \
qtquickscene3dplugin.h \
scene3ditem.h
SOURCES += \
qtquickscene3dplugin.cpp \
scene3ditem.cpp
OTHER_FILES += qmldir
load(qml_plugin)

View File

@ -0,0 +1,3 @@
module QtQuick.Scene3D
plugin qtquickscene3dplugin
classname QtQuickScene3DPlugin

View File

@ -0,0 +1,53 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtQml>
#include "qtquickscene3dplugin.h"
#include "scene3ditem.h"
QT_BEGIN_NAMESPACE
void QtQuickScene3DPlugin::registerTypes(const char *uri)
{
qmlRegisterType<Scene3DItem>(uri, 2, 0, "Scene3D");
}
QT_END_NAMESPACE

View File

@ -0,0 +1,59 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QTQUICKSCENE3DPLUGIN_H
#define QTQUICKSCENE3DPLUGIN_H
#include <QtQml/QQmlExtensionPlugin>
QT_BEGIN_NAMESPACE
class QtQuickScene3DPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface/1.0")
public:
void registerTypes(const char *uri) Q_DECL_OVERRIDE;
};
QT_END_NAMESPACE
#endif // QTQUICKSCENE3DPLUGIN_H

View File

@ -0,0 +1,164 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "scene3ditem.h"
#include <Qt3DCore/QAspectEngine>
#include <Qt3DRenderer/QRenderAspect>
#include <QOpenGLContext>
#include <QOpenGLFramebufferObject>
#include <QOpenGLFramebufferObjectFormat>
#include <QScopedPointer>
#include <QSurface>
#include <QSGSimpleTextureNode>
QT_BEGIN_NAMESPACE
class ContextSaver
{
public:
explicit ContextSaver(QOpenGLContext *context = QOpenGLContext::currentContext())
: m_context(context),
m_surface(context ? context->surface() : Q_NULLPTR)
{
}
~ContextSaver()
{
if (m_context)
m_context->makeCurrent(m_surface);
}
QOpenGLContext *context() const { return m_context; }
QSurface *surface() const { return m_surface; }
private:
QOpenGLContext * const m_context;
QSurface * const m_surface;
};
class FrameBufferObjectRenderer : public QQuickFramebufferObject::Renderer
{
public:
FrameBufferObjectRenderer(const Scene3DItem *item)
: m_item(item),
m_aspectEngine(new Qt3D::QAspectEngine),
m_renderAspect(new Qt3D::QRenderAspect(Qt3D::QRenderAspect::Synchronous))
{
ContextSaver saver;
m_aspectEngine->registerAspect(m_renderAspect);
m_aspectEngine->initialize();
QVariantMap data;
data.insert(QStringLiteral("surface"), QVariant::fromValue(saver.surface()));
m_aspectEngine->setData(data);
m_renderAspect->renderInitialize(saver.context());
}
void render() Q_DECL_OVERRIDE
{
if (m_aspectEngine->rootEntity() != m_item->entity())
m_aspectEngine->setRootEntity(m_item->entity());
ContextSaver saver;
Q_UNUSED(saver)
m_renderAspect->renderSynchronous();
update();
}
QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) Q_DECL_OVERRIDE
{
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setSamples(4);
return new QOpenGLFramebufferObject(size, format);
}
const Scene3DItem *m_item;
QScopedPointer<Qt3D::QAspectEngine> m_aspectEngine;
Qt3D::QRenderAspect *m_renderAspect;
};
Scene3DItem::Scene3DItem(QQuickItem *parent)
: QQuickFramebufferObject(parent),
m_entity(Q_NULLPTR)
{
setFlag(QQuickItem::ItemHasContents, true);
}
Qt3D::QEntity *Scene3DItem::entity() const
{
return m_entity;
}
void Scene3DItem::setEntity(Qt3D::QEntity *entity)
{
if (entity == m_entity)
return;
m_entity = entity;
emit entityChanged();
}
QQuickFramebufferObject::Renderer *Scene3DItem::createRenderer() const
{
return new FrameBufferObjectRenderer(this);
}
QSGNode *Scene3DItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *nodeData)
{
if (!node) {
node = QQuickFramebufferObject::updatePaintNode(node, nodeData);
QSGSimpleTextureNode *textureNode = static_cast<QSGSimpleTextureNode *>(node);
if (textureNode)
textureNode->setTextureCoordinatesTransform(QSGSimpleTextureNode::MirrorVertically);
return node;
}
return QQuickFramebufferObject::updatePaintNode(node, nodeData);
}
QT_END_NAMESPACE

View File

@ -0,0 +1,79 @@
/****************************************************************************
**
** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef SCENE3DITEM_H
#define SCENE3DITEM_H
#include <QQuickFramebufferObject>
QT_BEGIN_NAMESPACE
namespace Qt3D
{
class QEntity;
}
class Scene3DItem : public QQuickFramebufferObject
{
Q_OBJECT
Q_PROPERTY(Qt3D::QEntity* entity READ entity WRITE setEntity NOTIFY entityChanged)
Q_CLASSINFO("DefaultProperty", "entity")
public:
explicit Scene3DItem(QQuickItem *parent = 0);
Qt3D::QEntity *entity() const;
public Q_SLOTS:
void setEntity(Qt3D::QEntity *entity);
Q_SIGNALS:
void entityChanged();
private:
Renderer *createRenderer() const Q_DECL_OVERRIDE;
QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *nodeData) Q_DECL_OVERRIDE;
Qt3D::QEntity *m_entity;
};
QT_END_NAMESPACE
#endif

View File

@ -38,6 +38,10 @@ src_quick3d_imports_render.file = $$PWD/quick3d/imports/render/importsrender.pro
src_quick3d_imports_render.target = sub-quick3d-imports-render
src_quick3d_imports_render.depends = src_quick3d_render
src_quick3d_imports_scene3d.file = $$PWD/quick3d/imports/scene3d/importsscene3d.pro
src_quick3d_imports_scene3d.target = sub-quick3d-imports-scene3d
src_quick3d_imports_scene3d.depends = src_quick3d_render
src_quick3d_imports_bulletphysics.file = $$PWD/quick3d/imports/bulletphysics/importsbulletphysics.pro
src_quick3d_imports_bulletphysics.target = sub-quick3d-imports-bulletphysics
src_quick3d_imports_bulletphysics.depends = src_bulletphysics
@ -60,6 +64,7 @@ SUBDIRS += \
src_quick3d_core_imports \
src_quick3d_render \
src_quick3d_imports_render \
src_quick3d_imports_scene3d \
src_quick3d_imports_input \
src_plugins_sceneparsers