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 Plain-C API */
|
2008-08-08 18:29:05 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "../include/assimp.h"
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
// public ASSIMP headers
|
2008-08-28 17:35:36 +00:00
|
|
|
#include "../include/aiFileIO.h"
|
|
|
|
|
|
|
|
#include "GenericProperty.h"
|
2008-05-09 17:24:28 +00:00
|
|
|
|
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
# include <boost/thread/thread.hpp>
|
|
|
|
# include <boost/thread/mutex.hpp>
|
|
|
|
#endif
|
|
|
|
|
2008-08-08 18:29:05 +00:00
|
|
|
using namespace Assimp;
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** Stores the importer objects for all active import processes */
|
|
|
|
typedef std::map< const aiScene*, Assimp::Importer* > ImporterMap;
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** Local storage of all active import processes */
|
|
|
|
static ImporterMap gActiveImports;
|
|
|
|
|
|
|
|
/** Error message of the last failed import process */
|
|
|
|
static std::string gLastErrorString;
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
/** Configuration properties */
|
|
|
|
static Importer::IntPropertyMap gIntProperties;
|
|
|
|
static Importer::FloatPropertyMap gFloatProperties;
|
|
|
|
static Importer::StringPropertyMap gStringProperties;
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
/** Global mutex to manage the access to the importer map */
|
|
|
|
static boost::mutex gMutex;
|
|
|
|
#endif
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
class CIOSystemWrapper;
|
|
|
|
class CIOStreamWrapper;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Custom IOStream implementation for the C-API
|
|
|
|
class CIOStreamWrapper : public IOStream
|
|
|
|
{
|
|
|
|
friend class CIOSystemWrapper;
|
|
|
|
public:
|
|
|
|
|
|
|
|
CIOStreamWrapper(aiFile* pFile)
|
|
|
|
: mFile(pFile)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
size_t Read(void* pvBuffer,
|
2008-08-28 17:35:36 +00:00
|
|
|
size_t pSize,
|
|
|
|
size_t pCount)
|
|
|
|
{
|
|
|
|
// need to typecast here as C has no void*
|
|
|
|
return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
size_t Write(const void* pvBuffer,
|
2008-08-28 17:35:36 +00:00
|
|
|
size_t pSize,
|
|
|
|
size_t pCount)
|
2009-01-12 22:06:54 +00:00
|
|
|
{
|
|
|
|
// need to typecast here as C has no void*
|
|
|
|
return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
|
|
|
|
}
|
2008-08-28 17:35:36 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
aiReturn Seek(size_t pOffset,
|
|
|
|
aiOrigin pOrigin)
|
|
|
|
{
|
|
|
|
return mFile->SeekProc(mFile,pOffset,pOrigin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
size_t Tell(void) const
|
2008-08-28 17:35:36 +00:00
|
|
|
{
|
|
|
|
return mFile->TellProc(mFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
size_t FileSize() const
|
|
|
|
{
|
|
|
|
return mFile->FileSizeProc(mFile);
|
|
|
|
}
|
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
void Flush ()
|
|
|
|
{
|
|
|
|
return mFile->FlushProc(mFile);
|
|
|
|
}
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
private:
|
|
|
|
aiFile* mFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Custom IOStream implementation for the C-API
|
|
|
|
class CIOSystemWrapper : public IOSystem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CIOSystemWrapper(aiFileIO* pFile)
|
|
|
|
: mFileSystem(pFile)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
bool Exists( const std::string& pFile) const
|
|
|
|
{
|
|
|
|
CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
|
|
|
|
IOStream* p = pip->Open(pFile);
|
2009-01-12 22:06:54 +00:00
|
|
|
if (p){
|
|
|
|
pip->Close(p);
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-28 17:35:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
std::string getOsSeparator() const
|
|
|
|
{
|
2009-01-12 22:06:54 +00:00
|
|
|
// FIXME
|
2008-08-28 17:35:36 +00:00
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
IOStream* Open(const std::string& pFile,
|
|
|
|
const std::string& pMode = std::string("rb"))
|
|
|
|
{
|
|
|
|
aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile.c_str(),pMode.c_str());
|
|
|
|
if (!p)return NULL;
|
|
|
|
return new CIOStreamWrapper(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
void Close( IOStream* pFile)
|
|
|
|
{
|
|
|
|
if (!pFile)return;
|
|
|
|
mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
|
|
|
|
delete pFile;
|
|
|
|
}
|
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
aiFileIO* mFileSystem;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ReportSceneNotFoundError()
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error("Unable to find the Importer instance for this scene. "
|
|
|
|
"Are you sure it has been created by aiImportFile(ex)(...)?");
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the given file and returns its content.
|
|
|
|
const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
|
2008-08-28 17:35:36 +00:00
|
|
|
{
|
|
|
|
return aiImportFileEx(pFile,pFlags,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
|
|
|
|
aiFileIO* pFS)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
ai_assert(NULL != pFile);
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// create an Importer for this file
|
|
|
|
Assimp::Importer* imp = new Assimp::Importer;
|
2008-08-28 17:35:36 +00:00
|
|
|
|
|
|
|
// copy the global property lists to the Importer instance
|
|
|
|
// (we are a friend of Importer)
|
|
|
|
imp->mIntProperties = gIntProperties;
|
|
|
|
imp->mFloatProperties = gFloatProperties;
|
|
|
|
imp->mStringProperties = gStringProperties;
|
|
|
|
|
|
|
|
// setup a custom IO system if necessary
|
|
|
|
if (pFS)
|
|
|
|
{
|
|
|
|
imp->SetIOHandler( new CIOSystemWrapper (pFS) );
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// and have it read the file
|
|
|
|
const aiScene* scene = imp->ReadFile( pFile, pFlags);
|
|
|
|
|
|
|
|
// if succeeded, place it in the collection of active processes
|
|
|
|
if( scene)
|
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
boost::mutex::scoped_lock lock(gMutex);
|
|
|
|
#endif
|
2008-05-05 12:36:31 +00:00
|
|
|
gActiveImports[scene] = imp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if failed, extract error code and destroy the import
|
|
|
|
gLastErrorString = imp->GetErrorString();
|
|
|
|
delete imp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return imported data. If the import failed the pointer is NULL anyways
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Releases all resources associated with the given import process.
|
|
|
|
void aiReleaseImport( const aiScene* pScene)
|
|
|
|
{
|
2008-05-09 17:24:28 +00:00
|
|
|
if (!pScene)return;
|
|
|
|
|
|
|
|
// lock the mutex
|
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
boost::mutex::scoped_lock lock(gMutex);
|
|
|
|
#endif
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// find the importer associated with this data
|
|
|
|
ImporterMap::iterator it = gActiveImports.find( pScene);
|
|
|
|
// it should be there... else the user is playing fools with us
|
|
|
|
if( it == gActiveImports.end())
|
2008-08-08 18:29:05 +00:00
|
|
|
{
|
2008-08-28 17:35:36 +00:00
|
|
|
ReportSceneNotFoundError();
|
2008-05-05 12:36:31 +00:00
|
|
|
return;
|
2008-08-08 18:29:05 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// kill the importer, the data dies with it
|
|
|
|
delete it->second;
|
|
|
|
gActiveImports.erase( it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns the error text of the last failed import process.
|
|
|
|
const char* aiGetErrorString()
|
|
|
|
{
|
|
|
|
return gLastErrorString.c_str();
|
|
|
|
}
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns the error text of the last failed import process.
|
|
|
|
int aiIsExtensionSupported(const char* szExtension)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != szExtension);
|
|
|
|
|
|
|
|
// lock the mutex
|
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
boost::mutex::scoped_lock lock(gMutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!gActiveImports.empty())
|
|
|
|
{
|
|
|
|
return (int)((*(gActiveImports.begin())).second->IsExtensionSupported(
|
|
|
|
std::string ( szExtension )));
|
|
|
|
}
|
|
|
|
// need to create a temporary Importer instance.
|
|
|
|
// TODO: Find a better solution ...
|
|
|
|
Assimp::Importer* pcTemp = new Assimp::Importer();
|
|
|
|
int i = (int)pcTemp->IsExtensionSupported(std::string ( szExtension ));
|
|
|
|
delete pcTemp;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Get a list of all file extensions supported by ASSIMP
|
|
|
|
void aiGetExtensionList(aiString* szOut)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != szOut);
|
|
|
|
|
|
|
|
// lock the mutex
|
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
boost::mutex::scoped_lock lock(gMutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string szTemp;
|
|
|
|
if (!gActiveImports.empty())
|
|
|
|
{
|
|
|
|
(*(gActiveImports.begin())).second->GetExtensionList(szTemp);
|
|
|
|
szOut->Set ( szTemp );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// need to create a temporary Importer instance.
|
|
|
|
// TODO: Find a better solution ...
|
|
|
|
Assimp::Importer* pcTemp = new Assimp::Importer();
|
|
|
|
pcTemp->GetExtensionList(szTemp);
|
|
|
|
szOut->Set ( szTemp );
|
|
|
|
delete pcTemp;
|
|
|
|
}
|
2008-08-08 18:29:05 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
|
2008-08-08 18:34:14 +00:00
|
|
|
C_STRUCT aiMemoryInfo* in)
|
2008-08-08 18:29:05 +00:00
|
|
|
{
|
|
|
|
// lock the mutex
|
|
|
|
#if (defined AI_C_THREADSAFE)
|
|
|
|
boost::mutex::scoped_lock lock(gMutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// find the importer associated with this data
|
|
|
|
ImporterMap::iterator it = gActiveImports.find( pIn);
|
|
|
|
// it should be there... else the user is playing fools with us
|
|
|
|
if( it == gActiveImports.end())
|
|
|
|
{
|
2008-08-28 17:35:36 +00:00
|
|
|
ReportSceneNotFoundError();
|
2008-08-08 18:34:14 +00:00
|
|
|
return;
|
2008-08-08 18:29:05 +00:00
|
|
|
}
|
|
|
|
// get memory statistics
|
|
|
|
it->second->GetMemoryRequirements(*in);
|
|
|
|
}
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
|
|
|
|
{
|
|
|
|
SetGenericProperty<int>(gIntProperties,szName,value,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
|
|
|
|
{
|
|
|
|
SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
ASSIMP_API void aiSetImportPropertyString(const char* szName,
|
|
|
|
const C_STRUCT aiString* st)
|
|
|
|
{
|
|
|
|
if (!st)return;
|
|
|
|
|
|
|
|
SetGenericProperty<std::string>(gStringProperties,szName,
|
|
|
|
std::string( st->data ),NULL);
|
|
|
|
}
|