2012-06-23 01:36:55 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2019-01-30 08:41:39 +00:00
|
|
|
Copyright (c) 2006-2019, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2012-06-23 01:36:55 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2012-06-23 01:36:55 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
2016-02-29 14:57:47 +00:00
|
|
|
r
|
2012-06-23 01:36:55 +00:00
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2012-06-23 01:36:55 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2012-06-23 01:36:55 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2012-06-23 01:36:55 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2012-06-23 01:36:55 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file FBXImporter.cpp
|
|
|
|
* @brief Implementation of the FBX importer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
|
|
|
|
|
|
|
|
#include "FBXImporter.h"
|
|
|
|
|
|
|
|
#include "FBXTokenizer.h"
|
|
|
|
#include "FBXParser.h"
|
2012-06-25 21:03:06 +00:00
|
|
|
#include "FBXUtil.h"
|
2012-06-26 16:31:19 +00:00
|
|
|
#include "FBXDocument.h"
|
2012-06-28 18:02:09 +00:00
|
|
|
#include "FBXConverter.h"
|
2012-06-23 01:36:55 +00:00
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StreamReader.h>
|
|
|
|
#include <assimp/MemoryIOWrapper.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/Importer.hpp>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2016-12-02 10:32:34 +00:00
|
|
|
|
2012-06-23 01:36:55 +00:00
|
|
|
namespace Assimp {
|
2019-02-18 20:43:45 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
const char* LogFunctions<FBXImporter>::Prefix() {
|
|
|
|
static auto prefix = "FBX: ";
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Formatter;
|
|
|
|
using namespace Assimp::FBX;
|
|
|
|
|
|
|
|
namespace {
|
2019-02-18 20:43:45 +00:00
|
|
|
|
2012-06-23 01:36:55 +00:00
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"Autodesk FBX Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"fbx"
|
2012-06-23 01:36:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by #Importer
|
|
|
|
FBXImporter::FBXImporter()
|
2016-03-03 17:38:50 +00:00
|
|
|
{
|
|
|
|
}
|
2012-06-23 01:36:55 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Destructor, private as well
|
2012-06-23 01:36:55 +00:00
|
|
|
FBXImporter::~FBXImporter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2012-06-23 01:36:55 +00:00
|
|
|
bool FBXImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string& extension = GetExtension(pFile);
|
2016-12-02 10:32:34 +00:00
|
|
|
if (extension == std::string( desc.mFileExtensions ) ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ((!extension.length() || checkSig) && pIOHandler) {
|
2016-03-03 17:38:50 +00:00
|
|
|
// at least ASCII-FBX files usually have a 'FBX' somewhere in their head
|
2015-05-19 03:57:13 +00:00
|
|
|
const char* tokens[] = {"fbx"};
|
|
|
|
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// List all extensions handled by this loader
|
|
|
|
const aiImporterDesc* FBXImporter::GetInfo () const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup configuration properties for the loader
|
|
|
|
void FBXImporter::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
settings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
|
|
|
|
settings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
|
|
|
|
settings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
|
2015-11-23 18:49:54 +00:00
|
|
|
settings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
|
2015-05-19 03:57:13 +00:00
|
|
|
settings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
|
|
|
|
settings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
|
|
|
|
settings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
|
|
|
|
settings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
|
|
|
|
settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
|
|
|
|
settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
|
2017-12-18 20:14:54 +00:00
|
|
|
settings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false);
|
2019-05-05 11:39:10 +00:00
|
|
|
settings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true);
|
2019-05-06 19:38:45 +00:00
|
|
|
settings.convertToMeters = pImp->GetPropertyBool(AI_CONFIG_FBX_CONVERT_TO_M, false);
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2018-01-23 14:13:23 +00:00
|
|
|
void FBXImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
|
2012-06-23 01:36:55 +00:00
|
|
|
{
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<IOStream> stream(pIOHandler->Open(pFile,"rb"));
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!stream) {
|
|
|
|
ThrowException("Could not open file for reading");
|
|
|
|
}
|
|
|
|
|
|
|
|
// read entire file into memory - no streaming for this, fbx
|
|
|
|
// files can grow large, but the assimp output data structure
|
|
|
|
// then becomes very large, too. Assimp doesn't support
|
|
|
|
// streaming for its output data structures so the net win with
|
|
|
|
// streaming input data would be very low.
|
|
|
|
std::vector<char> contents;
|
|
|
|
contents.resize(stream->FileSize()+1);
|
2015-06-19 13:24:34 +00:00
|
|
|
stream->Read( &*contents.begin(), 1, contents.size()-1 );
|
2015-06-19 13:04:30 +00:00
|
|
|
contents[ contents.size() - 1 ] = 0;
|
2015-05-19 03:57:13 +00:00
|
|
|
const char* const begin = &*contents.begin();
|
|
|
|
|
|
|
|
// broadphase tokenizing pass in which we identify the core
|
|
|
|
// syntax elements of FBX (brackets, commas, key:value mappings)
|
|
|
|
TokenList tokens;
|
|
|
|
try {
|
|
|
|
|
|
|
|
bool is_binary = false;
|
|
|
|
if (!strncmp(begin,"Kaydara FBX Binary",18)) {
|
|
|
|
is_binary = true;
|
2019-05-14 14:24:23 +00:00
|
|
|
TokenizeBinary(tokens,begin,contents.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Tokenize(tokens,begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// use this information to construct a very rudimentary
|
|
|
|
// parse-tree representing the FBX scope structure
|
|
|
|
Parser parser(tokens, is_binary);
|
|
|
|
|
|
|
|
// take the raw parse-tree and convert it to a FBX DOM
|
|
|
|
Document doc(parser,settings);
|
|
|
|
|
2019-05-07 18:32:45 +00:00
|
|
|
FbxUnit unit(FbxUnit::cm);
|
|
|
|
if (settings.convertToMeters) {
|
|
|
|
unit = FbxUnit::m;
|
|
|
|
}
|
2019-08-21 19:29:46 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// convert the FBX DOM to aiScene
|
2019-08-21 19:29:46 +00:00
|
|
|
ConvertToAssimpScene(pScene, doc, settings.removeEmptyBones, unit);
|
|
|
|
|
2019-08-26 19:37:49 +00:00
|
|
|
// units is relative to CM :) we need it in meters for assimp
|
|
|
|
SetFileScale( doc.GlobalSettings().UnitScaleFactor() * 0.01f);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
|
|
|
|
}
|
|
|
|
catch(std::exception&) {
|
|
|
|
std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
|
|
|
|
throw;
|
|
|
|
}
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ASSIMP_BUILD_NO_FBX_IMPORTER
|