Main.cpp 4.5 KB

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