/src/assimp/code/AssetLib/FBX/FBXImporter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2022, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | r |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /** @file FBXImporter.cpp |
43 | | * @brief Implementation of the FBX importer. |
44 | | */ |
45 | | |
46 | | #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER |
47 | | |
48 | | #include "FBXImporter.h" |
49 | | |
50 | | #include "FBXConverter.h" |
51 | | #include "FBXDocument.h" |
52 | | #include "FBXParser.h" |
53 | | #include "FBXTokenizer.h" |
54 | | #include "FBXUtil.h" |
55 | | |
56 | | #include <assimp/MemoryIOWrapper.h> |
57 | | #include <assimp/StreamReader.h> |
58 | | #include <assimp/importerdesc.h> |
59 | | #include <assimp/Importer.hpp> |
60 | | |
61 | | namespace Assimp { |
62 | | |
63 | | template <> |
64 | 0 | const char *LogFunctions<FBXImporter>::Prefix() { |
65 | 0 | static auto prefix = "FBX: "; |
66 | 0 | return prefix; |
67 | 0 | } |
68 | | |
69 | | } // namespace Assimp |
70 | | |
71 | | using namespace Assimp; |
72 | | using namespace Assimp::Formatter; |
73 | | using namespace Assimp::FBX; |
74 | | |
75 | | namespace { |
76 | | |
77 | | static const aiImporterDesc desc = { |
78 | | "Autodesk FBX Importer", |
79 | | "", |
80 | | "", |
81 | | "", |
82 | | aiImporterFlags_SupportTextFlavour, |
83 | | 0, |
84 | | 0, |
85 | | 0, |
86 | | 0, |
87 | | "fbx" |
88 | | }; |
89 | | } |
90 | | |
91 | | // ------------------------------------------------------------------------------------------------ |
92 | | // Constructor to be privately used by #Importer |
93 | 26.0k | FBXImporter::FBXImporter() { |
94 | 26.0k | } |
95 | | |
96 | | // ------------------------------------------------------------------------------------------------ |
97 | | // Destructor, private as well |
98 | 25.8k | FBXImporter::~FBXImporter() { |
99 | 25.8k | } |
100 | | |
101 | | // ------------------------------------------------------------------------------------------------ |
102 | | // Returns whether the class can handle the format of the given file. |
103 | 7.78k | bool FBXImporter::CanRead(const std::string & pFile, IOSystem * pIOHandler, bool /*checkSig*/) const { |
104 | | // at least ASCII-FBX files usually have a 'FBX' somewhere in their head |
105 | 7.78k | static const char *tokens[] = { "fbx" }; |
106 | 7.78k | return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens)); |
107 | 7.78k | } |
108 | | |
109 | | // ------------------------------------------------------------------------------------------------ |
110 | | // List all extensions handled by this loader |
111 | 28.8k | const aiImporterDesc *FBXImporter::GetInfo() const { |
112 | 28.8k | return &desc; |
113 | 28.8k | } |
114 | | |
115 | | // ------------------------------------------------------------------------------------------------ |
116 | | // Setup configuration properties for the loader |
117 | 2.78k | void FBXImporter::SetupProperties(const Importer *pImp) { |
118 | 2.78k | settings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true); |
119 | 2.78k | settings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false); |
120 | 2.78k | settings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true); |
121 | 2.78k | settings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true); |
122 | 2.78k | settings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true); |
123 | 2.78k | settings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true); |
124 | 2.78k | settings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true); |
125 | 2.78k | settings.readWeights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_WEIGHTS, true); |
126 | 2.78k | settings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false); |
127 | 2.78k | settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true); |
128 | 2.78k | settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true); |
129 | 2.78k | settings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false); |
130 | 2.78k | settings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true); |
131 | 2.78k | settings.convertToMeters = pImp->GetPropertyBool(AI_CONFIG_FBX_CONVERT_TO_M, false); |
132 | 2.78k | } |
133 | | |
134 | | // ------------------------------------------------------------------------------------------------ |
135 | | // Imports the given file into the given scene structure. |
136 | 2.78k | void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) { |
137 | 2.78k | auto streamCloser = [&](IOStream *pStream) { |
138 | 2.78k | pIOHandler->Close(pStream); |
139 | 2.78k | }; |
140 | 2.78k | std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, "rb"), streamCloser); |
141 | 2.78k | if (!stream) { |
142 | 0 | ThrowException("Could not open file for reading"); |
143 | 0 | } |
144 | | |
145 | 2.78k | ASSIMP_LOG_DEBUG("Reading FBX file"); |
146 | | |
147 | | // read entire file into memory - no streaming for this, fbx |
148 | | // files can grow large, but the assimp output data structure |
149 | | // then becomes very large, too. Assimp doesn't support |
150 | | // streaming for its output data structures so the net win with |
151 | | // streaming input data would be very low. |
152 | 2.78k | std::vector<char> contents; |
153 | 2.78k | contents.resize(stream->FileSize() + 1); |
154 | 2.78k | stream->Read(&*contents.begin(), 1, contents.size() - 1); |
155 | 2.78k | contents[contents.size() - 1] = 0; |
156 | 2.78k | const char *const begin = &*contents.begin(); |
157 | | |
158 | | // broadphase tokenizing pass in which we identify the core |
159 | | // syntax elements of FBX (brackets, commas, key:value mappings) |
160 | 2.78k | TokenList tokens; |
161 | 2.78k | try { |
162 | | |
163 | 2.78k | bool is_binary = false; |
164 | 2.78k | if (!strncmp(begin, "Kaydara FBX Binary", 18)) { |
165 | 0 | is_binary = true; |
166 | 0 | TokenizeBinary(tokens, begin, contents.size()); |
167 | 2.78k | } else { |
168 | 2.78k | Tokenize(tokens, begin); |
169 | 2.78k | } |
170 | | |
171 | | // use this information to construct a very rudimentary |
172 | | // parse-tree representing the FBX scope structure |
173 | 2.78k | Parser parser(tokens, is_binary); |
174 | | |
175 | | // take the raw parse-tree and convert it to a FBX DOM |
176 | 2.78k | Document doc(parser, settings); |
177 | | |
178 | | // convert the FBX DOM to aiScene |
179 | 2.78k | ConvertToAssimpScene(pScene, doc, settings.removeEmptyBones); |
180 | | |
181 | | // size relative to cm |
182 | 2.78k | float size_relative_to_cm = doc.GlobalSettings().UnitScaleFactor(); |
183 | 2.78k | if (size_relative_to_cm == 0.0) |
184 | 0 | { |
185 | | // BaseImporter later asserts that fileScale is non-zero. |
186 | 0 | ThrowException("The UnitScaleFactor must be non-zero"); |
187 | 0 | } |
188 | | |
189 | | // Set FBX file scale is relative to CM must be converted to M for |
190 | | // assimp universal format (M) |
191 | 2.78k | SetFileScale(size_relative_to_cm * 0.01f); |
192 | | |
193 | 2.78k | std::for_each(tokens.begin(), tokens.end(), Util::delete_fun<Token>()); |
194 | 2.78k | } catch (std::exception &) { |
195 | 2.78k | std::for_each(tokens.begin(), tokens.end(), Util::delete_fun<Token>()); |
196 | 2.78k | throw; |
197 | 2.78k | } |
198 | 2.78k | } |
199 | | |
200 | | #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER |