2012-06-23 01:36:55 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2012, assimp team
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2012-06-28 18:02:09 +00:00
|
|
|
#include <exception>
|
2012-06-23 01:36:55 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "StreamReader.h"
|
|
|
|
#include "MemoryIOWrapper.h"
|
2015-04-15 23:00:17 +00:00
|
|
|
#include "../include/assimp/Importer.hpp"
|
2012-06-23 01:36:55 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
template<> const std::string LogFunctions<FBXImporter>::log_prefix = "FBX: ";
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Formatter;
|
|
|
|
using namespace Assimp::FBX;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
"Autodesk FBX Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"fbx"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by #Importer
|
|
|
|
FBXImporter::FBXImporter()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
FBXImporter::~FBXImporter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
|
|
|
bool FBXImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
|
|
|
{
|
|
|
|
const std::string& extension = GetExtension(pFile);
|
|
|
|
if (extension == "fbx") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ((!extension.length() || checkSig) && pIOHandler) {
|
|
|
|
// at least ascii FBX files usually have a 'FBX' somewhere in their head
|
2014-10-10 13:59:42 +00:00
|
|
|
const char* tokens[] = {"fbx"};
|
2012-06-23 01:36:55 +00:00
|
|
|
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// List all extensions handled by this loader
|
|
|
|
const aiImporterDesc* FBXImporter::GetInfo () const
|
|
|
|
{
|
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup configuration properties for the loader
|
|
|
|
void FBXImporter::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
2012-08-11 14:41:44 +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);
|
|
|
|
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);
|
2012-06-23 01:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
|
|
|
void FBXImporter::InternReadFile( const std::string& pFile,
|
|
|
|
aiScene* pScene, IOSystem* pIOHandler)
|
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> stream(pIOHandler->Open(pFile,"rb"));
|
|
|
|
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;
|
2015-06-19 13:04:30 +00:00
|
|
|
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;
|
2012-06-23 01:36:55 +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;
|
2012-06-25 21:03:06 +00:00
|
|
|
try {
|
2012-08-09 00:08:12 +00:00
|
|
|
|
2012-08-10 19:39:47 +00:00
|
|
|
bool is_binary = false;
|
2012-08-09 00:08:12 +00:00
|
|
|
if (!strncmp(begin,"Kaydara FBX Binary",18)) {
|
2012-08-10 19:39:47 +00:00
|
|
|
is_binary = true;
|
2012-08-09 00:08:12 +00:00
|
|
|
TokenizeBinary(tokens,begin,contents.size());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Tokenize(tokens,begin);
|
|
|
|
}
|
2012-06-23 01:36:55 +00:00
|
|
|
|
2012-06-25 21:03:06 +00:00
|
|
|
// use this information to construct a very rudimentary
|
|
|
|
// parse-tree representing the FBX scope structure
|
2012-08-10 19:39:47 +00:00
|
|
|
Parser parser(tokens, is_binary);
|
2012-06-26 16:31:19 +00:00
|
|
|
|
|
|
|
// take the raw parse-tree and convert it to a FBX DOM
|
2012-06-28 17:16:14 +00:00
|
|
|
Document doc(parser,settings);
|
2012-06-28 18:02:09 +00:00
|
|
|
|
|
|
|
// convert the FBX DOM to aiScene
|
|
|
|
ConvertToAssimpScene(pScene,doc);
|
2014-07-13 12:11:58 +00:00
|
|
|
|
|
|
|
std::for_each(tokens.begin(),tokens.end(),Util::delete_fun<Token>());
|
2012-06-25 21:03:06 +00:00
|
|
|
}
|
2012-06-28 18:02:09 +00:00
|
|
|
catch(std::exception&) {
|
2012-06-25 21:03:06 +00:00
|
|
|
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
|