-
Notifications
You must be signed in to change notification settings - Fork 1
/
uv.cc
74 lines (61 loc) · 2.87 KB
/
uv.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "uv.h"
/* bool ProgressCallback(xatlas::ProgressCategory::Enum category, int progress, void *userData) {
std::cerr << xatlas::StringForEnum(category) << std::endl;
return true;
} */
void uvParameterize(float *positions, unsigned int numPositions, float *normals, unsigned int numNormals, unsigned int *faces, unsigned int numFaces, float *outPositions, unsigned int &numOutPositions, float *outNormals, unsigned int &numOutNormals, unsigned int *outFaces, float *uvs, unsigned int &numUVs) {
xatlas::Atlas *atlas = xatlas::Create();
// xatlas::SetProgressCallback(atlas, ProgressCallback, nullptr);
// uint32_t totalVertices = 0, totalFaces = 0;
{
// std::cout << "uv 4" << std::endl;
xatlas::MeshDecl meshDecl;
meshDecl.vertexCount = numPositions / 3;
meshDecl.vertexPositionData = positions;
meshDecl.vertexPositionStride = sizeof(float) * 3;
meshDecl.indexCount = 0;
meshDecl.indexData = nullptr;
meshDecl.indexCount = numFaces;
meshDecl.indexData = faces;
meshDecl.indexFormat = xatlas::IndexFormat::UInt32;
// std::cout << "input vertex count " << meshDecl.vertexCount << std::endl;
xatlas::AddMeshError::Enum error = xatlas::AddMesh(atlas, meshDecl, 1);
if (error != xatlas::AddMeshError::Success) {
xatlas::Destroy(atlas);
std::cerr << "Error adding mesh " << xatlas::StringForEnum(error) << std::endl;
return;
}
// std::cout << "uv 6" << std::endl;
// totalVertices += meshDecl.vertexCount;
// totalFaces += meshDecl.indexCount / 3;
}
xatlas::ChartOptions chartOptions;
// chartOptions.maxIterations = 10;
xatlas::ParameterizeOptions parameterizeOptions;
parameterizeOptions.fixTJunctions = false;
xatlas::PackOptions packOptions;
packOptions.texelsPerUnit = 2048;
packOptions.resolution = 2048;
// packOptions.bruteForce = true;
xatlas::Generate(atlas, chartOptions, parameterizeOptions, packOptions);
// std::cout << "uv 7" << std::endl;
// std::cout << "mesh count " << atlas->chartCount << " " << atlas->atlasCount << " " << atlas->width << " " << atlas->height << " " << atlas->meshCount << std::endl;
numOutPositions = 0;
numOutNormals = 0;
numUVs = 0;
for (uint32_t i = 0; i < atlas->meshCount; i++) {
const xatlas::Mesh &mesh = atlas->meshes[i];
for (uint32_t v = 0; v < mesh.vertexCount; v++) {
const xatlas::Vertex &vertex = mesh.vertexArray[v];
memcpy(&outPositions[numOutPositions], &positions[vertex.xref * 3], 3*sizeof(float));
numOutPositions += 3;
memcpy(&outNormals[numOutNormals], &normals[vertex.xref * 3], 3*sizeof(float));
numOutNormals += 3;
uvs[numUVs++] = vertex.uv[0] / atlas->width;
uvs[numUVs++] = vertex.uv[1] / atlas->height;
}
memcpy(outFaces, mesh.indexArray, mesh.indexCount*sizeof(unsigned int));
}
xatlas::Destroy(atlas);
// std::cout << "vertex count " << numUVs << std::endl;
}