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
|
// Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#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(QLatin1String("Qt3D.Renderer.RenderNodes=true"));
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
view.setRootEntity(rootEntity);
rootEntity->setObjectName(QLatin1String("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(QLatin1String("Green transform"));
Qt3DExtras::QPhongMaterial *greenMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
greenMaterial->setObjectName(QLatin1String("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(QLatin1String("Green Grandparent"));
parentNode->setObjectName(QLatin1String("Green Parent"));
leafNode->setObjectName(QLatin1String("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(QLatin1String("Yellow Transform"));
Qt3DExtras::QPhongMaterial *yellowMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
yellowMaterial->setObjectName(QLatin1String("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(QLatin1String("Yellow Leaf"));
grandParentNode->setObjectName(QLatin1String("Yellow Grandparent"));
parentNode->setObjectName(QLatin1String("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(QLatin1String("Red Tree1-Node1"));
tree1node2->setObjectName(QLatin1String("Red Tree1-Node2"));
tree1node3->setObjectName(QLatin1String("Red Tree1-Node3"));
Qt3DCore::QNode *tree2node1 = new Qt3DCore::QNode();
Qt3DCore::QEntity *tree2node2 = new Qt3DCore::QEntity();
Qt3DCore::QNode *tree2node3 = new Qt3DCore::QNode();
tree2node1->setObjectName(QLatin1String("Red Tree2-Node1"));
tree2node2->setObjectName(QLatin1String("Red Tree2-Node2"));
tree2node3->setObjectName(QLatin1String("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(QLatin1String("Red BR Transform"));
wrongRedTransform->setObjectName(QLatin1String("Red Wrong Transform"));
Qt3DExtras::QPhongMaterial *redMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
redMaterial->setDiffuse(Qt::red);
redMaterial->setObjectName(QLatin1String("Red Material"));
Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity();
leafNode->setObjectName(QLatin1String("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(QLatin1String("Blue Tree1-Node1"));
tree1node2->setObjectName(QLatin1String("Blue Tree1-Node2"));
tree1node3->setObjectName(QLatin1String("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(QLatin1String("Blue Tree2-Node1"));
tree2node2->setObjectName(QLatin1String("Blue Tree2-Node2"));
tree2node3->setObjectName(QLatin1String("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(QLatin1String("Blue Wrong Transform"));
topRightTransform->setObjectName(QLatin1String("Blue TR Transform"));
Qt3DExtras::QPhongMaterial *blueMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
blueMaterial->setObjectName(QLatin1String("Blue Material"));
blueMaterial->setDiffuse(Qt::blue);
Qt3DCore::QEntity *leafNode = new Qt3DCore::QEntity(rootEntity);
leafNode->addComponent(mesh);
leafNode->addComponent(blueMaterial);
leafNode->setObjectName(QLatin1String("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();
}
|