18 #include <Qt3DRender/QAttribute>
19 #include <Qt3DRender/QBuffer>
20 #include <Qt3DRender/QBufferDataGenerator>
31 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
32 mVertexBuffer =
new Qt3DRender::QBuffer( Qt3DRender::QBuffer::VertexBuffer,
this );
34 mVertexBuffer =
new Qt3DRender::QBuffer(
this );
38 const int stride = tmpTess.
stride();
40 mPositionAttribute =
new Qt3DRender::QAttribute(
this );
41 mPositionAttribute->setName( Qt3DRender::QAttribute::defaultPositionAttributeName() );
42 mPositionAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
43 mPositionAttribute->setVertexSize( 3 );
44 mPositionAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
45 mPositionAttribute->setBuffer( mVertexBuffer );
46 mPositionAttribute->setByteStride( stride );
47 addAttribute( mPositionAttribute );
51 mNormalAttribute =
new Qt3DRender::QAttribute(
this );
52 mNormalAttribute->setName( Qt3DRender::QAttribute::defaultNormalAttributeName() );
53 mNormalAttribute->setVertexBaseType( Qt3DRender::QAttribute::Float );
54 mNormalAttribute->setVertexSize( 3 );
55 mNormalAttribute->setAttributeType( Qt3DRender::QAttribute::VertexAttribute );
56 mNormalAttribute->setBuffer( mVertexBuffer );
57 mNormalAttribute->setByteStride( stride );
58 mNormalAttribute->setByteOffset( 3 *
sizeof(
float ) );
59 addAttribute( mNormalAttribute );
65 Q_ASSERT( polygons.count() == featureIds.count() );
66 mTriangleIndexStartingIndices.reserve( polygons.count() );
67 mTriangleIndexFids.reserve( polygons.count() );
69 QgsTessellator tessellator( origin.
x(), origin.
y(), mWithNormals, mInvertNormals, mAddBackFaces );
70 for (
int i = 0; i < polygons.count(); ++i )
73 uint startingTriangleIndex =
static_cast<uint
>( tessellator.
dataVerticesCount() / 3 );
74 mTriangleIndexStartingIndices.append( startingTriangleIndex );
75 mTriangleIndexFids.append( featureIds[i] );
78 float extr = extrusionHeightPerPolygon.
isEmpty() ? extrusionHeight : extrusionHeightPerPolygon.at( i );
82 qDeleteAll( polygons );
84 QByteArray data( (
const char * )tessellator.
data().constData(), tessellator.
data().count() *
sizeof(
float ) );
85 int nVerts = data.count() / tessellator.
stride();
87 mVertexBuffer->setData( data );
88 mPositionAttribute->setCount( nVerts );
89 if ( mNormalAttribute )
90 mNormalAttribute->setCount( nVerts );
93 void QgsTessellatedPolygonGeometry::setData(
const QByteArray &vertexBufferData,
int vertexCount,
const QVector<QgsFeatureId> &triangleIndexFids,
const QVector<uint> &triangleIndexStartingIndices )
95 mTriangleIndexStartingIndices = triangleIndexStartingIndices;
96 mTriangleIndexFids = triangleIndexFids;
98 mVertexBuffer->setData( vertexBufferData );
99 mPositionAttribute->setCount( vertexCount );
100 if ( mNormalAttribute )
101 mNormalAttribute->setCount( vertexCount );
106 static int binary_search( uint v,
const uint *data,
int count )
109 int idx1 = count - 1;
111 if ( v < data[0] || v >= data[count - 1] )
114 while ( idx0 != idx1 )
116 int idxPivot = ( idx0 + idx1 ) / 2;
117 uint pivot = data[idxPivot];
120 if ( data[idxPivot + 1] > v )
134 int i = binary_search( triangleIndex, mTriangleIndexStartingIndices.constData(), mTriangleIndexStartingIndices.count() );
135 return i != -1 ? mTriangleIndexFids[i] :
FID_NULL;