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.
 
 
 
 
 
 

190 regels
6.3 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXImporter.cpp
  34. * @brief Implementation of the FBX importer.
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include <exception>
  39. #include <iterator>
  40. #include <boost/tuple/tuple.hpp>
  41. #include "FBXImporter.h"
  42. #include "FBXTokenizer.h"
  43. #include "FBXParser.h"
  44. #include "FBXUtil.h"
  45. #include "FBXDocument.h"
  46. #include "FBXConverter.h"
  47. #include "StreamReader.h"
  48. #include "MemoryIOWrapper.h"
  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. // Destructor, private as well
  75. FBXImporter::~FBXImporter()
  76. {
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Returns whether the class can handle the format of the given file.
  80. bool FBXImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  81. {
  82. const std::string& extension = GetExtension(pFile);
  83. if (extension == "fbx") {
  84. return true;
  85. }
  86. else if ((!extension.length() || checkSig) && pIOHandler) {
  87. // at least ascii FBX files usually have a 'FBX' somewhere in their head
  88. const char* tokens[] = {"FBX"};
  89. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  90. }
  91. return false;
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. // List all extensions handled by this loader
  95. const aiImporterDesc* FBXImporter::GetInfo () const
  96. {
  97. return &desc;
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // Setup configuration properties for the loader
  101. void FBXImporter::SetupProperties(const Importer* pImp)
  102. {
  103. settings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
  104. settings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
  105. settings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
  106. settings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
  107. settings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
  108. settings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
  109. settings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
  110. settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
  111. settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Imports the given file into the given scene structure.
  115. void FBXImporter::InternReadFile( const std::string& pFile,
  116. aiScene* pScene, IOSystem* pIOHandler)
  117. {
  118. boost::scoped_ptr<IOStream> stream(pIOHandler->Open(pFile,"rb"));
  119. if (!stream) {
  120. ThrowException("Could not open file for reading");
  121. }
  122. // read entire file into memory - no streaming for this, fbx
  123. // files can grow large, but the assimp output data structure
  124. // then becomes very large, too. Assimp doesn't support
  125. // streaming for its output data structures so the net win with
  126. // streaming input data would be very low.
  127. std::vector<char> contents;
  128. contents.resize(stream->FileSize());
  129. stream->Read(&*contents.begin(),contents.size(),1);
  130. const char* const begin = &*contents.begin();
  131. // broadphase tokenizing pass in which we identify the core
  132. // syntax elements of FBX (brackets, commas, key:value mappings)
  133. TokenList tokens;
  134. try {
  135. bool is_binary = false;
  136. if (!strncmp(begin,"Kaydara FBX Binary",18)) {
  137. is_binary = true;
  138. TokenizeBinary(tokens,begin,contents.size());
  139. }
  140. else {
  141. Tokenize(tokens,begin);
  142. }
  143. // use this information to construct a very rudimentary
  144. // parse-tree representing the FBX scope structure
  145. Parser parser(tokens, is_binary);
  146. // take the raw parse-tree and convert it to a FBX DOM
  147. Document doc(parser,settings);
  148. // convert the FBX DOM to aiScene
  149. ConvertToAssimpScene(pScene,doc);
  150. }
  151. catch(std::exception&) {
  152. std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
  153. throw;
  154. }
  155. }
  156. #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER