| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2019 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:GPL-EXCEPT$ | 
| 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 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QGuiApplication> | 
| 30 | #include <QTimer> | 
| 31 |  | 
| 32 | #include <Qt3DInput/QInputAspect> | 
| 33 |  | 
| 34 | #include <Qt3DRender/qcamera.h> | 
| 35 | #include <Qt3DRender/qcameralens.h> | 
| 36 | #include <Qt3DExtras/qcylindermesh.h> | 
| 37 | #include <Qt3DRender/qmesh.h> | 
| 38 | #include <Qt3DRender/qtechnique.h> | 
| 39 | #include <Qt3DExtras/qphongmaterial.h> | 
| 40 | #include <Qt3DRender/qeffect.h> | 
| 41 | #include <Qt3DRender/qtexture.h> | 
| 42 | #include <Qt3DRender/qrenderpass.h> | 
| 43 | #include <Qt3DRender/qrenderaspect.h> | 
| 44 | #include <Qt3DExtras/qforwardrenderer.h> | 
| 45 |  | 
| 46 | #include <Qt3DCore/qentity.h> | 
| 47 | #include <Qt3DCore/qtransform.h> | 
| 48 | #include <Qt3DCore/qaspectengine.h> | 
| 49 |  | 
| 50 | #include <Qt3DExtras/qt3dwindow.h> | 
| 51 | #include <Qt3DExtras/qorbitcameracontroller.h> | 
| 52 | #include <QThread> | 
| 53 | #include <QLoggingCategory> | 
| 54 |  | 
| 55 | #include <type_traits> | 
| 56 |  | 
| 57 | int main(int argc, char **argv) | 
| 58 | { | 
| 59 |     QGuiApplication app(argc, argv); | 
| 60 |     Qt3DExtras::Qt3DWindow view; | 
| 61 |  | 
| 62 |     QLoggingCategory::setFilterRules("Qt3D.Renderer.RenderNodes=true" ); | 
| 63 |  | 
| 64 |     // Root entity | 
| 65 |     Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); | 
| 66 |     view.setRootEntity(rootEntity); | 
| 67 |     rootEntity->setObjectName("Root Entity" ); | 
| 68 |  | 
| 69 |     // Set root object of the scene | 
| 70 |     view.show(); | 
| 71 |  | 
| 72 |     // Camera | 
| 73 |     Qt3DRender::QCamera *camera = view.camera(); | 
| 74 |     camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f / 9.0f, nearPlane: 0.1f, farPlane: 1000.0f); | 
| 75 |     camera->setPosition(QVector3D(0, 0, 20.0f)); | 
| 76 |     camera->setUpVector(QVector3D(0, 1, 0)); | 
| 77 |     camera->setViewCenter(QVector3D(0, 0, 0)); | 
| 78 |  | 
| 79 |     // For camera controls | 
| 80 |     Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity); | 
| 81 |     cameraController->setCamera(camera); | 
| 82 |  | 
| 83 |     // Cylinder shape data | 
| 84 |     Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(); | 
| 85 |  | 
| 86 |     qDebug() << "Setup complete.  Creating cylinders\n" ; | 
| 87 |  | 
| 88 |     // simple setParent from nullptr (OK for QTBUG-73905) | 
| 89 |     // green cylinder, bottom left | 
| 90 |     { | 
| 91 |         Qt3DCore::QTransform *leftTransform = new Qt3DCore::QTransform; | 
| 92 |         leftTransform->setTranslation(QVector3D(-5, -2, 0)); | 
| 93 |         leftTransform->setObjectName("Green transform" ); | 
| 94 |  | 
| 95 |         Qt3DExtras::QPhongMaterial *greenMaterial = new Qt3DExtras::QPhongMaterial(rootEntity); | 
| 96 |         greenMaterial->setObjectName("Green Material" ); | 
| 97 |         greenMaterial->setDiffuse(Qt::green); | 
| 98 |  | 
| 99 |         Qt3DCore::QEntity *grandParentNode = new Qt3DCore::QEntity(); | 
| 100 |         Qt3DCore::QEntity *parentNode = new Qt3DCore::QEntity(); | 
| 101 |         Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity(); | 
| 102 |         grandParentNode->setObjectName("Green Grandparent" ); | 
| 103 |         parentNode->setObjectName("Green Parent" ); | 
| 104 |         leafNode->setObjectName("Green Leaf" ); | 
| 105 |  | 
| 106 |         leafNode->addComponent(comp: mesh); | 
| 107 |         leafNode->addComponent(comp: greenMaterial); | 
| 108 |         parentNode->addComponent(comp: leftTransform); | 
| 109 |  | 
| 110 |         grandParentNode->setParent(rootEntity); | 
| 111 |         parentNode->setParent(grandParentNode); | 
| 112 |         leafNode->setParent(parentNode); | 
| 113 |     } | 
| 114 |  | 
| 115 |     // simple setParent from rootEntity (doesn't work QTBUG-73905) | 
| 116 |     // yellow cylinder, top left | 
| 117 |     { | 
| 118 |         Qt3DCore::QTransform *leftTransform = new Qt3DCore::QTransform; | 
| 119 |         leftTransform->setTranslation(QVector3D(-5, 2, 0)); | 
| 120 |         leftTransform->setObjectName("Yellow Transform" ); | 
| 121 |  | 
| 122 |         Qt3DExtras::QPhongMaterial *yellowMaterial = new Qt3DExtras::QPhongMaterial(rootEntity); | 
| 123 |         yellowMaterial->setObjectName("Yellow Material" ); | 
| 124 |         yellowMaterial->setDiffuse(Qt::yellow); | 
| 125 |  | 
| 126 |         Qt3DCore::QEntity *grandParentNode = new Qt3DCore::QEntity(rootEntity); | 
| 127 |         Qt3DCore::QEntity *parentNode = new Qt3DCore::QEntity(rootEntity); | 
| 128 |         Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity(rootEntity); | 
| 129 |         leafNode->setObjectName("Yellow Leaf" ); | 
| 130 |         grandParentNode->setObjectName("Yellow Grandparent" ); | 
| 131 |         parentNode->setObjectName("Yellow Parent" ); | 
| 132 |  | 
| 133 |         leafNode->addComponent(comp: mesh); | 
| 134 |         leafNode->addComponent(comp: yellowMaterial); | 
| 135 |         parentNode->addComponent(comp: leftTransform); | 
| 136 |  | 
| 137 |         // sometimes this can change things | 
| 138 |         //QCoreApplication::processEvents(); | 
| 139 |  | 
| 140 |         grandParentNode->setParent(rootEntity); | 
| 141 |         parentNode->setParent(grandParentNode); | 
| 142 |         leafNode->setParent(parentNode); | 
| 143 |     } | 
| 144 |  | 
| 145 |     // complex setParent from nullptr (OK QTBUG-73905?) | 
| 146 |     // red cylinder, Bottom-right | 
| 147 |     { | 
| 148 |         Qt3DCore::QNode *tree1node1 = new Qt3DCore::QNode(); | 
| 149 |         Qt3DCore::QEntity *tree1node2 = new Qt3DCore::QEntity(); | 
| 150 |         Qt3DCore::QNode *tree1node3 = new Qt3DCore::QNode(); | 
| 151 |         tree1node1->setObjectName("Red Tree1-Node1" ); | 
| 152 |         tree1node2->setObjectName("Red Tree1-Node2" ); | 
| 153 |         tree1node3->setObjectName("Red Tree1-Node3" ); | 
| 154 |  | 
| 155 |         Qt3DCore::QNode *tree2node1 = new Qt3DCore::QNode(); | 
| 156 |         Qt3DCore::QEntity *tree2node2 = new Qt3DCore::QEntity(); | 
| 157 |         Qt3DCore::QNode *tree2node3 = new Qt3DCore::QNode(); | 
| 158 |         tree2node1->setObjectName("Red Tree2-Node1" ); | 
| 159 |         tree2node2->setObjectName("Red Tree2-Node2" ); | 
| 160 |         tree2node3->setObjectName("Red Tree2-Node3" ); | 
| 161 |  | 
| 162 |         Qt3DCore::QTransform *wrongRedTransform = new Qt3DCore::QTransform; | 
| 163 |         wrongRedTransform->setTranslation(QVector3D(1, -1, 0)); | 
| 164 |         Qt3DCore::QTransform *bottomRightTransform = new Qt3DCore::QTransform; | 
| 165 |         bottomRightTransform->setTranslation(QVector3D(5, -2, 0)); | 
| 166 |         bottomRightTransform->setObjectName("Red BR Transform" ); | 
| 167 |         wrongRedTransform->setObjectName("Red Wrong Transform" ); | 
| 168 |  | 
| 169 |         Qt3DExtras::QPhongMaterial *redMaterial = new Qt3DExtras::QPhongMaterial(rootEntity); | 
| 170 |         redMaterial->setDiffuse(Qt::red); | 
| 171 |         redMaterial->setObjectName("Red Material" ); | 
| 172 |         Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity(); | 
| 173 |         leafNode->setObjectName("Red Leaf" ); | 
| 174 |         leafNode->addComponent(comp: mesh); | 
| 175 |         leafNode->addComponent(comp: redMaterial); | 
| 176 |  | 
| 177 |         tree1node2->addComponent(comp: wrongRedTransform); | 
| 178 |         tree2node2->addComponent(comp: bottomRightTransform); | 
| 179 |  | 
| 180 |         tree1node1->setParent(rootEntity); | 
| 181 |         tree1node2->setParent(tree1node1); | 
| 182 |         tree1node3->setParent(tree1node2); | 
| 183 |  | 
| 184 |         tree2node1->setParent(rootEntity); | 
| 185 |         tree2node2->setParent(tree2node1); | 
| 186 |         tree2node3->setParent(tree2node2); | 
| 187 |  | 
| 188 |         leafNode->setParent(tree1node3); | 
| 189 |         leafNode->setParent(tree2node3); | 
| 190 |     } | 
| 191 |  | 
| 192 |     // complex setParent from rootEntity (doesn't work QTBUG-73905) | 
| 193 |     // blue cylinder, top right | 
| 194 |     { | 
| 195 |         Qt3DCore::QNode *tree1node1 = new Qt3DCore::QNode(rootEntity); | 
| 196 |         Qt3DCore::QEntity *tree1node2 = new Qt3DCore::QEntity(rootEntity); | 
| 197 |         Qt3DCore::QNode *tree1node3 = new Qt3DCore::QNode(rootEntity); | 
| 198 |         tree1node1->setObjectName("Blue Tree1-Node1" ); | 
| 199 |         tree1node2->setObjectName("Blue Tree1-Node2" ); | 
| 200 |         tree1node3->setObjectName("Blue Tree1-Node3" ); | 
| 201 |  | 
| 202 |         Qt3DCore::QNode *tree2node1 = new Qt3DCore::QNode(rootEntity); | 
| 203 |         Qt3DCore::QEntity *tree2node2 = new Qt3DCore::QEntity(rootEntity); | 
| 204 |         Qt3DCore::QNode *tree2node3 = new Qt3DCore::QNode(rootEntity); | 
| 205 |         tree2node1->setObjectName("Blue Tree2-Node1" ); | 
| 206 |         tree2node2->setObjectName("Blue Tree2-Node2" ); | 
| 207 |         tree2node3->setObjectName("Blue Tree2-Node3" ); | 
| 208 |  | 
| 209 |         Qt3DCore::QTransform *wrongBlueTransform = new Qt3DCore::QTransform; | 
| 210 |         wrongBlueTransform->setTranslation(QVector3D(1, 1, 0)); | 
| 211 |         Qt3DCore::QTransform *topRightTransform = new Qt3DCore::QTransform; | 
| 212 |         topRightTransform->setTranslation(QVector3D(5, 2, 0)); | 
| 213 |         wrongBlueTransform->setObjectName("Blue Wrong Transform" ); | 
| 214 |         topRightTransform->setObjectName("Blue TR Transform" ); | 
| 215 |  | 
| 216 |         Qt3DExtras::QPhongMaterial *blueMaterial = new Qt3DExtras::QPhongMaterial(rootEntity); | 
| 217 |         blueMaterial->setObjectName("Blue Material" ); | 
| 218 |         blueMaterial->setDiffuse(Qt::blue); | 
| 219 |         Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity(rootEntity); | 
| 220 |         leafNode->addComponent(comp: mesh); | 
| 221 |         leafNode->addComponent(comp: blueMaterial); | 
| 222 |         leafNode->setObjectName("Blue Leaf" ); | 
| 223 |  | 
| 224 |         // sometimes this can change things | 
| 225 |         //QCoreApplication::processEvents(); | 
| 226 |  | 
| 227 |         tree1node2->addComponent(comp: wrongBlueTransform); | 
| 228 |         tree2node2->addComponent(comp: topRightTransform); | 
| 229 |  | 
| 230 |         tree1node1->setParent(rootEntity); | 
| 231 |         tree1node2->setParent(tree1node1); | 
| 232 |         tree1node3->setParent(tree1node2); | 
| 233 |  | 
| 234 |         tree2node1->setParent(rootEntity); | 
| 235 |         tree2node2->setParent(tree2node1); | 
| 236 |         tree2node3->setParent(tree2node2); | 
| 237 |  | 
| 238 |         leafNode->setParent(tree1node3); | 
| 239 |         leafNode->setParent(tree2node3); | 
| 240 |     } | 
| 241 |  | 
| 242 |     return app.exec(); | 
| 243 | } | 
| 244 |  |