Main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (C) 2009-2021, 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 <float> : The decimate factor for each LOD. Default 0.25
  15. -light-scale <float> : Multiply the light intensity with this number. Default 1.0
  16. )";
  17. class CmdLineArgs
  18. {
  19. public:
  20. HeapAllocator<U8> m_alloc = {allocAligned, nullptr};
  21. StringAuto m_inputFname = {m_alloc};
  22. StringAuto m_outDir = {m_alloc};
  23. StringAuto m_rpath = {m_alloc};
  24. StringAuto m_texRpath = {m_alloc};
  25. Bool m_optimizeMeshes = true;
  26. U32 m_threadCount = MAX_U32;
  27. U32 m_lodCount = 1;
  28. F32 m_lodFactor = 0.25f;
  29. F32 m_lightIntensityScale = 1.0f;
  30. };
  31. static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
  32. {
  33. Bool rpathFound = false;
  34. Bool texrpathFound = false;
  35. // Parse config
  36. if(argc < 3)
  37. {
  38. return Error::USER_DATA;
  39. }
  40. info.m_inputFname.create(argv[1]);
  41. info.m_outDir.sprintf("%s/", argv[2]);
  42. for(I i = 3; i < argc; i++)
  43. {
  44. if(strcmp(argv[i], "-texrpath") == 0)
  45. {
  46. texrpathFound = true;
  47. ++i;
  48. if(i < argc)
  49. {
  50. if(std::strlen(argv[i]) > 0)
  51. {
  52. info.m_texRpath.sprintf("%s/", argv[i]);
  53. }
  54. else
  55. {
  56. info.m_texRpath.sprintf("");
  57. }
  58. }
  59. else
  60. {
  61. return Error::USER_DATA;
  62. }
  63. }
  64. else if(strcmp(argv[i], "-rpath") == 0)
  65. {
  66. rpathFound = true;
  67. ++i;
  68. if(i < argc)
  69. {
  70. if(std::strlen(argv[i]) > 0)
  71. {
  72. info.m_rpath.sprintf("%s/", argv[i]);
  73. }
  74. else
  75. {
  76. info.m_rpath.sprintf("");
  77. }
  78. }
  79. else
  80. {
  81. return Error::USER_DATA;
  82. }
  83. }
  84. else if(strcmp(argv[i], "-optimize-meshes") == 0)
  85. {
  86. ++i;
  87. if(i < argc)
  88. {
  89. I optimize = 1;
  90. ANKI_CHECK(CString(argv[i]).toNumber(optimize));
  91. info.m_optimizeMeshes = optimize != 0;
  92. }
  93. else
  94. {
  95. return Error::USER_DATA;
  96. }
  97. }
  98. else if(strcmp(argv[i], "-j") == 0)
  99. {
  100. ++i;
  101. if(i < argc)
  102. {
  103. U32 count = 0;
  104. ANKI_CHECK(CString(argv[i]).toNumber(count));
  105. info.m_threadCount = count;
  106. }
  107. else
  108. {
  109. return Error::USER_DATA;
  110. }
  111. }
  112. else if(strcmp(argv[i], "-lod-count") == 0)
  113. {
  114. ++i;
  115. if(i < argc)
  116. {
  117. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodCount));
  118. }
  119. else
  120. {
  121. return Error::USER_DATA;
  122. }
  123. }
  124. else if(strcmp(argv[i], "-lod-factor") == 0)
  125. {
  126. ++i;
  127. if(i < argc)
  128. {
  129. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodFactor));
  130. }
  131. else
  132. {
  133. return Error::USER_DATA;
  134. }
  135. }
  136. else if(strcmp(argv[i], "-light-scale") == 0)
  137. {
  138. ++i;
  139. if(i < argc)
  140. {
  141. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lightIntensityScale));
  142. }
  143. else
  144. {
  145. return Error::USER_DATA;
  146. }
  147. }
  148. else
  149. {
  150. return Error::USER_DATA;
  151. }
  152. }
  153. if(!rpathFound)
  154. {
  155. info.m_rpath = info.m_outDir;
  156. }
  157. if(!texrpathFound)
  158. {
  159. info.m_texRpath = info.m_outDir;
  160. }
  161. return Error::NONE;
  162. }
  163. int main(int argc, char** argv)
  164. {
  165. CmdLineArgs cmdArgs;
  166. if(parseCommandLineArgs(argc, argv, cmdArgs))
  167. {
  168. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  169. return 1;
  170. }
  171. HeapAllocator<U8> alloc(allocAligned, nullptr);
  172. StringAuto comment(alloc);
  173. for(I32 i = 0; i < argc; ++i)
  174. {
  175. if(i != 0)
  176. {
  177. comment.append(" ");
  178. }
  179. comment.append(argv[i]);
  180. }
  181. GltfImporterInitInfo initInfo;
  182. initInfo.m_inputFilename = cmdArgs.m_inputFname;
  183. initInfo.m_outDirectory = cmdArgs.m_outDir;
  184. initInfo.m_rpath = cmdArgs.m_rpath;
  185. initInfo.m_texrpath = cmdArgs.m_texRpath;
  186. initInfo.m_optimizeMeshes = cmdArgs.m_optimizeMeshes;
  187. initInfo.m_lodFactor = cmdArgs.m_lodFactor;
  188. initInfo.m_lodCount = cmdArgs.m_lodCount;
  189. initInfo.m_lightIntensityScale = cmdArgs.m_lightIntensityScale;
  190. initInfo.m_threadCount = cmdArgs.m_threadCount;
  191. initInfo.m_comment = comment;
  192. GltfImporter importer(alloc);
  193. if(importer.init(initInfo))
  194. {
  195. return 1;
  196. }
  197. if(importer.writeAll())
  198. {
  199. return 1;
  200. }
  201. return 0;
  202. }