2008-05-09 17:24:28 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2008-05-22 10:20:31 +00:00
|
|
|
Open Asset Import Library (ASSIMP)
|
2008-05-09 17:24:28 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development 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 Development 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.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** @file Implementation of the CPP-API class #Importer */
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "../include/assimp.hpp"
|
2008-05-22 22:47:20 +00:00
|
|
|
#include "../include/aiAssert.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "../include/aiScene.h"
|
2008-05-22 22:47:20 +00:00
|
|
|
#include "../include/aiPostProcess.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "BaseImporter.h"
|
|
|
|
#include "BaseProcess.h"
|
|
|
|
#include "DefaultIOStream.h"
|
|
|
|
#include "DefaultIOSystem.h"
|
2008-05-09 17:24:28 +00:00
|
|
|
|
|
|
|
#if (!defined AI_BUILD_NO_X_IMPORTER)
|
|
|
|
# include "XFileImporter.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_3DS_IMPORTER)
|
|
|
|
# include "3DSLoader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD3_IMPORTER)
|
|
|
|
# include "MD3Loader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD4_IMPORTER)
|
|
|
|
# include "MD4Loader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MDL_IMPORTER)
|
|
|
|
# include "MDLLoader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD2_IMPORTER)
|
|
|
|
# include "MD2Loader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_PLY_IMPORTER)
|
|
|
|
# include "PlyLoader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_ASE_IMPORTER)
|
|
|
|
# include "ASELoader.h"
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_OBJ_IMPORTER)
|
|
|
|
# include "ObjFileImporter.h"
|
|
|
|
#endif
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "CalcTangentsProcess.h"
|
|
|
|
#include "JoinVerticesProcess.h"
|
|
|
|
#include "ConvertToLHProcess.h"
|
|
|
|
#include "TriangulateProcess.h"
|
|
|
|
#include "GenFaceNormalsProcess.h"
|
|
|
|
#include "GenVertexNormalsProcess.h"
|
|
|
|
#include "KillNormalsProcess.h"
|
|
|
|
#include "SplitLargeMeshes.h"
|
2008-05-26 21:28:19 +00:00
|
|
|
#include "../include/DefaultLogger.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor.
|
|
|
|
Importer::Importer() :
|
|
|
|
mIOHandler(NULL),
|
|
|
|
mScene(NULL),
|
|
|
|
mErrorString("")
|
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
// allocate a default IO handler
|
2008-05-05 12:36:31 +00:00
|
|
|
mIOHandler = new DefaultIOSystem;
|
|
|
|
|
|
|
|
// add an instance of each worker class here
|
2008-05-09 17:24:28 +00:00
|
|
|
#if (!defined AI_BUILD_NO_X_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new XFileImporter());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_OBJ_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new ObjFileImporter());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_3DS_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new Dot3DSImporter());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD3_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new MD3Importer());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD2_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new MD2Importer());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_PLY_IMPORTER)
|
2008-05-05 12:36:31 +00:00
|
|
|
mImporter.push_back( new PLYImporter());
|
2008-05-09 17:24:28 +00:00
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MDL_IMPORTER)
|
|
|
|
mImporter.push_back( new MDLImporter());
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_MD4_IMPORTER)
|
|
|
|
mImporter.push_back( new MD4Importer());
|
|
|
|
#endif
|
|
|
|
#if (!defined AI_BUILD_NO_ASE_IMPORTER)
|
|
|
|
mImporter.push_back( new ASEImporter());
|
|
|
|
#endif
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// add an instance of each post processing step here in the order of sequence it is executed
|
|
|
|
mPostProcessingSteps.push_back( new TriangulateProcess());
|
2008-05-09 17:24:28 +00:00
|
|
|
mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
|
2008-05-05 12:36:31 +00:00
|
|
|
mPostProcessingSteps.push_back( new KillNormalsProcess());
|
|
|
|
mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
|
|
|
|
mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
|
|
|
|
mPostProcessingSteps.push_back( new CalcTangentsProcess());
|
|
|
|
mPostProcessingSteps.push_back( new JoinVerticesProcess());
|
2008-05-09 17:24:28 +00:00
|
|
|
mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
|
2008-05-05 12:36:31 +00:00
|
|
|
mPostProcessingSteps.push_back( new ConvertToLHProcess());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor.
|
|
|
|
Importer::~Importer()
|
|
|
|
{
|
|
|
|
for( unsigned int a = 0; a < mImporter.size(); a++)
|
|
|
|
delete mImporter[a];
|
|
|
|
for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
|
|
|
|
delete mPostProcessingSteps[a];
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// delete the assigned IO handler
|
2008-05-05 12:36:31 +00:00
|
|
|
delete mIOHandler;
|
|
|
|
|
|
|
|
// kill imported scene. Destructors should do that recursivly
|
|
|
|
delete mScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Supplies a custom IO handler to the importer to open and access files.
|
|
|
|
void Importer::SetIOHandler( IOSystem* pIOHandler)
|
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
if (NULL == pIOHandler)
|
|
|
|
{
|
|
|
|
delete mIOHandler;
|
|
|
|
mIOHandler = new DefaultIOSystem();
|
|
|
|
}
|
|
|
|
else if (mIOHandler != pIOHandler)
|
|
|
|
{
|
|
|
|
delete mIOHandler;
|
|
|
|
mIOHandler = pIOHandler;
|
|
|
|
}
|
|
|
|
return;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-05-22 22:47:20 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Validate post process step flags
|
|
|
|
bool ValidateFlags(unsigned int pFlags)
|
|
|
|
{
|
|
|
|
if (pFlags & aiProcess_GenSmoothNormals &&
|
|
|
|
pFlags & aiProcess_GenNormals)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error("aiProcess_GenSmoothNormals and aiProcess_GenNormals "
|
|
|
|
"may not be specified together");
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-05-22 22:47:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the given file and returns its contents if successful.
|
|
|
|
const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
|
|
|
|
{
|
2008-05-22 22:47:20 +00:00
|
|
|
// validate the flags
|
|
|
|
ai_assert(ValidateFlags(pFlags));
|
|
|
|
|
2008-05-23 12:30:52 +00:00
|
|
|
// check whether this Importer instance has already loaded
|
|
|
|
// a scene. In this case we need to delete the old one
|
|
|
|
if (this->mScene)
|
|
|
|
{
|
|
|
|
delete mScene;
|
|
|
|
this->mScene = NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// first check if the file is accessable at all
|
|
|
|
if( !mIOHandler->Exists( pFile))
|
|
|
|
{
|
|
|
|
mErrorString = "Unable to open file \"" + pFile + "\".";
|
2008-05-13 23:26:52 +00:00
|
|
|
DefaultLogger::get()->error(mErrorString);
|
2008-05-05 12:36:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find an worker class which can handle the file
|
|
|
|
BaseImporter* imp = NULL;
|
|
|
|
for( unsigned int a = 0; a < mImporter.size(); a++)
|
|
|
|
{
|
|
|
|
if( mImporter[a]->CanRead( pFile, mIOHandler))
|
|
|
|
{
|
|
|
|
imp = mImporter[a];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// put a proper error message if no suitable importer was found
|
|
|
|
if( !imp)
|
|
|
|
{
|
|
|
|
mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
|
2008-05-13 23:26:52 +00:00
|
|
|
DefaultLogger::get()->error(mErrorString);
|
2008-05-05 12:36:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatch the reading to the worker class for this format
|
|
|
|
mScene = imp->ReadFile( pFile, mIOHandler);
|
|
|
|
// if failed, extract the error string
|
|
|
|
if( !mScene)
|
|
|
|
mErrorString = imp->GetErrorText();
|
|
|
|
|
|
|
|
// if successful, apply all active post processing steps to the imported data
|
|
|
|
if( mScene)
|
|
|
|
{
|
|
|
|
for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
|
|
|
|
{
|
|
|
|
BaseProcess* process = mPostProcessingSteps[a];
|
|
|
|
if( process->IsActive( pFlags))
|
|
|
|
process->Execute( mScene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// either successful or failure - the pointer expresses it anyways
|
|
|
|
return mScene;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-05-09 17:24:28 +00:00
|
|
|
// Empty and private copy constructor
|
2008-05-05 12:36:31 +00:00
|
|
|
Importer::Importer(const Importer &other)
|
|
|
|
{
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Helper function to check whether an extension is supported by ASSIMP
|
|
|
|
bool Importer::IsExtensionSupported(const std::string& szExtension)
|
|
|
|
{
|
|
|
|
for (std::vector<BaseImporter*>::const_iterator
|
|
|
|
i = this->mImporter.begin();
|
|
|
|
i != this->mImporter.end();++i)
|
|
|
|
{
|
|
|
|
// pass the file extension to the CanRead(..,NULL)-method
|
|
|
|
if ((*i)->CanRead(szExtension,NULL))return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Helper function to build a list of all file extensions supported by ASSIMP
|
|
|
|
void Importer::GetExtensionList(std::string& szOut)
|
|
|
|
{
|
|
|
|
unsigned int iNum = 0;
|
|
|
|
for (std::vector<BaseImporter*>::const_iterator
|
|
|
|
i = this->mImporter.begin();
|
|
|
|
i != this->mImporter.end();++i,++iNum)
|
|
|
|
{
|
|
|
|
// insert a comma as delimiter character
|
|
|
|
if (0 != iNum)
|
|
|
|
szOut.append(";");
|
|
|
|
|
|
|
|
(*i)->GetExtensionList(szOut);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|