-
Notifications
You must be signed in to change notification settings - Fork 223
/
example.cpp
459 lines (435 loc) · 16.8 KB
/
example.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
MIT License
Copyright (c) 2018-2020 Jonathan Young
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
example
This example shows how to use the xatlas API to generate a unique set of texture coordinates.
Input: an .obj model file.
Output:
* an .obj model file (example_output.obj). This is simplistic for example purposes, it doesn't copy materials from the input .obj file.
* texture coordinates rasterized to images, colored by chart (example_charts*.tga) and by triangle (example_tris*.tga).
*/
#include <mutex>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <stb_image_write.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4201)
#endif
#include <tiny_obj_loader.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <xatlas.h>
#ifdef _MSC_VER
#define FOPEN(_file, _filename, _mode) { if (fopen_s(&_file, _filename, _mode) != 0) _file = NULL; }
#define STRICMP _stricmp
#else
#define FOPEN(_file, _filename, _mode) _file = fopen(_filename, _mode)
#include <strings.h>
#define STRICMP strcasecmp
#endif
#define OBJ_TRIANGULATE 1 // Pass tinyobj::triangulation flag to tinyobjloader and treat all geometry as triangles.
static bool s_verbose = false;
class Stopwatch
{
public:
Stopwatch() { reset(); }
void reset() { m_start = clock(); }
double elapsed() const { return (clock() - m_start) * 1000.0 / CLOCKS_PER_SEC; }
private:
clock_t m_start;
};
static int Print(const char *format, ...)
{
va_list arg;
va_start(arg, format);
printf("\r"); // Clear progress text.
const int result = vprintf(format, arg);
va_end(arg);
return result;
}
// May be called from any thread.
static bool ProgressCallback(xatlas::ProgressCategory category, int progress, void *userData)
{
// Don't interupt verbose printing.
if (s_verbose)
return true;
Stopwatch *stopwatch = (Stopwatch *)userData;
static std::mutex progressMutex;
std::unique_lock<std::mutex> lock(progressMutex);
if (progress == 0)
stopwatch->reset();
printf("\r %s [", xatlas::StringForEnum(category));
for (int i = 0; i < 10; i++)
printf(progress / ((i + 1) * 10) ? "*" : " ");
printf("] %d%%", progress);
fflush(stdout);
if (progress == 100)
printf("\n %.2f seconds (%g ms) elapsed\n", stopwatch->elapsed() / 1000.0, stopwatch->elapsed());
return true;
}
static void RandomColor(uint8_t *color)
{
for (int i = 0; i < 3; i++)
color[i] = uint8_t((rand() % 255 + 192) * 0.5f);
}
static void SetPixel(uint8_t *dest, int destWidth, int x, int y, const uint8_t *color)
{
uint8_t *pixel = &dest[x * 3 + y * (destWidth * 3)];
pixel[0] = color[0];
pixel[1] = color[1];
pixel[2] = color[2];
}
// https://github.com/miloyip/line/blob/master/line_bresenham.c
// License: public domain.
static void RasterizeLine(uint8_t *dest, int destWidth, const int *p1, const int *p2, const uint8_t *color)
{
const int dx = abs(p2[0] - p1[0]), sx = p1[0] < p2[0] ? 1 : -1;
const int dy = abs(p2[1] - p1[1]), sy = p1[1] < p2[1] ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;
int current[2];
current[0] = p1[0];
current[1] = p1[1];
while (SetPixel(dest, destWidth, current[0], current[1], color), current[0] != p2[0] || current[1] != p2[1])
{
const int e2 = err;
if (e2 > -dx) { err -= dy; current[0] += sx; }
if (e2 < dy) { err += dx; current[1] += sy; }
}
}
/*
https://github.com/ssloy/tinyrenderer/wiki/Lesson-2:-Triangle-rasterization-and-back-face-culling
Copyright Dmitry V. Sokolov
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
static void RasterizeTriangle(uint8_t *dest, int destWidth, const int *t0, const int *t1, const int *t2, const uint8_t *color)
{
if (t0[1] > t1[1]) std::swap(t0, t1);
if (t0[1] > t2[1]) std::swap(t0, t2);
if (t1[1] > t2[1]) std::swap(t1, t2);
int total_height = t2[1] - t0[1];
for (int i = 0; i < total_height; i++) {
bool second_half = i > t1[1] - t0[1] || t1[1] == t0[1];
int segment_height = second_half ? t2[1] - t1[1] : t1[1] - t0[1];
float alpha = (float)i / total_height;
float beta = (float)(i - (second_half ? t1[1] - t0[1] : 0)) / segment_height;
int A[2], B[2];
for (int j = 0; j < 2; j++) {
A[j] = int(t0[j] + (t2[j] - t0[j]) * alpha);
B[j] = int(second_half ? t1[j] + (t2[j] - t1[j]) * beta : t0[j] + (t1[j] - t0[j]) * beta);
}
if (A[0] > B[0]) std::swap(A, B);
for (int j = A[0]; j <= B[0]; j++)
SetPixel(dest, destWidth, j, t0[1] + i, color);
}
}
#if !OBJ_TRIANGULATE
// public-domain code by Darel Rex Finley, 2007
// http://alienryderflex.com/polygon_fill/
static void RasterizePolygon(uint8_t *dest, int destWidth, int vertices[][2], const int vertexCount, const uint8_t *color)
{
int IMAGE_TOP = INT_MAX, IMAGE_BOT = 0, IMAGE_LEFT = INT_MAX, IMAGE_RIGHT = 0;
for (int i = 0; i < vertexCount; i++) {
const int *vertex = vertices[i];
IMAGE_TOP = vertex[1] < IMAGE_TOP ? vertex[1] : IMAGE_TOP;
IMAGE_BOT = vertex[1] > IMAGE_BOT ? vertex[1] : IMAGE_BOT;
IMAGE_LEFT = vertex[0] < IMAGE_LEFT ? vertex[0] : IMAGE_LEFT;
IMAGE_RIGHT = vertex[0] > IMAGE_RIGHT ? vertex[0] : IMAGE_RIGHT;
}
int nodes, nodeX[255], pixelX, pixelY, i, j, swap;
// Loop through the rows of the image.
for (pixelY=IMAGE_TOP; pixelY<IMAGE_BOT; pixelY++) {
// Build a list of nodes.
nodes=0; j=vertexCount-1;
for (i=0; i<vertexCount; i++) {
if (vertices[i][1]<(double) pixelY && vertices[j][1]>=(double) pixelY || vertices[j][1]<(double) pixelY && vertices[i][1]>=(double) pixelY) {
nodeX[nodes++]=(int) (vertices[i][0]+(pixelY-vertices[i][1])/(vertices[j][1]-vertices[i][1])*(vertices[j][0]-vertices[i][0]));
}
j=i;
}
// Sort the nodes, via a simple “Bubble” sort.
i=0;
while (i<nodes-1) {
if (nodeX[i]>nodeX[i+1]) {
swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--; }
else {
i++;
}
}
// Fill the pixels between node pairs.
for (i=0; i<nodes; i+=2) {
if (nodeX[i ]>=IMAGE_RIGHT)
break;
if (nodeX[i+1]> IMAGE_LEFT ) {
if (nodeX[i ]< IMAGE_LEFT )
nodeX[i ]=IMAGE_LEFT ;
if (nodeX[i+1]> IMAGE_RIGHT)
nodeX[i+1]=IMAGE_RIGHT;
for (pixelX=nodeX[i]; pixelX<nodeX[i+1]; pixelX++)
SetPixel(dest, destWidth, pixelX, pixelY, color);
}
}
}
}
#endif
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: %s input_file.obj [options]\n", argv[0]);
printf(" Options:\n");
printf(" -verbose\n");
return 1;
}
s_verbose = (argc >= 3 && STRICMP(argv[2], "-verbose") == 0);
// Load object file.
printf("Loading '%s'...\n", argv[1]);
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
unsigned int flags = 0;
#if OBJ_TRIANGULATE
flags = tinyobj::triangulation;
#endif
if (!tinyobj::LoadObj(shapes, materials, err, argv[1], NULL, flags)) {
printf("Error: %s\n", err.c_str());
return EXIT_FAILURE;
}
if (shapes.size() == 0) {
printf("Error: no shapes in obj file\n");
return EXIT_FAILURE;
}
printf(" %d shapes\n", (int)shapes.size());
// Create empty atlas.
xatlas::SetPrint(Print, s_verbose);
xatlas::Atlas *atlas = xatlas::Create();
// Set progress callback.
Stopwatch globalStopwatch, stopwatch;
xatlas::SetProgressCallback(atlas, ProgressCallback, &stopwatch);
// Add meshes to atlas.
uint32_t totalVertices = 0, totalFaces = 0;
for (int i = 0; i < (int)shapes.size(); i++) {
const tinyobj::mesh_t &objMesh = shapes[i].mesh;
xatlas::MeshDecl meshDecl;
meshDecl.vertexCount = (uint32_t)objMesh.positions.size() / 3;
meshDecl.vertexPositionData = objMesh.positions.data();
meshDecl.vertexPositionStride = sizeof(float) * 3;
if (!objMesh.normals.empty()) {
meshDecl.vertexNormalData = objMesh.normals.data();
meshDecl.vertexNormalStride = sizeof(float) * 3;
}
if (!objMesh.texcoords.empty()) {
meshDecl.vertexUvData = objMesh.texcoords.data();
meshDecl.vertexUvStride = sizeof(float) * 2;
}
meshDecl.indexCount = (uint32_t)objMesh.indices.size();
meshDecl.indexData = objMesh.indices.data();
meshDecl.indexFormat = xatlas::IndexFormat::UInt32;
#if !OBJ_TRIANGULATE
if (objMesh.num_vertices.size() != objMesh.indices.size() / 3) {
meshDecl.faceVertexCount = objMesh.num_vertices.data();
meshDecl.faceCount = (uint32_t)objMesh.num_vertices.size();
}
#endif
xatlas::AddMeshError error = xatlas::AddMesh(atlas, meshDecl, (uint32_t)shapes.size());
if (error != xatlas::AddMeshError::Success) {
xatlas::Destroy(atlas);
printf("\rError adding mesh %d '%s': %s\n", i, shapes[i].name.c_str(), xatlas::StringForEnum(error));
return EXIT_FAILURE;
}
totalVertices += meshDecl.vertexCount;
if (meshDecl.faceCount > 0)
totalFaces += meshDecl.faceCount;
else
totalFaces += meshDecl.indexCount / 3; // Assume triangles if MeshDecl::faceCount not specified.
}
xatlas::AddMeshJoin(atlas); // Not necessary. Only called here so geometry totals are printed after the AddMesh progress indicator.
printf(" %u total vertices\n", totalVertices);
printf(" %u total faces\n", totalFaces);
// Generate atlas.
printf("Generating atlas\n");
xatlas::Generate(atlas);
printf(" %d charts\n", atlas->chartCount);
printf(" %d atlases\n", atlas->atlasCount);
for (uint32_t i = 0; i < atlas->atlasCount; i++)
printf(" %d: %0.2f%% utilization\n", i, atlas->utilization[i] * 100.0f);
printf(" %ux%u resolution\n", atlas->width, atlas->height);
totalVertices = 0;
for (uint32_t i = 0; i < atlas->meshCount; i++) {
const xatlas::Mesh &mesh = atlas->meshes[i];
totalVertices += mesh.vertexCount;
// Input and output index counts always match.
assert(mesh.indexCount == (uint32_t)shapes[i].mesh.indices.size());
}
printf(" %u total vertices\n", totalVertices);
printf("%.2f seconds (%g ms) elapsed total\n", globalStopwatch.elapsed() / 1000.0, globalStopwatch.elapsed());
// Write meshes.
const char *modelFilename = "example_output.obj";
printf("Writing '%s'...\n", modelFilename);
FILE *file;
FOPEN(file, modelFilename, "w");
if (file) {
uint32_t firstVertex = 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];
const float *pos = &shapes[i].mesh.positions[vertex.xref * 3];
fprintf(file, "v %g %g %g\n", pos[0], pos[1], pos[2]);
if (!shapes[i].mesh.normals.empty()) {
const float *normal = &shapes[i].mesh.normals[vertex.xref * 3];
fprintf(file, "vn %g %g %g\n", normal[0], normal[1], normal[2]);
}
fprintf(file, "vt %g %g\n", vertex.uv[0] / atlas->width, vertex.uv[1] / atlas->height);
}
fprintf(file, "o %s\n", shapes[i].name.c_str());
fprintf(file, "s off\n");
#if !OBJ_TRIANGULATE
auto faceCount = (const uint32_t)shapes[i].mesh.num_vertices.size();
uint32_t currentIndex = 0;
for (uint32_t f = 0; f < faceCount; f++) {
fprintf(file, "f ");
auto faceVertexCount = (const uint32_t)shapes[i].mesh.num_vertices[f];
for (uint32_t j = 0; j < faceVertexCount; j++) {
const uint32_t index = firstVertex + mesh.indexArray[currentIndex++] + 1; // 1-indexed
fprintf(file, "%d/%d/%d%c", index, index, index, j == (faceVertexCount - 1) ? '\n' : ' ');
}
}
#else
for (uint32_t f = 0; f < mesh.indexCount; f += 3) {
fprintf(file, "f ");
for (uint32_t j = 0; j < 3; j++) {
const uint32_t index = firstVertex + mesh.indexArray[f + j] + 1; // 1-indexed
fprintf(file, "%d/%d/%d%c", index, index, index, j == 2 ? '\n' : ' ');
}
}
#endif
firstVertex += mesh.vertexCount;
}
fclose(file);
}
if (atlas->width > 0 && atlas->height > 0) {
printf("Rasterizing result...\n");
// Dump images.
std::vector<uint8_t> outputTrisImage, outputChartsImage;
const uint32_t imageDataSize = atlas->width * atlas->height * 3;
outputTrisImage.resize(atlas->atlasCount * imageDataSize);
outputChartsImage.resize(atlas->atlasCount * imageDataSize);
for (uint32_t i = 0; i < atlas->meshCount; i++) {
const xatlas::Mesh &mesh = atlas->meshes[i];
// Rasterize mesh triangles.
const uint8_t white[] = { 255, 255, 255 };
#if OBJ_TRIANGULATE
const uint32_t faceCount = mesh.indexCount / 3;
#else
auto faceCount = (const uint32_t)shapes[i].mesh.num_vertices.size();
#endif
uint32_t faceFirstIndex = 0;
for (uint32_t f = 0; f < faceCount; f++) {
int32_t atlasIndex = -1;
int verts[255][2];
#if OBJ_TRIANGULATE
const uint32_t faceVertexCount = 3;
#else
const uint32_t faceVertexCount = shapes[i].mesh.num_vertices[f];
#endif
for (uint32_t j = 0; j < faceVertexCount; j++) {
const xatlas::Vertex &v = mesh.vertexArray[mesh.indexArray[faceFirstIndex + j]];
atlasIndex = v.atlasIndex; // The same for every vertex in the face.
verts[j][0] = int(v.uv[0]);
verts[j][1] = int(v.uv[1]);
}
if (atlasIndex < 0)
continue; // Skip faces that weren't atlased.
uint8_t color[3];
RandomColor(color);
uint8_t *imageData = &outputTrisImage[atlasIndex * imageDataSize];
#if OBJ_TRIANGULATE
RasterizeTriangle(imageData, atlas->width, verts[0], verts[1], verts[2], color);
#else
if (faceVertexCount == 3)
RasterizeTriangle(imageData, atlas->width, verts[0], verts[1], verts[2], color);
else
RasterizePolygon(imageData, atlas->width, verts, (int)faceVertexCount, color);
#endif
for (uint32_t j = 0; j < faceVertexCount; j++)
RasterizeLine(imageData, atlas->width, verts[j], verts[(j + 1) % faceVertexCount], white);
faceFirstIndex += faceVertexCount;
}
// Rasterize mesh charts.
for (uint32_t j = 0; j < mesh.chartCount; j++) {
const xatlas::Chart *chart = &mesh.chartArray[j];
uint8_t color[3];
RandomColor(color);
for (uint32_t k = 0; k < chart->faceCount; k++) {
const uint32_t face = chart->faceArray[k];
#if OBJ_TRIANGULATE
const uint32_t faceVertexCount = 3;
faceFirstIndex = face * 3;
#else
const uint32_t faceVertexCount = shapes[i].mesh.num_vertices[face];
faceFirstIndex = 0;
for (uint32_t l = 0; l < face; l++)
faceFirstIndex += shapes[i].mesh.num_vertices[l];
#endif
int verts[255][2];
for (uint32_t l = 0; l < faceVertexCount; l++) {
const xatlas::Vertex &v = mesh.vertexArray[mesh.indexArray[faceFirstIndex + l]];
verts[l][0] = int(v.uv[0]);
verts[l][1] = int(v.uv[1]);
}
uint8_t *imageData = &outputChartsImage[chart->atlasIndex * imageDataSize];
#if OBJ_TRIANGULATE
RasterizeTriangle(imageData, atlas->width, verts[0], verts[1], verts[2], color);
#else
if (faceVertexCount == 3)
RasterizeTriangle(imageData, atlas->width, verts[0], verts[1], verts[2], color);
else
RasterizePolygon(imageData, atlas->width, verts, (int)faceVertexCount, color);
#endif
for (uint32_t l = 0; l < faceVertexCount; l++)
RasterizeLine(imageData, atlas->width, verts[l], verts[(l + 1) % faceVertexCount], white);
}
}
}
for (uint32_t i = 0; i < atlas->atlasCount; i++) {
char filename[256];
snprintf(filename, sizeof(filename), "example_tris%02u.tga", i);
printf("Writing '%s'...\n", filename);
stbi_write_tga(filename, atlas->width, atlas->height, 3, &outputTrisImage[i * imageDataSize]);
snprintf(filename, sizeof(filename), "example_charts%02u.tga", i);
printf("Writing '%s'...\n", filename);
stbi_write_tga(filename, atlas->width,atlas->height, 3, &outputChartsImage[i * imageDataSize]);
}
}
// Cleanup.
xatlas::Destroy(atlas);
printf("Done\n");
return EXIT_SUCCESS;
}