Qt 6.x
The Qt SDK
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
31namespace QSSGMesh {
32
33struct AssetVertexEntry;
34struct AssetMeshSubset;
35struct RuntimeMeshData;
36struct AssetLodEntry;
37
38class Q_QUICK3DUTILS_EXPORT Mesh
39{
40public:
44
46 ComponentType componentType = ComponentType::Float32;
47 quint32 componentCount = 0;
50
53 QSSGRenderComponentType(componentType),
54 componentCount,
55 offset);
56 }
57 };
58
59 struct VertexBuffer {
63 };
64
65 struct IndexBuffer {
66 ComponentType componentType = ComponentType::UnsignedInt32;
68 };
69
70 struct TargetBuffer {
71 quint32 numTargets = 0;
74 };
75
76 struct SubsetBounds {
79 };
80
81 struct Lod {
84 float distance = 0.0f;
85 };
86
87 struct Subset {
94 };
95
96 // can just return by value (big data is all implicitly shared)
97 VertexBuffer vertexBuffer() const { return m_vertexBuffer; }
98 IndexBuffer indexBuffer() const { return m_indexBuffer; }
99 TargetBuffer targetBuffer() const { return m_targetBuffer; }
100 QVector<Subset> subsets() const { return m_subsets; }
101
102 // id 0 == first, otherwise has to match
103 static Mesh loadMesh(QIODevice *device, quint32 id = 0);
104
105 static QMap<quint32, Mesh> loadAll(QIODevice *device);
106
107 static Mesh fromAssetData(const QVector<AssetVertexEntry> &vbufEntries,
108 const QByteArray &indexBufferData,
109 ComponentType indexComponentType,
110 const QVector<AssetMeshSubset> &subsets,
111 quint32 numTargets = 0, quint32 numTargetComps = 0);
112
113 static Mesh fromRuntimeData(const RuntimeMeshData &data,
114 QString *error);
115
116 bool isValid() const { return !m_subsets.isEmpty(); }
117
118 DrawMode drawMode() const { return m_drawMode; }
119 Winding winding() const { return m_winding; }
120
121 // id 0 == generate new id; otherwise uses it as-is, and must be an unused one
122 quint32 save(QIODevice *device, quint32 id = 0) const;
123
124 bool hasLightmapUVChannel() const;
125 bool createLightmapUVChannel(uint lightmapBaseResolution);
126
127private:
128 DrawMode m_drawMode = DrawMode::Triangles;
129 Winding m_winding = Winding::CounterClockwise;
130 VertexBuffer m_vertexBuffer;
131 IndexBuffer m_indexBuffer;
132 TargetBuffer m_targetBuffer;
133 QVector<Subset> m_subsets;
134 friend struct MeshInternal;
135};
136
137struct Q_QUICK3DUTILS_EXPORT AssetVertexEntry // for asset importer plugins (Assimp, FBX)
138{
141 Mesh::ComponentType componentType = Mesh::ComponentType::Float32;
142 quint32 componentCount = 0;
143 qint32 morphTargetId = -1; // -1 menas that this entry belongs to the original mesh.
144};
145
146struct Q_QUICK3DUTILS_EXPORT AssetMeshSubset // for asset importer plugins (Assimp, FBX)
147{
151 quint32 boundsPositionEntryIndex = std::numeric_limits<quint32>::max();
152 quint32 lightmapWidth = 0;
153 quint32 lightmapHeight = 0;
155};
156
157struct Q_QUICK3DUTILS_EXPORT RuntimeMeshData // for custom geometry (QQuick3DGeometry, QSSGRenderGeometry)
158{
159 struct Attribute {
160 enum Semantic {
161 IndexSemantic = 0,
162 PositionSemantic, // attr_pos
163 NormalSemantic, // attr_norm
164 TexCoordSemantic, // attr_uv0
165 TangentSemantic, // attr_textan
166 BinormalSemantic, // attr_binormal
167 JointSemantic, // attr_joints
168 WeightSemantic, // attr_weights
169 ColorSemantic, // attr_color
170 TexCoord1Semantic, // attr_uv1
171 TexCoord0Semantic = TexCoordSemantic // attr_uv0
172 };
173
174 Semantic semantic = PositionSemantic;
175 Mesh::ComponentType componentType = Mesh::ComponentType::Float32;
176 int offset = 0;
177
178 int componentCount() const {
179 switch (semantic) {
180 case IndexSemantic: return 1;
181 case PositionSemantic: return 3;
182 case NormalSemantic: return 3;
183 case TexCoord0Semantic: return 2;
184 case TexCoord1Semantic: return 2;
185 case TangentSemantic: return 3;
186 case BinormalSemantic: return 3;
187 case JointSemantic: return 4;
188 case WeightSemantic: return 4;
189 case ColorSemantic: return 4;
190 }
191 Q_UNREACHABLE_RETURN(0);
192 }
193 };
194
199 };
200
201 static const int MAX_ATTRIBUTES = 16;
202 static const int MAX_TARGET_ATTRIBUTES = 32;
203
204 void clear()
205 {
206 clearVertexAndIndex();
207 clearTarget();
208 }
210 {
211 m_vertexBuffer.clear();
212 m_indexBuffer.clear();
213 m_subsets.clear();
214 m_attributeCount = 0;
215 m_primitiveType = Mesh::DrawMode::Triangles;
216 }
218 {
219 m_targetBuffer.clear();
220 m_targetAttributeCount = 0;
221 }
222
227
228 Attribute m_attributes[MAX_ATTRIBUTES];
229 int m_attributeCount = 0;
230 TargetAttribute m_targetAttributes[MAX_TARGET_ATTRIBUTES];
231 int m_targetAttributeCount = 0;
232 Mesh::DrawMode m_primitiveType = Mesh::DrawMode::Triangles;
233 int m_stride = 0;
234};
235
236struct Q_QUICK3DUTILS_EXPORT MeshInternal
237{
240 quint32 fileVersion = 0;
242 static const quint32 FILE_ID = 555777497;
243 static const quint32 FILE_VERSION = 1;
244 bool isValid() const {
245 return fileId == FILE_ID && fileVersion == FILE_VERSION;
246 }
248 return { FILE_ID, FILE_VERSION, {} };
249 }
250 };
251
254 quint16 fileVersion = 0;
256 quint32 sizeInBytes = 0;
257
258 static const quint32 FILE_ID = 3365961549;
259
260 // Version 3 and 4 is only different in the "offset" values that are
261 // all zeroes in version 4 because they are not used by the new mesh
262 // reader. So to support both with the new loader, no branching is
263 // needed at all, it just needs to accept both versions.
264 static const quint32 LEGACY_MESH_FILE_VERSION = 3;
265 // Version 5 differs from 4 with the added lightmapSizeHint per subset.
266 // This needs branching in the deserializer.
267 // Version 6 differs from 5 with additional lodCount per subset as well
268 // as a list of Level of Detail data after the subset names.
269 // Version 7 will split the morph target data
270 static const quint32 FILE_VERSION = 7;
271
273 return { FILE_ID, FILE_VERSION, 0, 0 };
274 }
275
276 bool isValid() const {
277 return fileId == FILE_ID
278 && fileVersion <= FILE_VERSION
279 && fileVersion >= LEGACY_MESH_FILE_VERSION;
280 }
281
282 bool hasLightmapSizeHint() const {
283 return fileVersion >= 5;
284 }
285
286 bool hasLodDataHint() const {
287 return fileVersion >= 6;
288 }
289
291 return fileVersion >= 7;
292 }
293 };
294
296 quint32 startOffset = 0;
297 quint32 byteCounter = 0;
298
300 : startOffset(offset) {}
301
302 int offset() {
303 return startOffset + byteCounter;
304 }
305
306 quint32 alignedAdvance(int advanceAmount) {
307 advance(advanceAmount);
308 quint32 alignmentAmount = 4 - (byteCounter % 4);
309 byteCounter += alignmentAmount;
310 return alignmentAmount;
311 }
312
313 void advance(int advanceAmount) {
314 byteCounter += advanceAmount;
315 }
316 };
317
318 struct Subset {
320 quint32 nameLength = 0;
325 quint32 lodCount = 0;
326
328 Mesh::Subset subset;
329 if (nameLength > 0)
330 subset.name = QString::fromUtf16(reinterpret_cast<const char16_t *>(rawNameUtf16.constData()), nameLength - 1);
331 subset.bounds.min = bounds.min;
332 subset.bounds.max = bounds.max;
333 subset.count = count;
334 subset.offset = offset;
335 subset.lightmapSizeHint = lightmapSizeHint;
336 subset.lods.resize(lodCount);
337 return subset;
338 }
339 };
340
341 static MultiMeshInfo readFileHeader(QIODevice *device);
342 static void writeFileHeader(QIODevice *device, const MultiMeshInfo &meshFileInfo);
343 static quint64 readMeshData(QIODevice *device, quint64 offset, Mesh *mesh, MeshDataHeader *header);
344 static void writeMeshHeader(QIODevice *device, const MeshDataHeader &header);
345 static quint64 writeMeshData(QIODevice *device, const Mesh &mesh);
346
348
349 static const char *getPositionAttrName() { return "attr_pos"; }
350 static const char *getNormalAttrName() { return "attr_norm"; }
351 static const char *getUV0AttrName() { return "attr_uv0"; }
352 static const char *getUV1AttrName() { return "attr_uv1"; }
353 static const char *getLightmapUVAttrName() { return "attr_lightmapuv"; }
354 static const char *getTexTanAttrName() { return "attr_textan"; }
355 static const char *getTexBinormalAttrName() { return "attr_binormal"; }
356 static const char *getColorAttrName() { return "attr_color"; }
357 static const char *getJointAttrName() { return "attr_joints"; }
358 static const char *getWeightAttrName() { return "attr_weights"; }
359
360 static QSSGBounds3 calculateSubsetBounds(const Mesh::VertexBufferEntry &entry,
361 const QByteArray &vertexBufferData,
362 quint32 vertexBufferStride,
363 const QByteArray &indexBufferData,
364 Mesh::ComponentType indexComponentType,
365 quint32 subsetCount,
366 quint32 subsetOffset);
367};
368
369size_t Q_QUICK3DUTILS_EXPORT simplifyMesh(unsigned int* destination,
370 const unsigned int* indices,
371 size_t indexCount,
372 const float* vertexPositions,
373 size_t vertexCount,
374 size_t vertexPositionsStride,
375 size_t targetIndexCount,
376 float targetError,
377 unsigned int options,
378 float* resultError);
379
380float Q_QUICK3DUTILS_EXPORT simplifyScale(const float* vertexPositions,
381 size_t vertexCount,
382 size_t vertexPositionsStride);
383
384void Q_QUICK3DUTILS_EXPORT optimizeVertexCache(unsigned int* destination,
385 const unsigned int* indices,
386 size_t indexCount,
387 size_t vertexCount);
388
389} // namespace QSSGMesh
390
392
393#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:122
\inmodule QtCore \reentrant
Definition qiodevice.h:34
Definition qlist.h:74
Definition qmap.h:186
static size_t getSizeOfType(QSSGRenderComponentType type)
Class representing 3D range or axis aligned bounding box.
bool isValid() const
Definition qssgmesh_p.h:116
VertexBuffer vertexBuffer() const
Definition qssgmesh_p.h:97
Winding winding() const
Definition qssgmesh_p.h:119
IndexBuffer indexBuffer() const
Definition qssgmesh_p.h:98
QVector< Subset > subsets() const
Definition qssgmesh_p.h:100
TargetBuffer targetBuffer() const
Definition qssgmesh_p.h:99
DrawMode drawMode() const
Definition qssgmesh_p.h:118
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromUtf16(const char16_t *, qsizetype size=-1)
Definition qstring.cpp:5883
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
const void GLsizei GLsizei stride
GLsizei GLsizei GLfloat distance
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLuint name
GLsizei GLenum const void * indices
GLuint entry
QSSGRenderComponentType
unsigned int quint32
Definition qtypes.h:45
unsigned short quint16
Definition qtypes.h:43
int qint32
Definition qtypes.h:44
unsigned long long quint64
Definition qtypes.h:56
unsigned int uint
Definition qtypes.h:29
QVector< Mesh::Lod > lods
Definition qssgmesh_p.h:154
static MeshDataHeader withDefaults()
Definition qssgmesh_p.h:272
quint32 alignedAdvance(int advanceAmount)
Definition qssgmesh_p.h:306
static MultiMeshInfo withDefaults()
Definition qssgmesh_p.h:247
QMap< quint32, quint64 > meshEntries
Definition qssgmesh_p.h:241
Mesh::Subset toMeshSubset() const
Definition qssgmesh_p.h:327
Mesh::SubsetBounds bounds
Definition qssgmesh_p.h:321
static quint32 byteSizeForComponentType(Mesh::ComponentType componentType)
Definition qssgmesh_p.h:347
static const char * getLightmapUVAttrName()
Definition qssgmesh_p.h:353
static const char * getNormalAttrName()
Definition qssgmesh_p.h:350
static const char * getUV1AttrName()
Definition qssgmesh_p.h:352
static const char * getTexBinormalAttrName()
Definition qssgmesh_p.h:355
static const char * getPositionAttrName()
Definition qssgmesh_p.h:349
static const char * getTexTanAttrName()
Definition qssgmesh_p.h:354
static const char * getColorAttrName()
Definition qssgmesh_p.h:356
static const char * getJointAttrName()
Definition qssgmesh_p.h:357
static const char * getUV0AttrName()
Definition qssgmesh_p.h:351
static const char * getWeightAttrName()
Definition qssgmesh_p.h:358
SubsetBounds bounds
Definition qssgmesh_p.h:89
QVector< Lod > lods
Definition qssgmesh_p.h:93
QVector< VertexBufferEntry > entries
Definition qssgmesh_p.h:72
QSSGRenderVertexBufferEntry toRenderVertexBufferEntry() const
Definition qssgmesh_p.h:51
QVector< VertexBufferEntry > entries
Definition qssgmesh_p.h:61
QVector< Mesh::Subset > m_subsets
Definition qssgmesh_p.h:226