Main.cpp 5.4 KB

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