You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

515 rivejä
16 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Main.cpp
  35. * @brief main() function of assimp_cmd
  36. */
  37. #include "Main.h"
  38. const char* AICMD_MSG_ABOUT =
  39. "------------------------------------------------------ \n"
  40. "Open Asset Import Library (\"Assimp\", http://assimp.sourceforge.net) \n"
  41. " -- Commandline toolchain --\n"
  42. "------------------------------------------------------ \n\n"
  43. "Version %i.%i %s%s%s%s%s(GIT commit %x)\n\n";
  44. const char* AICMD_MSG_HELP =
  45. "assimp <verb> <parameters>\n\n"
  46. " verbs:\n"
  47. " \tinfo - Quick file stats\n"
  48. " \tlistext - List all known file extensions available for import\n"
  49. " \tknowext - Check whether a file extension is recognized by Assimp\n"
  50. #ifndef ASSIMP_BUILD_NO_EXPORT
  51. " \texport - Export a file to one of the supported output formats\n"
  52. " \tlistexport - List all supported export formats\n"
  53. " \texportinfo - Show basic information on a specific export format\n"
  54. #endif
  55. " \textract - Extract embedded texture images\n"
  56. " \tdump - Convert models to a binary or textual dump (ASSBIN/ASSXML)\n"
  57. " \tcmpdump - Compare dumps created using \'assimp dump <file> -s ...\'\n"
  58. " \tversion - Display Assimp version\n"
  59. "\n Use \'assimp <verb> --help\' for detailed help on a command.\n"
  60. ;
  61. /*extern*/ Assimp::Importer* globalImporter = NULL;
  62. #ifndef ASSIMP_BUILD_NO_EXPORT
  63. /*extern*/ Assimp::Exporter* globalExporter = NULL;
  64. #endif
  65. // ------------------------------------------------------------------------------
  66. // Application entry point
  67. int main (int argc, char* argv[])
  68. {
  69. if (argc <= 1) {
  70. printf("assimp: No command specified. Use \'assimp help\' for a detailed command list\n");
  71. return 0;
  72. }
  73. // assimp version
  74. // Display version information
  75. if (! strcmp(argv[1], "version")) {
  76. const unsigned int flags = aiGetCompileFlags();
  77. printf(AICMD_MSG_ABOUT,
  78. aiGetVersionMajor(),
  79. aiGetVersionMinor(),
  80. (flags & ASSIMP_CFLAGS_DEBUG ? "-debug " : ""),
  81. (flags & ASSIMP_CFLAGS_NOBOOST ? "-noboost " : ""),
  82. (flags & ASSIMP_CFLAGS_SHARED ? "-shared " : ""),
  83. (flags & ASSIMP_CFLAGS_SINGLETHREADED ? "-st " : ""),
  84. (flags & ASSIMP_CFLAGS_STLPORT ? "-stlport " : ""),
  85. aiGetVersionRevision());
  86. return 0;
  87. }
  88. // assimp help
  89. // Display some basic help (--help and -h work as well
  90. // because people could try them intuitively)
  91. if (!strcmp(argv[1], "help") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
  92. printf("%s",AICMD_MSG_HELP);
  93. return 0;
  94. }
  95. // assimp cmpdump
  96. // Compare two mini model dumps (regression suite)
  97. if (! strcmp(argv[1], "cmpdump")) {
  98. return Assimp_CompareDump (&argv[2],argc-2);
  99. }
  100. // construct global importer and exporter instances
  101. Assimp::Importer imp;
  102. imp.SetPropertyBool("GLOB_MEASURE_TIME",true);
  103. globalImporter = &imp;
  104. #ifndef ASSIMP_BUILD_NO_EXPORT
  105. //
  106. Assimp::Exporter exp;
  107. globalExporter = &exp;
  108. #endif
  109. // assimp listext
  110. // List all file extensions supported by Assimp
  111. if (! strcmp(argv[1], "listext")) {
  112. aiString s;
  113. imp.GetExtensionList(s);
  114. printf("%s\n",s.data);
  115. return 0;
  116. }
  117. #ifndef ASSIMP_BUILD_NO_EXPORT
  118. // assimp listexport
  119. // List all export file formats supported by Assimp (not the file extensions, just the format identifiers!)
  120. if (! strcmp(argv[1], "listexport")) {
  121. aiString s;
  122. for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) {
  123. const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i);
  124. s.Append( e->id );
  125. if (i!=end-1) {
  126. s.Append("\n");
  127. }
  128. }
  129. printf("%s\n",s.data);
  130. return 0;
  131. }
  132. // assimp exportinfo
  133. // stat an export format
  134. if (! strcmp(argv[1], "exportinfo")) {
  135. aiString s;
  136. if (argc<3) {
  137. printf("Expected file format id\n");
  138. return -11;
  139. }
  140. for(size_t i = 0, end = exp.GetExportFormatCount(); i < end; ++i) {
  141. const aiExportFormatDesc* const e = exp.GetExportFormatDescription(i);
  142. if (!strcmp(e->id,argv[2])) {
  143. printf("%s\n%s\n%s\n",e->id,e->fileExtension,e->description);
  144. return 0;
  145. }
  146. }
  147. printf("Unknown file format id: \'%s\'\n",argv[2]);
  148. return -12;
  149. }
  150. // assimp export
  151. // Export a model to a file
  152. if (! strcmp(argv[1], "export")) {
  153. return Assimp_Export (&argv[2],argc-2);
  154. }
  155. #endif
  156. // assimp knowext
  157. // Check whether a particular file extension is known by us, return 0 on success
  158. if (! strcmp(argv[1], "knowext")) {
  159. if (argc<3) {
  160. printf("Expected file extension");
  161. return -10;
  162. }
  163. const bool b = imp.IsExtensionSupported(argv[2]);
  164. printf("File extension \'%s\' is %sknown\n",argv[2],(b?"":"not "));
  165. return b?0:-1;
  166. }
  167. // assimp info
  168. // Print basic model statistics
  169. if (! strcmp(argv[1], "info")) {
  170. return Assimp_Info ((const char**)&argv[2],argc-2);
  171. }
  172. // assimp dump
  173. // Dump a model to a file
  174. if (! strcmp(argv[1], "dump")) {
  175. return Assimp_Dump (&argv[2],argc-2);
  176. }
  177. // assimp extract
  178. // Extract an embedded texture from a file
  179. if (! strcmp(argv[1], "extract")) {
  180. return Assimp_Extract (&argv[2],argc-2);
  181. }
  182. // assimp testbatchload
  183. // Used by /test/other/streamload.py to load a list of files
  184. // using the same importer instance to check for incompatible
  185. // importers.
  186. if (! strcmp(argv[1], "testbatchload")) {
  187. return Assimp_TestBatchLoad (&argv[2],argc-2);
  188. }
  189. printf("Unrecognized command. Use \'assimp help\' for a detailed command list\n");
  190. return 1;
  191. }
  192. // ------------------------------------------------------------------------------
  193. void SetLogStreams(const ImportData& imp)
  194. {
  195. printf("\nAttaching log stream ... OK\n");
  196. unsigned int flags = 0;
  197. if (imp.logFile.length()) {
  198. flags |= aiDefaultLogStream_FILE;
  199. }
  200. if (imp.showLog) {
  201. flags |= aiDefaultLogStream_STDERR;
  202. }
  203. DefaultLogger::create(imp.logFile.c_str(),imp.verbose ? Logger::VERBOSE : Logger::NORMAL,flags);
  204. }
  205. // ------------------------------------------------------------------------------
  206. void FreeLogStreams()
  207. {
  208. DefaultLogger::kill();
  209. }
  210. // ------------------------------------------------------------------------------
  211. void PrintHorBar()
  212. {
  213. printf("-----------------------------------------------------------------\n");
  214. }
  215. // ------------------------------------------------------------------------------
  216. // Import a specific file
  217. const aiScene* ImportModel(
  218. const ImportData& imp,
  219. const std::string& path)
  220. {
  221. // Attach log streams
  222. if (imp.log) {
  223. SetLogStreams(imp);
  224. }
  225. printf("Launching asset import ... OK\n");
  226. // Now validate this flag combination
  227. if(!globalImporter->ValidateFlags(imp.ppFlags)) {
  228. printf("ERROR: Unsupported post-processing flags \n");
  229. return NULL;
  230. }
  231. printf("Validating postprocessing flags ... OK\n");
  232. if (imp.showLog) {
  233. PrintHorBar();
  234. }
  235. // do the actual import, measure time
  236. const clock_t first = clock();
  237. const aiScene* scene = globalImporter->ReadFile(path,imp.ppFlags);
  238. if (imp.showLog) {
  239. PrintHorBar();
  240. }
  241. if (!scene) {
  242. printf("ERROR: Failed to load file\n");
  243. return NULL;
  244. }
  245. const clock_t second = ::clock();
  246. const double seconds = static_cast<double>(second-first) / CLOCKS_PER_SEC;
  247. printf("Importing file ... OK \n import took approx. %.5f seconds\n"
  248. "\n",seconds);
  249. if (imp.log) {
  250. FreeLogStreams();
  251. }
  252. return scene;
  253. }
  254. #ifndef ASSIMP_BUILD_NO_EXPORT
  255. // ------------------------------------------------------------------------------
  256. bool ExportModel(const aiScene* pOut,
  257. const ImportData& imp,
  258. const std::string& path,
  259. const char* pID)
  260. {
  261. // Attach log streams
  262. if (imp.log) {
  263. SetLogStreams(imp);
  264. }
  265. printf("Launching asset export ... OK\n");
  266. if (imp.showLog) {
  267. PrintHorBar();
  268. }
  269. // do the actual export, measure time
  270. const clock_t first = clock();
  271. const aiReturn res = globalExporter->Export(pOut,pID,path);
  272. if (imp.showLog) {
  273. PrintHorBar();
  274. }
  275. if (res != AI_SUCCESS) {
  276. printf("ERROR: Failed to write file\n");
  277. return false;
  278. }
  279. const clock_t second = ::clock();
  280. const double seconds = static_cast<double>(second-first) / CLOCKS_PER_SEC;
  281. printf("Exporting file ... OK \n export took approx. %.5f seconds\n"
  282. "\n",seconds);
  283. if (imp.log) {
  284. FreeLogStreams();
  285. }
  286. return true;
  287. }
  288. #endif
  289. // ------------------------------------------------------------------------------
  290. // Process standard arguments
  291. int ProcessStandardArguments(
  292. ImportData& fill,
  293. const char* const * params,
  294. unsigned int num)
  295. {
  296. // -ptv --pretransform-vertices
  297. // -gsn --gen-smooth-normals
  298. // -gn --gen-normals
  299. // -cts --calc-tangent-space
  300. // -jiv --join-identical-vertices
  301. // -rrm --remove-redundant-materials
  302. // -fd --find-degenerates
  303. // -slm --split-large-meshes
  304. // -lbw --limit-bone-weights
  305. // -vds --validate-data-structure
  306. // -icl --improve-cache-locality
  307. // -sbpt --sort-by-ptype
  308. // -lh --convert-to-lh
  309. // -fuv --flip-uv
  310. // -fwo --flip-winding-order
  311. // -tuv --transform-uv-coords
  312. // -guv --gen-uvcoords
  313. // -fid --find-invalid-data
  314. // -fixn --fix normals
  315. // -tri --triangulate
  316. // -fi --find-instances
  317. // -fi --find-instances
  318. // -og --optimize-graph
  319. // -om --optimize-meshes
  320. // -db --debone
  321. // -sbc --split-by-bone-count
  322. //
  323. // -c<file> --config-file=<file>
  324. for (unsigned int i = 0; i < num;++i)
  325. {
  326. if (! strcmp(params[i], "-ptv") || ! strcmp(params[i], "--pretransform-vertices")) {
  327. fill.ppFlags |= aiProcess_PreTransformVertices;
  328. }
  329. else if (! strcmp(params[i], "-gsn") || ! strcmp(params[i], "--gen-smooth-normals")) {
  330. fill.ppFlags |= aiProcess_GenSmoothNormals;
  331. }
  332. else if (! strcmp(params[i], "-gn") || ! strcmp(params[i], "--gen-normals")) {
  333. fill.ppFlags |= aiProcess_GenNormals;
  334. }
  335. else if (! strcmp(params[i], "-jiv") || ! strcmp(params[i], "--join-identical-vertices")) {
  336. fill.ppFlags |= aiProcess_JoinIdenticalVertices;
  337. }
  338. else if (! strcmp(params[i], "-rrm") || ! strcmp(params[i], "--remove-redundant-materials")) {
  339. fill.ppFlags |= aiProcess_RemoveRedundantMaterials;
  340. }
  341. else if (! strcmp(params[i], "-fd") || ! strcmp(params[i], "--find-degenerates")) {
  342. fill.ppFlags |= aiProcess_FindDegenerates;
  343. }
  344. else if (! strcmp(params[i], "-slm") || ! strcmp(params[i], "--split-large-meshes")) {
  345. fill.ppFlags |= aiProcess_SplitLargeMeshes;
  346. }
  347. else if (! strcmp(params[i], "-lbw") || ! strcmp(params[i], "--limit-bone-weights")) {
  348. fill.ppFlags |= aiProcess_LimitBoneWeights;
  349. }
  350. else if (! strcmp(params[i], "-vds") || ! strcmp(params[i], "--validate-data-structure")) {
  351. fill.ppFlags |= aiProcess_ValidateDataStructure;
  352. }
  353. else if (! strcmp(params[i], "-icl") || ! strcmp(params[i], "--improve-cache-locality")) {
  354. fill.ppFlags |= aiProcess_ImproveCacheLocality;
  355. }
  356. else if (! strcmp(params[i], "-sbpt") || ! strcmp(params[i], "--sort-by-ptype")) {
  357. fill.ppFlags |= aiProcess_SortByPType;
  358. }
  359. else if (! strcmp(params[i], "-lh") || ! strcmp(params[i], "--left-handed")) {
  360. fill.ppFlags |= aiProcess_ConvertToLeftHanded;
  361. }
  362. else if (! strcmp(params[i], "-fuv") || ! strcmp(params[i], "--flip-uv")) {
  363. fill.ppFlags |= aiProcess_FlipUVs;
  364. }
  365. else if (! strcmp(params[i], "-fwo") || ! strcmp(params[i], "--flip-winding-order")) {
  366. fill.ppFlags |= aiProcess_FlipWindingOrder;
  367. }
  368. else if (! strcmp(params[i], "-tuv") || ! strcmp(params[i], "--transform-uv-coords")) {
  369. fill.ppFlags |= aiProcess_TransformUVCoords;
  370. }
  371. else if (! strcmp(params[i], "-guv") || ! strcmp(params[i], "--gen-uvcoords")) {
  372. fill.ppFlags |= aiProcess_GenUVCoords;
  373. }
  374. else if (! strcmp(params[i], "-fid") || ! strcmp(params[i], "--find-invalid-data")) {
  375. fill.ppFlags |= aiProcess_FindInvalidData;
  376. }
  377. else if (! strcmp(params[i], "-fixn") || ! strcmp(params[i], "--fix-normals")) {
  378. fill.ppFlags |= aiProcess_FixInfacingNormals;
  379. }
  380. else if (! strcmp(params[i], "-tri") || ! strcmp(params[i], "--triangulate")) {
  381. fill.ppFlags |= aiProcess_Triangulate;
  382. }
  383. else if (! strcmp(params[i], "-cts") || ! strcmp(params[i], "--calc-tangent-space")) {
  384. fill.ppFlags |= aiProcess_CalcTangentSpace;
  385. }
  386. else if (! strcmp(params[i], "-fi") || ! strcmp(params[i], "--find-instances")) {
  387. fill.ppFlags |= aiProcess_FindInstances;
  388. }
  389. else if (! strcmp(params[i], "-og") || ! strcmp(params[i], "--optimize-graph")) {
  390. fill.ppFlags |= aiProcess_OptimizeGraph;
  391. }
  392. else if (! strcmp(params[i], "-om") || ! strcmp(params[i], "--optimize-meshes")) {
  393. fill.ppFlags |= aiProcess_OptimizeMeshes;
  394. }
  395. else if (! strcmp(params[i], "-db") || ! strcmp(params[i], "--debone")) {
  396. fill.ppFlags |= aiProcess_Debone;
  397. }
  398. else if (! strcmp(params[i], "-sbc") || ! strcmp(params[i], "--split-by-bone-count")) {
  399. fill.ppFlags |= aiProcess_SplitByBoneCount;
  400. }
  401. else if (! strncmp(params[i], "-c",2) || ! strncmp(params[i], "--config=",9)) {
  402. const unsigned int ofs = (params[i][1] == '-' ? 9 : 2);
  403. // use default configurations
  404. if (! strncmp(params[i]+ofs,"full",4)) {
  405. fill.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality;
  406. }
  407. else if (! strncmp(params[i]+ofs,"default",7)) {
  408. fill.ppFlags |= aiProcessPreset_TargetRealtime_Quality;
  409. }
  410. else if (! strncmp(params[i]+ofs,"fast",4)) {
  411. fill.ppFlags |= aiProcessPreset_TargetRealtime_Fast;
  412. }
  413. }
  414. else if (! strcmp(params[i], "-l") || ! strcmp(params[i], "--show-log")) {
  415. fill.showLog = true;
  416. }
  417. else if (! strcmp(params[i], "-v") || ! strcmp(params[i], "--verbose")) {
  418. fill.verbose = true;
  419. }
  420. else if (! strncmp(params[i], "--log-out=",10) || ! strncmp(params[i], "-lo",3)) {
  421. fill.logFile = std::string(params[i]+(params[i][1] == '-' ? 10 : 3));
  422. if (!fill.logFile.length()) {
  423. fill.logFile = "assimp-log.txt";
  424. }
  425. }
  426. }
  427. if (fill.logFile.length() || fill.showLog || fill.verbose) {
  428. fill.log = true;
  429. }
  430. return 0;
  431. }
  432. // ------------------------------------------------------------------------------
  433. int Assimp_TestBatchLoad (
  434. const char* const* params,
  435. unsigned int num)
  436. {
  437. for(unsigned int i = 0; i < num; ++i) {
  438. globalImporter->ReadFile(params[i],aiProcessPreset_TargetRealtime_MaxQuality);
  439. // we're totally silent. scene destructs automatically.
  440. }
  441. return 0;
  442. }