Main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/importer/GltfImporter.h>
  6. using namespace anki;
  7. static const char* USAGE = R"(Usage: %s in_file out_dir [options]
  8. Options:
  9. -rpath <string> : Replace all absolute paths of assets with that path
  10. -texrpath <string> : Same as rpath but for textures
  11. -optimize-meshes <0|1> : Optimize meshes. Default is 1
  12. -j <thread_count> : Number of threads. Defaults to system's max
  13. -lod-count <1|2|3> : The number of geometry LODs to generate. Default: 1
  14. -lod-factor : The decimate factor for each LOD. Default 0.25
  15. )";
  16. class CmdLineArgs
  17. {
  18. public:
  19. HeapAllocator<U8> m_alloc{allocAligned, nullptr};
  20. StringAuto m_inputFname = {m_alloc};
  21. StringAuto m_outDir = {m_alloc};
  22. StringAuto m_rpath = {m_alloc};
  23. StringAuto m_texRpath = {m_alloc};
  24. Bool m_optimizeMeshes = true;
  25. U32 m_threadCount = MAX_U32;
  26. U32 m_lodCount = 1;
  27. F32 m_lodFactor = 0.25f;
  28. F32 m_lightIntensityScale = 1.0f;
  29. };
  30. static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
  31. {
  32. Bool rpathFound = false;
  33. Bool texrpathFound = false;
  34. // Parse config
  35. if(argc < 3)
  36. {
  37. return Error::USER_DATA;
  38. }
  39. info.m_inputFname.create(argv[1]);
  40. info.m_outDir.sprintf("%s/", argv[2]);
  41. for(I i = 3; i < argc; i++)
  42. {
  43. if(strcmp(argv[i], "-texrpath") == 0)
  44. {
  45. texrpathFound = true;
  46. ++i;
  47. if(i < argc)
  48. {
  49. if(std::strlen(argv[i]) > 0)
  50. {
  51. info.m_texRpath.sprintf("%s/", argv[i]);
  52. }
  53. else
  54. {
  55. info.m_texRpath.sprintf("");
  56. }
  57. }
  58. else
  59. {
  60. return Error::USER_DATA;
  61. }
  62. }
  63. else if(strcmp(argv[i], "-rpath") == 0)
  64. {
  65. rpathFound = true;
  66. ++i;
  67. if(i < argc)
  68. {
  69. if(std::strlen(argv[i]) > 0)
  70. {
  71. info.m_rpath.sprintf("%s/", argv[i]);
  72. }
  73. else
  74. {
  75. info.m_rpath.sprintf("");
  76. }
  77. }
  78. else
  79. {
  80. return Error::USER_DATA;
  81. }
  82. }
  83. else if(strcmp(argv[i], "-optimize-meshes") == 0)
  84. {
  85. ++i;
  86. if(i < argc)
  87. {
  88. I optimize = 1;
  89. ANKI_CHECK(CString(argv[i]).toNumber(optimize));
  90. info.m_optimizeMeshes = optimize != 0;
  91. }
  92. else
  93. {
  94. return Error::USER_DATA;
  95. }
  96. }
  97. else if(strcmp(argv[i], "-j") == 0)
  98. {
  99. ++i;
  100. if(i < argc)
  101. {
  102. U32 count = 0;
  103. ANKI_CHECK(CString(argv[i]).toNumber(count));
  104. info.m_threadCount = count;
  105. }
  106. else
  107. {
  108. return Error::USER_DATA;
  109. }
  110. }
  111. else if(strcmp(argv[i], "-lod-count") == 0)
  112. {
  113. ++i;
  114. if(i < argc)
  115. {
  116. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodCount));
  117. }
  118. else
  119. {
  120. return Error::USER_DATA;
  121. }
  122. }
  123. else if(strcmp(argv[i], "-lod-factor") == 0)
  124. {
  125. ++i;
  126. if(i < argc)
  127. {
  128. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodFactor));
  129. }
  130. else
  131. {
  132. return Error::USER_DATA;
  133. }
  134. }
  135. else if(strcmp(argv[i], "-light-scale") == 0)
  136. {
  137. ++i;
  138. if(i < argc)
  139. {
  140. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lightIntensityScale));
  141. }
  142. else
  143. {
  144. return Error::USER_DATA;
  145. }
  146. }
  147. else
  148. {
  149. return Error::USER_DATA;
  150. }
  151. }
  152. if(!rpathFound)
  153. {
  154. info.m_rpath = info.m_outDir;
  155. }
  156. if(!texrpathFound)
  157. {
  158. info.m_texRpath = info.m_outDir;
  159. }
  160. return Error::NONE;
  161. }
  162. int main(int argc, char** argv)
  163. {
  164. CmdLineArgs info;
  165. if(parseCommandLineArgs(argc, argv, info))
  166. {
  167. ANKI_GLTF_LOGE(USAGE, argv[0]);
  168. return 1;
  169. }
  170. HeapAllocator<U8> alloc(allocAligned, nullptr);
  171. StringAuto comment(alloc);
  172. for(I32 i = 0; i < argc; ++i)
  173. {
  174. if(i != 0)
  175. {
  176. comment.append(" ");
  177. }
  178. comment.append(argv[i]);
  179. }
  180. GltfImporter importer{alloc};
  181. if(importer.init(info.m_inputFname.toCString(), info.m_outDir.toCString(), info.m_rpath.toCString(),
  182. info.m_texRpath.toCString(), info.m_optimizeMeshes, info.m_lodFactor, info.m_lodCount,
  183. info.m_lightIntensityScale, info.m_threadCount, comment))
  184. {
  185. return 1;
  186. }
  187. if(importer.writeAll())
  188. {
  189. return 1;
  190. }
  191. return 0;
  192. }