vrshoot

annotate libs/assimp/FBXImporter.cpp @ 1:e7ca128b8713

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