23#include <Qt3DCore/QAttribute>
24#include <Qt3DCore/QBuffer>
25#include <Qt3DCore/QGeometry>
30QgsLineVertexData::QgsLineVertexData()
33 vertices << QVector3D();
38 altClamping = clamping;
41 renderContext = context;
45QByteArray QgsLineVertexData::createVertexBuffer()
47 QByteArray vertexBufferData;
48 vertexBufferData.resize( vertices.size() * 3 *
sizeof(
float ) );
49 float *rawVertexArray =
reinterpret_cast<float *
>( vertexBufferData.data() );
51 for (
const auto &v : std::as_const( vertices ) )
53 rawVertexArray[idx++] = v.x();
54 rawVertexArray[idx++] = v.y();
55 rawVertexArray[idx++] = v.z();
57 return vertexBufferData;
60QByteArray QgsLineVertexData::createIndexBuffer()
62 QByteArray indexBufferData;
63 indexBufferData.resize( indexes.size() *
sizeof(
int ) );
64 unsigned int *rawIndexArray =
reinterpret_cast<unsigned int *
>( indexBufferData.data() );
66 for (
unsigned int indexVal : std::as_const( indexes ) )
68 rawIndexArray[idx++] = indexVal;
70 return indexBufferData;
73Qt3DCore::QGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent )
75 Qt3DCore::QBuffer *vertexBuffer =
new Qt3DCore::QBuffer( parent );
76 vertexBuffer->setData( createVertexBuffer() );
78 Qt3DCore::QBuffer *indexBuffer =
new Qt3DCore::QBuffer( parent );
79 indexBuffer->setData( createIndexBuffer() );
81 QgsDebugMsgLevel( QString(
"vertex buffer %1 MB index buffer %2 MB " ).arg( vertexBuffer->data().count() / 1024. / 1024. ).arg( indexBuffer->data().count() / 1024. / 1024. ), 2 );
83 Qt3DCore::QAttribute *positionAttribute =
new Qt3DCore::QAttribute( parent );
84 positionAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
85 positionAttribute->setBuffer( vertexBuffer );
86 positionAttribute->setVertexBaseType( Qt3DCore::QAttribute::Float );
87 positionAttribute->setVertexSize( 3 );
88 positionAttribute->setByteStride( 3 *
sizeof(
float ) );
89 positionAttribute->setByteOffset( 0 );
90 positionAttribute->setName( Qt3DCore::QAttribute::defaultPositionAttributeName() );
92 Qt3DCore::QAttribute *indexAttribute =
new Qt3DCore::QAttribute( parent );
93 indexAttribute->setAttributeType( Qt3DCore::QAttribute::IndexAttribute );
94 indexAttribute->setBuffer( indexBuffer );
95 indexAttribute->setByteOffset( 0 );
96 indexAttribute->setByteStride(
sizeof( uint ) );
97 indexAttribute->setVertexBaseType( Qt3DCore::QAttribute::UnsignedInt );
99 Qt3DCore::QGeometry *geom =
new Qt3DCore::QGeometry;
100 geom->addAttribute( positionAttribute );
101 geom->addAttribute( indexAttribute );
106void QgsLineVertexData::addLineString(
const QgsLineString &lineString,
float extraHeightOffset,
bool closePolygon )
109 indexes << vertices.count();
112 switch ( altBinding )
121 const int firstIndex = vertices.count();
123 for (
int i = 0; i < lineString.
vertexCount(); ++i )
126 if ( geocentricCoordinates )
130 vertices << QVector3D( static_cast<float>( p.
x() - origin.x() ),
static_cast<float>( p.
y() - origin.y() ),
static_cast<float>( p.
z() - origin.z() ) );
134 const float z =
Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
135 vertices << QVector3D( static_cast<float>( p.
x() - origin.x() ),
static_cast<float>( p.
y() - origin.y() ),
static_cast<float>( z - origin.z() ) );
137 indexes << vertices.count() - 1;
141 indexes << firstIndex;
144 indexes << vertices.count() - 1;
149void QgsLineVertexData::addVerticalLines(
const QgsLineString &lineString,
float verticalLength,
float extraHeightOffset )
152 switch ( altBinding )
161 for (
int i = 0; i < lineString.
vertexCount(); ++i )
164 float z =
Qgs3DUtils::clampAltitude( p, altClamping, altBinding, baseHeight + extraHeightOffset, centroid, renderContext );
165 float z2 = z + verticalLength;
168 indexes << vertices.count();
170 vertices << QVector3D( static_cast<float>( p.
x() - origin.x() ),
static_cast<float>( p.
y() - origin.y() ), z );
171 indexes << vertices.count() - 1;
172 vertices << QVector3D( static_cast<float>( p.
x() - origin.x() ),
static_cast<float>( p.
y() - origin.y() ), z2 );
173 indexes << vertices.count() - 1;
176 indexes << vertices.count() - 1;
AltitudeClamping
Altitude clamping.
AltitudeBinding
Altitude binding.
@ Centroid
Clamp just centroid of feature.
@ Vertex
Clamp every vertex of feature.
Rendering context for preparation of 3D entities.
static float clampAltitude(const QgsPoint &p, Qgis::AltitudeClamping altClamp, Qgis::AltitudeBinding altBind, float offset, const QgsPoint ¢roid, const Qgs3DRenderContext &context)
Clamps altitude of a vertex according to the settings, returns Z value.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Line string geometry type, with support for z-dimension and m-values.
QgsPoint pointN(int i) const
Returns the specified point from inside the line string.
QgsPoint centroid() const override
Returns the centroid of the geometry.
Point geometry type, with support for z-dimension and m-values.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
#define QgsDebugMsgLevel(str, level)