-
Notifications
You must be signed in to change notification settings - Fork 147
/
UVAtlas.cpp
1841 lines (1624 loc) · 61.6 KB
/
UVAtlas.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------
// File: UVAtlas.cpp
//
// UVAtlas command-line tool (sample for UVAtlas library)
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=512686
//--------------------------------------------------------------------------------------
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4005)
#endif
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#define NODRAWTEXT
#define NOGDI
#define NOMCX
#define NOSERVICE
#define NOHELP
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#if __cplusplus < 201703L
#error Requires C++17 (and /Zc:__cplusplus with MSVC)
#endif
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <cwctype>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <list>
#include <locale>
#include <memory>
#include <new>
#include <set>
#include <string>
#include <tuple>
#include <conio.h>
#include <dxgiformat.h>
#include "UVAtlas.h"
#include "DirectXTex.h"
#include "Mesh.h"
//Uncomment to add support for OpenEXR (.exr)
//#define USE_OPENEXR
#ifdef USE_OPENEXR
// See <https://github.com/Microsoft/DirectXTex/wiki/Adding-OpenEXR> for details
#include "DirectXTexEXR.h"
#endif
using namespace DirectX;
namespace
{
enum OPTIONS : uint64_t
{
OPT_RECURSIVE = 1,
OPT_QUALITY,
OPT_MAXCHARTS,
OPT_MAXSTRETCH,
OPT_LIMIT_MERGE_STRETCH,
OPT_LIMIT_FACE_STRETCH,
OPT_GUTTER,
OPT_WIDTH,
OPT_HEIGHT,
OPT_TOPOLOGICAL_ADJ,
OPT_GEOMETRIC_ADJ,
OPT_NORMALS,
OPT_WEIGHT_BY_AREA,
OPT_WEIGHT_BY_EQUAL,
OPT_TANGENTS,
OPT_CTF,
OPT_COLOR_MESH,
OPT_UV_MESH,
OPT_IMT_TEXFILE,
OPT_IMT_VERTEX,
OPT_OUTPUTFILE,
OPT_TOLOWER,
OPT_SDKMESH,
OPT_SDKMESH_V2,
OPT_CMO,
OPT_VBO,
OPT_WAVEFRONT_OBJ,
OPT_CLOCKWISE,
OPT_FORCE_32BIT_IB,
OPT_OVERWRITE,
OPT_NODDS,
OPT_FLIP,
OPT_FLIPU,
OPT_FLIPV,
OPT_FLIPZ,
OPT_VERT_NORMAL_FORMAT,
OPT_VERT_UV_FORMAT,
OPT_VERT_COLOR_FORMAT,
OPT_SECOND_UV,
OPT_VIZ_NORMALS,
OPT_OUTPUT_REMAPPING,
OPT_NOLOGO,
OPT_FILELIST,
OPT_MAX
};
static_assert(OPT_MAX <= 64, "dwOptions is a unsigned int bitfield");
enum CHANNELS
{
CHANNEL_NONE = 0,
CHANNEL_NORMAL,
CHANNEL_COLOR,
CHANNEL_TEXCOORD,
};
struct SConversion
{
std::wstring szSrc;
};
template<typename T>
struct SValue
{
const wchar_t* name;
T value;
};
const XMFLOAT3 g_ColorList[8] =
{
XMFLOAT3(1.0f, 0.5f, 0.5f),
XMFLOAT3(0.5f, 1.0f, 0.5f),
XMFLOAT3(1.0f, 1.0f, 0.5f),
XMFLOAT3(0.5f, 1.0f, 1.0f),
XMFLOAT3(1.0f, 0.5f, 0.75f),
XMFLOAT3(0.0f, 0.5f, 0.75f),
XMFLOAT3(0.5f, 0.5f, 0.75f),
XMFLOAT3(0.5f, 0.5f, 1.0f),
};
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
const SValue<uint64_t> g_pOptions[] =
{
{ L"r", OPT_RECURSIVE },
{ L"q", OPT_QUALITY },
{ L"n", OPT_MAXCHARTS },
{ L"st", OPT_MAXSTRETCH },
{ L"lms", OPT_LIMIT_MERGE_STRETCH },
{ L"lfs", OPT_LIMIT_FACE_STRETCH },
{ L"g", OPT_GUTTER },
{ L"w", OPT_WIDTH },
{ L"h", OPT_HEIGHT },
{ L"ta", OPT_TOPOLOGICAL_ADJ },
{ L"ga", OPT_GEOMETRIC_ADJ },
{ L"nn", OPT_NORMALS },
{ L"na", OPT_WEIGHT_BY_AREA },
{ L"ne", OPT_WEIGHT_BY_EQUAL },
{ L"tt", OPT_TANGENTS },
{ L"tb", OPT_CTF },
{ L"c", OPT_COLOR_MESH },
{ L"t", OPT_UV_MESH },
{ L"it", OPT_IMT_TEXFILE },
{ L"iv", OPT_IMT_VERTEX },
{ L"o", OPT_OUTPUTFILE },
{ L"l", OPT_TOLOWER },
{ L"sdkmesh", OPT_SDKMESH },
{ L"sdkmesh2", OPT_SDKMESH_V2 },
{ L"cmo", OPT_CMO },
{ L"vbo", OPT_VBO },
{ L"wf", OPT_WAVEFRONT_OBJ },
{ L"cw", OPT_CLOCKWISE },
{ L"ib32", OPT_FORCE_32BIT_IB },
{ L"y", OPT_OVERWRITE },
{ L"nodds", OPT_NODDS },
{ L"flip", OPT_FLIP },
{ L"flipu", OPT_FLIPU },
{ L"flipv", OPT_FLIPV },
{ L"flipz", OPT_FLIPZ },
{ L"fn", OPT_VERT_NORMAL_FORMAT },
{ L"fuv", OPT_VERT_UV_FORMAT },
{ L"fc", OPT_VERT_COLOR_FORMAT },
{ L"uv2", OPT_SECOND_UV },
{ L"vn", OPT_VIZ_NORMALS },
{ L"m", OPT_OUTPUT_REMAPPING },
{ L"nologo", OPT_NOLOGO },
{ L"flist", OPT_FILELIST },
{ nullptr, 0 }
};
const SValue<uint32_t> g_vertexNormalFormats[] =
{
{ L"float3", DXGI_FORMAT_R32G32B32_FLOAT },
{ L"float16_4", DXGI_FORMAT_R16G16B16A16_FLOAT },
{ L"r11g11b10", DXGI_FORMAT_R11G11B10_FLOAT },
{ nullptr, 0 }
};
const SValue<uint32_t> g_vertexUVFormats[] =
{
{ L"float2", DXGI_FORMAT_R32G32_FLOAT },
{ L"float16_2", DXGI_FORMAT_R16G16_FLOAT },
{ nullptr, 0 }
};
const SValue<uint32_t> g_vertexColorFormats[] =
{
{ L"bgra", DXGI_FORMAT_B8G8R8A8_UNORM },
{ L"rgba", DXGI_FORMAT_R8G8B8A8_UNORM },
{ L"float4", DXGI_FORMAT_R32G32B32A32_FLOAT },
{ L"float16_4", DXGI_FORMAT_R16G16B16A16_FLOAT },
{ L"rgba_10", DXGI_FORMAT_R10G10B10A2_UNORM },
{ L"r11g11b10", DXGI_FORMAT_R11G11B10_FLOAT },
{ nullptr, 0 }
};
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
HRESULT LoadFromOBJ(const wchar_t* szFilename,
std::unique_ptr<Mesh>& inMesh, std::vector<Mesh::Material>& inMaterial,
bool ccw, bool dds);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
namespace
{
inline HANDLE safe_handle(HANDLE h) noexcept { return (h == INVALID_HANDLE_VALUE) ? nullptr : h; }
struct find_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } };
using ScopedFindHandle = std::unique_ptr<void, find_closer>;
#ifdef __PREFAST__
#pragma prefast(disable : 26018, "Only used with static internal arrays")
#endif
template<typename T>
T LookupByName(const wchar_t *pName, const SValue<T> *pArray)
{
while (pArray->name)
{
if (!_wcsicmp(pName, pArray->name))
return pArray->value;
pArray++;
}
return 0;
}
void SearchForFiles(const std::filesystem::path& path, std::list<SConversion>& files, bool recursive)
{
// Process files
WIN32_FIND_DATAW findData = {};
ScopedFindHandle hFile(safe_handle(FindFirstFileExW(path.c_str(),
FindExInfoBasic, &findData,
FindExSearchNameMatch, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (hFile)
{
for (;;)
{
if (!(findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)))
{
SConversion conv = {};
conv.szSrc = path.parent_path().append(findData.cFileName).native();
files.push_back(conv);
}
if (!FindNextFileW(hFile.get(), &findData))
break;
}
}
// Process directories
if (recursive)
{
auto searchDir = path.parent_path().append(L"*");
hFile.reset(safe_handle(FindFirstFileExW(searchDir.c_str(),
FindExInfoBasic, &findData,
FindExSearchLimitToDirectories, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (!hFile)
return;
for (;;)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] != L'.')
{
auto subdir = path.parent_path().append(findData.cFileName).append(path.filename().c_str());
SearchForFiles(subdir, files, recursive);
}
}
if (!FindNextFileW(hFile.get(), &findData))
break;
}
}
}
void ProcessFileList(std::wifstream& inFile, std::list<SConversion>& files)
{
std::list<SConversion> flist;
std::set<std::wstring> excludes;
for (;;)
{
std::wstring fname;
std::getline(inFile, fname);
if (!inFile)
break;
if (fname[0] == L'#')
{
// Comment
}
else if (fname[0] == L'-')
{
if (flist.empty())
{
wprintf(L"WARNING: Ignoring the line '%ls' in -flist\n", fname.c_str());
}
else
{
std::filesystem::path path(fname.c_str() + 1);
auto& npath = path.make_preferred();
if (wcspbrk(fname.c_str(), L"?*") != nullptr)
{
std::list<SConversion> removeFiles;
SearchForFiles(npath, removeFiles, false);
for (auto& it : removeFiles)
{
std::wstring name = it.szSrc;
std::transform(name.begin(), name.end(), name.begin(), towlower);
excludes.insert(name);
}
}
else
{
std::wstring name = npath.c_str();
std::transform(name.begin(), name.end(), name.begin(), towlower);
excludes.insert(name);
}
}
}
else if (wcspbrk(fname.c_str(), L"?*") != nullptr)
{
std::filesystem::path path(fname.c_str());
SearchForFiles(path.make_preferred(), flist, false);
}
else
{
SConversion conv = {};
std::filesystem::path path(fname.c_str());
conv.szSrc = path.make_preferred().native();
flist.push_back(conv);
}
}
inFile.close();
if (!excludes.empty())
{
// Remove any excluded files
for (auto it = flist.begin(); it != flist.end();)
{
std::wstring name = it->szSrc;
std::transform(name.begin(), name.end(), name.begin(), towlower);
auto item = it;
++it;
if (excludes.find(name) != excludes.end())
{
flist.erase(item);
}
}
}
if (flist.empty())
{
wprintf(L"WARNING: No file names found in -flist\n");
}
else
{
files.splice(files.end(), flist);
}
}
void PrintList(size_t cch, const SValue<uint32_t>* pValue)
{
while (pValue->name)
{
const size_t cchName = wcslen(pValue->name);
if (cch + cchName + 2 >= 80)
{
wprintf(L"\n ");
cch = 6;
}
wprintf(L"%ls ", pValue->name);
cch += cchName + 2;
pValue++;
}
wprintf(L"\n");
}
void PrintLogo(bool versionOnly)
{
wchar_t version[32] = {};
wchar_t appName[_MAX_PATH] = {};
if (GetModuleFileNameW(nullptr, appName, _MAX_PATH))
{
const DWORD size = GetFileVersionInfoSizeW(appName, nullptr);
if (size > 0)
{
auto verInfo = std::make_unique<uint8_t[]>(size);
if (GetFileVersionInfoW(appName, 0, size, verInfo.get()))
{
LPVOID lpstr = nullptr;
UINT strLen = 0;
if (VerQueryValueW(verInfo.get(), L"\\StringFileInfo\\040904B0\\ProductVersion", &lpstr, &strLen))
{
wcsncpy_s(version, reinterpret_cast<const wchar_t*>(lpstr), strLen);
}
}
}
}
if (!*version || wcscmp(version, L"1.0.0.0") == 0)
{
swprintf_s(version, L"%03d (library)", UVATLAS_VERSION);
}
if (versionOnly)
{
wprintf(L"uvatlastool version %ls\n", version);
}
else
{
wprintf(L"Microsoft (R) UVAtlas Command-line Tool Version %ls\n", version);
wprintf(L"Copyright (C) Microsoft Corp.\n");
#ifdef _DEBUG
wprintf(L"*** Debug build ***\n");
#endif
wprintf(L"\n");
}
}
void PrintUsage()
{
PrintLogo(false);
static const wchar_t* const s_usage =
L"Usage: uvatlas <options> [--] <files>\n"
L"\n"
L" Input file type must be Wavefront Object (.obj)\n"
L"\n"
L" Output file type:\n"
L" -sdkmesh DirectX SDK .sdkmesh format (default)\n"
L" -sdkmesh2 .sdkmesh format version 2 (PBR materials)\n"
L" -cmo Visual Studio Content Pipeline .cmo format\n"
L" -vbo Vertex Buffer Object (.vbo) format\n"
L" -wf WaveFront Object (.obj) format\n"
L"\n"
L" -r wildcard filename search is recursive\n"
L" -q <level> sets quality level to DEFAULT, FAST or QUALITY\n"
L" -n <number> maximum number of charts to generate (def: 0)\n"
L" -st <float> maximum amount of stretch 0.0 to 1.0 (def: 0.16667)\n"
L" -lms enable limit merge stretch option\n"
L" -lfs enable limit face stretch option\n"
L" -g <float> the gutter width betwen charts in texels (def: 2.0)\n"
L" -w <number> texture width (def: 512)\n"
L" -h <number> texture height (def: 512)\n"
L" -ta | -ga generate topological vs. geometric adjancecy (def: ta)\n"
L" -nn | -na | -ne generate normals weighted by angle/area/equal\n"
L" -tt generate tangents\n"
L" -tb generate tangents & bi-tangents\n"
L" -cw faces are clockwise (defaults to counter-clockwise)\n"
L" -c generate mesh with colors showing charts\n"
L" -t generates a separate mesh with uvs - (*_texture)\n"
L" -vn with -t creates per vertex colors from normals\n"
L" -m generates a text file with vertex remapping (*_map)\n"
L" -it <filename> calculate IMT for the mesh using this texture map\n"
L" -iv <channel> calculate IMT using per-vertex data\n"
L" NORMAL, COLOR, TEXCOORD\n"
L" -nodds prevents extension renaming in exported materials\n"
L" -flip reverse winding of faces\n"
L" -flipu inverts the u texcoords\n"
L" -flipv inverts the v texcoords\n"
L" -flipz flips the handedness of the positions/normals\n"
L" -o <filename> output filename\n"
L" -l force output filename to lower case\n"
L" -y overwrite existing output file (if any)\n"
L" -nologo suppress copyright message\n"
L" -flist <filename> use text file with a list of input files (one per line)\n"
L"\n"
L" (sdkmesh/sdkmesh2 only)\n"
L" -ib32 use 32-bit index buffer\n"
L" -fn <normal-format> format to use for writing normals/tangents/normals\n"
L" -fuv <uv-format> format to use for texture coordinates\n"
L" -fc <color-format> format to use for writing colors\n"
L" -uv2 place UVs into a second texture coordinate channel\n"
L"\n"
L" '-- ' is needed if any input filepath starts with the '-' or '/' character\n";
wprintf(L"%ls", s_usage);
wprintf(L"\n <normal-format>: ");
PrintList(13, g_vertexNormalFormats);
wprintf(L"\n <uv-format>: ");
PrintList(13, g_vertexUVFormats);
wprintf(L"\n <color-format>: ");
PrintList(13, g_vertexColorFormats);
}
const wchar_t* GetErrorDesc(HRESULT hr)
{
static wchar_t desc[1024] = {};
LPWSTR errorText = nullptr;
const DWORD result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
nullptr, static_cast<DWORD>(hr),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>(&errorText), 0, nullptr);
*desc = 0;
if (result > 0 && errorText)
{
swprintf_s(desc, L": %ls", errorText);
size_t len = wcslen(desc);
if (len >= 1)
{
desc[len - 1] = 0;
}
if (errorText)
LocalFree(errorText);
for (wchar_t* ptr = desc; *ptr != 0; ++ptr)
{
if (*ptr == L'\r' || *ptr == L'\n')
{
*ptr = L' ';
}
}
}
return desc;
}
//--------------------------------------------------------------------------------------
HRESULT __cdecl UVAtlasCallback(float fPercentDone)
{
static ULONGLONG s_lastTick = 0;
const ULONGLONG tick = GetTickCount64();
if ((tick - s_lastTick) > 1000)
{
wprintf(L"%.2f%% \r", double(fPercentDone) * 100);
s_lastTick = tick;
}
if (_kbhit())
{
if (_getch() == 27)
{
wprintf(L"*** ABORT ***");
return E_ABORT;
}
}
return S_OK;
}
}
//--------------------------------------------------------------------------------------
// Entry-point
//--------------------------------------------------------------------------------------
#ifdef __PREFAST__
#pragma prefast(disable : 28198, "Command-line tool, frees all memory on exit")
#endif
int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
{
// Parameters and defaults
size_t maxCharts = 0;
float maxStretch = 0.16667f;
float gutter = 2.f;
size_t width = 512;
size_t height = 512;
CHANNELS perVertex = CHANNEL_NONE;
UVATLAS uvOptions = UVATLAS_DEFAULT;
UVATLAS uvOptionsEx = UVATLAS_DEFAULT;
DXGI_FORMAT normalFormat = DXGI_FORMAT_R32G32B32_FLOAT;
DXGI_FORMAT uvFormat = DXGI_FORMAT_R32G32_FLOAT;
DXGI_FORMAT colorFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
std::wstring texFile;
std::wstring outputFile;
// Set locale for output since GetErrorDesc can get localized strings.
std::locale::global(std::locale(""));
// Initialize COM (needed for WIC)
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
{
wprintf(L"Failed to initialize COM (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
return 1;
}
// Process command line
uint64_t dwOptions = 0;
std::list<SConversion> conversion;
bool allowOpts = true;
for (int iArg = 1; iArg < argc; iArg++)
{
PWSTR pArg = argv[iArg];
if (allowOpts
&& ('-' == pArg[0]) && ('-' == pArg[1]))
{
if (pArg[2] == 0)
{
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
allowOpts = false;
}
else if (!_wcsicmp(pArg, L"--version"))
{
PrintLogo(true);
return 0;
}
else if (!_wcsicmp(pArg, L"--help"))
{
PrintUsage();
return 0;
}
else
{
wprintf(L"Unknown option: %ls\n", pArg);
return 1;
}
}
else if (allowOpts
&& (('-' == pArg[0]) || ('/' == pArg[0])))
{
pArg++;
PWSTR pValue;
for (pValue = pArg; *pValue && (':' != *pValue); pValue++);
if (*pValue)
*pValue++ = 0;
const uint64_t dwOption = LookupByName(pArg, g_pOptions);
if (!dwOption || (dwOptions & (uint64_t(1) << dwOption)))
{
wprintf(L"ERROR: unknown command-line option '%ls'\n\n", pArg);
PrintUsage();
return 1;
}
dwOptions |= (uint64_t(1) << dwOption);
// Handle options with additional value parameter
switch (dwOption)
{
case OPT_QUALITY:
case OPT_MAXCHARTS:
case OPT_MAXSTRETCH:
case OPT_GUTTER:
case OPT_WIDTH:
case OPT_HEIGHT:
case OPT_IMT_TEXFILE:
case OPT_IMT_VERTEX:
case OPT_OUTPUTFILE:
case OPT_VERT_NORMAL_FORMAT:
case OPT_VERT_UV_FORMAT:
case OPT_VERT_COLOR_FORMAT:
case OPT_FILELIST:
if (!*pValue)
{
if ((iArg + 1 >= argc))
{
wprintf(L"ERROR: missing value for command-line option '%ls'\n\n", pArg);
PrintUsage();
return 1;
}
iArg++;
pValue = argv[iArg];
}
break;
}
switch (dwOption)
{
case OPT_QUALITY:
if (!_wcsicmp(pValue, L"DEFAULT"))
{
uvOptions = UVATLAS_DEFAULT;
}
else if (!_wcsicmp(pValue, L"FAST"))
{
uvOptions = UVATLAS_GEODESIC_FAST;
}
else if (!_wcsicmp(pValue, L"QUALITY"))
{
uvOptions = UVATLAS_GEODESIC_QUALITY;
}
else
{
wprintf(L"Invalid value specified with -q (%ls)\n", pValue);
return 1;
}
break;
case OPT_LIMIT_MERGE_STRETCH:
uvOptionsEx |= UVATLAS_LIMIT_MERGE_STRETCH;
break;
case OPT_LIMIT_FACE_STRETCH:
uvOptionsEx |= UVATLAS_LIMIT_FACE_STRETCH;
break;
case OPT_MAXCHARTS:
if (swscanf_s(pValue, L"%zu", &maxCharts) != 1)
{
wprintf(L"Invalid value specified with -n (%ls)\n", pValue);
return 1;
}
break;
case OPT_MAXSTRETCH:
if (swscanf_s(pValue, L"%f", &maxStretch) != 1
|| maxStretch < 0.f
|| maxStretch > 1.f)
{
wprintf(L"Invalid value specified with -st (%ls)\n", pValue);
return 1;
}
break;
case OPT_GUTTER:
if (swscanf_s(pValue, L"%f", &gutter) != 1
|| gutter < 0.f)
{
wprintf(L"Invalid value specified with -g (%ls)\n", pValue);
return 1;
}
break;
case OPT_WIDTH:
if (swscanf_s(pValue, L"%zu", &width) != 1)
{
wprintf(L"Invalid value specified with -w (%ls)\n", pValue);
return 1;
}
break;
case OPT_HEIGHT:
if (swscanf_s(pValue, L"%zu", &height) != 1)
{
wprintf(L"Invalid value specified with -h (%ls)\n", pValue);
return 1;
}
break;
case OPT_WEIGHT_BY_AREA:
if (dwOptions & (uint64_t(1) << OPT_WEIGHT_BY_EQUAL))
{
wprintf(L"Can only use one of nn, na, or ne\n");
return 1;
}
dwOptions |= (uint64_t(1) << OPT_NORMALS);
break;
case OPT_WEIGHT_BY_EQUAL:
if (dwOptions & (uint64_t(1) << OPT_WEIGHT_BY_AREA))
{
wprintf(L"Can only use one of nn, na, or ne\n");
return 1;
}
dwOptions |= (uint64_t(1) << OPT_NORMALS);
break;
case OPT_IMT_TEXFILE:
if (dwOptions & (uint64_t(1) << OPT_IMT_VERTEX))
{
wprintf(L"Cannot use both if and iv at the same time\n");
return 1;
}
else
{
std::filesystem::path path(pValue);
texFile = path.make_preferred().native();
}
break;
case OPT_IMT_VERTEX:
if (dwOptions & (uint64_t(1) << OPT_IMT_TEXFILE))
{
wprintf(L"Cannot use both if and iv at the same time\n");
return 1;
}
if (!_wcsicmp(pValue, L"COLOR"))
{
perVertex = CHANNEL_COLOR;
}
else if (!_wcsicmp(pValue, L"NORMAL"))
{
perVertex = CHANNEL_NORMAL;
}
else if (!_wcsicmp(pValue, L"TEXCOORD"))
{
perVertex = CHANNEL_TEXCOORD;
}
else
{
wprintf(L"Invalid value specified with -iv (%ls)\n", pValue);
return 1;
}
break;
case OPT_OUTPUTFILE:
{
std::filesystem::path path(pValue);
outputFile = path.make_preferred().native();
}
break;
case OPT_TOPOLOGICAL_ADJ:
if (dwOptions & (uint64_t(1) << OPT_GEOMETRIC_ADJ))
{
wprintf(L"Cannot use both ta and ga at the same time\n");
return 1;
}
break;
case OPT_GEOMETRIC_ADJ:
if (dwOptions & (uint64_t(1) << OPT_TOPOLOGICAL_ADJ))
{
wprintf(L"Cannot use both ta and ga at the same time\n");
return 1;
}
break;
case OPT_SDKMESH:
case OPT_SDKMESH_V2:
if (dwOptions & ((uint64_t(1) << OPT_VBO) | (uint64_t(1) << OPT_CMO) | (uint64_t(1) << OPT_WAVEFRONT_OBJ)))
{
wprintf(L"Can only use one of sdkmesh, cmo, vbo, or wf\n");
return 1;
}
if (dwOption == OPT_SDKMESH_V2)
{
dwOptions |= (uint64_t(1) << OPT_SDKMESH);
}
break;
case OPT_CMO:
if (dwOptions & (uint64_t(1) << OPT_SECOND_UV))
{
wprintf(L"-uv2 is not supported by CMO\n");
return 1;
}
if (dwOptions & ((uint64_t(1) << OPT_VBO) | (uint64_t(1) << OPT_SDKMESH) | (uint64_t(1) << OPT_WAVEFRONT_OBJ)))
{
wprintf(L"Can only use one of sdkmesh, cmo, vbo, or wf\n");
return 1;
}
break;
case OPT_VBO:
if (dwOptions & (uint64_t(1) << OPT_SECOND_UV))
{
wprintf(L"-uv2 is not supported by VBO\n");
return 1;
}
if (dwOptions & ((uint64_t(1) << OPT_SDKMESH) | (uint64_t(1) << OPT_CMO) | (uint64_t(1) << OPT_WAVEFRONT_OBJ)))
{
wprintf(L"Can only use one of sdkmesh, cmo, vbo, or wf\n");
return 1;
}
break;
case OPT_WAVEFRONT_OBJ:
if (dwOptions & (uint64_t(1) << OPT_SECOND_UV))
{
wprintf(L"-uv2 is not supported by Wavefront OBJ\n");
return 1;
}
if (dwOptions & ((uint64_t(1) << OPT_VBO) | (uint64_t(1) << OPT_SDKMESH) | (uint64_t(1) << OPT_CMO)))
{
wprintf(L"Can only use one of sdkmesh, cmo, vbo, or wf\n");
return 1;
}
break;
case OPT_SECOND_UV:
if (dwOptions & ((uint64_t(1) << OPT_VBO) | (uint64_t(1) << OPT_CMO) | (uint64_t(1) << OPT_WAVEFRONT_OBJ)))
{
wprintf(L"-uv2 is only supported by sdkmesh\n");
return 1;
}
break;
case OPT_VERT_NORMAL_FORMAT:
normalFormat = static_cast<DXGI_FORMAT>(LookupByName(pValue, g_vertexNormalFormats));
if (!normalFormat)
{
wprintf(L"Invalid value specified with -fn (%ls)\n", pValue);
wprintf(L"\n");
PrintUsage();
return 1;
}
break;
case OPT_VERT_UV_FORMAT:
uvFormat = static_cast<DXGI_FORMAT>(LookupByName(pValue, g_vertexUVFormats));
if (!uvFormat)
{
wprintf(L"Invalid value specified with -fuv (%ls)\n", pValue);
wprintf(L"\n");
PrintUsage();
return 1;
}
break;
case OPT_VERT_COLOR_FORMAT:
colorFormat = static_cast<DXGI_FORMAT>(LookupByName(pValue, g_vertexColorFormats));
if (!colorFormat)
{
wprintf(L"Invalid value specified with -fc (%ls)\n", pValue);
wprintf(L"\n");
PrintUsage();
return 1;
}
break;
case OPT_FILELIST:
{
std::filesystem::path path(pValue);
std::wifstream inFile(path.make_preferred().c_str());
if (!inFile)
{
wprintf(L"Error opening -flist file %ls\n", pValue);
return 1;
}
inFile.imbue(std::locale::classic());
ProcessFileList(inFile, conversion);
}
break;
}
}
else if (wcspbrk(pArg, L"?*") != nullptr)
{
const size_t count = conversion.size();
std::filesystem::path path(pArg);
SearchForFiles(path.make_preferred(), conversion, (dwOptions& (1 << OPT_RECURSIVE)) != 0);
if (conversion.size() <= count)
{