Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qheightfieldshape.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
6
7#include <QFileInfo>
8#include <QImage>
9#include <QQmlContext>
10#include <QQmlFile>
11#include <QtQuick3D/QQuick3DGeometry>
12#include <extensions/PxExtensionsAPI.h>
13
14//########################################################################################
15// NOTE:
16// Triangle mesh, heightfield or plane geometry shapes configured as eSIMULATION_SHAPE are
17// not supported for non-kinematic PxRigidDynamic instances.
18//########################################################################################
19
20#include "foundation/PxVec3.h"
21//#include "cooking/PxTriangleMeshDesc.h"
22#include "extensions/PxDefaultStreams.h"
23#include "geometry/PxHeightField.h"
24#include "geometry/PxHeightFieldDesc.h"
25
27
29
30// TODO: Unify with QQuick3DPhysicsMeshManager??? It's the same basic logic,
31// but we're using images instead of meshes.
32
34{
35public:
36 QQuick3DPhysicsHeightField(const QString &qmlSource);
37 QQuick3DPhysicsHeightField(QQuickImage *image);
39
40 void ref() { ++refCount; }
41 int deref() { return --refCount; }
42 void writeSamples(const QImage &heightMap);
43 physx::PxHeightField *heightField();
44
45 int rows() const;
46 int columns() const;
47
48private:
49 QString m_sourcePath;
50 // This raw pointer is safe to store since when the Image or
51 // HeightFieldShape is destroyed, this heightfield will be dereferenced
52 // from all shapes and deleted.
53 QQuickImage *m_image = nullptr;
54 physx::PxHeightFieldSample *m_samples = nullptr;
55 physx::PxHeightField *m_heightField = nullptr;
56 int m_rows = 0;
57 int m_columns = 0;
58 int refCount = 0;
59};
60
62{
63public:
64 static QQuick3DPhysicsHeightField *getHeightField(const QUrl &source,
65 const QObject *contextObject);
66 static QQuick3DPhysicsHeightField *getHeightField(QQuickImage *source);
68
69private:
70 static QHash<QString, QQuick3DPhysicsHeightField *> heightFieldHash;
71 static QHash<QQuickImage *, QQuick3DPhysicsHeightField *> heightFieldImageHash;
72};
73
74QHash<QString, QQuick3DPhysicsHeightField *> QQuick3DPhysicsHeightFieldManager::heightFieldHash;
75QHash<QQuickImage *, QQuick3DPhysicsHeightField *>
76 QQuick3DPhysicsHeightFieldManager::heightFieldImageHash;
77
79QQuick3DPhysicsHeightFieldManager::getHeightField(const QUrl &source, const QObject *contextObject)
80{
81 const QQmlContext *context = qmlContext(contextObject);
82
83 const auto resolvedUrl = context ? context->resolvedUrl(source) : source;
84 const auto qmlSource = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
85
86 auto *heightField = heightFieldHash.value(qmlSource);
87 if (!heightField) {
88 heightField = new QQuick3DPhysicsHeightField(qmlSource);
89 heightFieldHash[qmlSource] = heightField;
90 }
91 heightField->ref();
92 return heightField;
93}
94
96{
97 auto *heightField = heightFieldImageHash.value(source);
98 if (!heightField) {
99 heightField = new QQuick3DPhysicsHeightField(source);
100 heightFieldImageHash[source] = heightField;
101 }
102 heightField->ref();
103 return heightField;
104}
105
107{
108 if (heightField != nullptr && heightField->deref() == 0) {
109 qCDebug(lcQuick3dPhysics()) << "deleting height field" << heightField;
110 erase_if(heightFieldHash,
111 [heightField](std::pair<const QString &, QQuick3DPhysicsHeightField *&> h) {
112 return h.second == heightField;
113 });
114 erase_if(heightFieldImageHash,
115 [heightField](std::pair<QQuickImage *, QQuick3DPhysicsHeightField *&> h) {
116 return h.second == heightField;
117 });
118 delete heightField;
119 }
120}
121
126
128
133
134void QQuick3DPhysicsHeightField::writeSamples(const QImage &heightMap)
135{
136 if (Q_UNLIKELY(heightMap.isNull())) {
137 m_rows = 0;
138 m_columns = 0;
139 free(m_samples);
140 m_samples = nullptr;
141 return;
142 }
143
144 m_rows = heightMap.height();
145 m_columns = heightMap.width();
146 int numRows = m_rows;
147 int numCols = m_columns;
148
149 free(m_samples);
150 m_samples = reinterpret_cast<physx::PxHeightFieldSample *>(
151 malloc(sizeof(physx::PxHeightFieldSample) * (numRows * numCols)));
152 for (int i = 0; i < numCols; i++)
153 for (int j = 0; j < numRows; j++) {
154 float f = heightMap.pixelColor(i, j).valueF() - 0.5;
155 // qDebug() << i << j << f;
156 m_samples[i * numRows + j] = { qint16(0xffff * f), 0, 0 }; //{qint16(i%3*2 + j), 0, 0};
157 }
158}
159
161{
162 if (m_heightField)
163 return m_heightField;
164
165 physx::PxPhysics *thePhysics = QPhysicsWorld::getPhysics();
166 if (thePhysics == nullptr)
167 return nullptr;
168
169 // No source set
170 if (m_image == nullptr && m_sourcePath.isEmpty())
171 return nullptr;
172
173 // Reading from image property has precedence
174 const bool readFromFile = m_image == nullptr;
175
176 // Security note: This code reads user provided images and create heightfields from them.
177 // This is safe since we assume that QImage properly rejects invalid image files.
178 // It also reads cached and cooked heightfields but that file is marked.
179 if (readFromFile) {
180 // Try read cached file
181 m_heightField = QCacheUtils::readCachedHeightField(m_sourcePath, *thePhysics);
182 if (m_heightField != nullptr) {
183 m_rows = m_heightField->getNbRows();
184 m_columns = m_heightField->getNbColumns();
185 return m_heightField;
186 }
187
188 // Try read cooked file
189 m_heightField = QCacheUtils::readCookedHeightField(m_sourcePath, *thePhysics);
190 if (m_heightField != nullptr) {
191 m_rows = m_heightField->getNbRows();
192 m_columns = m_heightField->getNbColumns();
193 return m_heightField;
194 }
195
196 // Try read image file
197 writeSamples(QImage(m_sourcePath));
198 } else {
199 writeSamples(m_image->image());
200 }
201
202 int numRows = m_rows;
203 int numCols = m_columns;
204 auto samples = m_samples;
205
206 physx::PxHeightFieldDesc hfDesc;
207 hfDesc.format = physx::PxHeightFieldFormat::eS16_TM;
208 hfDesc.nbColumns = numRows;
209 hfDesc.nbRows = numCols;
210 hfDesc.samples.data = samples;
211 hfDesc.samples.stride = sizeof(physx::PxHeightFieldSample);
212
213 physx::PxDefaultMemoryOutputStream buf;
214
215 const auto cooking = QPhysicsWorld::getCooking();
216 if (numRows && numCols && cooking && cooking->cookHeightField(hfDesc, buf)) {
217 auto size = buf.getSize();
218 auto *data = buf.getData();
219 physx::PxDefaultMemoryInputData input(data, size);
220 m_heightField = thePhysics->createHeightField(input);
221 qCDebug(lcQuick3dPhysics) << "created height field" << m_heightField << numCols << numRows
222 << "from"
223 << (readFromFile ? m_sourcePath : QString::fromUtf8("image"));
224 if (readFromFile)
225 QCacheUtils::writeCachedHeightField(m_sourcePath, buf);
226 } else {
227 qCWarning(lcQuick3dPhysics) << "Could not create height field from"
228 << (readFromFile ? m_sourcePath : QString::fromUtf8("image"));
229 }
230
231 return m_heightField;
232}
233
235{
236 return m_rows;
237}
238
240{
241 return m_columns;
242}
243
244/*!
245 \qmltype HeightFieldShape
246 \inqmlmodule QtQuick3D.Physics
247 \inherits CollisionShape
248 \since 6.4
249 \brief A collision shape where the elevation is defined by a height map.
250
251 The HeightFieldShape type defines a physical surface where the height is determined by
252 the \l {QColor#The HSV Color Model}{value} of the pixels of the \l {source} image. The
253 x-axis of the image is mapped to the positive x-axis of the scene, and the y-axis of the
254 image is mapped to the negative z-axis of the scene. A typical use case is to represent
255 natural terrain.
256
257 Objects that are controlled by the physics simulation cannot use HeightFieldShape: It can only
258 be used with \l StaticRigidBody and \l {DynamicRigidBody::isKinematic}{kinematic bodies}.
259
260 \l [QtQuick3D]{HeightFieldGeometry}{QtQuick3D.Helpers.HeightFieldGeometry} is API compatible
261 with the HeightFieldShape type, and can be used to show the height field visually. To
262 improve performance, use a lower resolution version of the height map for the HeightFieldShape:
263 As long as the \l{extents} and the image aspect ratio are the same, the physics body and the
264 visual item will overlap.
265
266 \sa {Qt Quick 3D Physics Shapes and Bodies}{Shapes and Bodies overview documentation}
267*/
268
269/*!
270 \qmlproperty vector3d HeightFieldShape::extents
271 This property defines the extents of the height field. The default value
272 is \c{(100, 100, 100)} when the heightMap is square. If the heightMap is
273 non-square, the default value is reduced along the x- or z-axis, so the height
274 field will keep the aspect ratio of the image.
275*/
276
277/*!
278 \qmlproperty url HeightFieldShape::source
279 This property defines the location of the heightMap file.
280
281 Internally, HeightFieldShape converts the height map image to an optimized data structure. This
282 conversion can be done in advance. See the \l{Qt Quick 3D Physics Cooking}{cooking overview
283 documentation} for details.
284
285 \note If both the \l{HeightFieldShape::}{image} and \l{HeightFieldShape::}{source} properties
286 are set then only \l{HeightFieldShape::}{image} will be used.
287 \sa HeightFieldShape::image
288*/
289
290/*!
291 \qmlproperty Image HeightFieldShape::image
292 This property defines the image holding the heightMap.
293
294 Internally, HeightFieldShape converts the height map image to an optimized data structure. This
295 conversion can be done in advance. See the \l{Qt Quick 3D Physics Cooking}{cooking overview
296 documentation} for details.
297
298 \note If both the \l{HeightFieldShape::}{image} and \l{HeightFieldShape::}{source} properties
299 are set then only \l{HeightFieldShape::}{image} will be used.
300 \sa HeightFieldShape::source
301 \since 6.7
302*/
303
304QHeightFieldShape::QHeightFieldShape() = default;
305
306QHeightFieldShape::~QHeightFieldShape()
307{
308 delete m_heightFieldGeometry;
309 if (m_heightField)
310 QQuick3DPhysicsHeightFieldManager::releaseHeightField(m_heightField);
311}
312
313physx::PxGeometry *QHeightFieldShape::getPhysXGeometry()
314{
315 if (m_dirtyPhysx || m_scaleDirty || !m_heightFieldGeometry) {
316 updatePhysXGeometry();
317 }
318 return m_heightFieldGeometry;
319}
320
321void QHeightFieldShape::updatePhysXGeometry()
322{
323 delete m_heightFieldGeometry;
324 m_heightFieldGeometry = nullptr;
325 if (!m_heightField)
326 return;
327
328 auto *hf = m_heightField->heightField();
329 float rows = m_heightField->rows();
330 float cols = m_heightField->columns();
331 updateExtents();
332 if (hf && cols > 1 && rows > 1) {
333 QVector3D scaledExtents = m_extents * sceneScale();
334 m_heightFieldGeometry = new physx::PxHeightFieldGeometry(
335 hf, physx::PxMeshGeometryFlags(), scaledExtents.y() / 0x10000,
336 scaledExtents.x() / (cols - 1), scaledExtents.z() / (rows - 1));
337 m_hfOffset = { -scaledExtents.x() / 2, 0, -scaledExtents.z() / 2 };
338
339 qCDebug(lcQuick3dPhysics) << "created height field geom" << m_heightFieldGeometry << "scale"
340 << scaledExtents << m_heightField->columns()
341 << m_heightField->rows();
342 }
343 m_dirtyPhysx = false;
344}
345
346void QHeightFieldShape::updateExtents()
347{
348 if (!m_heightField || m_extentsSetExplicitly)
349 return;
350 int numRows = m_heightField->rows();
351 int numCols = m_heightField->columns();
352 auto prevExt = m_extents;
353 if (numRows == numCols) {
354 m_extents = { 100, 100, 100 };
355 } else if (numRows < numCols) {
356 float f = float(numRows) / float(numCols);
357 m_extents = { 100.f, 100.f, 100.f * f };
358 } else {
359 float f = float(numCols) / float(numRows);
360 m_extents = { 100.f * f, 100.f, 100.f };
361 }
362 if (m_extents != prevExt) {
363 emit extentsChanged();
364 }
365}
366
367const QUrl &QHeightFieldShape::source() const
368{
369 return m_heightMapSource;
370}
371
372void QHeightFieldShape::setSource(const QUrl &newSource)
373{
374 if (m_heightMapSource == newSource)
375 return;
376 m_heightMapSource = newSource;
377
378 // If we get a new source and our heightfield was from the old source
379 // (meaning it was NOT from an image) we deref
380 if (m_image == nullptr) {
381 QQuick3DPhysicsHeightFieldManager::releaseHeightField(m_heightField);
382 m_heightField = nullptr;
383 }
384
385 // Load new height field only if we don't have image as source
386 if (m_image == nullptr && !newSource.isEmpty()) {
387 m_heightField = QQuick3DPhysicsHeightFieldManager::getHeightField(m_heightMapSource, this);
388 emit needsRebuild(this);
389 }
390
391 m_dirtyPhysx = true;
392 emit sourceChanged();
393}
394
395QQuickImage *QHeightFieldShape::image() const
396{
397 return m_image;
398}
399
400void QHeightFieldShape::setImage(QQuickImage *newImage)
401{
402 if (m_image == newImage)
403 return;
404
405 if (m_image)
406 m_image->disconnect(this);
407
408 m_image = newImage;
409
410 if (m_image != nullptr) {
411 connect(m_image, &QObject::destroyed, this, &QHeightFieldShape::imageDestroyed);
412 connect(m_image, &QQuickImage::paintedGeometryChanged, this,
413 &QHeightFieldShape::imageGeometryChanged);
414 }
415
416 // New image means we get a new heightfield so deref the old one
417 QQuick3DPhysicsHeightFieldManager::releaseHeightField(m_heightField);
418 m_heightField = nullptr;
419
420 if (m_image != nullptr)
421 m_heightField = QQuick3DPhysicsHeightFieldManager::getHeightField(m_image);
422 else if (!m_heightMapSource.isEmpty())
423 m_heightField = QQuick3DPhysicsHeightFieldManager::getHeightField(m_heightMapSource, this);
424
425 m_dirtyPhysx = true;
426 emit needsRebuild(this);
427 emit imageChanged();
428}
429
430void QHeightFieldShape::imageDestroyed(QObject *image)
431{
432 Q_ASSERT(m_image == image);
433 // Set image to null and the old one will be disconnected and dereferenced
434 setImage(nullptr);
435}
436
437void QHeightFieldShape::imageGeometryChanged()
438{
439 Q_ASSERT(m_image);
440 // Using image has precedence so it is safe to assume this is the current source
441 QQuick3DPhysicsHeightFieldManager::releaseHeightField(m_heightField);
442 m_heightField = QQuick3DPhysicsHeightFieldManager::getHeightField(m_image);
443 m_dirtyPhysx = true;
444 emit needsRebuild(this);
445}
446
447const QVector3D &QHeightFieldShape::extents() const
448{
449 return m_extents;
450}
451
452void QHeightFieldShape::setExtents(const QVector3D &newExtents)
453{
454 m_extentsSetExplicitly = true;
455 if (m_extents == newExtents)
456 return;
457 m_extents = newExtents;
458
459 m_dirtyPhysx = true;
460
461 emit needsRebuild(this);
462 emit extentsChanged();
463}
464
465QT_END_NAMESPACE
static QQuick3DPhysicsHeightField * getHeightField(QQuickImage *source)
static QQuick3DPhysicsHeightField * getHeightField(const QUrl &source, const QObject *contextObject)
static void releaseHeightField(QQuick3DPhysicsHeightField *heightField)
QQuick3DPhysicsHeightField(const QString &qmlSource)
QQuick3DPhysicsHeightField(QQuickImage *image)
void writeSamples(const QImage &heightMap)
physx::PxHeightField * heightField()