FBXImporter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. r
  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 FBXImporter.cpp
  35. * @brief Implementation of the FBX importer.
  36. */
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXImporter.h"
  39. #include "FBXTokenizer.h"
  40. #include "FBXParser.h"
  41. #include "FBXUtil.h"
  42. #include "FBXDocument.h"
  43. #include "FBXConverter.h"
  44. #include "StreamReader.h"
  45. #include "MemoryIOWrapper.h"
  46. #include <assimp/Importer.hpp>
  47. #include <exception>
  48. #include <iterator>
  49. namespace Assimp {
  50. template<> const std::string LogFunctions<FBXImporter>::log_prefix = "FBX: ";
  51. }
  52. using namespace Assimp;
  53. using namespace Assimp::Formatter;
  54. using namespace Assimp::FBX;
  55. namespace {
  56. static const aiImporterDesc desc = {
  57. "Autodesk FBX Importer",
  58. "",
  59. "",
  60. "",
  61. aiImporterFlags_SupportTextFlavour,
  62. 0,
  63. 0,
  64. 0,
  65. 0,
  66. "fbx"
  67. };
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Constructor to be privately used by #Importer
  71. FBXImporter::FBXImporter()
  72. {
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Destructor, private as well
  76. FBXImporter::~FBXImporter()
  77. {
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Returns whether the class can handle the format of the given file.
  81. bool FBXImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  82. {
  83. const std::string& extension = GetExtension(pFile);
  84. if (extension == std::string( desc.mFileExtensions ) ) {
  85. return true;
  86. }
  87. else if ((!extension.length() || checkSig) && pIOHandler) {
  88. // at least ASCII-FBX files usually have a 'FBX' somewhere in their head
  89. const char* tokens[] = {"fbx"};
  90. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  91. }
  92. return false;
  93. }
  94. // ------------------------------------------------------------------------------------------------
  95. // List all extensions handled by this loader
  96. const aiImporterDesc* FBXImporter::GetInfo () const
  97. {
  98. return &desc;
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. // Setup configuration properties for the loader
  102. void FBXImporter::SetupProperties(const Importer* pImp)
  103. {
  104. settings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
  105. settings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
  106. settings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
  107. settings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
  108. settings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
  109. settings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
  110. settings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
  111. settings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
  112. settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
  113. settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Imports the given file into the given scene structure.
  117. void FBXImporter::InternReadFile( const std::string& pFile,
  118. aiScene* pScene, IOSystem* pIOHandler)
  119. {
  120. std::unique_ptr<IOStream> stream(pIOHandler->Open(pFile,"rb"));
  121. if (!stream) {
  122. ThrowException("Could not open file for reading");
  123. }
  124. // read entire file into memory - no streaming for this, fbx
  125. // files can grow large, but the assimp output data structure
  126. // then becomes very large, too. Assimp doesn't support
  127. // streaming for its output data structures so the net win with
  128. // streaming input data would be very low.
  129. std::vector<char> contents;
  130. contents.resize(stream->FileSize()+1);
  131. stream->Read( &*contents.begin(), 1, contents.size()-1 );
  132. contents[ contents.size() - 1 ] = 0;
  133. const char* const begin = &*contents.begin();
  134. // broadphase tokenizing pass in which we identify the core
  135. // syntax elements of FBX (brackets, commas, key:value mappings)
  136. TokenList tokens;
  137. try {
  138. bool is_binary = false;
  139. if (!strncmp(begin,"Kaydara FBX Binary",18)) {
  140. is_binary = true;
  141. TokenizeBinary(tokens,begin,static_cast<unsigned int>(contents.size()));
  142. }
  143. else {
  144. Tokenize(tokens,begin);
  145. }
  146. // use this information to construct a very rudimentary
  147. // parse-tree representing the FBX scope structure
  148. Parser parser(tokens, is_binary);
  149. // take the raw parse-tree and convert it to a FBX DOM
  150. Document doc(parser,settings);
  151. // convert the FBX DOM to aiScene
  152. ConvertToAssimpScene(pScene,doc);
  153. std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
  154. }
  155. catch(std::exception&) {
  156. std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
  157. throw;
  158. }
  159. }
  160. #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER