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
qssglightmapuvgenerator.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5#include "xatlas.h"
6
8
10 const QByteArray &normals,
11 const QByteArray &uv0,
12 const QByteArray &index,
13 QSSGMesh::Mesh::ComponentType indexComponentType,
14 uint baseResolution)
15{
17
18 xatlas::MeshDecl meshInfo;
19
20 if (indexComponentType == QSSGMesh::Mesh::ComponentType::UnsignedInt16) {
21 meshInfo.indexFormat = xatlas::IndexFormat::UInt16;
22 } else if (indexComponentType == QSSGMesh::Mesh::ComponentType::UnsignedInt32) {
23 meshInfo.indexFormat = xatlas::IndexFormat::UInt32;
24 } else {
25 qWarning("Lightmap UV generator: Unknown index type %d; cannot generate",
26 int(indexComponentType));
27 return result;
28 }
29
30 const quint32 indexComponentByteSize = QSSGMesh::MeshInternal::byteSizeForComponentType(indexComponentType);
31 const quint32 indexCount = index.size() / indexComponentByteSize;
32
33 meshInfo.indexCount = indexCount;
34 meshInfo.indexData = index.constData();
35
36 const quint32 positionStride = 3 * sizeof(float);
37 const quint32 normalStride = 3 * sizeof(float);
38 const quint32 uvStride = 2 * sizeof(float);
39
40 meshInfo.vertexCount = positions.size() / positionStride;
41 meshInfo.vertexPositionData = positions.data();
42 meshInfo.vertexPositionStride = positionStride;
43
44 if (!normals.isEmpty()) {
45 meshInfo.vertexNormalData = normals.constData();
46 meshInfo.vertexNormalStride = normalStride;
47 } else {
48 meshInfo.vertexNormalData = nullptr;
49 meshInfo.vertexNormalStride = 0;
50 }
51
52 if (!uv0.isEmpty()) {
53 meshInfo.vertexUvData = uv0.constData();
54 meshInfo.vertexUvStride = uvStride;
55 } else {
56 meshInfo.vertexUvData = nullptr;
57 meshInfo.vertexUvStride = 0;
58 }
59
60 xatlas::PackOptions packOptions;
61 packOptions.maxChartSize = 4096;
62 packOptions.padding = 1;
63 packOptions.resolution = baseResolution;
64 packOptions.blockAlign = true;
65
66 xatlas::ChartOptions chartOptions;
67
68 xatlas::Atlas *atlas = xatlas::Create();
69 xatlas::AddMeshError err = xatlas::AddMesh(atlas, meshInfo, 1);
70 if (err != xatlas::AddMeshError::Success) {
71 qWarning("Failed to register mesh for UV unwrapping (error %d)", int(err));
72 xatlas::Destroy(atlas);
73 return result;
74 }
75 xatlas::Generate(atlas, chartOptions, packOptions);
76
77 const uint32_t textureWidth = atlas->width;
78 const uint32_t textureHeight = atlas->height;
79 if (textureWidth == 0 || textureHeight == 0) {
80 qWarning("Texture size is empty, UV unwrapping failed");
81 xatlas::Destroy(atlas);
82 return result;
83 }
84 result.lightmapWidth = textureWidth;
85 result.lightmapHeight = textureHeight;
86
87 const xatlas::Mesh &output = atlas->meshes[0];
88 result.lightmapUVChannel.resize(output.vertexCount * uvStride);
89 result.vertexMap.resize(output.vertexCount);
90
91 float *uvPtr = reinterpret_cast<float *>(result.lightmapUVChannel.data());
92 for (uint32_t i = 0; i < output.vertexCount; ++i) {
93 const float u = output.vertexArray[i].uv[0] / float(textureWidth);
94 const float v = output.vertexArray[i].uv[1] / float(textureHeight);
95 *uvPtr++ = u;
96 *uvPtr++ = v;
97 result.vertexMap[i] = output.vertexArray[i].xref;
98 }
99
100 result.indexData.resize(output.indexCount * sizeof(quint32));
101 quint32 *indexPtr = reinterpret_cast<quint32 *>(result.indexData.data());
102 for (uint32_t i = 0; i < output.indexCount; ++i)
103 *indexPtr++ = output.indexArray[i];
104
105 xatlas::Destroy(atlas);
106
107 return result;
108}
109
\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
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
QSSGLightmapUVGeneratorResult run(const QByteArray &positions, const QByteArray &normals, const QByteArray &uv0, const QByteArray &index, QSSGMesh::Mesh::ComponentType indexComponentType, uint baseResolution)
Combined button and popup list for selecting options.
static const QCssKnownValue positions[NumKnownPositionModes - 1]
#define qWarning
Definition qlogging.h:166
GLsizei const GLfloat * v
[13]
GLuint index
[2]
GLuint64EXT * result
[6]
QSSGRenderComponentType
unsigned int quint32
Definition qtypes.h:50
unsigned int uint
Definition qtypes.h:34
QT_BEGIN_NAMESPACE typedef uchar * output
static quint32 byteSizeForComponentType(Mesh::ComponentType componentType)
Definition qssgmesh_p.h:390