QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsgltf3dutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgltf3dutils.cpp
3 --------------------------------------
4 Date : July 2023
5 Copyright : (C) 2023 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#include "qgsgltf3dutils.h"
18
19#include <memory>
20
23#include "qgsgltfutils.h"
24#include "qgslogger.h"
26#include "qgstexturematerial.h"
27#include "qgsziputils.h"
28
29#include <QFile>
30#include <QFileInfo>
31#include <QMatrix4x4>
32#include <QString>
33#include <Qt3DCore/QAttribute>
34#include <Qt3DCore/QBuffer>
35#include <Qt3DCore/QEntity>
36#include <Qt3DCore/QGeometry>
37#include <Qt3DRender/QGeometryRenderer>
38#include <Qt3DRender/QTexture>
39
40using namespace Qt::StringLiterals;
41
43
44static Qt3DCore::QAttribute::VertexBaseType parseVertexBaseType( int componentType )
45{
46 switch ( componentType )
47 {
48 case TINYGLTF_COMPONENT_TYPE_BYTE:
49 return Qt3DCore::QAttribute::Byte;
50 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
51 return Qt3DCore::QAttribute::UnsignedByte;
52 case TINYGLTF_COMPONENT_TYPE_SHORT:
53 return Qt3DCore::QAttribute::Short;
54 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
55 return Qt3DCore::QAttribute::UnsignedShort;
56 case TINYGLTF_COMPONENT_TYPE_INT:
57 return Qt3DCore::QAttribute::Int;
58 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
59 return Qt3DCore::QAttribute::UnsignedInt;
60 case TINYGLTF_COMPONENT_TYPE_FLOAT:
61 return Qt3DCore::QAttribute::Float;
62 case TINYGLTF_COMPONENT_TYPE_DOUBLE:
63 return Qt3DCore::QAttribute::Double;
64 }
65 Q_ASSERT( false );
66 return Qt3DCore::QAttribute::UnsignedInt;
67}
68
69
70static Qt3DRender::QAbstractTexture::Filter parseTextureFilter( int filter )
71{
72 switch ( filter )
73 {
74 case TINYGLTF_TEXTURE_FILTER_NEAREST:
75 return Qt3DRender::QTexture2D::Nearest;
76 case TINYGLTF_TEXTURE_FILTER_LINEAR:
77 return Qt3DRender::QTexture2D::Linear;
78 case TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST:
79 return Qt3DRender::QTexture2D::NearestMipMapNearest;
80 case TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_NEAREST:
81 return Qt3DRender::QTexture2D::LinearMipMapNearest;
82 case TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_LINEAR:
83 return Qt3DRender::QTexture2D::NearestMipMapLinear;
84 case TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR:
85 return Qt3DRender::QTexture2D::LinearMipMapLinear;
86 }
87
88 // play it safe and handle malformed models
89 return Qt3DRender::QTexture2D::Nearest;
90}
91
92static Qt3DRender::QTextureWrapMode::WrapMode parseTextureWrapMode( int wrapMode )
93{
94 switch ( wrapMode )
95 {
96 case TINYGLTF_TEXTURE_WRAP_REPEAT:
97 return Qt3DRender::QTextureWrapMode::Repeat;
98 case TINYGLTF_TEXTURE_WRAP_CLAMP_TO_EDGE:
99 return Qt3DRender::QTextureWrapMode::ClampToEdge;
100 case TINYGLTF_TEXTURE_WRAP_MIRRORED_REPEAT:
101 return Qt3DRender::QTextureWrapMode::MirroredRepeat;
102 }
103 // some malformed GLTF models have incorrect texture wrap modes (eg
104 // https://qld.digitaltwin.terria.io/api/v0/data/b73ccb60-66ef-4470-8c3c-44af36c4d69b/CBD/tileset.json )
105 return Qt3DRender::QTextureWrapMode::Repeat;
106}
107
108
109static Qt3DCore::QAttribute *parseAttribute( tinygltf::Model &model, int accessorIndex )
110{
111 tinygltf::Accessor &accessor = model.accessors[accessorIndex];
112 tinygltf::BufferView &bv = model.bufferViews[accessor.bufferView];
113 tinygltf::Buffer &b = model.buffers[bv.buffer];
114
115 // TODO: only ever create one QBuffer for a buffer even if it is used multiple times
116 QByteArray byteArray( reinterpret_cast<const char *>( b.data.data() ),
117 static_cast<int>( b.data.size() ) ); // makes a deep copy
118 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer();
119 buffer->setData( byteArray );
120
121 Qt3DCore::QAttribute *attribute = new Qt3DCore::QAttribute();
122
123 // "target" is optional, can be zero
124 if ( bv.target == TINYGLTF_TARGET_ARRAY_BUFFER )
125 attribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
126 else if ( bv.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER )
127 attribute->setAttributeType( Qt3DCore::QAttribute::IndexAttribute );
128
129 attribute->setBuffer( buffer );
130 attribute->setByteOffset( bv.byteOffset + accessor.byteOffset );
131 attribute->setByteStride( bv.byteStride ); // could be zero, it seems that's fine (assuming packed)
132 attribute->setCount( accessor.count );
133 attribute->setVertexBaseType( parseVertexBaseType( accessor.componentType ) );
134 attribute->setVertexSize( tinygltf::GetNumComponentsInType( accessor.type ) );
135
136 return attribute;
137}
138
139
140static Qt3DCore::QAttribute *reprojectPositions( tinygltf::Model &model, int accessorIndex, const QgsGltf3DUtils::EntityTransform &transform, const QgsVector3D &tileTranslationEcef, QMatrix4x4 *matrix )
141{
142 tinygltf::Accessor &accessor = model.accessors[accessorIndex];
143
144 QVector<double> vx, vy, vz;
145 bool res = QgsGltfUtils::accessorToMapCoordinates( model, accessorIndex, transform.tileTransform, transform.ecefToTargetCrs, tileTranslationEcef, matrix, transform.gltfUpAxis, vx, vy, vz );
146 if ( !res )
147 return nullptr;
148
149 QByteArray byteArray;
150 byteArray.resize( accessor.count * 4 * 3 );
151 float *out = reinterpret_cast<float *>( byteArray.data() );
152
153 QgsVector3D sceneOrigin = transform.chunkOriginTargetCrs;
154 for ( int i = 0; i < static_cast<int>( accessor.count ); ++i )
155 {
156 double x = vx[i] - sceneOrigin.x();
157 double y = vy[i] - sceneOrigin.y();
158 double z = ( vz[i] * transform.zValueScale ) + transform.zValueOffset - sceneOrigin.z();
159
160 out[i * 3 + 0] = static_cast<float>( x );
161 out[i * 3 + 1] = static_cast<float>( y );
162 out[i * 3 + 2] = static_cast<float>( z );
163 }
164
165 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer();
166 buffer->setData( byteArray );
167
168 Qt3DCore::QAttribute *attribute = new Qt3DCore::QAttribute();
169 attribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
170 attribute->setBuffer( buffer );
171 attribute->setByteOffset( 0 );
172 attribute->setByteStride( 12 );
173 attribute->setCount( accessor.count );
174 attribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
175 attribute->setVertexSize( 3 );
176
177 return attribute;
178}
179
180class TinyGltfTextureImageDataGenerator : public Qt3DRender::QTextureImageDataGenerator
181{
182 public:
183 TinyGltfTextureImageDataGenerator( Qt3DRender::QTextureImageDataPtr imagePtr )
184 : mImagePtr( imagePtr ) {}
185
186 Qt3DRender::QTextureImageDataPtr operator()() override
187 {
188 return mImagePtr;
189 }
190
191 qintptr id() const override
192 {
193 return reinterpret_cast<qintptr>( &Qt3DCore::FunctorType<TinyGltfTextureImageDataGenerator>::id );
194 }
195
196 bool operator==( const QTextureImageDataGenerator &other ) const override
197 {
198 const TinyGltfTextureImageDataGenerator *otherFunctor = dynamic_cast<const TinyGltfTextureImageDataGenerator *>( &other );
199 return otherFunctor && mImagePtr.get() == otherFunctor->mImagePtr.get();
200 }
201
202 Qt3DRender::QTextureImageDataPtr mImagePtr;
203};
204
205class TinyGltfTextureImage : public Qt3DRender::QAbstractTextureImage
206{
207 Q_OBJECT
208 public:
209 TinyGltfTextureImage( tinygltf::Image &image )
210 {
211 Q_ASSERT( image.bits == 8 );
212 Q_ASSERT( image.component == 4 );
213 Q_ASSERT( image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE );
214
215 imgDataPtr.reset( new Qt3DRender::QTextureImageData );
216 imgDataPtr->setWidth( image.width );
217 imgDataPtr->setHeight( image.height );
218 imgDataPtr->setDepth( 1 ); // not sure what this is
219 imgDataPtr->setFaces( 1 );
220 imgDataPtr->setLayers( 1 );
221 imgDataPtr->setMipLevels( 1 );
222 QByteArray imageBytes( reinterpret_cast<const char *>( image.image.data() ), image.image.size() );
223 imgDataPtr->setData( imageBytes, 4 );
224 imgDataPtr->setFormat( QOpenGLTexture::RGBA8_UNorm );
225 imgDataPtr->setPixelFormat( QOpenGLTexture::BGRA ); // when using tinygltf with STB_image, pixel format is QOpenGLTexture::RGBA
226 imgDataPtr->setPixelType( QOpenGLTexture::UInt8 );
227 imgDataPtr->setTarget( QOpenGLTexture::Target2D );
228 }
229
230 Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const override
231 {
232 return Qt3DRender::QTextureImageDataGeneratorPtr( new TinyGltfTextureImageDataGenerator( imgDataPtr ) );
233 }
234
235 Qt3DRender::QTextureImageDataPtr imgDataPtr;
236};
237
238
239// TODO: move elsewhere
240static QByteArray fetchUri( const QUrl &url, QStringList *errors )
241{
242 if ( url.scheme().startsWith( "http" ) )
243 {
244 QNetworkRequest request = QNetworkRequest( url );
245 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
246 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
247 QgsBlockingNetworkRequest networkRequest;
248 // TODO: setup auth, setup headers
249 if ( networkRequest.get( request ) != QgsBlockingNetworkRequest::NoError )
250 {
251 if ( errors )
252 *errors << u"Failed to download image: %1"_s.arg( url.toString() );
253 }
254 else
255 {
256 const QgsNetworkReplyContent content = networkRequest.reply();
257 return content.content();
258 }
259 }
260 else if ( url.isLocalFile() )
261 {
262 QString localFilePath = url.toLocalFile();
263 if ( localFilePath.contains( ".slpk/" ) ) // we need to extract the image from SLPK archive
264 {
265 const QStringList parts = localFilePath.split( u".slpk/"_s );
266 if ( parts.size() == 2 )
267 {
268 QString slpkPath = parts[0] + ".slpk";
269 QString imagePath = parts[1];
270
271 QByteArray imageData;
272 if ( QgsZipUtils::extractFileFromZip( slpkPath, imagePath, imageData ) )
273 {
274 return imageData;
275 }
276 else
277 {
278 if ( errors )
279 *errors << u"Unable to extract image '%1' from SLPK archive: %2"_s.arg( imagePath ).arg( slpkPath );
280 }
281 }
282 else
283 {
284 if ( errors )
285 *errors << u"Missing image path in SLPK archive: %1"_s.arg( localFilePath );
286 }
287 }
288 else if ( QFile::exists( localFilePath ) )
289 {
290 QFile f( localFilePath );
291 if ( f.open( QIODevice::ReadOnly ) )
292 {
293 return f.readAll();
294 }
295 }
296 else
297 {
298 if ( errors )
299 *errors << u"Unable to open image: %1"_s.arg( url.toString() );
300 }
301 }
302 return QByteArray();
303}
304
305// Returns NULLPTR if primitive should not be rendered
306static QgsMaterial *parseMaterial( tinygltf::Model &model, int materialIndex, QString baseUri, QStringList *errors )
307{
308 if ( materialIndex < 0 )
309 {
310 // material unspecified - using default
311 QgsMetalRoughMaterial *defaultMaterial = new QgsMetalRoughMaterial;
312 defaultMaterial->setMetalness( 1 );
313 defaultMaterial->setRoughness( 1 );
314 defaultMaterial->setBaseColor( QColor::fromRgbF( 1, 1, 1 ) );
315 return defaultMaterial;
316 }
317
318 tinygltf::Material &material = model.materials[materialIndex];
319 tinygltf::PbrMetallicRoughness &pbr = material.pbrMetallicRoughness;
320
321 if ( pbr.baseColorTexture.index >= 0 )
322 {
323 tinygltf::Texture &tex = model.textures[pbr.baseColorTexture.index];
324
325 // Source can be undefined if texture is provided by an extension
326 if ( tex.source < 0 )
327 {
328 QgsMetalRoughMaterial *pbrMaterial = new QgsMetalRoughMaterial;
329 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
330 pbrMaterial->setRoughness( pbr.roughnessFactor );
331 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
332 return pbrMaterial;
333 }
334
335 tinygltf::Image &img = model.images[tex.source];
336
337 if ( !img.uri.empty() )
338 {
339 QString imgUri = QString::fromStdString( img.uri );
340 QUrl url = QUrl( baseUri ).resolved( imgUri );
341 QByteArray ba = fetchUri( url, errors );
342 if ( !ba.isEmpty() )
343 {
344 if ( !QgsGltfUtils::loadImageDataWithQImage( &img, -1, nullptr, nullptr, 0, 0, ( const unsigned char * ) ba.constData(), ba.size(), nullptr ) )
345 {
346 if ( errors )
347 *errors << u"Failed to load image: %1"_s.arg( imgUri );
348 }
349 }
350 }
351
352 if ( img.image.empty() )
353 {
354 QgsMetalRoughMaterial *pbrMaterial = new QgsMetalRoughMaterial;
355 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
356 pbrMaterial->setRoughness( pbr.roughnessFactor );
357 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
358 return pbrMaterial;
359 }
360
361 TinyGltfTextureImage *textureImage = new TinyGltfTextureImage( img );
362
363 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
364 texture->addTextureImage( textureImage ); // textures take the ownership of textureImage if has no parant
365
366 // let's use linear (rather than nearest) filtering by default to avoid blocky look of textures
367 texture->setMinificationFilter( Qt3DRender::QTexture2D::Linear );
368 texture->setMagnificationFilter( Qt3DRender::QTexture2D::Linear );
369
370 if ( tex.sampler >= 0 )
371 {
372 tinygltf::Sampler &sampler = model.samplers[tex.sampler];
373 if ( sampler.minFilter >= 0 )
374 texture->setMinificationFilter( parseTextureFilter( sampler.minFilter ) );
375 if ( sampler.magFilter >= 0 )
376 texture->setMagnificationFilter( parseTextureFilter( sampler.magFilter ) );
377 Qt3DRender::QTextureWrapMode wrapMode;
378 wrapMode.setX( parseTextureWrapMode( sampler.wrapS ) );
379 wrapMode.setY( parseTextureWrapMode( sampler.wrapT ) );
380 texture->setWrapMode( wrapMode );
381 }
382
383 // We should be using PBR material unless unlit material is requested using KHR_materials_unlit
384 // GLTF extension, but in various datasets that extension is not used (even though it should have been).
385 // In the future we may want to have a switch whether to use unlit material or PBR material...
386 QgsTextureMaterial *mat = new QgsTextureMaterial;
387 mat->setTexture( texture );
388 return mat;
389 }
390
391 if ( qgsDoubleNear( pbr.baseColorFactor[3], 0 ) )
392 return nullptr; // completely transparent primitive, just skip it
393
394 QgsMetalRoughMaterial *pbrMaterial = new QgsMetalRoughMaterial;
395 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
396 pbrMaterial->setRoughness( pbr.roughnessFactor );
397 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
398 return pbrMaterial;
399}
400
401
402static QVector<Qt3DCore::QEntity *> parseNode( tinygltf::Model &model, int nodeIndex, const QgsGltf3DUtils::EntityTransform &transform, const QgsVector3D &tileTranslationEcef, QString baseUri, QMatrix4x4 parentTransform, QStringList *errors )
403{
404 tinygltf::Node &node = model.nodes[nodeIndex];
405
406 QVector<Qt3DCore::QEntity *> entities;
407
408 // transform
409 std::unique_ptr<QMatrix4x4> matrix = QgsGltfUtils::parseNodeTransform( node );
410 if ( !parentTransform.isIdentity() )
411 {
412 if ( matrix )
413 *matrix = parentTransform * *matrix;
414 else
415 {
416 matrix = std::make_unique<QMatrix4x4>( parentTransform );
417 }
418 }
419
420 // mesh
421 if ( node.mesh >= 0 )
422 {
423 tinygltf::Mesh &mesh = model.meshes[node.mesh];
424
425 for ( const tinygltf::Primitive &primitive : mesh.primitives )
426 {
427 if ( primitive.mode != TINYGLTF_MODE_TRIANGLES )
428 {
429 if ( errors )
430 *errors << u"Unsupported mesh primitive: %1"_s.arg( primitive.mode );
431 continue;
432 }
433
434 auto posIt = primitive.attributes.find( "POSITION" );
435 Q_ASSERT( posIt != primitive.attributes.end() );
436 int positionAccessorIndex = posIt->second;
437
438 tinygltf::Accessor &posAccessor = model.accessors[positionAccessorIndex];
439 if ( posAccessor.componentType != TINYGLTF_PARAMETER_TYPE_FLOAT || posAccessor.type != TINYGLTF_TYPE_VEC3 )
440 {
441 if ( errors )
442 *errors << u"Unsupported position accessor type: %1 / %2"_s.arg( posAccessor.componentType ).arg( posAccessor.type );
443 continue;
444 }
445
446 QgsMaterial *material = parseMaterial( model, primitive.material, baseUri, errors );
447 if ( !material )
448 {
449 // primitive should be skipped, eg fully transparent material
450 continue;
451 }
452
453 Qt3DCore::QGeometry *geom = new Qt3DCore::QGeometry;
454
455 Qt3DCore::QAttribute *positionAttribute = reprojectPositions( model, positionAccessorIndex, transform, tileTranslationEcef, matrix.get() );
456 positionAttribute->setName( Qt3DCore::QAttribute::defaultPositionAttributeName() );
457 geom->addAttribute( positionAttribute );
458
459 auto normalIt = primitive.attributes.find( "NORMAL" );
460 if ( normalIt != primitive.attributes.end() )
461 {
462 int normalAccessorIndex = normalIt->second;
463 Qt3DCore::QAttribute *normalAttribute = parseAttribute( model, normalAccessorIndex );
464 normalAttribute->setName( Qt3DCore::QAttribute::defaultNormalAttributeName() );
465 geom->addAttribute( normalAttribute );
466
467 // TODO: we may need to transform normal vectors when we are altering positions
468 // (but quite often normals are actually note needed - e.g. when using textured data)
469 }
470
471 auto texIt = primitive.attributes.find( "TEXCOORD_0" );
472 if ( texIt != primitive.attributes.end() )
473 {
474 int texAccessorIndex = texIt->second;
475 Qt3DCore::QAttribute *texAttribute = parseAttribute( model, texAccessorIndex );
476 texAttribute->setName( Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName() );
477 geom->addAttribute( texAttribute );
478 }
479
480 Qt3DCore::QAttribute *indexAttribute = nullptr;
481 if ( primitive.indices != -1 )
482 {
483 indexAttribute = parseAttribute( model, primitive.indices );
484 geom->addAttribute( indexAttribute );
485 }
486
487 Qt3DRender::QGeometryRenderer *geomRenderer = new Qt3DRender::QGeometryRenderer;
488 geomRenderer->setGeometry( geom );
489 geomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Triangles ); // looks like same values as "mode"
490 geomRenderer->setVertexCount( indexAttribute ? indexAttribute->count() : model.accessors[positionAccessorIndex].count );
491
492 // if we are using PBR material, and normal vectors are not present in the data,
493 // they should be auto-generated by us (according to GLTF spec)
494 if ( normalIt == primitive.attributes.end() )
495 {
496 if ( QgsMetalRoughMaterial *pbrMat = qobject_cast<QgsMetalRoughMaterial *>( material ) )
497 {
498 pbrMat->setFlatShadingEnabled( true );
499 }
500 }
501
502 Qt3DCore::QEntity *primitiveEntity = new Qt3DCore::QEntity;
503 primitiveEntity->addComponent( geomRenderer );
504 primitiveEntity->addComponent( material );
505 entities << primitiveEntity;
506 }
507 }
508
509 // recursively add children
510 for ( int childNodeIndex : node.children )
511 {
512 entities << parseNode( model, childNodeIndex, transform, tileTranslationEcef, baseUri, matrix ? *matrix : QMatrix4x4(), errors );
513 }
514
515 return entities;
516}
517
518
519Qt3DCore::QEntity *QgsGltf3DUtils::parsedGltfToEntity( tinygltf::Model &model, const QgsGltf3DUtils::EntityTransform &transform, QString baseUri, QStringList *errors )
520{
521 bool sceneOk = false;
522 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
523 if ( !sceneOk )
524 {
525 if ( errors )
526 *errors << "No scenes present in the gltf data!";
527 return nullptr;
528 }
529
530 tinygltf::Scene &scene = model.scenes[sceneIndex];
531
532 if ( scene.nodes.size() == 0 )
533 {
534 if ( errors )
535 *errors << "No nodes present in the gltf data!";
536 return nullptr;
537 }
538
539 const QgsVector3D tileTranslationEcef = QgsGltfUtils::extractTileTranslation( model );
540
541 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
542 for ( const int nodeIndex : scene.nodes )
543 {
544 const QVector<Qt3DCore::QEntity *> entities = parseNode( model, nodeIndex, transform, tileTranslationEcef, baseUri, QMatrix4x4(), errors );
545 for ( Qt3DCore::QEntity *e : entities )
546 e->setParent( rootEntity );
547 }
548 return rootEntity;
549}
550
551
552Qt3DCore::QEntity *QgsGltf3DUtils::gltfToEntity( const QByteArray &data, const QgsGltf3DUtils::EntityTransform &transform, const QString &baseUri, QStringList *errors )
553{
554 tinygltf::Model model;
555 QString gltfErrors, gltfWarnings;
556
557 bool res = QgsGltfUtils::loadGltfModel( data, model, &gltfErrors, &gltfWarnings );
558 if ( !gltfErrors.isEmpty() )
559 {
560 QgsDebugError( u"Error raised reading %1: %2"_s.arg( baseUri, gltfErrors ) );
561 }
562 if ( !gltfWarnings.isEmpty() )
563 {
564 QgsDebugError( u"Warnings raised reading %1: %2"_s.arg( baseUri, gltfWarnings ) );
565 }
566 if ( !res )
567 {
568 if ( errors )
569 {
570 errors->append( u"GLTF load error: "_s + gltfErrors );
571 }
572 return nullptr;
573 }
574
575 return parsedGltfToEntity( model, transform, baseUri, errors );
576}
577
578// For TinyGltfTextureImage
579#include "qgsgltf3dutils.moc"
580
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
Base class for all materials used within QGIS 3D views.
Definition qgsmaterial.h:39
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:52
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:54
double x() const
Returns X coordinate.
Definition qgsvector3d.h:50
static bool extractFileFromZip(const QString &zipFilename, const QString &filenameInZip, QByteArray &bytesOut)
Extracts a file from a zip archive, returns true on success.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
#define QgsDebugError(str)
Definition qgslogger.h:59