diff options
author | Mika Salmela <mika.salmela@theqtcompany.com> | 2015-06-10 13:00:38 +0300 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2015-06-10 12:48:09 +0000 |
commit | 0eec20fc1e5ee4822bd8560eb6c3c551879528ea (patch) | |
tree | 1f6629a1db837bacdf499025a0d7113efdfbfc30 /examples/qt3d/basicshapes-cpp | |
parent | 9db7beabdbaa0182cc9fd25603a790db293d56eb (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>
Diffstat (limited to 'examples/qt3d/basicshapes-cpp')
-rw-r--r-- | examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro | 14 | ||||
-rw-r--r-- | examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc | 32 | ||||
-rw-r--r-- | examples/qt3d/basicshapes-cpp/main.cpp | 187 | ||||
-rw-r--r-- | examples/qt3d/basicshapes-cpp/scenemodifier.cpp | 178 | ||||
-rw-r--r-- | examples/qt3d/basicshapes-cpp/scenemodifier.h | 78 |
5 files changed, 489 insertions, 0 deletions
diff --git a/examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro b/examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro new file mode 100644 index 000000000..546715fca --- /dev/null +++ b/examples/qt3d/basicshapes-cpp/basicshapes-cpp.pro @@ -0,0 +1,14 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +QT += 3dcore 3drenderer 3dinput +QT += widgets + +SOURCES += main.cpp \ + scenemodifier.cpp + +HEADERS += \ + scenemodifier.h + + diff --git a/examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc b/examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc new file mode 100644 index 000000000..f408bc43b --- /dev/null +++ b/examples/qt3d/basicshapes-cpp/doc/src/basicshapes.qdoc @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example basicshapes-cpp + \title Qt3D: Basic Shapes C++ Example + \ingroup qt3d-examples-cpp +*/ diff --git a/examples/qt3d/basicshapes-cpp/main.cpp b/examples/qt3d/basicshapes-cpp/main.cpp new file mode 100644 index 000000000..3aa42f7f8 --- /dev/null +++ b/examples/qt3d/basicshapes-cpp/main.cpp @@ -0,0 +1,187 @@ +/**************************************************************************** +** +** 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 <QGuiApplication> + +#include <Qt3DCore/window.h> +#include <Qt3DCore/qcamera.h> +#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> +#include <Qt3DRenderer/qmesh.h> +#include <Qt3DRenderer/qtechnique.h> +#include <Qt3DRenderer/qmaterial.h> +#include <Qt3DRenderer/qeffect.h> +#include <Qt3DRenderer/qtexture.h> +#include <Qt3DRenderer/qrenderpass.h> +#include <Qt3DRenderer/qsceneloader.h> + +#include <Qt3DCore/qscaletransform.h> +#include <Qt3DCore/qrotatetransform.h> +#include <Qt3DCore/qlookattransform.h> +#include <Qt3DCore/qtransform.h> +#include <Qt3DCore/qaspectengine.h> + +#include <Qt3DRenderer/qrenderaspect.h> +#include <Qt3DRenderer/qframegraph.h> +#include <Qt3DRenderer/qforwardrenderer.h> + +int main(int argc, char **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::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); + + // Root entity + Qt3D::QEntity *rootEntity = new Qt3D::QEntity(); + + // Camera + Qt3D::QCamera *cameraEntity = new Qt3D::QCamera(rootEntity); + cameraEntity->setObjectName(QStringLiteral("cameraEntity")); + + cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); + cameraEntity->setPosition(QVector3D(0, 0, -20.0f)); + cameraEntity->setUpVector(QVector3D(0, 1, 0)); + cameraEntity->setViewCenter(QVector3D(0, 0, 0)); + input->setCamera(cameraEntity); + + // FrameGraph + Qt3D::QFrameGraph *frameGraph = new Qt3D::QFrameGraph(); + Qt3D::QForwardRenderer *forwardRenderer = new Qt3D::QForwardRenderer(); + + forwardRenderer->setCamera(cameraEntity); + forwardRenderer->setClearColor(QColor(QRgb(0x4d4d4f))); + frameGraph->setActiveFrameGraph(forwardRenderer); + + // 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 + 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 + |