QGIS API Documentation 4.1.0-Master (9af12b5a203)
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
21#include "qgs3dutils.h"
24#include "qgsgltfutils.h"
25#include "qgslogger.h"
28#include "qgstexturematerial.h"
29#include "qgsziputils.h"
30
31#include <QFile>
32#include <QFileInfo>
33#include <QMatrix4x4>
34#include <QString>
35#include <Qt3DCore/QAttribute>
36#include <Qt3DCore/QBuffer>
37#include <Qt3DCore/QEntity>
38#include <Qt3DCore/QGeometry>
39#include <Qt3DRender/QGeometryRenderer>
40#include <Qt3DRender/QTexture>
41
42using namespace Qt::StringLiterals;
43
45
46static Qt3DCore::QAttribute::VertexBaseType parseVertexBaseType( int componentType )
47{
48 switch ( componentType )
49 {
50 case TINYGLTF_COMPONENT_TYPE_BYTE:
51 return Qt3DCore::QAttribute::Byte;
52 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
53 return Qt3DCore::QAttribute::UnsignedByte;
54 case TINYGLTF_COMPONENT_TYPE_SHORT:
55 return Qt3DCore::QAttribute::Short;
56 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
57 return Qt3DCore::QAttribute::UnsignedShort;
58 case TINYGLTF_COMPONENT_TYPE_INT:
59 return Qt3DCore::QAttribute::Int;
60 case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
61 return Qt3DCore::QAttribute::UnsignedInt;
62 case TINYGLTF_COMPONENT_TYPE_FLOAT:
63 return Qt3DCore::QAttribute::Float;
64 case TINYGLTF_COMPONENT_TYPE_DOUBLE:
65 return Qt3DCore::QAttribute::Double;
66 }
67 Q_ASSERT( false );
68 return Qt3DCore::QAttribute::UnsignedInt;
69}
70
71
72static Qt3DRender::QAbstractTexture::Filter parseTextureFilter( int filter )
73{
74 switch ( filter )
75 {
76 case TINYGLTF_TEXTURE_FILTER_NEAREST:
77 return Qt3DRender::QTexture2D::Nearest;
78 case TINYGLTF_TEXTURE_FILTER_LINEAR:
79 return Qt3DRender::QTexture2D::Linear;
80 case TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST:
81 return Qt3DRender::QTexture2D::NearestMipMapNearest;
82 case TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_NEAREST:
83 return Qt3DRender::QTexture2D::LinearMipMapNearest;
84 case TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_LINEAR:
85 return Qt3DRender::QTexture2D::NearestMipMapLinear;
86 case TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR:
87 return Qt3DRender::QTexture2D::LinearMipMapLinear;
88 }
89
90 // play it safe and handle malformed models
91 return Qt3DRender::QTexture2D::Nearest;
92}
93
94static Qt3DRender::QTextureWrapMode::WrapMode parseTextureWrapMode( int wrapMode )
95{
96 switch ( wrapMode )
97 {
98 case TINYGLTF_TEXTURE_WRAP_REPEAT:
99 return Qt3DRender::QTextureWrapMode::Repeat;
100 case TINYGLTF_TEXTURE_WRAP_CLAMP_TO_EDGE:
101 return Qt3DRender::QTextureWrapMode::ClampToEdge;
102 case TINYGLTF_TEXTURE_WRAP_MIRRORED_REPEAT:
103 return Qt3DRender::QTextureWrapMode::MirroredRepeat;
104 }
105 // some malformed GLTF models have incorrect texture wrap modes (eg
106 // https://qld.digitaltwin.terria.io/api/v0/data/b73ccb60-66ef-4470-8c3c-44af36c4d69b/CBD/tileset.json )
107 return Qt3DRender::QTextureWrapMode::Repeat;
108}
109
110
111static Qt3DCore::QAttribute *parseAttribute( tinygltf::Model &model, int accessorIndex )
112{
113 tinygltf::Accessor &accessor = model.accessors[accessorIndex];
114 tinygltf::BufferView &bv = model.bufferViews[accessor.bufferView];
115 tinygltf::Buffer &b = model.buffers[bv.buffer];
116
117 // TODO: only ever create one QBuffer for a buffer even if it is used multiple times
118 QByteArray byteArray( reinterpret_cast<const char *>( b.data.data() ),
119 static_cast<int>( b.data.size() ) ); // makes a deep copy
120 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer();
121 buffer->setData( byteArray );
122
123 Qt3DCore::QAttribute *attribute = new Qt3DCore::QAttribute();
124
125 // "target" is optional, can be zero
126 if ( bv.target == TINYGLTF_TARGET_ARRAY_BUFFER )
127 attribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
128 else if ( bv.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER )
129 attribute->setAttributeType( Qt3DCore::QAttribute::IndexAttribute );
130
131 attribute->setBuffer( buffer );
132 attribute->setByteOffset( bv.byteOffset + accessor.byteOffset );
133 attribute->setByteStride( bv.byteStride ); // could be zero, it seems that's fine (assuming packed)
134 attribute->setCount( accessor.count );
135 attribute->setVertexBaseType( parseVertexBaseType( accessor.componentType ) );
136 attribute->setVertexSize( tinygltf::GetNumComponentsInType( accessor.type ) );
137
138 return attribute;
139}
140
141
142static Qt3DCore::QAttribute *reprojectPositions( tinygltf::Model &model, int accessorIndex, const QgsGltf3DUtils::EntityTransform &transform, const QgsVector3D &tileTranslationEcef, QMatrix4x4 *matrix )
143{
144 tinygltf::Accessor &accessor = model.accessors[accessorIndex];
145
146 QVector<double> vx, vy, vz;
147 bool res = QgsGltfUtils::accessorToMapCoordinates( model, accessorIndex, transform.tileTransform, transform.ecefToTargetCrs, tileTranslationEcef, matrix, transform.gltfUpAxis, vx, vy, vz );
148 if ( !res )
149 return nullptr;
150
151 QByteArray byteArray;
152 byteArray.resize( accessor.count * 4 * 3 );
153 float *out = reinterpret_cast<float *>( byteArray.data() );
154
155 QgsVector3D sceneOrigin = transform.chunkOriginTargetCrs;
156 for ( int i = 0; i < static_cast<int>( accessor.count ); ++i )
157 {
158 double x = vx[i] - sceneOrigin.x();
159 double y = vy[i] - sceneOrigin.y();
160 double z = ( vz[i] * transform.zValueScale ) + transform.zValueOffset - sceneOrigin.z();
161
162 out[i * 3 + 0] = static_cast<float>( x );
163 out[i * 3 + 1] = static_cast<float>( y );
164 out[i * 3 + 2] = static_cast<float>( z );
165 }
166
167 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer();
168 buffer->setData( byteArray );
169
170 Qt3DCore::QAttribute *attribute = new Qt3DCore::QAttribute();
171 attribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
172 attribute->setBuffer( buffer );
173 attribute->setByteOffset( 0 );
174 attribute->setByteStride( 12 );
175 attribute->setCount( accessor.count );
176 attribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
177 attribute->setVertexSize( 3 );
178
179 return attribute;
180}
181
182class TinyGltfTextureImageDataGenerator : public Qt3DRender::QTextureImageDataGenerator
183{
184 public:
185 TinyGltfTextureImageDataGenerator( Qt3DRender::QTextureImageDataPtr imagePtr )
186 : mImagePtr( imagePtr )
187 {}
188
189 Qt3DRender::QTextureImageDataPtr operator()() override { return mImagePtr; }
190
191 qintptr id() const override { return reinterpret_cast<qintptr>( &Qt3DCore::FunctorType<TinyGltfTextureImageDataGenerator>::id ); }
192
193 bool operator==( const QTextureImageDataGenerator &other ) const override
194 {
195 const TinyGltfTextureImageDataGenerator *otherFunctor = dynamic_cast<const TinyGltfTextureImageDataGenerator *>( &other );
196 return otherFunctor && mImagePtr.get() == otherFunctor->mImagePtr.get();
197 }
198
199 Qt3DRender::QTextureImageDataPtr mImagePtr;
200};
201
202class TinyGltfTextureImage : public Qt3DRender::QAbstractTextureImage
203{
204 Q_OBJECT
205 public:
206 TinyGltfTextureImage( tinygltf::Image &image )
207 {
208 Q_ASSERT( image.bits == 8 );
209 Q_ASSERT( image.component == 4 );
210 Q_ASSERT( image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE );
211
212 imgDataPtr.reset( new Qt3DRender::QTextureImageData );
213 imgDataPtr->setWidth( image.width );
214 imgDataPtr->setHeight( image.height );
215 imgDataPtr->setDepth( 1 ); // not sure what this is
216 imgDataPtr->setFaces( 1 );
217 imgDataPtr->setLayers( 1 );
218 imgDataPtr->setMipLevels( 1 );
219 QByteArray imageBytes( reinterpret_cast<const char *>( image.image.data() ), image.image.size() );
220 imgDataPtr->setData( imageBytes, 4 );
221 imgDataPtr->setFormat( QOpenGLTexture::RGBA8_UNorm );
222 imgDataPtr->setPixelFormat( QOpenGLTexture::BGRA ); // when using tinygltf with STB_image, pixel format is QOpenGLTexture::RGBA
223 imgDataPtr->setPixelType( QOpenGLTexture::UInt8 );
224 imgDataPtr->setTarget( QOpenGLTexture::Target2D );
225 }
226
227 Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const override { return Qt3DRender::QTextureImageDataGeneratorPtr( new TinyGltfTextureImageDataGenerator( imgDataPtr ) ); }
228
229 Qt3DRender::QTextureImageDataPtr imgDataPtr;
230};
231
232
233// TODO: move elsewhere
234static QByteArray fetchUri( const QUrl &url, QStringList *errors )
235{
236 if ( url.scheme().startsWith( "http" ) )
237 {
238 QNetworkRequest request = QNetworkRequest( url );
239 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
240 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
241 QgsBlockingNetworkRequest networkRequest;
242 // TODO: setup auth, setup headers
243 if ( networkRequest.get( request ) != QgsBlockingNetworkRequest::NoError )
244 {
245 if ( errors )
246 *errors << u"Failed to download image: %1"_s.arg( url.toString() );
247 }
248 else
249 {
250 const QgsNetworkReplyContent content = networkRequest.reply();
251 return content.content();
252 }
253 }
254 else if ( url.isLocalFile() )
255 {
256 QString localFilePath = url.toLocalFile();
257 if ( localFilePath.contains( ".slpk/" ) ) // we need to extract the image from SLPK archive
258 {
259 const QStringList parts = localFilePath.split( u".slpk/"_s );
260 if ( parts.size() == 2 )
261 {
262 QString slpkPath = parts[0] + ".slpk";
263 QString imagePath = parts[1];
264
265 QByteArray imageData;
266 if ( QgsZipUtils::extractFileFromZip( slpkPath, imagePath, imageData ) )
267 {
268 return imageData;
269 }
270 else
271 {
272 if ( errors )
273 *errors << u"Unable to extract image '%1' from SLPK archive: %2"_s.arg( imagePath ).arg( slpkPath );
274 }
275 }
276 else
277 {
278 if ( errors )
279 *errors << u"Missing image path in SLPK archive: %1"_s.arg( localFilePath );
280 }
281 }
282 else if ( QFile::exists( localFilePath ) )
283 {
284 QFile f( localFilePath );
285 if ( f.open( QIODevice::ReadOnly ) )
286 {
287 return f.readAll();
288 }
289 }
290 else
291 {
292 if ( errors )
293 *errors << u"Unable to open image: %1"_s.arg( url.toString() );
294 }
295 }
296 return QByteArray();
297}
298
299// Returns NULLPTR if primitive should not be rendered
300static std::unique_ptr<QgsMaterial> parseMaterial( tinygltf::Model &model, int materialIndex, QString baseUri, QStringList *errors, const QgsMaterialContext &context )
301{
302 if ( materialIndex < 0 )
303 {
304 // material unspecified - using default
305 auto defaultMaterial = std::make_unique<QgsMetalRoughMaterial>();
306 defaultMaterial->setMetalness( 1 );
307 defaultMaterial->setRoughness( 1 );
308 defaultMaterial->setBaseColor( QColor::fromRgbF( 1, 1, 1 ) );
309 return defaultMaterial;
310 }
311
312 tinygltf::Material &material = model.materials[materialIndex];
313 tinygltf::PbrMetallicRoughness &pbr = material.pbrMetallicRoughness;
314
315 if ( pbr.baseColorTexture.index >= 0 )
316 {
317 tinygltf::Texture &tex = model.textures[pbr.baseColorTexture.index];
318
319 // Source can be undefined if texture is provided by an extension
320 if ( tex.source < 0 )
321 {
322 auto pbrMaterial = std::make_unique<QgsMetalRoughMaterial>();
323 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
324 pbrMaterial->setRoughness( pbr.roughnessFactor );
325 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
326 return pbrMaterial;
327 }
328
329 tinygltf::Image &img = model.images[tex.source];
330
331 if ( !img.uri.empty() )
332 {
333 QString imgUri = QString::fromStdString( img.uri );
334 QUrl url = QUrl( baseUri ).resolved( imgUri );
335 QByteArray ba = fetchUri( url, errors );
336 if ( !ba.isEmpty() )
337 {
338 if ( !QgsGltfUtils::loadImageDataWithQImage( &img, -1, nullptr, nullptr, 0, 0, ( const unsigned char * ) ba.constData(), ba.size(), nullptr ) )
339 {
340 if ( errors )
341 *errors << u"Failed to load image: %1"_s.arg( imgUri );
342 }
343 }
344 }
345
346 if ( img.image.empty() )
347 {
348 auto pbrMaterial = std::make_unique<QgsMetalRoughMaterial>();
349 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
350 pbrMaterial->setRoughness( pbr.roughnessFactor );
351 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
352 return pbrMaterial;
353 }
354
355 TinyGltfTextureImage *textureImage = new TinyGltfTextureImage( img );
356
357 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
358 texture->addTextureImage( textureImage ); // textures take the ownership of textureImage if has no parant
359
360 Qgs3DUtils::setTextureFiltering( texture, context );
361
362 texture->setFormat( Qt3DRender::QAbstractTexture::SRGB8_Alpha8 );
363
364 if ( tex.sampler >= 0 )
365 {
366 tinygltf::Sampler &sampler = model.samplers[tex.sampler];
367 if ( sampler.minFilter >= 0 )
368 texture->setMinificationFilter( parseTextureFilter( sampler.minFilter ) );
369 if ( sampler.magFilter >= 0 )
370 texture->setMagnificationFilter( parseTextureFilter( sampler.magFilter ) );
371 Qt3DRender::QTextureWrapMode wrapMode;
372 wrapMode.setX( parseTextureWrapMode( sampler.wrapS ) );
373 wrapMode.setY( parseTextureWrapMode( sampler.wrapT ) );
374 texture->setWrapMode( wrapMode );
375 }
376
377 // We should be using PBR material unless unlit material is requested using KHR_materials_unlit
378 // GLTF extension, but in various datasets that extension is not used (even though it should have been).
379 // In the future we may want to have a switch whether to use unlit material or PBR material...
380 auto mat = std::make_unique<QgsTextureMaterial>();
381 mat->setTexture( texture );
382 return mat;
383 }
384
385 if ( qgsDoubleNear( pbr.baseColorFactor[3], 0 ) )
386 return nullptr; // completely transparent primitive, just skip it
387
388 auto pbrMaterial = std::make_unique<QgsMetalRoughMaterial>();
389 pbrMaterial->setMetalness( pbr.metallicFactor ); // [0..1] or texture
390 pbrMaterial->setRoughness( pbr.roughnessFactor );
391 pbrMaterial->setBaseColor( QColor::fromRgbF( pbr.baseColorFactor[0], pbr.baseColorFactor[1], pbr.baseColorFactor[2], pbr.baseColorFactor[3] ) );
392 return pbrMaterial;
393}
394
395
396static QVector<Qt3DCore::QEntity *> parseNode(
397 tinygltf::Model &model,
398 int nodeIndex,
399 const QgsGltf3DUtils::EntityTransform &transform,
400 const QgsVector3D &tileTranslationEcef,
401 QString baseUri,
402 QMatrix4x4 parentTransform,
403 const Qgs3DRenderContext &context,
404 QStringList *errors
405)
406{
408 tinygltf::Node &node = model.nodes[nodeIndex];
409
410 QVector<Qt3DCore::QEntity *> entities;
411
412 // transform
413 std::unique_ptr<QMatrix4x4> matrix = QgsGltfUtils::parseNodeTransform( node );
414 if ( !parentTransform.isIdentity() )
415 {
416 if ( matrix )
417 *matrix = parentTransform * *matrix;
418 else
419 {
420 matrix = std::make_unique<QMatrix4x4>( parentTransform );
421 }
422 }
423
424 // mesh — skip nodes with EXT_mesh_gpu_instancing (handled separately by createInstancedEntities)
425 if ( node.mesh >= 0 && node.extensions.find( "EXT_mesh_gpu_instancing" ) == node.extensions.end() )
426 {
427 tinygltf::Mesh &mesh = model.meshes[node.mesh];
428
429 for ( const tinygltf::Primitive &primitive : mesh.primitives )
430 {
431 if ( primitive.mode != TINYGLTF_MODE_TRIANGLES )
432 {
433 if ( errors )
434 *errors << u"Unsupported mesh primitive: %1"_s.arg( primitive.mode );
435 continue;
436 }
437
438 auto posIt = primitive.attributes.find( "POSITION" );
439 Q_ASSERT( posIt != primitive.attributes.end() );
440 int positionAccessorIndex = posIt->second;
441
442 tinygltf::Accessor &posAccessor = model.accessors[positionAccessorIndex];
443 if ( posAccessor.componentType != TINYGLTF_PARAMETER_TYPE_FLOAT || posAccessor.type != TINYGLTF_TYPE_VEC3 )
444 {
445 if ( errors )
446 *errors << u"Unsupported position accessor type: %1 / %2"_s.arg( posAccessor.componentType ).arg( posAccessor.type );
447 continue;
448 }
449
450 std::unique_ptr<QgsMaterial> material = parseMaterial( model, primitive.material, baseUri, errors, materialContext );
451 if ( !material )
452 {
453 // primitive should be skipped, eg fully transparent material
454 continue;
455 }
456
457 Qt3DCore::QGeometry *geom = new Qt3DCore::QGeometry;
458
459 Qt3DCore::QAttribute *positionAttribute = reprojectPositions( model, positionAccessorIndex, transform, tileTranslationEcef, matrix.get() );
460 positionAttribute->setName( Qt3DCore::QAttribute::defaultPositionAttributeName() );
461 geom->addAttribute( positionAttribute );
462
463 auto normalIt = primitive.attributes.find( "NORMAL" );
464 if ( normalIt != primitive.attributes.end() )
465 {
466 int normalAccessorIndex = normalIt->second;
467 Qt3DCore::QAttribute *normalAttribute = parseAttribute( model, normalAccessorIndex );
468 normalAttribute->setName( Qt3DCore::QAttribute::defaultNormalAttributeName() );
469 geom->addAttribute( normalAttribute );
470
471 // TODO: we may need to transform normal vectors when we are altering positions
472 // (but quite often normals are actually note needed - e.g. when using textured data)
473 }
474
475 auto texIt = primitive.attributes.find( "TEXCOORD_0" );
476 if ( texIt != primitive.attributes.end() )
477 {
478 int texAccessorIndex = texIt->second;
479 Qt3DCore::QAttribute *texAttribute = parseAttribute( model, texAccessorIndex );
480 texAttribute->setName( Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName() );
481 geom->addAttribute( texAttribute );
482 }
483
484 Qt3DCore::QAttribute *indexAttribute = nullptr;
485 if ( primitive.indices != -1 )
486 {
487 indexAttribute = parseAttribute( model, primitive.indices );
488 geom->addAttribute( indexAttribute );
489 }
490
491 Qt3DRender::QGeometryRenderer *geomRenderer = new Qt3DRender::QGeometryRenderer;
492 geomRenderer->setGeometry( geom );
493 geomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Triangles ); // looks like same values as "mode"
494 geomRenderer->setVertexCount( indexAttribute ? indexAttribute->count() : model.accessors[positionAccessorIndex].count );
495
496 // if we are using PBR material, and normal vectors are not present in the data,
497 // they should be auto-generated by us (according to GLTF spec)
498 if ( normalIt == primitive.attributes.end() )
499 {
500 if ( QgsMetalRoughMaterial *pbrMat = qobject_cast<QgsMetalRoughMaterial *>( material.get() ) )
501 {
502 pbrMat->setFlatShadingEnabled( true );
503 }
504 }
505
506 Qt3DCore::QEntity *primitiveEntity = new Qt3DCore::QEntity;
507 primitiveEntity->addComponent( geomRenderer );
508 primitiveEntity->addComponent( material.release() );
509 entities << primitiveEntity;
510 }
511 }
512
513 // recursively add children
514 for ( int childNodeIndex : node.children )
515 {
516 entities << parseNode( model, childNodeIndex, transform, tileTranslationEcef, baseUri, matrix ? *matrix : QMatrix4x4(), context, errors );
517 }
518
519 return entities;
520}
521
522
526static QgsMatrix4x4 floatToDoubleMatrix( const QMatrix4x4 &fm )
527{
528 const float *f = fm.constData(); // column-major
529 return QgsMatrix4x4( f[0], f[4], f[8], f[12], f[1], f[5], f[9], f[13], f[2], f[6], f[10], f[14], f[3], f[7], f[11], f[15] );
530}
531
532
536static QMatrix3x3 matrixFromColumns( const QVector3D &col0, const QVector3D &col1, const QVector3D &col2 )
537{
538 // QMatrix3x3 constructor takes row-major data
539 const float d[9] = {
540 col0.x(),
541 col1.x(),
542 col2.x(),
543 col0.y(),
544 col1.y(),
545 col2.y(),
546 col0.z(),
547 col1.z(),
548 col2.z(),
549 };
550 return QMatrix3x3( d );
551}
552
553
559static QMatrix3x3 extractRotation3x3( const QgsMatrix4x4 &matrix, QVector3D &scale )
560{
561 const double *md = matrix.constData(); // column-major
562 // casting to float should be fine (does not extract translation which needs double precision)
563 const QVector3D col0( static_cast<float>( md[0] ), static_cast<float>( md[1] ), static_cast<float>( md[2] ) );
564 const QVector3D col1( static_cast<float>( md[4] ), static_cast<float>( md[5] ), static_cast<float>( md[6] ) );
565 const QVector3D col2( static_cast<float>( md[8] ), static_cast<float>( md[9] ), static_cast<float>( md[10] ) );
566
567 const float sx = col0.length();
568 const float sy = col1.length();
569 const float sz = col2.length();
570 scale = QVector3D( sx, sy, sz );
571
572 if ( sx == 0 || sy == 0 || sz == 0 )
573 return QMatrix3x3();
574
575 return matrixFromColumns( col0 / sx, col1 / sy, col2 / sz );
576}
577
578
579QMatrix3x3 QgsGltf3DUtils::ecefToTargetCrsRotationCorrection( const QgsVector3D &ecefPos, const QgsVector3D &mapPos, const QgsCoordinateTransform &ecefToTargetCrs )
580{
581 // Local ECEF basis vectors at this point:
582 // up = normalize(x, y, z) — geocentric up (≤0.3° error vs ellipsoid normal)
583 // east = normalize(-y, x, 0) — tangent to the parallel
584 // north = cross(up, east)
585 const double x = ecefPos.x(), y = ecefPos.y(), z = ecefPos.z();
586 const double len = std::sqrt( x * x + y * y + z * z );
587 const QgsVector3D upEcef( x / len, y / len, z / len );
588
589 const double eastLenXY = std::sqrt( y * y + x * x );
590 const QgsVector3D eastEcef( -y / eastLenXY, x / eastLenXY, 0 );
591
592 const QgsVector3D northEcef = QgsVector3D::crossProduct( upEcef, eastEcef );
593
594 // Compute corresponding vectors in target CRS by reprojecting perturbed ECEF points.
595 // Use a small delta (~1 meter) along each ECEF basis vector.
596 constexpr double delta = 1.0; // meters
597 double eX = x + delta * eastEcef.x(), eY = y + delta * eastEcef.y(), eZ = z + delta * eastEcef.z();
598 double nX = x + delta * northEcef.x(), nY = y + delta * northEcef.y(), nZ = z + delta * northEcef.z();
599 double uX = x + delta * upEcef.x(), uY = y + delta * upEcef.y(), uZ = z + delta * upEcef.z();
600
601 try
602 {
603 ecefToTargetCrs.transformInPlace( eX, eY, eZ );
604 ecefToTargetCrs.transformInPlace( nX, nY, nZ );
605 ecefToTargetCrs.transformInPlace( uX, uY, uZ );
606 }
607 catch ( QgsCsException & )
608 {
609 return QMatrix3x3(); // identity on failure
610 }
611
612 // Target CRS basis vectors (differences from the reprojected base point, then normalized)
613 QgsVector3D eastCrs( eX - mapPos.x(), eY - mapPos.y(), eZ - mapPos.z() );
614 QgsVector3D northCrs( nX - mapPos.x(), nY - mapPos.y(), nZ - mapPos.z() );
615 QgsVector3D upCrs( uX - mapPos.x(), uY - mapPos.y(), uZ - mapPos.z() );
616 eastCrs.normalize();
617 northCrs.normalize();
618 upCrs.normalize();
619
620 // Correction matrix C = T × Eᵀ
621 // where E = [east_ecef | north_ecef | up_ecef] (columns, orthonormal)
622 // T = [east_crs | north_crs | up_crs] (columns)
623 // Since E is orthonormal, E⁻¹ = Eᵀ, so C = T × Eᵀ.
624 const QMatrix3x3 ecefBasis = matrixFromColumns( eastEcef.toVector3D(), northEcef.toVector3D(), upEcef.toVector3D() ); // E
625 const QMatrix3x3 crsBasis = matrixFromColumns( eastCrs.toVector3D(), northCrs.toVector3D(), upCrs.toVector3D() ); // T
626 return crsBasis * ecefBasis.transposed();
627}
628
629
630QVector<QgsGltf3DUtils::InstanceChunkTransform> QgsGltf3DUtils::tileSpaceToChunkLocal( const QgsGltfUtils::InstancedPrimitive &primitive, const QgsGltf3DUtils::EntityTransform &transform )
631{
632 QVector<InstanceChunkTransform> result;
633 result.resize( primitive.instanceTransforms.size() );
634
635 if ( primitive.instanceTransforms.isEmpty() )
636 return result;
637
638 const QgsVector3D sceneOrigin = transform.chunkOriginTargetCrs;
639
640 // ECEF-to-target-CRS rotation correction matrix, computed once from first instance.
641 // All instances in a tile are close enough that one correction suffices.
642 QMatrix3x3 correctionMatrix;
643 bool hasCorrectionMatrix = false;
644
645 for ( int i = 0; i < primitive.instanceTransforms.size(); ++i )
646 {
647 // Compose with tile transform in double precision:
648 // ecefMatrix = tileTransform × instanceTransforms[i]
649 const QgsMatrix4x4 instanceDouble = floatToDoubleMatrix( primitive.instanceTransforms[i] );
650 const QgsMatrix4x4 ecefMatrix = transform.tileTransform * instanceDouble;
651
652 // Extract ECEF position from column 3 (double precision)
653 const double *md = ecefMatrix.constData(); // column-major
654 const QgsVector3D ecefPos( md[12], md[13], md[14] );
655
656 // Reproject ECEF → target CRS
657 double mapX = ecefPos.x();
658 double mapY = ecefPos.y();
659 double mapZ = ecefPos.z();
660 if ( transform.ecefToTargetCrs )
661 {
662 try
663 {
664 transform.ecefToTargetCrs->transformInPlace( mapX, mapY, mapZ );
665 }
666 catch ( QgsCsException & )
667 {
668 continue;
669 }
670 }
671
672 // Compute the correction matrix on the first successfully reprojected instance
673 if ( !hasCorrectionMatrix && transform.ecefToTargetCrs )
674 {
675 hasCorrectionMatrix = true;
676 correctionMatrix = ecefToTargetCrsRotationCorrection( ecefPos, QgsVector3D( mapX, mapY, mapZ ), *transform.ecefToTargetCrs );
677 }
678
679 // Apply z value modifications
680 mapZ = mapZ * transform.zValueScale + transform.zValueOffset;
681
682 // Chunk-local translation
683 result[i].translation = QVector3D( static_cast<float>( mapX - sceneOrigin.x() ), static_cast<float>( mapY - sceneOrigin.y() ), static_cast<float>( mapZ - sceneOrigin.z() ) );
684
685 // Extract rotation and scale from the 3×3 part of ecefMatrix (double precision)
686 QVector3D scale;
687 QMatrix3x3 rotEcef = extractRotation3x3( ecefMatrix, scale );
688
689 result[i].scale = scale;
690
691 if ( scale.x() == 0 || scale.y() == 0 || scale.z() == 0 )
692 {
693 result[i].rotation = QQuaternion();
694 continue;
695 }
696
697 // Apply ECEF-to-CRS rotation correction if available
698 const QMatrix3x3 rotCrs = hasCorrectionMatrix ? correctionMatrix * rotEcef : rotEcef;
699 result[i].rotation = QQuaternion::fromRotationMatrix( rotCrs );
700 }
701
702 return result;
703}
704
705
706void QgsGltf3DUtils::createInstanceBuffer( Qt3DCore::QGeometry *geometry, const QVector<InstanceChunkTransform> &instances )
707{
708 const int stride = 10 * sizeof( float ); // vec3 + vec4 + vec3 = 10 floats = 40 bytes
709 QByteArray bufferData;
710 bufferData.resize( instances.size() * stride );
711 float *dst = reinterpret_cast<float *>( bufferData.data() );
712
713 for ( const auto &inst : instances )
714 {
715 // translation (vec3)
716 *dst++ = inst.translation.x();
717 *dst++ = inst.translation.y();
718 *dst++ = inst.translation.z();
719 // rotation (vec4: x, y, z, w)
720 *dst++ = inst.rotation.x();
721 *dst++ = inst.rotation.y();
722 *dst++ = inst.rotation.z();
723 *dst++ = inst.rotation.scalar();
724 // scale (vec3)
725 *dst++ = inst.scale.x();
726 *dst++ = inst.scale.y();
727 *dst++ = inst.scale.z();
728 }
729
730 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer;
731 buffer->setData( bufferData );
732
733 // Translation attribute — matches "in vec3 instanceTranslation" in shader
734 Qt3DCore::QAttribute *transAttr = new Qt3DCore::QAttribute;
735 transAttr->setName( u"instanceTranslation"_s );
736 transAttr->setVertexBaseType( Qt3DCore::QAttribute::Float );
737 transAttr->setVertexSize( 3 );
738 transAttr->setByteStride( stride );
739 transAttr->setByteOffset( 0 );
740 transAttr->setDivisor( 1 );
741 transAttr->setCount( instances.size() );
742 transAttr->setBuffer( buffer );
743 geometry->addAttribute( transAttr );
744
745 // Rotation attribute — matches "in vec4 instanceRotation" in shader
746 Qt3DCore::QAttribute *rotAttr = new Qt3DCore::QAttribute;
747 rotAttr->setName( u"instanceRotation"_s );
748 rotAttr->setVertexBaseType( Qt3DCore::QAttribute::Float );
749 rotAttr->setVertexSize( 4 );
750 rotAttr->setByteStride( stride );
751 rotAttr->setByteOffset( 3 * sizeof( float ) );
752 rotAttr->setDivisor( 1 );
753 rotAttr->setCount( instances.size() );
754 rotAttr->setBuffer( buffer );
755 geometry->addAttribute( rotAttr );
756
757 // Scale attribute — matches "in vec3 instanceScale" in shader
758 Qt3DCore::QAttribute *scaleAttr = new Qt3DCore::QAttribute;
759 scaleAttr->setName( u"instanceScale"_s );
760 scaleAttr->setVertexBaseType( Qt3DCore::QAttribute::Float );
761 scaleAttr->setVertexSize( 3 );
762 scaleAttr->setByteStride( stride );
763 scaleAttr->setByteOffset( 7 * sizeof( float ) );
764 scaleAttr->setDivisor( 1 );
765 scaleAttr->setCount( instances.size() );
766 scaleAttr->setBuffer( buffer );
767 geometry->addAttribute( scaleAttr );
768}
769
770
771static Qt3DCore::QAttribute *rawPositions( tinygltf::Model &model, int accessorIndex )
772{
773 tinygltf::Accessor &accessor = model.accessors[accessorIndex];
774 tinygltf::BufferView &bv = model.bufferViews[accessor.bufferView];
775 tinygltf::Buffer &b = model.buffers[bv.buffer];
776
777 if ( accessor.componentType != TINYGLTF_PARAMETER_TYPE_FLOAT || accessor.type != TINYGLTF_TYPE_VEC3 )
778 return nullptr;
779
780 const unsigned char *ptr = b.data.data() + bv.byteOffset + accessor.byteOffset;
781 const int byteStride = bv.byteStride ? bv.byteStride : 3 * sizeof( float );
782
783 QByteArray byteArray;
784 byteArray.resize( accessor.count * 3 * sizeof( float ) );
785 float *out = reinterpret_cast<float *>( byteArray.data() );
786
787 for ( std::size_t i = 0; i < accessor.count; ++i )
788 {
789 const float *fptr = reinterpret_cast<const float *>( ptr + i * byteStride );
790 out[i * 3 + 0] = fptr[0];
791 out[i * 3 + 1] = fptr[1];
792 out[i * 3 + 2] = fptr[2];
793 }
794
795 Qt3DCore::QBuffer *buffer = new Qt3DCore::QBuffer;
796 buffer->setData( byteArray );
797
798 Qt3DCore::QAttribute *attribute = new Qt3DCore::QAttribute;
799 attribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
800 attribute->setBuffer( buffer );
801 attribute->setByteOffset( 0 );
802 attribute->setByteStride( 12 );
803 attribute->setCount( accessor.count );
804 attribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
805 attribute->setVertexSize( 3 );
806
807 return attribute;
808}
809
810
811QVector<Qt3DCore::QEntity *> QgsGltf3DUtils::createInstancedEntities(
812 tinygltf::Model &model,
813 const QVector<QgsGltfUtils::InstancedPrimitive> &primitives,
814 const QgsGltf3DUtils::EntityTransform &transform,
815 const QString &baseUri,
816 const QgsMaterialContext &context,
817 QStringList *errors
818)
819{
820 QVector<Qt3DCore::QEntity *> entities;
821
822 for ( const QgsGltfUtils::InstancedPrimitive &entry : primitives )
823 {
824 if ( entry.meshIndex < 0 || entry.meshIndex >= static_cast<int>( model.meshes.size() ) )
825 continue;
826
827 const tinygltf::Mesh &mesh = model.meshes[entry.meshIndex];
828 if ( entry.primitiveIndex < 0 || entry.primitiveIndex >= static_cast<int>( mesh.primitives.size() ) )
829 continue;
830
831 const tinygltf::Primitive &primitive = mesh.primitives[entry.primitiveIndex];
832 if ( primitive.mode != TINYGLTF_MODE_TRIANGLES )
833 {
834 if ( errors )
835 *errors << u"Unsupported mesh primitive mode for instancing: %1"_s.arg( primitive.mode );
836 continue;
837 }
838
839 auto posIt = primitive.attributes.find( "POSITION" );
840 if ( posIt == primitive.attributes.end() )
841 continue;
842
843 int positionAccessorIndex = posIt->second;
844
845 // Parse material
846 std::unique_ptr<QgsMaterial> material = parseMaterial( model, entry.materialIndex, baseUri, errors, context );
847 if ( !material )
848 continue;
849
850 // Enable instancing on the material
851 if ( QgsMetalRoughMaterial *pbrMat = qobject_cast<QgsMetalRoughMaterial *>( material.get() ) )
852 pbrMat->setInstancingEnabled( true );
853 else if ( QgsTextureMaterial *texMat = qobject_cast<QgsTextureMaterial *>( material.get() ) )
854 texMat->setInstancingEnabled( true );
855
856 // Build geometry with raw positions (no transform, no axis flip)
857 auto geom = std::make_unique<Qt3DCore::QGeometry>();
858
859 Qt3DCore::QAttribute *positionAttribute = rawPositions( model, positionAccessorIndex );
860 if ( !positionAttribute )
861 {
862 continue;
863 }
864 positionAttribute->setName( Qt3DCore::QAttribute::defaultPositionAttributeName() );
865 geom->addAttribute( positionAttribute );
866
867 auto normalIt = primitive.attributes.find( "NORMAL" );
868 if ( normalIt != primitive.attributes.end() )
869 {
870 Qt3DCore::QAttribute *normalAttribute = parseAttribute( model, normalIt->second );
871 normalAttribute->setName( Qt3DCore::QAttribute::defaultNormalAttributeName() );
872 geom->addAttribute( normalAttribute );
873 }
874 else
875 {
876 // Enable flat shading if no normals
877 if ( QgsMetalRoughMaterial *pbrMat = qobject_cast<QgsMetalRoughMaterial *>( material.get() ) )
878 pbrMat->setFlatShadingEnabled( true );
879 }
880
881 auto texIt = primitive.attributes.find( "TEXCOORD_0" );
882 if ( texIt != primitive.attributes.end() )
883 {
884 Qt3DCore::QAttribute *texAttribute = parseAttribute( model, texIt->second );
885 texAttribute->setName( Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName() );
886 geom->addAttribute( texAttribute );
887 }
888
889 Qt3DCore::QAttribute *indexAttribute = nullptr;
890 if ( primitive.indices != -1 )
891 {
892 indexAttribute = parseAttribute( model, primitive.indices );
893 geom->addAttribute( indexAttribute );
894 }
895
896 // Convert tile-space matrices to chunk-local T/R/S
897 const QVector<InstanceChunkTransform> chunkTransforms = tileSpaceToChunkLocal( entry, transform );
898 if ( chunkTransforms.isEmpty() )
899 {
900 continue;
901 }
902
903 // Add per-instance attributes
904 createInstanceBuffer( geom.get(), chunkTransforms );
905
906 // Create geometry renderer with instancing
907 Qt3DRender::QGeometryRenderer *geomRenderer = new Qt3DRender::QGeometryRenderer;
908 geomRenderer->setGeometry( geom.release() ); // geom gets parented to geomRenderer
909 geomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Triangles );
910 geomRenderer->setVertexCount( indexAttribute ? indexAttribute->count() : model.accessors[positionAccessorIndex].count );
911 geomRenderer->setInstanceCount( chunkTransforms.size() );
912
913 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
914 entity->addComponent( geomRenderer );
915 entity->addComponent( material.release() );
916 entities << entity;
917 }
918
919 return entities;
920}
921
922Qt3DCore::QEntity *QgsGltf3DUtils::parsedGltfToEntity( tinygltf::Model &model, const QgsGltf3DUtils::EntityTransform &transform, QString baseUri, const Qgs3DRenderContext &context, QStringList *errors )
923{
924 bool sceneOk = false;
925 const std::size_t sceneIndex = QgsGltfUtils::sourceSceneForModel( model, sceneOk );
926 if ( !sceneOk )
927 {
928 if ( errors )
929 *errors << "No scenes present in the gltf data!";
930 return nullptr;
931 }
932
933 tinygltf::Scene &scene = model.scenes[sceneIndex];
934
935 if ( scene.nodes.size() == 0 )
936 {
937 if ( errors )
938 *errors << "No nodes present in the gltf data!";
939 return nullptr;
940 }
941
942 const QgsVector3D tileTranslationEcef = QgsGltfUtils::extractTileTranslation( model );
943
944 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
945 for ( const int nodeIndex : scene.nodes )
946 {
947 const QVector<Qt3DCore::QEntity *> entities = parseNode( model, nodeIndex, transform, tileTranslationEcef, baseUri, QMatrix4x4(), context, errors );
948 for ( Qt3DCore::QEntity *e : entities )
949 e->setParent( rootEntity );
950 }
951 return rootEntity;
952}
953
954
955Qt3DCore::QEntity *QgsGltf3DUtils::gltfToEntity( const QByteArray &data, const QgsGltf3DUtils::EntityTransform &transform, const QString &baseUri, const Qgs3DRenderContext &context, QStringList *errors )
956{
957 tinygltf::Model model;
958 QString gltfErrors, gltfWarnings;
959
960 bool res = QgsGltfUtils::loadGltfModel( data, model, &gltfErrors, &gltfWarnings );
961 if ( !gltfErrors.isEmpty() )
962 {
963 QgsDebugError( u"Error raised reading %1: %2"_s.arg( baseUri, gltfErrors ) );
964 }
965 if ( !gltfWarnings.isEmpty() )
966 {
967 QgsDebugError( u"Warnings raised reading %1: %2"_s.arg( baseUri, gltfWarnings ) );
968 }
969 if ( !res )
970 {
971 if ( errors )
972 {
973 errors->append( u"GLTF load error: "_s + gltfErrors );
974 }
975 return nullptr;
976 }
977
978 return parsedGltfToEntity( model, transform, baseUri, context, errors );
979}
980
981// For TinyGltfTextureImage
982#include "qgsgltf3dutils.moc"
983
Rendering context for preparation of 3D entities.
static void setTextureFiltering(Qt3DRender::QAbstractTexture *texture, const QgsMaterialContext &context)
Sets the default filtering options for a texture.
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...
Handles coordinate transforms between two coordinate systems.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
Custom exception class for Coordinate Reference System related exceptions.
Context settings for a material.
static QgsMaterialContext fromRenderContext(const Qgs3DRenderContext &context)
Constructs a material context from the settings in a 3D render context.
A simple 4x4 matrix implementation useful for transformation in 3D space.
const double * constData() const
Returns pointer to the matrix data (stored in column-major order).
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:60
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:62
QVector3D toVector3D() const
Converts the current object to QVector3D.
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
static QgsVector3D crossProduct(const QgsVector3D &v1, const QgsVector3D &v2)
Returns the cross product of two vectors.
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:7222
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
#define QgsDebugError(str)
Definition qgslogger.h:59