1/****************************************************************************
2**
3** Copyright (C) 2014 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:LGPL$
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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qmesh.h"
41#include "qmesh_p.h"
42
43#include <QtCore/private/qfactoryloader_p.h>
44#include <QDebug>
45#include <QFile>
46#include <QFileInfo>
47#include <QScopedPointer>
48#include <QMimeDatabase>
49#include <QMimeType>
50#include <QtCore/QBuffer>
51#include <Qt3DRender/QRenderAspect>
52#include <Qt3DCore/QAspectEngine>
53#include <Qt3DCore/qpropertyupdatedchange.h>
54#include <Qt3DCore/private/qscene_p.h>
55#include <Qt3DCore/private/qdownloadhelperservice_p.h>
56#include <Qt3DRender/private/qrenderaspect_p.h>
57#include <Qt3DRender/private/nodemanagers_p.h>
58#include <Qt3DRender/private/qgeometryloaderinterface_p.h>
59#include <Qt3DRender/private/renderlogging_p.h>
60#include <Qt3DRender/private/qurlhelper_p.h>
61#include <Qt3DRender/private/qgeometryloaderfactory_p.h>
62#include <Qt3DRender/private/geometryrenderermanager_p.h>
63
64#include <algorithm>
65
66QT_BEGIN_NAMESPACE
67
68namespace Qt3DRender {
69
70Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, geometryLoader, (QGeometryLoaderFactory_iid, QLatin1String("/geometryloaders"), Qt::CaseInsensitive))
71
72QMeshPrivate::QMeshPrivate()
73 : QGeometryRendererPrivate()
74 , m_status(QMesh::None)
75{
76}
77
78QMeshPrivate *QMeshPrivate::get(QMesh *q)
79{
80 return q->d_func();
81}
82
83void QMeshPrivate::setScene(Qt3DCore::QScene *scene)
84{
85 QGeometryRendererPrivate::setScene(scene);
86 updateFunctor();
87}
88
89void QMeshPrivate::updateFunctor()
90{
91 Q_Q(QMesh);
92 q->setGeometryFactory(QGeometryFactoryPtr(new MeshLoaderFunctor(q)));
93}
94
95void QMeshPrivate::setStatus(QMesh::Status status)
96{
97 if (m_status != status) {
98 Q_Q(QMesh);
99 m_status = status;
100 const bool wasBlocked = q->blockNotifications(true);
101 emit q->statusChanged(status);
102 q->blockNotifications(wasBlocked);
103 }
104}
105
106/*!
107 * \qmltype Mesh
108 * \instantiates Qt3DRender::QMesh
109 * \inqmlmodule Qt3D.Render
110 * \brief A custom mesh loader.
111 *
112 * Loads mesh data from external files in a variety of formats.
113 *
114 * In Qt3D 5.9, Mesh supports the following formats:
115 *
116 * \list
117 * \li Wavefront OBJ
118 * \li Stanford Triangle Format PLY
119 * \li STL (STereoLithography)
120 * \endlist
121 *
122 * QMesh will also support the following format if the SDK is installed and the fbx geometry loader plugin is built and found.
123 * \list
124 * \li Autodesk FBX
125 * \endlist
126 */
127
128/*!
129 * \qmlproperty url Mesh::source
130 *
131 * Holds the source url to the file containing the custom mesh.
132 */
133
134/*!
135 * \qmlproperty string Mesh::meshName
136 *
137 * Filter indicating which part of the mesh should be loaded.
138 *
139 * If meshName is empty (the default), then the entire mesh is loaded.
140 *
141 * If meshName is a plain string, then only the sub-mesh matching that name, if present, will be loaded.
142 *
143 * If meshName is a regular expression, than all sub-meshes matching the expression will be loaded.
144 *
145 * \note Only Wavefront OBJ files support sub-meshes.
146 *
147 * \sa QRegularExpression
148 */
149
150/*!
151 \enum QMesh::Status
152
153 This enum identifies the status of shader used.
154
155 \value None A source mesh hasn't been assigned a source yet
156 \value Loading The mesh geometry is loading
157 \value Ready The mesh geometry was successfully loaded
158 \value Error An error occurred while loading the mesh
159*/
160
161/*!
162 \qmlproperty enumeration Mesh::status
163
164 Holds the status of the mesh loading.
165 \sa Qt3DRender::QMesh::Status
166 \readonly
167 */
168
169/*!
170 * \class Qt3DRender::QMesh
171 * \inheaderfile Qt3DRender/QMesh
172 * \inmodule Qt3DRender
173 *
174 * \inherits Qt3DRender::QGeometryRenderer
175 *
176 * \brief A custom mesh loader.
177 *
178 * Loads mesh data from external files in a variety of formats.
179 * Qt3DRender::QMesh loads data into a single mesh.
180 *
181 * In Qt3D 5.9, QMesh supports the following formats:
182 *
183 * \list
184 * \li Wavefront OBJ
185 * \li Stanford Triangle Format PLY
186 * \li STL (STereoLithography)
187 * \endlist
188 *
189 * QMesh will also support the following format if the SDK is installed and the fbx geometry loader plugin is built and found:
190 * \list
191 * \li Autodesk FBX
192 * \endlist
193 *
194 * If you wish to load an entire scene made of several objects, you should rather use the Qt3DRender::QSceneLoader instead.
195 *
196 * \sa Qt3DRender::QSceneLoader
197 */
198
199/*!
200 * Constructs a new QMesh with \a parent.
201 */
202QMesh::QMesh(QNode *parent)
203 : QGeometryRenderer(*new QMeshPrivate, parent)
204{
205}
206
207/*! \internal */
208QMesh::~QMesh()
209{
210}
211
212/*! \internal */
213QMesh::QMesh(QMeshPrivate &dd, QNode *parent)
214 : QGeometryRenderer(dd, parent)
215{
216}
217
218/*! \internal */
219void QMesh::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change)
220{
221 Q_D(QMesh);
222 if (change->type() == Qt3DCore::PropertyUpdated) {
223 const Qt3DCore::QPropertyUpdatedChangePtr e = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(change);
224 if (e->propertyName() == QByteArrayLiteral("status"))
225 d->setStatus(e->value().value<QMesh::Status>());
226 }
227 Qt3DRender::QGeometryRenderer::sceneChangeEvent(change);
228}
229
230void QMesh::setSource(const QUrl& source)
231{
232 Q_D(QMesh);
233 if (d->m_source == source)
234 return;
235 d->m_source = source;
236 d->updateFunctor();
237 const bool blocked = blockNotifications(true);
238 emit sourceChanged(source);
239 blockNotifications(blocked);
240}
241
242/*!
243 * \property QMesh::source
244 *
245 * Holds the \a source url to the file containing the custom mesh.
246 */
247QUrl QMesh::source() const
248{
249 Q_D(const QMesh);
250 return d->m_source;
251}
252
253void QMesh::setMeshName(const QString &meshName)
254{
255 Q_D(QMesh);
256 if (d->m_meshName == meshName)
257 return;
258 d->m_meshName = meshName;
259 d->updateFunctor();
260 const bool blocked = blockNotifications(true);
261 emit meshNameChanged(meshName);
262 blockNotifications(blocked);
263}
264
265/*!
266 * \property QMesh::meshName
267 *
268 * Holds the name of the mesh.
269 */
270QString QMesh::meshName() const
271{
272 Q_D(const QMesh);
273 return d->m_meshName;
274}
275
276/*!
277 \property QMesh::status
278
279 Holds the status of the mesh loading.
280 \sa Qt3DRender::QMesh::Status
281 */
282QMesh::Status QMesh::status() const
283{
284 Q_D(const QMesh);
285 return d->m_status;
286}
287
288/*!
289 * \internal
290 */
291MeshLoaderFunctor::MeshLoaderFunctor(QMesh *mesh, const QByteArray &sourceData)
292 : QGeometryFactory()
293 , m_mesh(mesh->id())
294 , m_sourcePath(mesh->source())
295 , m_meshName(mesh->meshName())
296 , m_sourceData(sourceData)
297 , m_nodeManagers(nullptr)
298 , m_downloaderService(nullptr)
299 , m_status(QMesh::None)
300{
301}
302
303/*!
304 * \internal
305 */
306QGeometry *MeshLoaderFunctor::operator()()
307{
308 m_status = QMesh::Loading;
309
310 if (m_sourcePath.isEmpty()) {
311 qCWarning(Render::Jobs) << Q_FUNC_INFO << "Mesh is empty, nothing to load";
312 m_status = QMesh::Error;
313 return nullptr;
314 }
315
316 QStringList ext;
317 if (!Qt3DCore::QDownloadHelperService::isLocal(m_sourcePath)) {
318 if (m_sourceData.isEmpty()) {
319 if (m_mesh) {
320 // Output a warning in the case a user is calling the functor directly
321 // in the frontend
322 if (m_nodeManagers == nullptr || m_downloaderService == nullptr) {
323 qWarning() << "Mesh source points to a remote URL. Remotes meshes can only be loaded if the geometry is processed by the Qt3DRender backend";
324 m_status = QMesh::Error;
325 return nullptr;
326 }
327 Qt3DCore::QDownloadRequestPtr request(new MeshDownloadRequest(m_mesh, m_sourcePath, m_nodeManagers));
328 m_downloaderService->submitRequest(request);
329 }
330 return nullptr;
331 }
332
333 QMimeDatabase db;
334 QMimeType mtype = db.mimeTypeForData(m_sourceData);
335 if (mtype.isValid()) {
336 ext = mtype.suffixes();
337 }
338 QFileInfo finfo(m_sourcePath.path());
339 ext << finfo.suffix();
340 ext.removeAll(QLatin1String(""));
341 if (!ext.contains(QLatin1String("obj")))
342 ext << QLatin1String("obj");
343 } else {
344 QString filePath = Qt3DRender::QUrlHelper::urlToLocalFileOrQrc(m_sourcePath);
345 QFileInfo finfo(filePath);
346 if (finfo.suffix().isEmpty())
347 ext << QLatin1String("obj");
348 else
349 ext << finfo.suffix();
350 }
351
352 QScopedPointer<QGeometryLoaderInterface> loader;
353 for (const QString &e: qAsConst(ext)) {
354 loader.reset(qLoadPlugin<QGeometryLoaderInterface, QGeometryLoaderFactory>(geometryLoader(), e));
355 if (loader)
356 break;
357 }
358 if (!loader) {
359 qCWarning(Render::Jobs, "unsupported format encountered (%s)", qPrintable(ext.join(QLatin1String(", "))));
360 m_status = QMesh::Error;
361 return nullptr;
362 }
363
364 if (m_sourceData.isEmpty()) {
365 QString filePath = Qt3DRender::QUrlHelper::urlToLocalFileOrQrc(m_sourcePath);
366 QFile file(filePath);
367 if (!file.open(QIODevice::ReadOnly)) {
368 qCDebug(Render::Jobs) << "Could not open file" << filePath << "for reading";
369 m_status = QMesh::Error;
370 return nullptr;
371 }
372
373 if (loader->load(&file, m_meshName)) {
374 Qt3DRender::QGeometry *geometry = loader->geometry();
375 m_status = geometry != nullptr ? QMesh::Ready : QMesh::Error;
376 return geometry;
377 }
378 qCWarning(Render::Jobs) << Q_FUNC_INFO << "Mesh loading failure for:" << filePath;
379 } else {
380 QT_PREPEND_NAMESPACE(QBuffer) buffer(&m_sourceData);
381 if (!buffer.open(QIODevice::ReadOnly)) {
382 m_status = QMesh::Error;
383 return nullptr;
384 }
385
386 if (loader->load(&buffer, m_meshName)) {
387 Qt3DRender::QGeometry *geometry = loader->geometry();
388 m_status = geometry != nullptr ? QMesh::Ready : QMesh::Error;
389 return geometry;
390 }
391
392 qCWarning(Render::Jobs) << Q_FUNC_INFO << "Mesh loading failure for:" << m_sourcePath;
393 }
394
395 return nullptr;
396}
397
398/*!
399 * \internal
400 */
401bool MeshLoaderFunctor::operator ==(const QGeometryFactory &other) const
402{
403 const MeshLoaderFunctor *otherFunctor = functor_cast<MeshLoaderFunctor>(&other);
404 if (otherFunctor != nullptr)
405 return (otherFunctor->m_sourcePath == m_sourcePath &&
406 otherFunctor->m_sourceData.isEmpty() == m_sourceData.isEmpty() &&
407 otherFunctor->m_meshName == m_meshName &&
408 otherFunctor->m_downloaderService == m_downloaderService &&
409 otherFunctor->m_nodeManagers == m_nodeManagers);
410 return false;
411}
412
413/*!
414 * \internal
415 */
416MeshDownloadRequest::MeshDownloadRequest(Qt3DCore::QNodeId mesh, QUrl source, Render::NodeManagers *managers)
417 : Qt3DCore::QDownloadRequest(source)
418 , m_mesh(mesh)
419 , m_nodeManagers(managers)
420{
421}
422
423// Called in Aspect Thread context (not a Qt3D AspectJob)
424// We are sure that when this is called, no AspectJob are running
425void MeshDownloadRequest::onCompleted()
426{
427 if (cancelled() || !succeeded())
428 return;
429
430 if (!m_nodeManagers)
431 return;
432
433 Render::GeometryRenderer *renderer = m_nodeManagers->geometryRendererManager()->lookupResource(m_mesh);
434 if (!renderer)
435 return;
436
437 QGeometryFactoryPtr geometryFactory = renderer->geometryFactory();
438 if (!geometryFactory.isNull() && geometryFactory->id() == Qt3DRender::functorTypeId<MeshLoaderFunctor>()) {
439 QSharedPointer<MeshLoaderFunctor> functor = qSharedPointerCast<MeshLoaderFunctor>(geometryFactory);
440
441 // We make sure we are setting the result for the right request
442 // (the functor for the mesh could have changed in the meantime)
443 if (m_url == functor->sourcePath()) {
444 functor->setSourceData(m_data);
445
446 // mark the component as dirty so that the functor runs again in the correct job
447 m_nodeManagers->geometryRendererManager()->addDirtyGeometryRenderer(m_mesh);
448 }
449 }
450}
451
452} // namespace Qt3DRender
453
454QT_END_NAMESPACE
455