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