Main.cpp 4.9 KB

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