summaryrefslogtreecommitdiffstats
path: root/examples/qt3d/dynamicscene-cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/qt3d/dynamicscene-cpp')
-rw-r--r--examples/qt3d/dynamicscene-cpp/boxentity.cpp108
-rw-r--r--examples/qt3d/dynamicscene-cpp/boxentity.h84
-rw-r--r--examples/qt3d/dynamicscene-cpp/dynamicscene-cpp.pro16
-rw-r--r--examples/qt3d/dynamicscene-cpp/examplescene.cpp87
-rw-r--r--examples/qt3d/dynamicscene-cpp/examplescene.h66
-rw-r--r--examples/qt3d/dynamicscene-cpp/forwardrenderer.cpp56
-rw-r--r--examples/qt3d/dynamicscene-cpp/forwardrenderer.h58
-rw-r--r--examples/qt3d/dynamicscene-cpp/main.cpp90
8 files changed, 565 insertions, 0 deletions
diff --git a/examples/qt3d/dynamicscene-cpp/boxentity.cpp b/examples/qt3d/dynamicscene-cpp/boxentity.cpp
new file mode 100644
index 000000000..a3f539564
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/boxentity.cpp
@@ -0,0 +1,108 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "boxentity.h"
+
+#include <qmath.h>
+
+BoxEntity::BoxEntity(QNode *parent)
+ : Qt3D::QEntity(parent)
+ , m_transform(new Qt3D::QTransform())
+ , m_translate(new Qt3D::QTranslateTransform())
+ , m_mesh(new Qt3D::QCuboidMesh())
+ , m_material(new Qt3D::QPhongMaterial())
+ , m_angle(0.0f)
+ , m_radius(1.0f)
+{
+ m_transform->addTransform(m_translate);
+
+ connect(m_material, SIGNAL(diffuseChanged()), this, SIGNAL(diffuseColorChanged()));
+ m_material->setAmbient(Qt::gray);
+ m_material->setSpecular(Qt::white);
+ m_material->setShininess(150.0f);
+
+ addComponent(m_transform);
+ addComponent(m_mesh);
+ addComponent(m_material);
+}
+
+void BoxEntity::setDiffuseColor(const QColor &diffuseColor)
+{
+ m_material->setDiffuse(diffuseColor);
+}
+
+void BoxEntity::setAngle(float arg)
+{
+ if (m_angle == arg)
+ return;
+
+ m_angle = arg;
+ emit angleChanged();
+ updateTransformation();
+}
+
+void BoxEntity::setRadius(float arg)
+{
+ if (m_radius == arg)
+ return;
+
+ m_radius = arg;
+ emit radiusChanged();
+ updateTransformation();
+}
+
+QColor BoxEntity::diffuseColor()
+{
+ return m_material->diffuse();
+}
+
+float BoxEntity::angle() const
+{
+ return m_angle;
+}
+
+float BoxEntity::radius() const
+{
+ return m_radius;
+}
+
+void BoxEntity::updateTransformation()
+{
+ m_translate->setTranslation(QVector3D(qCos(m_angle) * m_radius,
+ 1.0f,
+ qSin(m_angle) * m_radius));
+}
+
diff --git a/examples/qt3d/dynamicscene-cpp/boxentity.h b/examples/qt3d/dynamicscene-cpp/boxentity.h
new file mode 100644
index 000000000..6dcf81aff
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/boxentity.h
@@ -0,0 +1,84 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef BOXENTITY_H
+#define BOXENTITY_H
+
+#include <Qt3DCore/QEntity>
+#include <Qt3DCore/QTransform>
+#include <Qt3DCore/QTranslateTransform>
+#include <Qt3DCore/QScaleTransform>
+#include <Qt3DRenderer/QCuboidMesh>
+#include <Qt3DRenderer/QPhongMaterial>
+
+class BoxEntity : public Qt3D::QEntity
+{
+ Q_OBJECT
+
+ Q_PROPERTY(QColor diffuseColor READ diffuseColor WRITE setDiffuseColor NOTIFY diffuseColorChanged)
+ Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged)
+ Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)
+
+public:
+ BoxEntity(QNode *parent = 0);
+
+ QColor diffuseColor();
+ float angle() const;
+ float radius() const;
+
+public Q_SLOTS:
+ void setDiffuseColor(const QColor &diffuseColor);
+ void setAngle(float arg);
+ void setRadius(float arg);
+
+Q_SIGNALS:
+ void diffuseColorChanged();
+ void angleChanged();
+ void radiusChanged();
+
+private:
+ void updateTransformation();
+
+ Qt3D::QTransform *m_transform;
+ Qt3D::QTranslateTransform *m_translate;
+ Qt3D::QScaleTransform *m_scale;
+ Qt3D::QCuboidMesh *m_mesh;
+ Qt3D::QPhongMaterial *m_material;
+ float m_angle;
+ float m_radius;
+};
+
+#endif // BOXENTITY_H
diff --git a/examples/qt3d/dynamicscene-cpp/dynamicscene-cpp.pro b/examples/qt3d/dynamicscene-cpp/dynamicscene-cpp.pro
new file mode 100644
index 000000000..715460121
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/dynamicscene-cpp.pro
@@ -0,0 +1,16 @@
+TEMPLATE = app
+
+QT += 3dcore 3drenderer 3dinput
+
+include("../exampleresources/exampleresources.pri")
+
+SOURCES += \
+ main.cpp \
+ examplescene.cpp \
+ forwardrenderer.cpp \
+ boxentity.cpp
+
+HEADERS += \
+ examplescene.h \
+ forwardrenderer.h \
+ boxentity.h
diff --git a/examples/qt3d/dynamicscene-cpp/examplescene.cpp b/examples/qt3d/dynamicscene-cpp/examplescene.cpp
new file mode 100644
index 000000000..ca15b1a9f
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/examplescene.cpp
@@ -0,0 +1,87 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "examplescene.h"
+#include "boxentity.h"
+
+#include <QTimer>
+#include <qmath.h>
+
+ExampleScene::ExampleScene(Qt3D::QNode *parent)
+ : Qt3D::QEntity(parent)
+ , m_timer(new QTimer(this))
+ , m_even(true)
+{
+ buildScene();
+
+ QObject::connect(m_timer, SIGNAL(timeout()), SLOT(updateScene()));
+ m_timer->setInterval(500);
+ m_timer->start();
+}
+
+ExampleScene::~ExampleScene()
+{
+ qDeleteAll(m_entities);
+}
+
+void ExampleScene::updateScene()
+{
+ int i = 0;
+ Q_FOREACH (BoxEntity *entity, m_entities) {
+ if (i % 2 == 0)
+ entity->setParent(m_even ? Q_NULLPTR : this);
+ else
+ entity->setParent(m_even ? this : Q_NULLPTR);
+ ++i;
+ }
+ m_even = !m_even;
+}
+
+void ExampleScene::buildScene()
+{
+ int count = 20;
+ const float radius = 5.0f;
+
+ for (int i = 0; i < count; ++i) {
+ BoxEntity *entity = new BoxEntity;
+ const float angle = M_PI * 2.0f * float(i) / count;
+ entity->setAngle(angle);
+ entity->setRadius(radius);
+ entity->setDiffuseColor(QColor(qFabs(qCos(angle)) * 255, 204, 75));
+ m_entities.append(entity);
+ }
+}
+
diff --git a/examples/qt3d/dynamicscene-cpp/examplescene.h b/examples/qt3d/dynamicscene-cpp/examplescene.h
new file mode 100644
index 000000000..79351fe3e
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/examplescene.h
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef EXAMPLESCENE_H
+#define EXAMPLESCENE_H
+
+#include <Qt3DCore/QEntity>
+
+class BoxEntity;
+
+QT_BEGIN_NAMESPACE
+class QTimer;
+QT_END_NAMESPACE
+
+class ExampleScene : public Qt3D::QEntity
+{
+ Q_OBJECT
+
+public:
+ explicit ExampleScene(Qt3D::QNode *parent = 0);
+ ~ExampleScene();
+
+private Q_SLOTS:
+ void updateScene();
+ void buildScene();
+
+private:
+ QVector<BoxEntity *> m_entities;
+ QTimer *m_timer;
+ bool m_even;
+};
+
+#endif // EXAMPLESCENE_H
diff --git a/examples/qt3d/dynamicscene-cpp/forwardrenderer.cpp b/examples/qt3d/dynamicscene-cpp/forwardrenderer.cpp
new file mode 100644
index 000000000..3c4fd0758
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/forwardrenderer.cpp
@@ -0,0 +1,56 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "forwardrenderer.h"
+
+ForwardRenderer::ForwardRenderer(Qt3D::QNode *parent)
+ : Qt3D::QFrameGraph(parent)
+ , m_viewport(new Qt3D::QViewport())
+ , m_cameraSelector(new Qt3D::QCameraSelector())
+ , m_clearBuffer(new Qt3D::QClearBuffer())
+{
+ m_viewport->setRect(QRectF(0.0f, 0.0f, 1.0f, 1.0f));
+ m_viewport->setClearColor(Qt::white);
+ m_clearBuffer->setBuffers(Qt3D::QClearBuffer::ColorDepthBuffer);
+ m_cameraSelector->setParent(m_viewport);
+ m_clearBuffer->setParent(m_cameraSelector);
+ setActiveFrameGraph(m_viewport);
+}
+
+void ForwardRenderer::setCamera(Qt3D::QEntity *camera)
+{
+ m_cameraSelector->setCamera(camera);
+}
diff --git a/examples/qt3d/dynamicscene-cpp/forwardrenderer.h b/examples/qt3d/dynamicscene-cpp/forwardrenderer.h
new file mode 100644
index 000000000..faf6389f9
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/forwardrenderer.h
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef FORWARDRENDERER_H
+#define FORWARDRENDERER_H
+
+#include <Qt3DRenderer/QFrameGraph>
+#include <Qt3DRenderer/QViewport>
+#include <Qt3DRenderer/QCameraSelector>
+#include <Qt3DRenderer/QClearBuffer>
+
+class ForwardRenderer : public Qt3D::QFrameGraph
+{
+public:
+ ForwardRenderer(Qt3D::QNode *parent);
+
+ void setCamera(Qt3D::QEntity *camera);
+
+private:
+ Qt3D::QViewport *m_viewport;
+ Qt3D::QCameraSelector *m_cameraSelector;
+ Qt3D::QClearBuffer *m_clearBuffer;
+};
+
+#endif // FORWARDRENDERER_H
diff --git a/examples/qt3d/dynamicscene-cpp/main.cpp b/examples/qt3d/dynamicscene-cpp/main.cpp
new file mode 100644
index 000000000..8d265ec33
--- /dev/null
+++ b/examples/qt3d/dynamicscene-cpp/main.cpp
@@ -0,0 +1,90 @@
+/****************************************************************************
+**
+** 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:LGPL3$
+** 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 http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/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 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later 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 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QGuiApplication>
+
+#include <Qt3DCore/QAspectEngine>
+#include <Qt3DCore/QCamera>
+#include <Qt3DCore/Window>
+
+#include <Qt3DInput/QInputAspect>
+
+#include <Qt3DRenderer/QRenderAspect>
+#include <Qt3DRenderer/QFrameGraph>
+#include <Qt3DRenderer/QForwardRenderer>
+
+#include "forwardrenderer.h"
+#include "examplescene.h"
+
+int main(int argc, char* argv[])
+{
+ QGuiApplication app(argc, argv);
+
+ Qt3D::Window view;
+ Qt3D::QAspectEngine engine;
+ engine.registerAspect(new Qt3D::QRenderAspect());
+ Qt3D::QInputAspect *input = new Qt3D::QInputAspect;
+ engine.registerAspect(input);
+ engine.initialize();
+ QVariantMap data;
+ data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(&view)));
+ data.insert(QStringLiteral("eventSource"), QVariant::fromValue(&view));
+ engine.setData(data);
+
+ ExampleScene *sceneRoot = new ExampleScene();
+
+ // Scene Camera
+ Qt3D::QCamera *basicCamera = new Qt3D::QCamera(sceneRoot);
+ basicCamera->setProjectionType(Qt3D::QCameraLens::PerspectiveProjection);
+ basicCamera->setAspectRatio(view.width() / view.height());
+ basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
+ basicCamera->setViewCenter(QVector3D(0.0f, 3.5f, 0.0f));
+ basicCamera->setPosition(QVector3D(0.0f, 3.5f, 25.0f));
+ // For camera controls
+ input->setCamera(basicCamera);
+
+ // Forward Renderer FrameGraph
+ Qt3D::QFrameGraph *frameGraph = new Qt3D::QFrameGraph(sceneRoot);
+ Qt3D::QForwardRenderer *forwardRenderer = new Qt3D::QForwardRenderer();
+ forwardRenderer->setCamera(basicCamera);
+ frameGraph->setActiveFrameGraph(forwardRenderer);
+ sceneRoot->addComponent(frameGraph);
+
+ engine.setRootEntity(sceneRoot);
+ view.show();
+
+ return app.exec();
+}