| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // Copyright (C) 2018 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #include <QGuiApplication> |
| 6 | #include <QThread> |
| 7 | #include <QTimer> |
| 8 | |
| 9 | #include <Qt3DInput/QInputAspect> |
| 10 | |
| 11 | #include <Qt3DRender/qcamera.h> |
| 12 | #include <Qt3DRender/qcameralens.h> |
| 13 | #include <Qt3DExtras/qcylindermesh.h> |
| 14 | #include <Qt3DRender/qmesh.h> |
| 15 | #include <Qt3DRender/qtechnique.h> |
| 16 | #include <Qt3DExtras/qphongmaterial.h> |
| 17 | #include <Qt3DRender/qeffect.h> |
| 18 | #include <Qt3DRender/qtexture.h> |
| 19 | #include <Qt3DRender/qrenderpass.h> |
| 20 | #include <Qt3DRender/qrenderaspect.h> |
| 21 | #include <Qt3DExtras/qforwardrenderer.h> |
| 22 | |
| 23 | #include <Qt3DCore/qentity.h> |
| 24 | #include <Qt3DCore/qtransform.h> |
| 25 | #include <Qt3DCore/qaspectengine.h> |
| 26 | |
| 27 | #include <Qt3DExtras/qt3dwindow.h> |
| 28 | #include <Qt3DExtras/qorbitcameracontroller.h> |
| 29 | |
| 30 | #include <Qt3DRender/QLayer> |
| 31 | #include <Qt3DRender/QLayerFilter> |
| 32 | |
| 33 | #include <QtGui/qquaternion.h> |
| 34 | |
| 35 | using namespace Qt3DRender; |
| 36 | |
| 37 | int main(int argc, char **argv) |
| 38 | { |
| 39 | QGuiApplication app(argc, argv); |
| 40 | Qt3DExtras::Qt3DWindow view; |
| 41 | |
| 42 | // Root entity |
| 43 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 44 | |
| 45 | // Camera |
| 46 | Qt3DRender::QCamera *camera = view.camera(); |
| 47 | camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); |
| 48 | camera->setPosition(QVector3D(0, 0, 20.0f)); |
| 49 | camera->setUpVector(QVector3D(0, 1, 0)); |
| 50 | camera->setViewCenter(QVector3D(0, 0, 0)); |
| 51 | |
| 52 | // For camera controls |
| 53 | Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 54 | cameraController->setCamera(camera); |
| 55 | |
| 56 | // Cylinder shape data |
| 57 | Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(); |
| 58 | mesh->setRadius(1); |
| 59 | mesh->setLength(3); |
| 60 | mesh->setRings(100); |
| 61 | mesh->setSlices(20); |
| 62 | |
| 63 | // Transform for cylinder |
| 64 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 65 | transform->setScale(1.5f); |
| 66 | transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f)); |
| 67 | |
| 68 | // Material |
| 69 | Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); |
| 70 | material->setDiffuse(Qt::red); |
| 71 | |
| 72 | // Cylinder |
| 73 | Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity); |
| 74 | cylinder->addComponent(mesh); |
| 75 | cylinder->addComponent(transform); |
| 76 | cylinder->addComponent(material); |
| 77 | |
| 78 | QTimer *timer = new QTimer(rootEntity); |
| 79 | QObject::connect(timer, &QTimer::timeout, rootEntity, [=]() { |
| 80 | for (int i = 0; i < 2; i++) { |
| 81 | auto *dummy = new Qt3DCore::QNode(rootEntity); |
| 82 | auto *dummy2 = new Qt3DCore::QNode(dummy); |
| 83 | auto *layerFilter = new QLayerFilter(dummy2); |
| 84 | auto *layer = new QLayer(); |
| 85 | |
| 86 | layerFilter->addLayer(layer); |
| 87 | |
| 88 | cylinder->addComponent(layer); |
| 89 | } |
| 90 | }); |
| 91 | timer->start(1000); |
| 92 | |
| 93 | QTimer *timer2 = new QTimer(rootEntity); |
| 94 | QObject::connect(timer2, &QTimer::timeout, timer2, []() { |
| 95 | QThread::msleep(100); |
| 96 | }); |
| 97 | timer2->start(100); |
| 98 | |
| 99 | // Set root object of the scene |
| 100 | view.setRootEntity(rootEntity); |
| 101 | view.show(); |
| 102 | |
| 103 | return app.exec(); |
| 104 | } |