Main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2009-2023, 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. -optimize-animations <0|1> : Optimize animations. Default is 1
  13. -j <thread_count> : Number of threads. Defaults to system's max
  14. -lod-count <1|2|3> : The number of geometry LODs to generate. Default is 1
  15. -lod-factor <float> : The decimate factor for each LOD. Default 0.25
  16. -light-scale <float> : Multiply the light intensity with this number. Default is 1.0
  17. -import-testures <0|1> : Import textures. Default is 0
  18. -v : Enable verbose log
  19. )";
  20. class CmdLineArgs
  21. {
  22. public:
  23. HeapMemoryPool m_pool = {allocAligned, nullptr};
  24. StringRaii m_inputFname = {&m_pool};
  25. StringRaii m_outDir = {&m_pool};
  26. StringRaii m_rpath = {&m_pool};
  27. StringRaii m_texRpath = {&m_pool};
  28. Bool m_optimizeMeshes = true;
  29. Bool m_optimizeAnimations = true;
  30. Bool m_importTextures = false;
  31. U32 m_threadCount = kMaxU32;
  32. U32 m_lodCount = 1;
  33. F32 m_lodFactor = 0.25f;
  34. F32 m_lightIntensityScale = 1.0f;
  35. };
  36. static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
  37. {
  38. Bool rpathFound = false;
  39. Bool texrpathFound = false;
  40. // Parse config
  41. if(argc < 3)
  42. {
  43. return Error::kUserData;
  44. }
  45. info.m_inputFname.create(argv[1]);
  46. info.m_outDir.sprintf("%s/", argv[2]);
  47. for(I i = 3; i < argc; i++)
  48. {
  49. if(strcmp(argv[i], "-texrpath") == 0)
  50. {
  51. texrpathFound = true;
  52. ++i;
  53. if(i < argc)
  54. {
  55. if(std::strlen(argv[i]) > 0)
  56. {
  57. info.m_texRpath.sprintf("%s/", argv[i]);
  58. }
  59. else
  60. {
  61. info.m_texRpath.create("");
  62. }
  63. }
  64. else
  65. {
  66. return Error::kUserData;
  67. }
  68. }
  69. else if(strcmp(argv[i], "-v") == 0)
  70. {
  71. Logger::getSingleton().enableVerbosity(true);
  72. }
  73. else if(strcmp(argv[i], "-rpath") == 0)
  74. {
  75. rpathFound = true;
  76. ++i;
  77. if(i < argc)
  78. {
  79. if(std::strlen(argv[i]) > 0)
  80. {
  81. info.m_rpath.sprintf("%s/", argv[i]);
  82. }
  83. else
  84. {
  85. info.m_rpath.create("");
  86. }
  87. }
  88. else
  89. {
  90. return Error::kUserData;
  91. }
  92. }
  93. else if(strcmp(argv[i], "-optimize-meshes") == 0)
  94. {
  95. ++i;
  96. if(i < argc)
  97. {
  98. I optimize = 1;
  99. ANKI_CHECK(CString(argv[i]).toNumber(optimize));
  100. info.m_optimizeMeshes = optimize != 0;
  101. }
  102. else
  103. {
  104. return Error::kUserData;
  105. }
  106. }
  107. else if(strcmp(argv[i], "-j") == 0)
  108. {
  109. ++i;
  110. if(i < argc)
  111. {
  112. U32 count = 0;
  113. ANKI_CHECK(CString(argv[i]).toNumber(count));
  114. info.m_threadCount = count;
  115. }
  116. else
  117. {
  118. return Error::kUserData;
  119. }
  120. }
  121. else if(strcmp(argv[i], "-lod-count") == 0)
  122. {
  123. ++i;
  124. if(i < argc)
  125. {
  126. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodCount));
  127. }
  128. else
  129. {
  130. return Error::kUserData;
  131. }
  132. }
  133. else if(strcmp(argv[i], "-lod-factor") == 0)
  134. {
  135. ++i;
  136. if(i < argc)
  137. {
  138. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodFactor));
  139. }
  140. else
  141. {
  142. return Error::kUserData;
  143. }
  144. }
  145. else if(strcmp(argv[i], "-light-scale") == 0)
  146. {
  147. ++i;
  148. if(i < argc)
  149. {
  150. ANKI_CHECK(CString(argv[i]).toNumber(info.m_lightIntensityScale));
  151. }
  152. else
  153. {
  154. return Error::kUserData;
  155. }
  156. }
  157. else if(strcmp(argv[i], "-optimize-animations") == 0)
  158. {
  159. ++i;
  160. if(i < argc)
  161. {
  162. I optimize = 1;
  163. ANKI_CHECK(CString(argv[i]).toNumber(optimize));
  164. info.m_optimizeAnimations = optimize != 0;
  165. }
  166. else
  167. {
  168. return Error::kUserData;
  169. }
  170. }
  171. else if(strcmp(argv[i], "-import-textures") == 0)
  172. {
  173. ++i;
  174. if(i < argc)
  175. {
  176. I val = 1;
  177. ANKI_CHECK(CString(argv[i]).toNumber(val));
  178. info.m_importTextures = val != 0;
  179. }
  180. else
  181. {
  182. return Error::kUserData;
  183. }
  184. }
  185. else
  186. {
  187. return Error::kUserData;
  188. }
  189. }
  190. if(!rpathFound)
  191. {
  192. info.m_rpath = info.m_outDir;
  193. }
  194. if(!texrpathFound)
  195. {
  196. info.m_texRpath = info.m_rpath;
  197. }
  198. return Error::kNone;
  199. }
  200. ANKI_MAIN_FUNCTION(myMain)
  201. int myMain(int argc, char** argv)
  202. {
  203. CmdLineArgs cmdArgs;
  204. if(parseCommandLineArgs(argc, argv, cmdArgs))
  205. {
  206. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  207. return 1;
  208. }
  209. HeapMemoryPool pool(allocAligned, nullptr);
  210. StringRaii comment(&pool);
  211. for(I32 i = 0; i < argc; ++i)
  212. {
  213. if(i != 0)
  214. {
  215. comment.append(" ");
  216. }
  217. if(CString(argv[i]).getLength())
  218. {
  219. comment.append(argv[i]);
  220. }
  221. else
  222. {
  223. comment.append("\"\"");
  224. }
  225. }
  226. GltfImporterInitInfo initInfo;
  227. initInfo.m_inputFilename = cmdArgs.m_inputFname;
  228. initInfo.m_outDirectory = cmdArgs.m_outDir;
  229. initInfo.m_rpath = cmdArgs.m_rpath;
  230. initInfo.m_texrpath = cmdArgs.m_texRpath;
  231. initInfo.m_optimizeMeshes = cmdArgs.m_optimizeMeshes;
  232. initInfo.m_optimizeAnimations = cmdArgs.m_optimizeAnimations;
  233. initInfo.m_lodFactor = cmdArgs.m_lodFactor;
  234. initInfo.m_lodCount = cmdArgs.m_lodCount;
  235. initInfo.m_lightIntensityScale = cmdArgs.m_lightIntensityScale;
  236. initInfo.m_threadCount = cmdArgs.m_threadCount;
  237. initInfo.m_comment = comment;
  238. initInfo.m_importTextures = cmdArgs.m_importTextures;
  239. GltfImporter importer(&pool);
  240. if(importer.init(initInfo))
  241. {
  242. return 1;
  243. }
  244. if(importer.writeAll())
  245. {
  246. return 1;
  247. }
  248. return 0;
  249. }