| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "entity.h" |
| 52 | |
| 53 | #include <QGuiApplication> |
| 54 | |
| 55 | #include <QPropertyAnimation> |
| 56 | #include <QUrl> |
| 57 | #include <QTimer> |
| 58 | #include <Qt3DCore/QEntity> |
| 59 | #include <Qt3DRender/QCamera> |
| 60 | #include <Qt3DCore/QTransform> |
| 61 | #include <Qt3DCore/qaspectengine.h> |
| 62 | #include <Qt3DInput/QInputAspect> |
| 63 | #include <Qt3DRender/QParameter> |
| 64 | #include <Qt3DExtras/QCylinderMesh> |
| 65 | #include <Qt3DRender/QRenderAspect> |
| 66 | #include <Qt3DRender/QCameraSelector> |
| 67 | #include <Qt3DExtras/QPhongMaterial> |
| 68 | #include <Qt3DExtras/QForwardRenderer> |
| 69 | #include <qmath.h> |
| 70 | #include <Qt3DExtras/qt3dwindow.h> |
| 71 | #include <Qt3DExtras/qfirstpersoncameracontroller.h> |
| 72 | #include <Qt3DRender/QRenderSurfaceSelector> |
| 73 | #include <Qt3DRender/QCameraSelector> |
| 74 | #include <Qt3DRender/QViewport> |
| 75 | #include <Qt3DRender/QNoDraw> |
| 76 | #include <Qt3DRender/QDebugOverlay> |
| 77 | |
| 78 | using namespace Qt3DCore; |
| 79 | using namespace Qt3DRender; |
| 80 | |
| 81 | int main(int ac, char **av) |
| 82 | { |
| 83 | QGuiApplication app(ac, av); |
| 84 | Qt3DExtras::Qt3DWindow view; |
| 85 | |
| 86 | // FrameGraph |
| 87 | { |
| 88 | QRenderSurfaceSelector *surfaceSelector = new QRenderSurfaceSelector(); |
| 89 | QCameraSelector *cameraSelector = new Qt3DRender::QCameraSelector(surfaceSelector); |
| 90 | cameraSelector->setCamera(view.camera()); |
| 91 | QClearBuffers *clearBuffers = new Qt3DRender::QClearBuffers(cameraSelector); |
| 92 | clearBuffers->setClearColor(QColor(Qt::gray)); |
| 93 | clearBuffers->setBuffers(QClearBuffers::ColorDepthBuffer); |
| 94 | new QNoDraw(clearBuffers); |
| 95 | |
| 96 | const QRectF viewports[] = { |
| 97 | {0.0f, 0.0f, 0.25f, 0.25f}, |
| 98 | {0.25f, 0.0f, 0.25f, 0.25f}, |
| 99 | {0.5f, 0.0f, 0.25f, 0.25f}, |
| 100 | {0.75f, 0.0f, 0.25f, 0.25f}, |
| 101 | {0.0f, 0.25f, 0.25f, 0.25f}, |
| 102 | {0.25f, 0.25f, 0.25f, 0.25f}, |
| 103 | {0.5f, 0.25f, 0.25f, 0.25f}, |
| 104 | {0.75f, 0.25f, 0.25f, 0.25f}, |
| 105 | {0.0f, 0.5f, 0.25f, 0.25f}, |
| 106 | {0.25f, 0.5f, 0.25f, 0.25f}, |
| 107 | {0.5f, 0.5f, 0.25f, 0.25f}, |
| 108 | {0.75f, 0.5f, 0.25f, 0.25f}, |
| 109 | {0.0f, 0.75f, 0.25f, 0.25f}, |
| 110 | {0.25f, 0.75f, 0.25f, 0.25f}, |
| 111 | {0.5f, 0.75f, 0.25f, 0.25f}, |
| 112 | {0.75f, 0.75f, 0.25f, 0.25f}, |
| 113 | }; |
| 114 | |
| 115 | for (const QRectF &vp : viewports) { |
| 116 | QViewport *viewport = new Qt3DRender::QViewport(cameraSelector); |
| 117 | viewport->setNormalizedRect(vp); |
| 118 | } |
| 119 | |
| 120 | new QDebugOverlay(qobject_cast<Qt3DCore::QNode *>(object: cameraSelector->children().last())); |
| 121 | |
| 122 | view.setActiveFrameGraph(surfaceSelector); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | QEntity *root = new QEntity(); |
| 127 | |
| 128 | // Mesh |
| 129 | auto *mesh = new Qt3DExtras::QCylinderMesh(root); |
| 130 | mesh->setRings(50.0f); |
| 131 | mesh->setSlices(30.0f); |
| 132 | mesh->setRadius(2.5f); |
| 133 | mesh->setLength(5.0f); |
| 134 | |
| 135 | // Material |
| 136 | auto phongMaterial = new Qt3DExtras::QPhongMaterial(root); |
| 137 | auto effect = phongMaterial->effect(); |
| 138 | |
| 139 | // Camera |
| 140 | QCamera *cameraEntity = view.camera(); |
| 141 | cameraEntity->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); |
| 142 | cameraEntity->setPosition(QVector3D(0, 250.0f, 50.0f)); |
| 143 | cameraEntity->setUpVector(QVector3D(0, 1, 0)); |
| 144 | cameraEntity->setViewCenter(QVector3D(0, 0, 0)); |
| 145 | |
| 146 | // For camera controls |
| 147 | Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(root); |
| 148 | camController->setCamera(cameraEntity); |
| 149 | |
| 150 | const double radius = 100.0; |
| 151 | const int max = 1000; |
| 152 | const double det = 1.0 / max; |
| 153 | |
| 154 | // Scene |
| 155 | for (int i = 0; i < max; i++) { |
| 156 | Entity *e = new Entity(effect, root); |
| 157 | e->addComponent(comp: mesh); |
| 158 | const double angle = M_PI * 2.0 * double(i) * det * 10.; |
| 159 | |
| 160 | e->setDiffuseColor(QColor(int(qFabs(v: qCos(v: angle)) * 255.0), 204, 75)); |
| 161 | e->setPosition(QVector3D(float(radius * qCos(v: angle)), |
| 162 | float(-200.0 * i * det), |
| 163 | float(radius * qSin(v: angle)))); |
| 164 | e->setTheta(30.0f * i); |
| 165 | e->setPhi(45.0f * i); |
| 166 | |
| 167 | QPropertyAnimation *animX = new QPropertyAnimation(e, QByteArrayLiteral("theta" )); |
| 168 | animX->setDuration(2400 * (i + 1)); |
| 169 | animX->setStartValue(QVariant::fromValue(value: i * 30.0f)); |
| 170 | animX->setEndValue(QVariant::fromValue(value: (i + 1) * 390.0f)); |
| 171 | animX->setLoopCount(-1); |
| 172 | animX->start(); |
| 173 | |
| 174 | QPropertyAnimation *animZ = new QPropertyAnimation(e, QByteArrayLiteral("phi" )); |
| 175 | animZ->setDuration(2400 * (i + 1)); |
| 176 | animZ->setStartValue(QVariant::fromValue(value: i * 20.0f)); |
| 177 | animZ->setEndValue(QVariant::fromValue(value: (i + 1) * 380.0f)); |
| 178 | animZ->setLoopCount(-1); |
| 179 | animZ->start(); |
| 180 | } |
| 181 | |
| 182 | view.setRootEntity(root); |
| 183 | view.show(); |
| 184 | |
| 185 | if (app.arguments().contains(str: ("--bench" ))) |
| 186 | QTimer::singleShot(interval: 25 * 1000, context: &app, slot: &QCoreApplication::quit); |
| 187 | |
| 188 | return app.exec(); |
| 189 | } |
| 190 | |