| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:BSD$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** BSD License Usage |
| 19 | ** Alternatively, you may use this file under the terms of the BSD license |
| 20 | ** as follows: |
| 21 | ** |
| 22 | ** "Redistribution and use in source and binary forms, with or without |
| 23 | ** modification, are permitted provided that the following conditions are |
| 24 | ** met: |
| 25 | ** * Redistributions of source code must retain the above copyright |
| 26 | ** notice, this list of conditions and the following disclaimer. |
| 27 | ** * Redistributions in binary form must reproduce the above copyright |
| 28 | ** notice, this list of conditions and the following disclaimer in |
| 29 | ** the documentation and/or other materials provided with the |
| 30 | ** distribution. |
| 31 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 32 | ** contributors may be used to endorse or promote products derived |
| 33 | ** from this software without specific prior written permission. |
| 34 | ** |
| 35 | ** |
| 36 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 37 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 38 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 39 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 40 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 42 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 43 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 44 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 45 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 46 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 47 | ** |
| 48 | ** $QT_END_LICENSE$ |
| 49 | ** |
| 50 | ****************************************************************************/ |
| 51 | |
| 52 | #include <QGuiApplication> |
| 53 | #include <QThread> |
| 54 | #include <QTimer> |
| 55 | |
| 56 | #include <Qt3DInput/QInputAspect> |
| 57 | |
| 58 | #include <Qt3DRender/qcamera.h> |
| 59 | #include <Qt3DRender/qcameralens.h> |
| 60 | #include <Qt3DExtras/qcylindermesh.h> |
| 61 | #include <Qt3DRender/qmesh.h> |
| 62 | #include <Qt3DRender/qtechnique.h> |
| 63 | #include <Qt3DExtras/qphongmaterial.h> |
| 64 | #include <Qt3DRender/qeffect.h> |
| 65 | #include <Qt3DRender/qtexture.h> |
| 66 | #include <Qt3DRender/qrenderpass.h> |
| 67 | #include <Qt3DRender/qrenderaspect.h> |
| 68 | #include <Qt3DExtras/qforwardrenderer.h> |
| 69 | |
| 70 | #include <Qt3DCore/qentity.h> |
| 71 | #include <Qt3DCore/qtransform.h> |
| 72 | #include <Qt3DCore/qaspectengine.h> |
| 73 | |
| 74 | #include <Qt3DExtras/qt3dwindow.h> |
| 75 | #include <Qt3DExtras/qorbitcameracontroller.h> |
| 76 | |
| 77 | #include <Qt3DRender/QLayer> |
| 78 | #include <Qt3DRender/QLayerFilter> |
| 79 | |
| 80 | using namespace Qt3DRender; |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | QGuiApplication app(argc, argv); |
| 85 | Qt3DExtras::Qt3DWindow view; |
| 86 | |
| 87 | // Root entity |
| 88 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 89 | |
| 90 | // Camera |
| 91 | Qt3DRender::QCamera *camera = view.camera(); |
| 92 | camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); |
| 93 | camera->setPosition(QVector3D(0, 0, 20.0f)); |
| 94 | camera->setUpVector(QVector3D(0, 1, 0)); |
| 95 | camera->setViewCenter(QVector3D(0, 0, 0)); |
| 96 | |
| 97 | // For camera controls |
| 98 | Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 99 | cameraController->setCamera(camera); |
| 100 | |
| 101 | // Cylinder shape data |
| 102 | Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(); |
| 103 | mesh->setRadius(1); |
| 104 | mesh->setLength(3); |
| 105 | mesh->setRings(100); |
| 106 | mesh->setSlices(20); |
| 107 | |
| 108 | // Transform for cylinder |
| 109 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 110 | transform->setScale(1.5f); |
| 111 | transform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f)); |
| 112 | |
| 113 | // Material |
| 114 | Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); |
| 115 | material->setDiffuse(Qt::red); |
| 116 | |
| 117 | // Cylinder |
| 118 | Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity); |
| 119 | cylinder->addComponent(comp: mesh); |
| 120 | cylinder->addComponent(comp: transform); |
| 121 | cylinder->addComponent(comp: material); |
| 122 | |
| 123 | QTimer *timer = new QTimer(rootEntity); |
| 124 | QObject::connect(sender: timer, signal: &QTimer::timeout, slot: [=](){ |
| 125 | for (int i = 0; i < 2; i++) { |
| 126 | auto *dummy = new Qt3DCore::QNode(rootEntity); |
| 127 | auto *dummy2 = new Qt3DCore::QNode(dummy); |
| 128 | auto *layerFilter = new QLayerFilter(dummy2); |
| 129 | auto *layer = new QLayer(); |
| 130 | |
| 131 | layerFilter->addLayer(layer); |
| 132 | |
| 133 | cylinder->addComponent(comp: layer); |
| 134 | } |
| 135 | }); |
| 136 | timer->start(msec: 1000); |
| 137 | |
| 138 | QTimer *timer2 = new QTimer(rootEntity); |
| 139 | QObject::connect(sender: timer2, signal: &QTimer::timeout, slot: [](){ |
| 140 | QThread::msleep(100); |
| 141 | }); |
| 142 | timer2->start(msec: 100); |
| 143 | |
| 144 | // Set root object of the scene |
| 145 | view.setRootEntity(rootEntity); |
| 146 | view.show(); |
| 147 | |
| 148 | return app.exec(); |
| 149 | } |
| 150 | |