summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMika Salmela <mika.salmela@theqtcompany.com>2015-06-10 13:00:38 +0300
committerSean Harmer <sean.harmer@kdab.com>2015-06-10 12:48:09 +0000
commit0eec20fc1e5ee4822bd8560eb6c3c551879528ea (patch)
tree1f6629a1db837bacdf499025a0d7113efdfbfc30
parent9db7beabdbaa0182cc9fd25603a790db293d56eb (diff)
Reshape torus-cpp example to be basicshapes-cpp
Leave the cylinder-cpp example to show minimal code for setting up a basic element. Reshape torus-cpp to show several shapes available. Change-Id: Ic9dd7cb54258b5eb442ee0492ef4fc2e4b30f836 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r--examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro (renamed from examples/qt3d/torus-cpp/torus-cpp.pro)7
-rw-r--r--examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc (renamed from examples/qt3d/torus-cpp/doc/src/torus-cpp.qdoc)4
-rw-r--r--examples/qt3d/basicshapes-cpp/main.cpp (renamed from examples/qt3d/torus-cpp/main.cpp)113
-rw-r--r--examples/qt3d/basicshapes-cpp/scenemodifier.cpp178
-rw-r--r--examples/qt3d/basicshapes-cpp/scenemodifier.h78
-rw-r--r--examples/qt3d/qt3d.pro4
6 files changed, 349 insertions, 35 deletions
diff --git a/examples/qt3d/torus-cpp/torus-cpp.pro b/examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro
index eeefc5c22..546715fca 100644
--- a/examples/qt3d/torus-cpp/torus-cpp.pro
+++ b/examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro
@@ -3,7 +3,12 @@
}
QT += 3dcore 3drenderer 3dinput
+QT += widgets
-SOURCES += main.cpp
+SOURCES += main.cpp \
+ scenemodifier.cpp
+
+HEADERS += \
+ scenemodifier.h
diff --git a/examples/qt3d/torus-cpp/doc/src/torus-cpp.qdoc b/examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc
index 76f6d9782..f408bc43b 100644
--- a/examples/qt3d/torus-cpp/doc/src/torus-cpp.qdoc
+++ b/examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc
@@ -26,7 +26,7 @@
****************************************************************************/
/*!
- \example torus-cpp
- \title Qt3D: Torus C++ Example
+ \example basicshapes-cpp
+ \title Qt3D: Basic Shapes C++ Example
\ingroup qt3d-examples-cpp
*/
diff --git a/examples/qt3d/torus-cpp/main.cpp b/examples/qt3d/basicshapes-cpp/main.cpp
index b2cf5b24b..3aa42f7f8 100644
--- a/examples/qt3d/torus-cpp/main.cpp
+++ b/examples/qt3d/basicshapes-cpp/main.cpp
@@ -34,6 +34,8 @@
**
****************************************************************************/
+#include "scenemodifier.h"
+
#include <QGuiApplication>
#include <Qt3DCore/window.h>
@@ -41,6 +43,13 @@
#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qcameralens.h>
+#include <QtWidgets/QApplication>
+#include <QtWidgets/QWidget>
+#include <QtWidgets/QHBoxLayout>
+#include <QtWidgets/QCheckBox>
+#include <QtWidgets/QCommandLinkButton>
+#include <QtGui/QScreen>
+
#include <Qt3DInput/QInputAspect>
#include <Qt3DRenderer/qtorusmesh.h>
@@ -64,17 +73,33 @@
int main(int argc, char **argv)
{
- QGuiApplication app(argc, argv);
+ QApplication app(argc, argv);
+ Qt3D::Window *view = new Qt3D::Window();
+ QWidget *container = QWidget::createWindowContainer(view);
+
+ QSize screenSize = view->screen()->size();
+ container->setMinimumSize(QSize(screenSize.width() / 2, screenSize.height() / 1.5));
+ container->setMaximumSize(screenSize);
+ container->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+ container->setFocusPolicy(Qt::StrongFocus);
+
+ QWidget *widget = new QWidget;
+ QHBoxLayout *hLayout = new QHBoxLayout(widget);
+ QVBoxLayout *vLayout = new QVBoxLayout();
+ vLayout->setAlignment(Qt::AlignTop);
+ hLayout->addWidget(container, 1);
+ hLayout->addLayout(vLayout);
+
+ widget->setWindowTitle(QStringLiteral("Basic shapes"));
- 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));
+ data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(view)));
+ data.insert(QStringLiteral("eventSource"), QVariant::fromValue(view));
engine.setData(data);
// Root entity
@@ -95,40 +120,68 @@ int main(int argc, char **argv)
Qt3D::QForwardRenderer *forwardRenderer = new Qt3D::QForwardRenderer();
forwardRenderer->setCamera(cameraEntity);
- forwardRenderer->setClearColor(Qt::black);
+ forwardRenderer->setClearColor(QColor(QRgb(0x4d4d4f)));
frameGraph->setActiveFrameGraph(forwardRenderer);
- // Torus shape data
- Qt3D::QTorusMesh *torus = new Qt3D::QTorusMesh();
- torus->setRadius(5);
- torus->setMinorRadius(1);
- torus->setRings(100);
- torus->setSlices(20);
-
- // TorusMesh Transform
- Qt3D::QScaleTransform *torusScale = new Qt3D::QScaleTransform();
- Qt3D::QRotateTransform *torusRotation = new Qt3D::QRotateTransform();
- Qt3D::QTransform *torusTransforms = new Qt3D::QTransform();
-
- torusScale->setScale3D(QVector3D(1.5f, 1.0f, 0.5f));
- torusRotation->setAngleDeg(45.0f);
- torusRotation->setAxis(QVector3D(1, 0, 0));
-
- torusTransforms->addTransform(torusScale);
- torusTransforms->addTransform(torusRotation);
-
- // Torus
- Qt3D::QEntity *torusEntity = new Qt3D::QEntity(rootEntity);
- torusEntity->addComponent(torus);
- torusEntity->addComponent(torusTransforms);
-
// Setting the FrameGraph
rootEntity->addComponent(frameGraph);
+ // Scenemodifier
+ SceneModifier *modifier = new SceneModifier(rootEntity);
+
// Set root object of the scene
engine.setRootEntity(rootEntity);
+
+ // Create control widgets
+ QCommandLinkButton *info = new QCommandLinkButton();
+ info->setText(QStringLiteral("Qt3D ready-made meshes"));
+ info->setDescription(QStringLiteral("Qt3D provides several ready-made meshes, like torus, cylinder, cube and sphere."));
+ info->setIconSize(QSize(0,0));
+
+ QCheckBox *torusCB = new QCheckBox(widget);
+ torusCB->setChecked(true);
+ torusCB->setText(QStringLiteral("Torus"));
+
+ QCheckBox *cylinderCB = new QCheckBox(widget);
+ cylinderCB->setChecked(true);
+ cylinderCB->setText(QStringLiteral("Cylinder"));
+
+ QCheckBox *cuboidCB = new QCheckBox(widget);
+ cuboidCB->setChecked(true);
+ cuboidCB->setText(QStringLiteral("Cuboid"));
+
+ QCheckBox *sphereCB = new QCheckBox(widget);
+ sphereCB->setChecked(true);
+ sphereCB->setText(QStringLiteral("Sphere"));
+
+ vLayout->addWidget(info);
+ vLayout->addWidget(torusCB);
+ vLayout->addWidget(cylinderCB);
+ vLayout->addWidget(cuboidCB);
+ vLayout->addWidget(sphereCB);
+
+ QObject::connect(torusCB, &QCheckBox::stateChanged,
+ modifier, &SceneModifier::enableTorus);
+ QObject::connect(cylinderCB, &QCheckBox::stateChanged,
+ modifier, &SceneModifier::enableCylinder);
+ QObject::connect(cuboidCB, &QCheckBox::stateChanged,
+ modifier, &SceneModifier::enableCuboid);
+ QObject::connect(sphereCB, &QCheckBox::stateChanged,
+ modifier, &SceneModifier::enableSphere);
+
+ torusCB->setChecked(true);
+ cylinderCB->setChecked(true);
+ cuboidCB->setChecked(true);
+ sphereCB->setChecked(true);
+
// Show window
- view.show();
+ widget->show();
+ widget->resize(1280, 600);
+
+ // Update the aspect ratio
+ QSize widgetSize = container->size();
+ float aspectRatio = float(widgetSize.width()) / float(widgetSize.height());
+ cameraEntity->lens()->setPerspectiveProjection(45.0f, aspectRatio, 0.1f, 1000.0f);
return app.exec();
}
diff --git a/examples/qt3d/basicshapes-cpp/scenemodifier.cpp b/examples/qt3d/basicshapes-cpp/scenemodifier.cpp
new file mode 100644
index 000000000..030db1596
--- /dev/null
+++ b/examples/qt3d/basicshapes-cpp/scenemodifier.cpp
@@ -0,0 +1,178 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 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 "scenemodifier.h"
+
+#include <QtCore/QDebug>
+
+SceneModifier::SceneModifier(Qt3D::QEntity *rootEntity)
+ : m_rootEntity(rootEntity)
+{
+ // Torus shape data
+ m_torus = new Qt3D::QTorusMesh();
+ m_torus->setRadius(1.0f);
+ m_torus->setMinorRadius(0.4f);
+ m_torus->setRings(100);
+ m_torus->setSlices(20);
+
+ // TorusMesh Transform
+ Qt3D::QScaleTransform *torusScale = new Qt3D::QScaleTransform();
+ Qt3D::QTranslateTransform *torusTranslation = new Qt3D::QTranslateTransform();
+ Qt3D::QRotateTransform *torusRotation = new Qt3D::QRotateTransform();
+ Qt3D::QTransform *torusTransforms = new Qt3D::QTransform();
+
+ torusScale->setScale3D(QVector3D(2.0f, 2.0f, 2.0f));
+ torusTranslation->setTranslation(QVector3D(1.7f, 1.7f, 0.0f));
+ torusRotation->setAngleDeg(25.0f);
+ torusRotation->setAxis(QVector3D(0, 1, 0));
+
+ torusTransforms->addTransform(torusRotation);
+ torusTransforms->addTransform(torusTranslation);
+ torusTransforms->addTransform(torusScale);
+
+ Qt3D::QPhongMaterial *torusMaterial = new Qt3D::QPhongMaterial();
+ torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));
+
+ // Torus
+ m_torusEntity = new Qt3D::QEntity(m_rootEntity);
+ m_torusEntity->addComponent(m_torus);
+ m_torusEntity->addComponent(torusMaterial);
+ m_torusEntity->addComponent(torusTransforms);
+
+ // Cylinder shape data
+ Qt3D::QCylinderMesh *cylinder = new Qt3D::QCylinderMesh();
+ cylinder->setRadius(1);
+ cylinder->setLength(3);
+ cylinder->setRings(100);
+ cylinder->setSlices(20);
+
+ // CylinderMesh Transform
+ Qt3D::QScaleTransform *cylinderScale = new Qt3D::QScaleTransform();
+ Qt3D::QRotateTransform *cylinderRotation = new Qt3D::QRotateTransform();
+ Qt3D::QTranslateTransform *cylinderTranslation = new Qt3D::QTranslateTransform();
+ Qt3D::QTransform *cylinderTransforms = new Qt3D::QTransform();
+
+ cylinderScale->setScale3D(QVector3D(1.5f, 1.5f, 1.5f));
+ cylinderTranslation->setTranslation(QVector3D(-2.6f, 2.4f, -1.5));
+ cylinderRotation->setAngleDeg(45.0f);
+ cylinderRotation->setAxis(QVector3D(1, 0, 0));
+
+ cylinderTransforms->addTransform(cylinderTranslation);
+ cylinderTransforms->addTransform(cylinderRotation);
+ cylinderTransforms->addTransform(cylinderScale);
+
+ Qt3D::QPhongMaterial *cylinderMaterial = new Qt3D::QPhongMaterial();
+ cylinderMaterial->setDiffuse(QColor(QRgb(0x928327)));
+
+ // Cylinder
+ m_cylinderEntity = new Qt3D::QEntity(m_rootEntity);
+ m_cylinderEntity->addComponent(cylinder);
+ m_cylinderEntity->addComponent(cylinderMaterial);
+ m_cylinderEntity->addComponent(cylinderTransforms);
+
+ // Cuboid shape data
+ Qt3D::QCuboidMesh *cuboid = new Qt3D::QCuboidMesh();
+
+ // CuboidMesh Transform
+ Qt3D::QScaleTransform *cuboidScale = new Qt3D::QScaleTransform();
+ Qt3D::QTranslateTransform *cuboidTranslation = new Qt3D::QTranslateTransform();
+ Qt3D::QTransform *cuboidTransforms = new Qt3D::QTransform();
+
+ cuboidScale->setScale3D(QVector3D(4.0f, 4.0f, 4.0f));
+ cuboidTranslation->setTranslation(QVector3D(0.9f, -0.9f, 0.0f));
+
+ cuboidTransforms->addTransform(cuboidTranslation);
+ cuboidTransforms->addTransform(cuboidScale);
+
+ Qt3D::QPhongMaterial *cuboidMaterial = new Qt3D::QPhongMaterial();
+ cuboidMaterial->setDiffuse(QColor(QRgb(0x665423)));
+
+ //Cuboid
+ m_cuboidEntity = new Qt3D::QEntity(m_rootEntity);
+ m_cuboidEntity->addComponent(cuboid);
+ m_cuboidEntity->addComponent(cuboidMaterial);
+ m_cuboidEntity->addComponent(cuboidTransforms);
+
+ // Sphere shape data
+ Qt3D::QSphereMesh *sphereMesh = new Qt3D::QSphereMesh();
+ sphereMesh->setRings(20);
+ sphereMesh->setSlices(20);
+ sphereMesh->setRadius(2);
+
+ // Sphere mesh transform
+ Qt3D::QScaleTransform *sphereScale = new Qt3D::QScaleTransform();
+ Qt3D::QTranslateTransform *sphereTranslation = new Qt3D::QTranslateTransform();
+ Qt3D::QTransform *sphereTransforms = new Qt3D::QTransform();
+
+ sphereScale->setScale3D(QVector3D(1.3f, 1.3f, 1.3f));
+ sphereTranslation->setTranslation(QVector3D(-2.9f, -2.9f, 0.0f));
+
+ sphereTransforms->addTransform(sphereTranslation);
+ sphereTransforms->addTransform(sphereScale);
+
+ Qt3D::QPhongMaterial *sphereMaterial = new Qt3D::QPhongMaterial();
+ sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));
+
+ // Sphere
+ m_sphereEntity = new Qt3D::QEntity(m_rootEntity);
+ m_sphereEntity->addComponent(sphereMesh);
+ m_sphereEntity->addComponent(sphereMaterial);
+ m_sphereEntity->addComponent(sphereTransforms);
+}
+
+SceneModifier::~SceneModifier()
+{
+}
+
+void SceneModifier::enableTorus(bool enabled)
+{
+ m_torusEntity->setParent(enabled ? m_rootEntity : Q_NULLPTR);
+}
+
+void SceneModifier::enableCylinder(bool enabled)
+{
+ m_cylinderEntity->setParent(enabled ? m_rootEntity : Q_NULLPTR);
+}
+
+void SceneModifier::enableCuboid(bool enabled)
+{
+ m_cuboidEntity->setParent(enabled ? m_rootEntity : Q_NULLPTR);
+}
+
+void SceneModifier::enableSphere(bool enabled)
+{
+ m_sphereEntity->setParent(enabled ? m_rootEntity : Q_NULLPTR);
+}
diff --git a/examples/qt3d/basicshapes-cpp/scenemodifier.h b/examples/qt3d/basicshapes-cpp/scenemodifier.h
new file mode 100644
index 000000000..ba4718401
--- /dev/null
+++ b/examples/qt3d/basicshapes-cpp/scenemodifier.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 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 SCENEMODIFIER_H
+#define SCENEMODIFIER_H
+
+#include <QtCore/QObject>
+
+#include <Qt3DCore/qentity.h>
+#include <Qt3DCore/qscaletransform.h>
+#include <Qt3DCore/qrotatetransform.h>
+#include <Qt3DCore/qtransform.h>
+#include <Qt3DCore/QTranslateTransform>
+
+#include <Qt3DRenderer/QTorusMesh>
+#include <Qt3DRenderer/QCylinderMesh>
+#include <Qt3DRenderer/QCuboidMesh>
+#include <Qt3DRenderer/QSphereMesh>
+#include <Qt3DRenderer/QPhongMaterial>
+
+class SceneModifier : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit SceneModifier(Qt3D::QEntity *rootEntity);
+ ~SceneModifier();
+
+public slots:
+ void enableTorus(bool enabled);
+ void enableCylinder(bool enabled);
+ void enableCuboid(bool enabled);
+ void enableSphere(bool enabled);
+
+private:
+ Qt3D::QEntity *m_rootEntity;
+ Qt3D::QTorusMesh *m_torus;
+ Qt3D::QEntity *m_cylinderEntity;
+ Qt3D::QEntity *m_torusEntity;
+ Qt3D::QEntity *m_cuboidEntity;
+ Qt3D::QEntity *m_sphereEntity;
+};
+
+#endif // SCENEMODIFIER_H
+
diff --git a/examples/qt3d/qt3d.pro b/examples/qt3d/qt3d.pro
index 5882ad38b..2894e4760 100644
--- a/examples/qt3d/qt3d.pro
+++ b/examples/qt3d/qt3d.pro
@@ -9,7 +9,6 @@ SUBDIRS += \
cpp_example \
multiviewport \
torus-qml \
- torus-cpp \
cylinder-qml \
cylinder-cpp \
bigmodel-qml \
@@ -34,4 +33,5 @@ SUBDIRS += \
# TODO Port the old examples to new APIs
#SUBDIRS += qt3d
-qtHaveModule(widgets): SUBDIRS += assimp-cpp
+qtHaveModule(widgets): SUBDIRS += assimp-cpp \
+ basicshapes-cpp