Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qssgmesh_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QSSGMESHUTILITIES_P_H
5#define QSSGMESHUTILITIES_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h>
19
20#include <QtQuick3DUtils/private/qssgbounds3_p.h>
21
22#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
23
24#include <QtCore/qstring.h>
25#include <QtCore/qbytearray.h>
26#include <QtCore/qiodevice.h>
27#include <QtCore/qmap.h>
28
30
32{
40
43 quint32 numComponents,
44 quint32 firstItemOffset = 0)
45 : m_name(nm), m_componentType(type), m_numComponents(numComponents), m_firstItemOffset(firstItemOffset)
46 {
47 }
48
53
61
63 {
64 if (this != &inOther) {
65 m_name = inOther.m_name;
66 m_componentType = inOther.m_componentType;
67 m_numComponents = inOther.m_numComponents;
68 m_firstItemOffset = inOther.m_firstItemOffset;
69 }
70 return *this;
71 }
72};
73
74namespace QSSGMesh {
75
76struct AssetVertexEntry;
77struct AssetMeshSubset;
78struct RuntimeMeshData;
79struct AssetLodEntry;
80
81class Q_QUICK3DUTILS_EXPORT Mesh
82{
83public:
87
89 ComponentType componentType = ComponentType::Float32;
90 quint32 componentCount = 0;
93
100 };
101
104 QVector<VertexBufferEntry> entries;
106 };
107
108 struct IndexBuffer {
109 ComponentType componentType = ComponentType::UnsignedInt32;
111 };
112
114 quint32 numTargets = 0;
115 QVector<VertexBufferEntry> entries;
117 };
118
123
124 struct Lod {
127 float distance = 0.0f;
128 };
129
138
139 // can just return by value (big data is all implicitly shared)
140 VertexBuffer vertexBuffer() const { return m_vertexBuffer; }
141 IndexBuffer indexBuffer() const { return m_indexBuffer; }
142 TargetBuffer targetBuffer() const { return m_targetBuffer; }
143 QVector<Subset> subsets() const { return m_subsets; }
144
145 // id 0 == first, otherwise has to match
146 static Mesh loadMesh(QIODevice *device, quint32 id = 0);
147
148 static QMap<quint32, Mesh> loadAll(QIODevice *device);
149
150 static Mesh fromAssetData(const QVector<AssetVertexEntry> &vbufEntries,
151 const QByteArray &indexBufferData,
152 ComponentType indexComponentType,
153 const QVector<AssetMeshSubset> &subsets,
154 quint32 numTargets = 0, quint32 numTargetComps = 0);
155
156 static Mesh fromRuntimeData(const RuntimeMeshData &data,
157 QString *error);
158
159 bool isValid() const { return !m_subsets.isEmpty(); }
160
161 DrawMode drawMode() const { return m_drawMode; }
162 Winding winding() const { return m_winding; }
163
164 // id 0 == generate new id; otherwise uses it as-is, and must be an unused one
165 quint32 save(QIODevice *device, quint32 id = 0) const;
166
167 bool hasLightmapUVChannel() const;
168 bool createLightmapUVChannel(uint lightmapBaseResolution);
169
170private:
171 DrawMode m_drawMode = DrawMode::Triangles;
172 Winding m_winding = Winding::CounterClockwise;
173 VertexBuffer m_vertexBuffer;
174 IndexBuffer m_indexBuffer;
175 TargetBuffer m_targetBuffer;
176 QVector<Subset> m_subsets;
177 friend struct MeshInternal;
178};
179
180struct Q_QUICK3DUTILS_EXPORT AssetVertexEntry // for asset importer plugins (Assimp, FBX)
181{
184 Mesh::ComponentType componentType = Mesh::ComponentType::Float32;
185 quint32 componentCount = 0;
186 qint32 morphTargetId = -1; // -1 menas that this entry belongs to the original mesh.
187};
188
189struct Q_QUICK3DUTILS_EXPORT AssetMeshSubset // for asset importer plugins (Assimp, FBX)
190{
194 quint32 boundsPositionEntryIndex = std::numeric_limits<quint32>::max();
195 quint32 lightmapWidth = 0;
196 quint32 lightmapHeight = 0;
197 QVector<Mesh::Lod> lods;
198};
199
200struct Q_QUICK3DUTILS_EXPORT RuntimeMeshData // for custom geometry (QQuick3DGeometry, QSSGRenderGeometry)
201{
202 struct Attribute {
203 enum Semantic {
204 IndexSemantic = 0,
205 PositionSemantic, // attr_pos
206 NormalSemantic, // attr_norm
207 TexCoordSemantic, // attr_uv0
208 TangentSemantic, // attr_textan
209 BinormalSemantic, // attr_binormal
210 JointSemantic, // attr_joints
211 WeightSemantic, // attr_weights
212 ColorSemantic, // attr_color
213 TexCoord1Semantic, // attr_uv1
214 TexCoord0Semantic = TexCoordSemantic // attr_uv0
215 };
216
217 Semantic semantic = PositionSemantic;
218 Mesh::ComponentType componentType = Mesh::ComponentType::Float32;
219 int offset = 0;
220
221 int componentCount() const {
222 switch (semantic) {
223 case IndexSemantic: return 1;
224 case PositionSemantic: return 3;
225 case NormalSemantic: return 3;
226 case TexCoord0Semantic: return 2;
227 case TexCoord1Semantic: return 2;
228 case TangentSemantic: return 3;
229 case BinormalSemantic: return 3;
230 case JointSemantic: return 4;
231 case WeightSemantic: return 4;
232 case ColorSemantic: return 4;
233 }
234 Q_UNREACHABLE_RETURN(0);
235 }
236 };
237
243
244 static const int MAX_ATTRIBUTES = 16;
245 static const int MAX_TARGET_ATTRIBUTES = 32;
246
247 void clear()
248 {
249 clearVertexAndIndex();
250 clearTarget();
251 }
253 {
254 m_vertexBuffer.clear();
255 m_indexBuffer.clear();
256 m_subsets.clear();
257 m_attributeCount = 0;
258 m_primitiveType = Mesh::DrawMode::Triangles;
259 }
261 {
262 m_targetBuffer.clear();
263 m_targetAttributeCount = 0;
264 }
265
269 QVector<Mesh::Subset> m_subsets;
270
271 Attribute m_attributes[MAX_ATTRIBUTES];
272 int m_attributeCount = 0;
273 TargetAttribute m_targetAttributes[MAX_TARGET_ATTRIBUTES];
274 int m_targetAttributeCount = 0;
275 Mesh::DrawMode m_primitiveType = Mesh::DrawMode::Triangles;
276 int m_stride = 0;
277};
278
279struct Q_QUICK3DUTILS_EXPORT MeshInternal
280{
283 quint32 fileVersion = 0;
284 QMap<quint32, quint64> meshEntries;
285 static const quint32 FILE_ID = 555777497;
286 static const quint32 FILE_VERSION = 1;
287 bool isValid() const {
288 return fileId == FILE_ID && fileVersion == FILE_VERSION;
289 }
291 return { FILE_ID, FILE_VERSION, {} };
292 }
293 };
294
297 quint16 fileVersion = 0;
299 quint32 sizeInBytes = 0;
300
301 static const quint32 FILE_ID = 3365961549;
302
303 // Version 3 and 4 is only different in the "offset" values that are
304 // all zeroes in version 4 because they are not used by the new mesh
305 // reader. So to support both with the new loader, no branching is
306 // needed at all, it just needs to accept both versions.
307 static const quint32 LEGACY_MESH_FILE_VERSION = 3;
308 // Version 5 differs from 4 with the added lightmapSizeHint per subset.
309 // This needs branching in the deserializer.
310 // Version 6 differs from 5 with additional lodCount per subset as well
311 // as a list of Level of Detail data after the subset names.
312 // Version 7 will split the morph target data
313 static const quint32 FILE_VERSION = 7;
314
316 return { FILE_ID, FILE_VERSION, 0, 0 };
317 }
318
319 bool isValid() const {
320 return fileId == FILE_ID
321 && fileVersion <= FILE_VERSION
322 && fileVersion >= LEGACY_MESH_FILE_VERSION;
323 }
324
325 bool hasLightmapSizeHint() const {
326 return fileVersion >= 5;
327 }
328
329 bool hasLodDataHint() const {
330 return fileVersion >= 6;
331 }
332
334 return fileVersion >= 7;
335 }
336 };
337
339 quint32 startOffset = 0;
340 quint32 byteCounter = 0;
341
343 : startOffset(offset) {}
344
345 int offset() {
346 return startOffset + byteCounter;
347 }
348
349 quint32 alignedAdvance(int advanceAmount) {
350 advance(advanceAmount);
351 quint32 alignmentAmount = 4 - (byteCounter % 4);
352 byteCounter += alignmentAmount;
353 return alignmentAmount;
354 }
355
356 void advance(int advanceAmount) {
357 byteCounter += advanceAmount;
358 }
359 };
360
361 struct Subset {
363 quint32 nameLength = 0;
368 quint32 lodCount = 0;
369
371 Mesh::Subset subset;
372 if (nameLength > 0)
373 subset.name = QString::fromUtf16(reinterpret_cast<const char16_t *>(rawNameUtf16.constData()), nameLength - 1);
374 subset.bounds.min = bounds.min;
375 subset.bounds.max = bounds.max;
376 subset.count = count;
377 subset.offset = offset;
378 subset.lightmapSizeHint = lightmapSizeHint;
379 subset.lods.resize(lodCount);
380 return subset;
381 }
382 };
383
384 static MultiMeshInfo readFileHeader(QIODevice *device);
385 static void writeFileHeader(QIODevice *device, const MultiMeshInfo &meshFileInfo);
386 static quint64 readMeshData(QIODevice *device, quint64 offset, Mesh *mesh, MeshDataHeader *header);
387 static void writeMeshHeader(QIODevice *device, const MeshDataHeader &header);
388 static quint64 writeMeshData(QIODevice *device, const Mesh &mesh);
389
391
392 static const char *getPositionAttrName() { return "attr_pos"; }
393 static const char *getNormalAttrName() { return "attr_norm"; }
394 static const char *getUV0AttrName() { return "attr_uv0"; }
395 static const char *getUV1AttrName() { return "attr_uv1"; }
396 static const char *getLightmapUVAttrName() { return "attr_lightmapuv"; }
397 static const char *getTexTanAttrName() { return "attr_textan"; }
398 static const char *getTexBinormalAttrName() { return "attr_binormal"; }
399 static const char *getColorAttrName() { return "attr_color"; }
400 static const char *getJointAttrName() { return "attr_joints"; }
401 static const char *getWeightAttrName() { return "attr_weights"; }
402
403 static QSSGBounds3 calculateSubsetBounds(const Mesh::VertexBufferEntry &entry,
404 const QByteArray &vertexBufferData,
405 quint32 vertexBufferStride,
406 const QByteArray &indexBufferData,
407 Mesh::ComponentType indexComponentType,
408 quint32 subsetCount,
409 quint32 subsetOffset);
410};
411
412size_t Q_QUICK3DUTILS_EXPORT simplifyMesh(unsigned int* destination,
413 const unsigned int* indices,
414 size_t indexCount,
415 const float* vertexPositions,
416 size_t vertexCount,
417 size_t vertexPositionsStride,
418 size_t targetIndexCount,
419 float targetError,
420 unsigned int options,
421 float* resultError);
422
423float Q_QUICK3DUTILS_EXPORT simplifyScale(const float* vertexPositions,
424 size_t vertexCount,
425 size_t vertexPositionsStride);
426
427void Q_QUICK3DUTILS_EXPORT optimizeVertexCache(unsigned int* destination,
428 const unsigned int* indices,
429 size_t indexCount,
430 size_t vertexCount);
431
432} // namespace QSSGMesh
433
435
436#endif // QSSGMESHUTILITIES_P_H
IOBluetoothDevice * device
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
\inmodule QtCore \reentrant
Definition qiodevice.h:34
static size_t getSizeOfType(QSSGRenderComponentType type)
Class representing 3D range or axis aligned bounding box.
bool isValid() const
Definition qssgmesh_p.h:159
VertexBuffer vertexBuffer() const
Definition qssgmesh_p.h:140
Winding winding() const
Definition qssgmesh_p.h:162
IndexBuffer indexBuffer() const
Definition qssgmesh_p.h:141
QVector< Subset > subsets() const
Definition qssgmesh_p.h:143
TargetBuffer targetBuffer() const
Definition qssgmesh_p.h:142
DrawMode drawMode() const
Definition qssgmesh_p.h:161
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:6045
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
float simplifyScale(const float *vertexPositions, size_t vertexCount, size_t vertexPositionsStride)
void optimizeVertexCache(unsigned int *destination, const unsigned int *indices, size_t indexCount, size_t vertexCount)
size_t simplifyMesh(unsigned int *destination, const unsigned int *indices, size_t indexCount, const float *vertexPositions, size_t vertexCount, size_t vertexPositionsStride, size_t targetIndexCount, float targetError, unsigned int options, float *resultError)
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char * destination
DBusConnection const char DBusError * error
static QString header(const QString &name)
static QByteArray fileId(HANDLE handle)
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
const void GLsizei GLsizei stride
GLsizei GLsizei GLfloat distance
GLenum type
GLbitfield flags
GLenum GLuint GLintptr offset
GLuint name
GLsizei GLenum const void * indices
GLuint entry
QSSGRenderComponentType
unsigned int quint32
Definition qtypes.h:50
unsigned short quint16
Definition qtypes.h:48
int qint32
Definition qtypes.h:49
unsigned long long quint64
Definition qtypes.h:61
unsigned int uint
Definition qtypes.h:34
QVector< Mesh::Lod > lods
Definition qssgmesh_p.h:197
static MeshDataHeader withDefaults()
Definition qssgmesh_p.h:315
quint32 alignedAdvance(int advanceAmount)
Definition qssgmesh_p.h:349
static MultiMeshInfo withDefaults()
Definition qssgmesh_p.h:290
QMap< quint32, quint64 > meshEntries
Definition qssgmesh_p.h:284
Mesh::Subset toMeshSubset() const
Definition qssgmesh_p.h:370
Mesh::SubsetBounds bounds
Definition qssgmesh_p.h:364
static quint32 byteSizeForComponentType(Mesh::ComponentType componentType)
Definition qssgmesh_p.h:390
static const char * getLightmapUVAttrName()
Definition qssgmesh_p.h:396
static const char * getNormalAttrName()
Definition qssgmesh_p.h:393
static const char * getUV1AttrName()
Definition qssgmesh_p.h:395
static const char * getTexBinormalAttrName()
Definition qssgmesh_p.h:398
static const char * getPositionAttrName()
Definition qssgmesh_p.h:392
static const char * getTexTanAttrName()
Definition qssgmesh_p.h:397
static const char * getColorAttrName()
Definition qssgmesh_p.h:399
static const char * getJointAttrName()
Definition qssgmesh_p.h:400
static const char * getUV0AttrName()
Definition qssgmesh_p.h:394
static const char * getWeightAttrName()
Definition qssgmesh_p.h:401
QVector< Lod > lods
Definition qssgmesh_p.h:136
QVector< VertexBufferEntry > entries
Definition qssgmesh_p.h:115
QSSGRenderVertexBufferEntry toRenderVertexBufferEntry() const
Definition qssgmesh_p.h:94
QVector< VertexBufferEntry > entries
Definition qssgmesh_p.h:104
QVector< Mesh::Subset > m_subsets
Definition qssgmesh_p.h:269
QSSGRenderVertexBufferEntry & operator=(const QSSGRenderVertexBufferEntry &inOther)
Definition qssgmesh_p.h:62
QSSGRenderVertexBufferEntry(const QSSGRenderVertexBufferEntry &inOther)
Definition qssgmesh_p.h:54
QSSGRenderVertexBufferEntry(const QByteArray &nm, QSSGRenderComponentType type, quint32 numComponents, quint32 firstItemOffset=0)
Definition qssgmesh_p.h:41
QSSGRenderComponentType m_componentType
Definition qssgmesh_p.h:35