18 #include <Qt3DRender/qattribute.h>
19 #include <Qt3DRender/qbuffer.h>
20 #include <Qt3DRender/qbufferdatagenerator.h>
31 static QByteArray createPlaneVertexData(
int res,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heights )
34 Q_ASSERT( heights.count() == res * res *
static_cast<int>(
sizeof(
float ) ) );
36 const float *zData = (
const float * ) heights.constData();
37 const float *zBits = zData;
39 const int nVerts = ( res + 2 ) * ( res + 2 );
43 const quint32 elementSize = 3 + 2 + 3;
44 const quint32 stride = elementSize *
sizeof( float );
45 QByteArray bufferBytes;
46 bufferBytes.resize( stride * nVerts );
47 float *fptr =
reinterpret_cast<float *
>( bufferBytes.data() );
50 QSize resolution( res, res );
51 const float x0 = -w / 2.0f;
52 const float z0 = -h / 2.0f;
53 const float dx = w / ( resolution.width() - 1 );
54 const float dz = h / ( resolution.height() - 1 );
55 const float du = 1.0 / ( resolution.width() - 1 );
56 const float dv = 1.0 / ( resolution.height() - 1 );
60 const float noDataHeight = 0;
62 const int iMax = resolution.width() - 1;
63 const int jMax = resolution.height() - 1;
66 for (
int j = -1; j <= resolution.height(); ++j )
68 int jBound = std::clamp( j, 0, jMax );
69 const float z = z0 +
static_cast<float>( jBound ) * dz;
70 const float v =
static_cast<float>( jBound ) * dv;
73 for (
int i = -1; i <= resolution.width(); ++i )
75 int iBound = std::clamp( i, 0, iMax );
76 const float x = x0 +
static_cast<float>( iBound ) * dx;
77 const float u =
static_cast<float>( iBound ) * du;
80 if ( i == iBound && j == jBound )
83 height = zData[ jBound * resolution.width() + iBound ] - skirtHeight;
85 if ( std::isnan( height ) )
86 height = noDataHeight;
90 *fptr++ = height / side * vertScale;
98 #define zAt( ii, jj ) zData[ jj * resolution.width() + ii ] * vertScale
99 float zi0 = zAt( std::clamp( i - 1, 0, iMax ), jBound );
100 float zi1 = zAt( std::clamp( i + 1, 0, iMax ), jBound );
101 float zj0 = zAt( iBound, std::clamp( j - 1, 0, jMax ) );
102 float zj1 = zAt( iBound, std::clamp( j + 1, 0, jMax ) );
105 if ( std::isnan( zi0 ) || std::isnan( zi1 ) || std::isnan( zj0 ) || std::isnan( zj1 ) )
106 n = QVector3D( 0, 1, 0 );
110 float zij = height * vertScale;
113 di = 2 * ( zij - zi1 );
114 else if ( i == iMax )
115 di = 2 * ( zi0 - zij );
120 dj = 2 * ( zij - zj1 );
121 else if ( j == jMax )
122 dj = 2 * ( zj0 - zij );
126 n = QVector3D( di, 2 * side / res, dj );
139 inline int ijToHeightMapIndex(
int i,
int j,
int resX,
int resZ )
141 i = std::clamp( i, 1, resX ) - 1;
142 j = std::clamp( j, 1, resZ ) - 1;
147 static bool hasNoData(
int i,
int j,
const float *heightMap,
int resX,
int resZ )
149 return std::isnan( heightMap[ ijToHeightMapIndex( i, j, resX, resZ ) ] ) ||
150 std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j, resX, resZ ) ] ) ||
151 std::isnan( heightMap[ ijToHeightMapIndex( i, j + 1, resX, resZ ) ] ) ||
152 std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j + 1, resX, resZ ) ] );
155 static QByteArray createPlaneIndexData(
int res,
const QByteArray &heightMap )
157 QSize resolution( res, res );
158 int numVerticesX = resolution.width() + 2;
159 int numVerticesZ = resolution.height() + 2;
162 const int faces = 2 * ( numVerticesX - 1 ) * ( numVerticesZ - 1 );
163 const quint32 indices = 3 * faces;
164 Q_ASSERT( indices < std::numeric_limits<quint32>::max() );
165 QByteArray indexBytes;
166 indexBytes.resize( indices *
sizeof( quint32 ) );
167 quint32 *indexPtr =
reinterpret_cast<quint32 *
>( indexBytes.data() );
169 const float *heightMapFloat =
reinterpret_cast<const float *
>( heightMap.constData() );
172 for (
int j = 0; j < numVerticesZ - 1; ++j )
174 const int rowStartIndex = j * numVerticesX;
175 const int nextRowStartIndex = ( j + 1 ) * numVerticesX;
178 for (
int i = 0; i < numVerticesX - 1; ++i )
180 if ( hasNoData( i, j, heightMapFloat, res, res ) )
184 *indexPtr++ = rowStartIndex + i;
185 *indexPtr++ = rowStartIndex + i;
186 *indexPtr++ = rowStartIndex + i;
188 *indexPtr++ = rowStartIndex + i;
189 *indexPtr++ = rowStartIndex + i;
190 *indexPtr++ = rowStartIndex + i;
195 *indexPtr++ = rowStartIndex + i;
196 *indexPtr++ = nextRowStartIndex + i;
197 *indexPtr++ = rowStartIndex + i + 1;
199 *indexPtr++ = nextRowStartIndex + i;
200 *indexPtr++ = nextRowStartIndex + i + 1;
201 *indexPtr++ = rowStartIndex + i + 1;
211 class PlaneVertexBufferFunctor :
public QBufferDataGenerator
214 explicit PlaneVertexBufferFunctor(
int resolution,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heightMap )
215 : mResolution( resolution )
217 , mVertScale( vertScale )
218 , mSkirtHeight( skirtHeight )
219 , mHeightMap( heightMap )
222 QByteArray operator()() final
224 return createPlaneVertexData( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap );
227 bool operator ==(
const QBufferDataGenerator &other )
const final
229 const PlaneVertexBufferFunctor *otherFunctor = functor_cast<PlaneVertexBufferFunctor>( &other );
230 if ( otherFunctor !=
nullptr )
231 return ( otherFunctor->mResolution == mResolution &&
232 otherFunctor->mSide == mSide &&
233 otherFunctor->mVertScale == mVertScale &&
234 otherFunctor->mSkirtHeight == mSkirtHeight &&
235 otherFunctor->mHeightMap == mHeightMap );
241 QT3D_FUNCTOR( PlaneVertexBufferFunctor )
249 QByteArray mHeightMap;
254 class PlaneIndexBufferFunctor :
public QBufferDataGenerator
257 explicit PlaneIndexBufferFunctor(
int resolution,
const QByteArray &heightMap )
258 : mResolution( resolution )
259 , mHeightMap( heightMap )
262 QByteArray operator()() final
264 return createPlaneIndexData( mResolution, mHeightMap );
267 bool operator ==(
const QBufferDataGenerator &other )
const final
269 const PlaneIndexBufferFunctor *otherFunctor = functor_cast<PlaneIndexBufferFunctor>( &other );
270 if ( otherFunctor !=
nullptr )
271 return ( otherFunctor->mResolution == mResolution );
277 QT3D_FUNCTOR( PlaneIndexBufferFunctor )
282 QByteArray mHeightMap;
289 DemTerrainTileGeometry::DemTerrainTileGeometry(
int resolution,
float side,
float vertScale,
float skirtHeight,
const QByteArray &heightMap, DemTerrainTileGeometry::QNode *parent )
290 : QGeometry( parent )
291 , mResolution( resolution )
293 , mVertScale( vertScale )
294 , mSkirtHeight( skirtHeight )
295 , mHeightMap( heightMap )
300 static bool intersectionDemTriangles(
const QByteArray &vertexBuf,
const QByteArray &indexBuf,
const QgsRayCastingUtils::Ray3D &r,
const QMatrix4x4 &worldTransform, QVector3D &intPt )
305 const float *vertices =
reinterpret_cast<const float *
>( vertexBuf.constData() );
306 const uint *indices =
reinterpret_cast<const uint *
>( indexBuf.constData() );
308 int vertexCnt = vertexBuf.count() /
sizeof( float );
309 Q_ASSERT( vertexCnt % 8 == 0 );
311 int indexCnt = indexBuf.count() /
sizeof( uint );
312 Q_ASSERT( indexCnt % 3 == 0 );
313 int triangleCount = indexCnt / 3;
315 QVector3D intersectionPt, minIntersectionPt;
317 float minDistance = -1;
319 for (
int i = 0; i < triangleCount; ++i )
321 int v0 = indices[i * 3], v1 = indices[i * 3 + 1], v2 = indices[i * 3 + 2];
322 QVector3D a( vertices[v0 * 8], vertices[v0 * 8 + 1], vertices[v0 * 8 + 2] );
323 QVector3D b( vertices[v1 * 8], vertices[v1 * 8 + 1], vertices[v1 * 8 + 2] );
324 QVector3D
c( vertices[v2 * 8], vertices[v2 * 8 + 1], vertices[v2 * 8 + 2] );
326 const QVector3D tA = worldTransform * a;
327 const QVector3D tB = worldTransform * b;
328 const QVector3D tC = worldTransform *
c;
332 if ( QgsRayCastingUtils::rayTriangleIntersection( r, tA, tB, tC, uvw, t ) )
334 intersectionPt = r.point( t * r.distance() );
335 distance = r.projectedDistance( intersectionPt );
338 if ( minDistance == -1 || distance < minDistance )
340 minDistance = distance;
341 minIntersectionPt = intersectionPt;
346 if ( minDistance != -1 )
348 intPt = minIntersectionPt;
355 bool DemTerrainTileGeometry::rayIntersection(
const QgsRayCastingUtils::Ray3D &ray,
const QMatrix4x4 &worldTransform, QVector3D &intersectionPoint )
357 return intersectionDemTriangles( mVertexBuffer->data(), mIndexBuffer->data(), ray, worldTransform, intersectionPoint );
360 void DemTerrainTileGeometry::init()
362 mPositionAttribute =
new QAttribute(
this );
363 mNormalAttribute =
new QAttribute(
this );
364 mTexCoordAttribute =
new QAttribute(
this );
365 mIndexAttribute =
new QAttribute(
this );
366 mVertexBuffer =
new Qt3DRender::QBuffer(
this );
367 mIndexBuffer =
new Qt3DRender::QBuffer(
this );
369 int nVertsX = mResolution + 2;
370 int nVertsZ = mResolution + 2;
371 const int nVerts = nVertsX * nVertsZ;
372 const int stride = ( 3 + 2 + 3 ) *
sizeof(
float );
373 const int faces = 2 * ( nVertsX - 1 ) * ( nVertsZ - 1 );
375 mPositionAttribute->setName( QAttribute::defaultPositionAttributeName() );
376 mPositionAttribute->setVertexBaseType( QAttribute::Float );
377 mPositionAttribute->setVertexSize( 3 );
378 mPositionAttribute->setAttributeType( QAttribute::VertexAttribute );
379 mPositionAttribute->setBuffer( mVertexBuffer );
380 mPositionAttribute->setByteStride( stride );
381 mPositionAttribute->setCount( nVerts );
383 mTexCoordAttribute->setName( QAttribute::defaultTextureCoordinateAttributeName() );
384 mTexCoordAttribute->setVertexBaseType( QAttribute::Float );
385 mTexCoordAttribute->setVertexSize( 2 );
386 mTexCoordAttribute->setAttributeType( QAttribute::VertexAttribute );
387 mTexCoordAttribute->setBuffer( mVertexBuffer );
388 mTexCoordAttribute->setByteStride( stride );
389 mTexCoordAttribute->setByteOffset( 3 *
sizeof(
float ) );
390 mTexCoordAttribute->setCount( nVerts );
392 mNormalAttribute->setName( QAttribute::defaultNormalAttributeName() );
393 mNormalAttribute->setVertexBaseType( QAttribute::Float );
394 mNormalAttribute->setVertexSize( 3 );
395 mNormalAttribute->setAttributeType( QAttribute::VertexAttribute );
396 mNormalAttribute->setBuffer( mVertexBuffer );
397 mNormalAttribute->setByteStride( stride );
398 mNormalAttribute->setByteOffset( 5 *
sizeof(
float ) );
399 mNormalAttribute->setCount( nVerts );
401 mIndexAttribute->setAttributeType( QAttribute::IndexAttribute );
402 mIndexAttribute->setVertexBaseType( QAttribute::UnsignedInt );
403 mIndexAttribute->setBuffer( mIndexBuffer );
406 mIndexAttribute->setCount( faces * 3 );
410 mVertexBuffer->setData( PlaneVertexBufferFunctor( mResolution, mSide, mVertScale, mSkirtHeight, mHeightMap )() );
411 mIndexBuffer->setData( PlaneIndexBufferFunctor( mResolution, mHeightMap )() );
413 addAttribute( mPositionAttribute );
414 addAttribute( mTexCoordAttribute );
415 addAttribute( mNormalAttribute );
416 addAttribute( mIndexAttribute );