Main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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) \n"
  41. "Command-line tools \n"
  42. "------------------------------------------------------ \n\n"
  43. "Major version: %i\n"
  44. "Minor version: %i\n"
  45. "SVN revision : %i\n"
  46. "Build flags : %s %s %s %s %s\n\n";
  47. const char* AICMD_MSG_HELP =
  48. "todo help";
  49. /*extern*/ Assimp::Importer* globalImporter = NULL;
  50. // ------------------------------------------------------------------------------
  51. // Application entry point
  52. int main (int argc, char* argv[])
  53. {
  54. if (argc <= 1) {
  55. printf("assimp: No command specified. Use \'assimp help\' for a detailed command list\n");
  56. return 0;
  57. }
  58. // assimp version
  59. // Display a version string
  60. if (! ::strcmp(argv[1], "version")) {
  61. const unsigned int flags = aiGetCompileFlags();
  62. printf(AICMD_MSG_ABOUT,
  63. aiGetVersionMajor(),
  64. aiGetVersionMinor(),
  65. aiGetVersionRevision(),
  66. (flags & ASSIMP_CFLAGS_DEBUG ? "-debug" : ""),
  67. (flags & ASSIMP_CFLAGS_NOBOOST ? "-noboost" : ""),
  68. (flags & ASSIMP_CFLAGS_SHARED ? "-shared" : ""),
  69. (flags & ASSIMP_CFLAGS_SINGLETHREADED ? "-st" : ""),
  70. (flags & ASSIMP_CFLAGS_STLPORT ? "-stlport" : ""));
  71. return 0;
  72. }
  73. // assimp help
  74. // Display some basic help
  75. if (! ::strcmp(argv[1], "help")) {
  76. printf(AICMD_MSG_HELP);
  77. return 0;
  78. }
  79. // construct a global Assimp::Importer instance
  80. Assimp::Importer imp;
  81. globalImporter = &imp;
  82. // assimp dump
  83. // Dump a model to a file
  84. if (! ::strcmp(argv[1], "dump")) {
  85. return Assimp_Dump ((const char**)&argv[2],argc-2);
  86. }
  87. // assimp extract
  88. // Extract an embedded texture from a file
  89. if (! ::strcmp(argv[1], "extract")) {
  90. return Assimp_Extract ((const char**)&argv[2],argc-2);
  91. }
  92. ::printf("Unrecognized command. Use \'assimp help\' for a detailed command list\n");
  93. return 1;
  94. }
  95. // ------------------------------------------------------------------------------
  96. // Import a specific file
  97. const aiScene* ImportModel(const ImportData& imp, const std::string& path)
  98. {
  99. // Attach log streams
  100. if (imp.log) {
  101. ::printf("\nAttaching log stream ... OK\n");
  102. unsigned int flags = 0;
  103. if (imp.logFile.length())
  104. flags |= DLS_FILE;
  105. if (imp.showLog)
  106. flags |= DLS_CERR;
  107. DefaultLogger::create(imp.logFile.c_str(),imp.verbose ? Logger::VERBOSE : Logger::NORMAL,flags);
  108. }
  109. ::printf("Launching model import ... OK\n");
  110. // Now validate this flag combination
  111. if(!globalImporter->ValidateFlags(imp.ppFlags)) {
  112. ::printf("ERROR: Unsupported post-processing flags \n");
  113. return NULL;
  114. }
  115. ::printf("Validating postprocessing flags ... OK\n");
  116. if (imp.showLog)
  117. ::printf("-----------------------------------------------------------------\n");
  118. // do the actual import, measure time
  119. const clock_t first = ::clock();
  120. const aiScene* scene = globalImporter->ReadFile(path,imp.ppFlags);
  121. if (imp.showLog)
  122. ::printf("-----------------------------------------------------------------\n");
  123. if (!scene) {
  124. printf("ERROR: Failed to load file\n");
  125. return NULL;
  126. }
  127. const clock_t second = ::clock();
  128. const float seconds = (float)(second-first) / CLOCKS_PER_SEC;
  129. ::printf("Importing file ... OK \n import took approx. %.5f seconds\n"
  130. "\n",seconds);
  131. if (imp.log) {
  132. DefaultLogger::kill();
  133. }
  134. return scene;
  135. }
  136. // ------------------------------------------------------------------------------
  137. // Process standard arguments
  138. int ProcessStandardArguments(ImportData& fill, const char** params,
  139. unsigned int num)
  140. {
  141. // -ptv --pretransform-vertices
  142. // -gsn --gen-smooth-normals
  143. // -gn --gen-normals
  144. // -cts --calc-tangent-space
  145. // -jiv --join-identical-vertices
  146. // -rrm --remove-redundant-materials
  147. // -fd --find-degenerates
  148. // -slm --split-large-meshes
  149. // -lbw --limit-bone-weights
  150. // -vds --validate-data-structure
  151. // -icl --improve-cache-locality
  152. // -sbpt --sort-by-ptype
  153. // -lh --convert-to-lh
  154. // -fuv --flip-uv
  155. // -fwo --flip-winding-order
  156. // -tuv --transform-uv-coords
  157. // -guv --gen-uvcoords
  158. // -fid --find-invalid-data
  159. // -fixn --fix normals
  160. // -tri --triangulate
  161. // -fi --find-instances
  162. // -fi --find-instances
  163. // -og --optimize-graph
  164. // -om --optimize-meshes
  165. //
  166. // -c<file> --config-file=<file>
  167. for (unsigned int i = 0; i < num;++i)
  168. {
  169. if (!params[i]) { // could happen if some args have already been processed
  170. continue;
  171. }
  172. bool has = true;
  173. if (! ::strcmp(params[i], "-ptv") || ! ::strcmp(params[i], "--pretransform-vertices")) {
  174. fill.ppFlags |= aiProcess_PreTransformVertices;
  175. }
  176. else if (! ::strcmp(params[i], "-gsn") || ! ::strcmp(params[i], "--gen-smooth-normals")) {
  177. fill.ppFlags |= aiProcess_GenSmoothNormals;
  178. }
  179. else if (! ::strcmp(params[i], "-gn") || ! ::strcmp(params[i], "--gen-normals")) {
  180. fill.ppFlags |= aiProcess_GenNormals;
  181. }
  182. else if (! ::strcmp(params[i], "-jiv") || ! ::strcmp(params[i], "--join-identical-vertices")) {
  183. fill.ppFlags |= aiProcess_JoinIdenticalVertices;
  184. }
  185. else if (! ::strcmp(params[i], "-rrm") || ! ::strcmp(params[i], "--remove-redundant-materials")) {
  186. fill.ppFlags |= aiProcess_RemoveRedundantMaterials;
  187. }
  188. else if (! ::strcmp(params[i], "-fd") || ! ::strcmp(params[i], "--find-degenerates")) {
  189. fill.ppFlags |= aiProcess_FindDegenerates;
  190. }
  191. else if (! ::strcmp(params[i], "-slm") || ! ::strcmp(params[i], "--split-large-meshes")) {
  192. fill.ppFlags |= aiProcess_SplitLargeMeshes;
  193. }
  194. else if (! ::strcmp(params[i], "-lbw") || ! ::strcmp(params[i], "--limit-bone-weights")) {
  195. fill.ppFlags |= aiProcess_LimitBoneWeights;
  196. }
  197. else if (! ::strcmp(params[i], "-vds") || ! ::strcmp(params[i], "--validate-data-structure")) {
  198. fill.ppFlags |= aiProcess_ValidateDataStructure;
  199. }
  200. else if (! ::strcmp(params[i], "-icl") || ! ::strcmp(params[i], "--improve-cache-locality")) {
  201. fill.ppFlags |= aiProcess_ImproveCacheLocality;
  202. }
  203. else if (! ::strcmp(params[i], "-sbpt") || ! ::strcmp(params[i], "--sort-by-ptype")) {
  204. fill.ppFlags |= aiProcess_SortByPType;
  205. }
  206. else if (! ::strcmp(params[i], "-lh") || ! ::strcmp(params[i], "--left-handed")) {
  207. fill.ppFlags |= aiProcess_ConvertToLeftHanded;
  208. }
  209. else if (! ::strcmp(params[i], "-fuv") || ! ::strcmp(params[i], "--flip-uv")) {
  210. fill.ppFlags |= aiProcess_ConvertToLeftHanded;
  211. }
  212. else if (! ::strcmp(params[i], "-fwo") || ! ::strcmp(params[i], "--flip-winding-order")) {
  213. fill.ppFlags |= aiProcess_ConvertToLeftHanded;
  214. }
  215. else if (! ::strcmp(params[i], "-tuv") || ! ::strcmp(params[i], "--transform-uv-coords")) {
  216. fill.ppFlags |= aiProcess_TransformUVCoords;
  217. }
  218. else if (! ::strcmp(params[i], "-guv") || ! ::strcmp(params[i], "--gen-uvcoords")) {
  219. fill.ppFlags |= aiProcess_GenUVCoords;
  220. }
  221. else if (! ::strcmp(params[i], "-fid") || ! ::strcmp(params[i], "--find-invalid-data")) {
  222. fill.ppFlags |= aiProcess_FindInvalidData;
  223. }
  224. else if (! ::strcmp(params[i], "-fixn") || ! ::strcmp(params[i], "--fix-normals")) {
  225. fill.ppFlags |= aiProcess_FixInfacingNormals;
  226. }
  227. else if (! ::strcmp(params[i], "-tri") || ! ::strcmp(params[i], "--triangulate")) {
  228. fill.ppFlags |= aiProcess_Triangulate;
  229. }
  230. else if (! ::strcmp(params[i], "-cts") || ! ::strcmp(params[i], "--calc-tangent-space")) {
  231. fill.ppFlags |= aiProcess_CalcTangentSpace;
  232. }
  233. else if (! ::strcmp(params[i], "-fi") || ! ::strcmp(params[i], "--find-instances")) {
  234. fill.ppFlags |= aiProcess_FindInstances;
  235. }
  236. else if (! ::strcmp(params[i], "-og") || ! ::strcmp(params[i], "--optimize-graph")) {
  237. fill.ppFlags |= aiProcess_OptimizeGraph;
  238. }
  239. else if (! ::strcmp(params[i], "-om") || ! ::strcmp(params[i], "--optimize-meshes")) {
  240. fill.ppFlags |= aiProcess_OptimizeMeshes;
  241. }
  242. #if 0
  243. else if (! ::strcmp(params[i], "-oa") || ! ::strcmp(params[i], "--optimize-anims")) {
  244. fill.ppFlags |= aiProcess_OptimizeAnims;
  245. }
  246. else if (! ::strcmp(params[i], "-gem") || ! ::strcmp(params[i], "--gen-entity-meshes")) {
  247. fill.ppFlags |= aiProcess_GenEntityMeshes;
  248. }
  249. else if (! ::strcmp(params[i], "-ftp") || ! ::strcmp(params[i], "--fix-texture-paths")) {
  250. fill.ppFlags |= aiProcess_FixTexturePaths;
  251. }
  252. #endif
  253. else if (! ::strncmp(params[i], "-c",2) || ! ::strncmp(params[i], "--config=",9)) {
  254. const unsigned int ofs = (params[i][1] == '-' ? 9 : 2);
  255. // use default configurations
  256. if (! ::strncmp(params[i]+ofs,"full",4))
  257. fill.ppFlags |= aiProcessPreset_TargetRealtime_MaxQuality;
  258. else if (! ::strncmp(params[i]+ofs,"default",7))
  259. fill.ppFlags |= aiProcessPreset_TargetRealtime_Quality;
  260. else if (! ::strncmp(params[i]+ofs,"fast",4))
  261. fill.ppFlags |= aiProcessPreset_TargetRealtime_Fast;
  262. }
  263. else if (! ::strcmp(params[i], "-l") || ! ::strcmp(params[i], "--show-log")) {
  264. fill.showLog = true;
  265. }
  266. else if (! ::strcmp(params[i], "-v") || ! ::strcmp(params[i], "--verbose")) {
  267. fill.verbose = true;
  268. }
  269. else if (! ::strncmp(params[i], "--log-out=",10) || ! ::strncmp(params[i], "-lo",3)) {
  270. fill.logFile = std::string(params[i]+(params[i][1] == '-' ? 10 : 3));
  271. if (!fill.logFile.length())
  272. fill.logFile = "assimp-log.txt";
  273. }
  274. else has = false;
  275. if (has) {
  276. params[i] = NULL;
  277. }
  278. }
  279. if (fill.logFile.length() || fill.showLog || fill.verbose)
  280. fill.log = true;
  281. return 0;
  282. }