| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QFileDialog> |
| 5 | #include <QApplication> |
| 6 | |
| 7 | #include <Qt3DRender/QCamera> |
| 8 | #include <Qt3DCore/QEntity> |
| 9 | #include <Qt3DCore/QAspectEngine> |
| 10 | #include <Qt3DInput/QInputAspect> |
| 11 | #include <Qt3DRender/QSceneLoader> |
| 12 | #include <Qt3DRender/QRenderAspect> |
| 13 | #include <Qt3DExtras/QForwardRenderer> |
| 14 | #include <Qt3DExtras/qt3dwindow.h> |
| 15 | #include <Qt3DExtras/qfirstpersoncameracontroller.h> |
| 16 | |
| 17 | class SceneWalker : public QObject |
| 18 | { |
| 19 | public: |
| 20 | SceneWalker(Qt3DRender::QSceneLoader *loader) : m_loader(loader) { } |
| 21 | |
| 22 | void onStatusChanged(); |
| 23 | |
| 24 | private: |
| 25 | void walkEntity(Qt3DCore::QEntity *e, int depth = 0); |
| 26 | |
| 27 | Qt3DRender::QSceneLoader *m_loader; |
| 28 | }; |
| 29 | |
| 30 | void SceneWalker::onStatusChanged() |
| 31 | { |
| 32 | qDebug() << "Status changed:" << m_loader->status(); |
| 33 | if (m_loader->status() != Qt3DRender::QSceneLoader::Ready) |
| 34 | return; |
| 35 | |
| 36 | // The QSceneLoader instance is a component of an entity. The loaded scene |
| 37 | // tree is added under this entity. |
| 38 | QList<Qt3DCore::QEntity *> entities = m_loader->entities(); |
| 39 | |
| 40 | // Technically there could be multiple entities referencing the scene loader |
| 41 | // but sharing is discouraged, and in our case there will be one anyhow. |
| 42 | if (entities.isEmpty()) |
| 43 | return; |
| 44 | Qt3DCore::QEntity *root = entities[0]; |
| 45 | // Print the tree. |
| 46 | walkEntity(root); |
| 47 | |
| 48 | // To access a given node (like a named mesh in the scene), use QObject::findChild(). |
| 49 | // The scene structure and names always depend on the asset. |
| 50 | Qt3DCore::QEntity *e = root->findChild<Qt3DCore::QEntity *>(QStringLiteral("PlanePropeller_mesh")); // toyplane.obj |
| 51 | if (e) |
| 52 | qDebug() << "Found propeller node" << e << "with components" << e->components(); |
| 53 | } |
| 54 | |
| 55 | void SceneWalker::walkEntity(Qt3DCore::QEntity *e, int depth) |
| 56 | { |
| 57 | Qt3DCore::QNodeVector nodes = e->childNodes(); |
| 58 | for (int i = 0; i < nodes.count(); ++i) { |
| 59 | Qt3DCore::QNode *node = nodes[i]; |
| 60 | Qt3DCore::QEntity *entity = qobject_cast<Qt3DCore::QEntity *>(node); |
| 61 | if (entity) { |
| 62 | QString indent; |
| 63 | indent.fill(' ', depth * 2); |
| 64 | qDebug().noquote() << indent << "Entity:" << entity << "Components:" << entity->components(); |
| 65 | walkEntity(entity, depth + 1); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | int main(int ac, char **av) |
| 71 | { |
| 72 | QApplication app(ac, av); |
| 73 | Qt3DExtras::Qt3DWindow view; |
| 74 | view.defaultFrameGraph()->setClearColor(Qt::black); |
| 75 | |
| 76 | // Root entity |
| 77 | Qt3DCore::QEntity *sceneRoot = new Qt3DCore::QEntity(); |
| 78 | |
| 79 | // Scene Camera |
| 80 | Qt3DRender::QCamera *camera = view.camera(); |
| 81 | camera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection); |
| 82 | camera->setViewCenter(QVector3D(0.0f, 3.5f, 0.0f)); |
| 83 | camera->setPosition(QVector3D(0.0f, 3.5f, 25.0f)); |
| 84 | camera->setNearPlane(0.001f); |
| 85 | camera->setFarPlane(10000.0f); |
| 86 | |
| 87 | // For camera controls |
| 88 | Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(sceneRoot); |
| 89 | camController->setCamera(camera); |
| 90 | |
| 91 | // Scene loader |
| 92 | Qt3DCore::QEntity *sceneLoaderEntity = new Qt3DCore::QEntity(sceneRoot); |
| 93 | Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(sceneLoaderEntity); |
| 94 | SceneWalker sceneWalker(sceneLoader); |
| 95 | QObject::connect(sceneLoader, &Qt3DRender::QSceneLoader::statusChanged, &sceneWalker, &SceneWalker::onStatusChanged); |
| 96 | sceneLoaderEntity->addComponent(sceneLoader); |
| 97 | |
| 98 | QStringList args = QCoreApplication::arguments(); |
| 99 | QUrl sourceFileName; |
| 100 | if (args.count() <= 1) { |
| 101 | QWidget *container = new QWidget(); |
| 102 | QFileDialog dialog; |
| 103 | dialog.setFileMode(QFileDialog::AnyFile); |
| 104 | sourceFileName = dialog.getOpenFileUrl(container, QStringLiteral("Open a scene file")); |
| 105 | } else { |
| 106 | sourceFileName = QUrl::fromLocalFile(args[1]); |
| 107 | } |
| 108 | |
| 109 | if (sourceFileName.isEmpty()) |
| 110 | return 0; |
| 111 | |
| 112 | sceneLoader->setSource(sourceFileName); |
| 113 | |
| 114 | view.setRootEntity(sceneRoot); |
| 115 | view.show(); |
| 116 | |
| 117 | return app.exec(); |
| 118 | } |