1/****************************************************************************
2**
3** Copyright (C) 2015 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 <QFileDialog>
52#include <QApplication>
53
54#include <Qt3DRender/QCamera>
55#include <Qt3DCore/QEntity>
56#include <Qt3DCore/QAspectEngine>
57#include <Qt3DInput/QInputAspect>
58#include <Qt3DRender/QSceneLoader>
59#include <Qt3DRender/QRenderAspect>
60#include <Qt3DExtras/QForwardRenderer>
61#include <Qt3DExtras/qt3dwindow.h>
62#include <Qt3DExtras/qfirstpersoncameracontroller.h>
63
64class SceneWalker : public QObject
65{
66public:
67 SceneWalker(Qt3DRender::QSceneLoader *loader) : m_loader(loader) { }
68
69 void onStatusChanged();
70
71private:
72 void walkEntity(Qt3DCore::QEntity *e, int depth = 0);
73
74 Qt3DRender::QSceneLoader *m_loader;
75};
76
77void SceneWalker::onStatusChanged()
78{
79 qDebug() << "Status changed:" << m_loader->status();
80 if (m_loader->status() != Qt3DRender::QSceneLoader::Ready)
81 return;
82
83 // The QSceneLoader instance is a component of an entity. The loaded scene
84 // tree is added under this entity.
85 QVector<Qt3DCore::QEntity *> entities = m_loader->entities();
86
87 // Technically there could be multiple entities referencing the scene loader
88 // but sharing is discouraged, and in our case there will be one anyhow.
89 if (entities.isEmpty())
90 return;
91 Qt3DCore::QEntity *root = entities[0];
92 // Print the tree.
93 walkEntity(e: root);
94
95 // To access a given node (like a named mesh in the scene), use QObject::findChild().
96 // The scene structure and names always depend on the asset.
97 Qt3DCore::QEntity *e = root->findChild<Qt3DCore::QEntity *>(QStringLiteral("PlanePropeller_mesh")); // toyplane.obj
98 if (e)
99 qDebug() << "Found propeller node" << e << "with components" << e->components();
100}
101
102void SceneWalker::walkEntity(Qt3DCore::QEntity *e, int depth)
103{
104 Qt3DCore::QNodeVector nodes = e->childNodes();
105 for (int i = 0; i < nodes.count(); ++i) {
106 Qt3DCore::QNode *node = nodes[i];
107 Qt3DCore::QEntity *entity = qobject_cast<Qt3DCore::QEntity *>(object: node);
108 if (entity) {
109 QString indent;
110 indent.fill(c: ' ', size: depth * 2);
111 qDebug().noquote() << indent << "Entity:" << entity << "Components:" << entity->components();
112 walkEntity(e: entity, depth: depth + 1);
113 }
114 }
115}
116
117int main(int ac, char **av)
118{
119 QApplication app(ac, av);
120 Qt3DExtras::Qt3DWindow view;
121 view.defaultFrameGraph()->setClearColor(Qt::black);
122
123 // Root entity
124 Qt3DCore::QEntity *sceneRoot = new Qt3DCore::QEntity();
125
126 // Scene Camera
127 Qt3DRender::QCamera *camera = view.camera();
128 camera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
129 camera->setViewCenter(QVector3D(0.0f, 3.5f, 0.0f));
130 camera->setPosition(QVector3D(0.0f, 3.5f, 25.0f));
131 camera->setNearPlane(0.001f);
132 camera->setFarPlane(10000.0f);
133
134 // For camera controls
135 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(sceneRoot);
136 camController->setCamera(camera);
137
138 // Scene loader
139 Qt3DCore::QEntity *sceneLoaderEntity = new Qt3DCore::QEntity(sceneRoot);
140 Qt3DRender::QSceneLoader *sceneLoader = new Qt3DRender::QSceneLoader(sceneLoaderEntity);
141 SceneWalker sceneWalker(sceneLoader);
142 QObject::connect(sender: sceneLoader, signal: &Qt3DRender::QSceneLoader::statusChanged, receiver: &sceneWalker, slot: &SceneWalker::onStatusChanged);
143 sceneLoaderEntity->addComponent(comp: sceneLoader);
144
145 QStringList args = QCoreApplication::arguments();
146 QUrl sourceFileName;
147 if (args.count() <= 1) {
148 QWidget *container = new QWidget();
149 QFileDialog dialog;
150 dialog.setFileMode(QFileDialog::AnyFile);
151 sourceFileName = dialog.getOpenFileUrl(parent: container, QStringLiteral("Open a scene file"));
152 } else {
153 sourceFileName = QUrl::fromLocalFile(localfile: args[1]);
154 }
155
156 if (sourceFileName.isEmpty())
157 return 0;
158
159 sceneLoader->setSource(sourceFileName);
160
161 view.setRootEntity(sceneRoot);
162 view.show();
163
164 return app.exec();
165}
166

source code of qt3d/tests/manual/assimp-cpp/main.cpp