Skip to content

Commit

Permalink
Stride fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 20, 2023
1 parent 9eaf210 commit 8c69bdc
Showing 1 changed file with 53 additions and 33 deletions.
86 changes: 53 additions & 33 deletions src/core/tiledmesh/qgstiledmeshlayerrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ QgsTiledMeshLayerRenderer::~QgsTiledMeshLayerRenderer() = default;

bool QgsTiledMeshLayerRenderer::render()
{

mReadyToCompose = true;
// Set up the render configuration options
QPainter *painter = renderContext()->painter();

Expand Down Expand Up @@ -250,29 +252,39 @@ bool QgsTiledMeshLayerRenderer::render()

tinygltf::Node &gltfNode = model.nodes[nodeIndex];

QgsVector3D tileTranslation;
QgsMatrix4x4 tileMatrix;
QgsMatrix4x4 gltfLocalTransform;
if ( gltfNode.translation.size() )
{
tileTranslation = QgsVector3D( gltfNode.translation[0], -gltfNode.translation[2], gltfNode.translation[1] );
gltfLocalTransform = QgsMatrix4x4( 1.0, 0.0, 0.0, gltfNode.translation[0],
0.0, 1.0, 0.0, gltfNode.translation[1],
0.0, 0.0, 1.0, gltfNode.translation[2],
0.0, 0.0, 0.0, 1.0
);
}
if ( gltfNode.rotation.size() )
{
Q_ASSERT( false );
}
if ( gltfNode.matrix.size() )
{
tileMatrix = QgsMatrix4x4( gltfNode.matrix[0], gltfNode.matrix[4], gltfNode.matrix[8], gltfNode.matrix[12],
gltfNode.matrix[1], gltfNode.matrix[5], gltfNode.matrix[9], gltfNode.matrix[13],
gltfNode.matrix[2], gltfNode.matrix[6], gltfNode.matrix[10], gltfNode.matrix[14],
gltfNode.matrix[3], gltfNode.matrix[7], gltfNode.matrix[11], gltfNode.matrix[15]
);
gltfLocalTransform = gltfLocalTransform * QgsMatrix4x4(
gltfNode.matrix[0], gltfNode.matrix[4], gltfNode.matrix[8], gltfNode.matrix[12],
gltfNode.matrix[1], gltfNode.matrix[5], gltfNode.matrix[9], gltfNode.matrix[13],
gltfNode.matrix[2], gltfNode.matrix[6], gltfNode.matrix[10], gltfNode.matrix[14],
gltfNode.matrix[3], gltfNode.matrix[7], gltfNode.matrix[11], gltfNode.matrix[15]
);
}


if ( gltfNode.mesh >= 0 )
{
tinygltf::Mesh &mesh = model.meshes[gltfNode.mesh];

for ( const tinygltf::Primitive &primitive : mesh.primitives )
{
// TODO: support other primitive types
if ( primitive.mode == TINYGLTF_MODE_LINE )
continue;

// see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_mesh_primitive_mode
Q_ASSERT( primitive.mode == 4 ); // triangles

Expand All @@ -286,35 +298,36 @@ bool QgsTiledMeshLayerRenderer::render()

Q_ASSERT( accessor.componentType == 5126 && accessor.type == TINYGLTF_TYPE_VEC3 );

void *ptr = b.data.data() + bv.byteOffset + accessor.byteOffset;
float *fptr = ( float * )ptr;
char *ptr = reinterpret_cast< char * >( b.data.data() ) + bv.byteOffset + accessor.byteOffset;

QVector< double > x( accessor.count );
QVector< double > y( accessor.count );
QVector< double > z( accessor.count );
for ( int i = 0; i < accessor.count; ++i )
for ( std::size_t i = 0; i < accessor.count; ++i )
{
QgsVector3D coord;

// flip coordinates from GLTF y-up to ECEF z-up
coord.setX( + fptr[i * 3 + 0] );
coord.setY( - fptr[i * 3 + 2] );
coord.setZ( + fptr[i * 3 + 1] );
float *fptr = ( float * )ptr;

if ( !tileMatrix.isIdentity() )
{
// hmm does the matrix override the y/z flip?
coord = tileMatrix.map( coord );
}
const QgsVector3D gltfCoord = gltfLocalTransform.map(
QgsVector3D( fptr[0], fptr[1], fptr[2] )
);

// flip coordinates from GLTF y-up to ECEF z-up
QgsVector3D coord(
gltfCoord.x(),
-gltfCoord.z(),
gltfCoord.y()
);
coord = node->transform().map( coord ) ;

x[i] = coord.x();
y[i] = coord.y();
z[i] = coord.z();
// coord.v[3] = 0;
}

if ( bv.byteStride )
ptr += bv.byteStride;
else
ptr += 3 * sizeof( float );
}

coordinateTransform.transformInPlace( x, y, z );

Expand All @@ -324,7 +337,8 @@ bool QgsTiledMeshLayerRenderer::render()
tinygltf::Material &material = model.materials[primitive.material];
tinygltf::PbrMetallicRoughness &pbr = material.pbrMetallicRoughness;
QImage image;
if ( pbr.baseColorTexture.index >= 0 )
if ( pbr.baseColorTexture.index >= 0
&& static_cast< int >( model.textures.size() ) > pbr.baseColorTexture.index )
{
tinygltf::Texture &tex = model.textures[pbr.baseColorTexture.index];
tinygltf::Image &img = model.images[tex.source];
Expand Down Expand Up @@ -355,13 +369,20 @@ bool QgsTiledMeshLayerRenderer::render()
tinygltf::BufferView &texbv = model.bufferViews[texAccessor.bufferView];
tinygltf::Buffer &texb = model.buffers[texbv .buffer];

void *ptr = texb.data.data() + texbv.byteOffset + texAccessor.byteOffset;
float *fptr = ( float * )ptr;
for ( int i = 0; i < texAccessor.count; i ++ )
char *texturePtr = reinterpret_cast< char * >( texb.data.data() ) + texbv.byteOffset + texAccessor.byteOffset;

for ( std::size_t i = 0; i < texAccessor.count; i ++ )
{
double xx = *fptr++;
double yy = *fptr++;
float *textureFptr = reinterpret_cast< float * >( texturePtr );

double xx = textureFptr[0];
double yy = textureFptr[1];
texturePoints.append( QPointF( xx, yy ) );

if ( texbv.byteStride )
texturePtr += texbv.byteStride;
else
texturePtr += 2 * sizeof( float );
}
}
}
Expand Down Expand Up @@ -413,7 +434,7 @@ bool QgsTiledMeshLayerRenderer::render()
unsigned short *fptrPrimitive = ( unsigned short * )primitivePtr;
QgsFillSymbol symbol2( QgsSymbolLayerList() << new QgsSimpleFillSymbolLayer() );
QgsSimpleFillSymbolLayer *simpleFill = qgis::down_cast< QgsSimpleFillSymbolLayer * >( symbol2.symbolLayer( 0 ) );
for ( int i = 0; i < primitiveAccessor.count; i += 3 )
for ( std::size_t i = 0; i < primitiveAccessor.count; i += 3 )
{
if ( renderContext()->renderingStopped() )
break;
Expand Down Expand Up @@ -470,7 +491,6 @@ bool QgsTiledMeshLayerRenderer::render()
renderNode( &rootNode );
}

mReadyToCompose = true;
return true;
}

Expand Down

0 comments on commit 8c69bdc

Please sign in to comment.