Skip to content
Permalink
Browse files

fix triangles flickering for 3D mesh rendering

  • Loading branch information
vcloarec authored and nyalldawson committed Jan 19, 2020
1 parent ec59d3b commit f00b056023e75c42820f6109325ccccbccf0c0e9
Showing with 25 additions and 13 deletions.
  1. +4 −3 src/3d/mesh/qgsmesh3dentity_p.cpp
  2. +17 −7 src/3d/mesh/qgsmesh3dgeometry_p.cpp
  3. +4 −3 src/3d/mesh/qgsmesh3dgeometry_p.h
@@ -53,14 +53,15 @@ void QgsMesh3dEntity::buildGeometry()
}

mesh->setGeometry( new QgsMesh3dGeometry( mTriangularMesh,
mMapSettings.origin(),
mExtent,
mSymbol.verticaleScale(),
mesh ) );

addComponent( mesh );

Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
tform->setTranslation( QVector3D( float( -mMapSettings.origin().x() ), 0, float( mMapSettings.origin().y() ) ) );
tform->setTranslation( QVector3D( 0, 0, 0 ) ) ;
addComponent( tform );
}

@@ -93,11 +94,11 @@ void QgsMesh3dTerrainTileEntity::buildGeometry()
{
Qt3DRender::QGeometryRenderer *mesh = new Qt3DRender::QGeometryRenderer;

mesh->setGeometry( new QgsMesh3dGeometry( mTriangularMesh, mExtent, mSymbol.verticaleScale(), mesh ) );
mesh->setGeometry( new QgsMesh3dGeometry( mTriangularMesh, mMapSettings.origin(), mExtent, mSymbol.verticaleScale(), mesh ) );
addComponent( mesh );

Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
tform->setTranslation( QVector3D( float( -mMapSettings.origin().x() ), 0, float( mMapSettings.origin().y() ) ) );
tform->setTranslation( QVector3D( 0, 0, 0 ) );
addComponent( tform );
}

@@ -26,7 +26,10 @@

using namespace Qt3DRender;

static QByteArray createPlaneVertexData( const QgsTriangularMesh &mesh, const QgsRectangle &extent, float vertScale )
static QByteArray createPlaneVertexData( const QgsTriangularMesh &mesh,
const QgsVector3D &origin,
const QgsRectangle &extent,
float vertScale )
{
const int nVerts = mesh.vertices().count();

@@ -49,9 +52,9 @@ static QByteArray createPlaneVertexData( const QgsTriangularMesh &mesh, const Qg
for ( int i = 0; i < nVerts; i++ )
{
const QgsMeshVertex &vert = mesh.vertices().at( i );
*fptr++ = float( vert.x() );
*fptr++ = float( vert.z() ) * vertScale ;
*fptr++ = float( -vert.y() );
*fptr++ = float( vert.x() - origin.x() );
*fptr++ = float( vert.z() - origin.z() ) * vertScale ;
*fptr++ = float( -vert.y() + origin.y() );

*fptr++ = float( ( vert.x() - x0 ) / w );
*fptr++ = float( ( y0 - vert.y() ) / h );
@@ -144,16 +147,20 @@ static QByteArray createPlaneIndexData( const QgsTriangularMesh &mesh )
class MeshPlaneVertexBufferFunctor : public QBufferDataGenerator
{
public:
explicit MeshPlaneVertexBufferFunctor( const QgsTriangularMesh &mesh, const QgsRectangle &extent, float vertScale )
explicit MeshPlaneVertexBufferFunctor( const QgsTriangularMesh &mesh,
const QgsVector3D &origin,
const QgsRectangle &extent,
float vertScale )
: mMesh( mesh ),
mOrigin( origin ),
mExtent( extent ),
mVertScale( vertScale )

{}

QByteArray operator()() final
{
return createPlaneVertexData( mMesh, mExtent, mVertScale );
return createPlaneVertexData( mMesh, mOrigin, mExtent, mVertScale );
}

bool operator ==( const QBufferDataGenerator &other ) const final
@@ -171,6 +178,7 @@ class MeshPlaneVertexBufferFunctor : public QBufferDataGenerator
private:

QgsTriangularMesh mMesh;
QgsVector3D mOrigin;
QgsRectangle mExtent;
float mVertScale;

@@ -253,11 +261,13 @@ class MeshPlaneVertexDatasetBufferFunctor : public QBufferDataGenerator

QgsMesh3dGeometry::QgsMesh3dGeometry(
const QgsTriangularMesh &mesh,
const QgsVector3D &origin,
const QgsRectangle &extent,
float verticaleScale,
QgsMesh3dGeometry::QNode *parent ):
QGeometry( parent ),
mTriangularMesh( mesh ),
mOrigin( origin ),
mExtent( extent ),
mVertScale( verticaleScale )
{
@@ -313,7 +323,7 @@ void QgsMesh3dGeometry::init()
// Each primitive has 3 vertives
mIndexAttribute->setCount( uint( mTriangularMesh.triangles().count() ) * 3 );

mVertexBuffer->setData( MeshPlaneVertexBufferFunctor( mTriangularMesh, mExtent, mVertScale )() );
mVertexBuffer->setData( MeshPlaneVertexBufferFunctor( mTriangularMesh, mOrigin, mExtent, mVertScale )() );
mIndexBuffer->setData( MeshPlaneIndexBufferFunctor( mTriangularMesh )() );

addAttribute( mPositionAttribute );
@@ -22,6 +22,8 @@
#include <Qt3DRender/qgeometry.h>
#include <QVector3D>

#include <qgsvector3d.h>

#include "qgstriangularmesh.h"

///@cond PRIVATE
@@ -51,12 +53,13 @@ class QgsMesh3dGeometry: public Qt3DRender::QGeometry
public:

//! Constructs a mesh layer geometry from triangular mesh.
explicit QgsMesh3dGeometry( const QgsTriangularMesh &mesh, const QgsRectangle &extent, float verticaleScale, QNode *parent );
explicit QgsMesh3dGeometry( const QgsTriangularMesh &mesh, const QgsVector3D &origin, const QgsRectangle &extent, float verticaleScale, QNode *parent );

private:
void init();

QgsTriangularMesh mTriangularMesh;
QgsVector3D mOrigin;
QgsRectangle mExtent;
float mVertScale;

@@ -66,10 +69,8 @@ class QgsMesh3dGeometry: public Qt3DRender::QGeometry
Qt3DRender::QAttribute *mIndexAttribute = nullptr;
Qt3DRender::QBuffer *mVertexBuffer = nullptr;
Qt3DRender::QBuffer *mIndexBuffer = nullptr;

};


///@endcond

#endif // QGSMESHGEOMETRY_P_H

0 comments on commit f00b056

Please sign in to comment.
You can’t perform that action at this time.