| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QGuiApplication> |
| 5 | |
| 6 | #include <Qt3DRender/qcamera.h> |
| 7 | #include <Qt3DRender/qgeometryrenderer.h> |
| 8 | #include <Qt3DExtras/qcylindermesh.h> |
| 9 | #include <Qt3DExtras/qspheremesh.h> |
| 10 | #include <Qt3DExtras/qphongmaterial.h> |
| 11 | |
| 12 | #include <Qt3DCore/qentity.h> |
| 13 | #include <Qt3DCore/qtransform.h> |
| 14 | |
| 15 | #include <Qt3DExtras/qt3dwindow.h> |
| 16 | #include <Qt3DExtras/qorbitcameracontroller.h> |
| 17 | |
| 18 | #include <QtGui/qquaternion.h> |
| 19 | |
| 20 | #include <QTimer> |
| 21 | |
| 22 | class ComponentSwapper : public QObject |
| 23 | { |
| 24 | Q_OBJECT |
| 25 | public: |
| 26 | ComponentSwapper(Qt3DCore::QEntity *entity, |
| 27 | Qt3DCore::QComponent *component1, |
| 28 | Qt3DCore::QComponent *component2, |
| 29 | QObject * parent = nullptr) |
| 30 | : QObject(parent) |
| 31 | , m_entity(entity) |
| 32 | , m_component1(component1) |
| 33 | , m_component2(component2) |
| 34 | , m_currentComponent(component1) |
| 35 | { |
| 36 | // Set initial state |
| 37 | m_entity->addComponent(m_component1); |
| 38 | } |
| 39 | |
| 40 | public slots: |
| 41 | void swapComponents() |
| 42 | { |
| 43 | if (m_currentComponent == m_component1) { |
| 44 | m_entity->removeComponent(m_component1); |
| 45 | m_entity->addComponent(m_component2); |
| 46 | m_currentComponent = m_component2; |
| 47 | } else { |
| 48 | m_entity->removeComponent(m_component2); |
| 49 | m_entity->addComponent(m_component1); |
| 50 | m_currentComponent = m_component1; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | Qt3DCore::QEntity *m_entity; |
| 56 | Qt3DCore::QComponent *m_component1; |
| 57 | Qt3DCore::QComponent *m_component2; |
| 58 | Qt3DCore::QComponent *m_currentComponent; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | int main(int argc, char **argv) |
| 63 | { |
| 64 | QGuiApplication app(argc, argv); |
| 65 | Qt3DExtras::Qt3DWindow view; |
| 66 | |
| 67 | // Root entity |
| 68 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 69 | |
| 70 | // Camera |
| 71 | Qt3DRender::QCamera *camera = view.camera(); |
| 72 | camera->setPosition(QVector3D(0, 0, 20.0f)); |
| 73 | camera->setViewCenter(QVector3D(0, 0, 0)); |
| 74 | |
| 75 | // For camera controls |
| 76 | Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 77 | cameraController->setCamera(camera); |
| 78 | |
| 79 | // Cylinder mesh data |
| 80 | Qt3DExtras::QCylinderMesh *cylinderMesh = new Qt3DExtras::QCylinderMesh(); |
| 81 | cylinderMesh->setRadius(1); |
| 82 | cylinderMesh->setLength(3); |
| 83 | cylinderMesh->setRings(5); |
| 84 | cylinderMesh->setSlices(40); |
| 85 | |
| 86 | // Sphere mesh data |
| 87 | Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh(); |
| 88 | sphereMesh->setRings(20); |
| 89 | sphereMesh->setSlices(40); |
| 90 | |
| 91 | // Transform for cylinder |
| 92 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 93 | transform->setScale(1.5f); |
| 94 | transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f)); |
| 95 | |
| 96 | // Material |
| 97 | Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); |
| 98 | material->setDiffuse(Qt::red); |
| 99 | |
| 100 | // Cylinder |
| 101 | Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity); |
| 102 | cylinder->addComponent(transform); |
| 103 | cylinder->addComponent(material); |
| 104 | |
| 105 | // Set root object of the scene |
| 106 | view.setRootEntity(rootEntity); |
| 107 | view.show(); |
| 108 | |
| 109 | ComponentSwapper *swapper = new ComponentSwapper(cylinder, cylinderMesh, sphereMesh); |
| 110 | QTimer *timer = new QTimer; |
| 111 | QObject::connect(timer, SIGNAL(timeout()), swapper, SLOT(swapComponents())); |
| 112 | timer->start(2000); |
| 113 | |
| 114 | return app.exec(); |
| 115 | } |
| 116 | |
| 117 | #include "main.moc" |