在3D渲染的时候,工作量比较大且比较麻烦的一件事就是建模,如果想降低这种麻烦就需要借用网络上已经存在的一些模型素材,至少这是非商用渲染程序常用手段(咱们自己写例子的时候经常这样做)。但是,由于网络中的模型格式众多,如果自己一一去解析代价也挺高的。ASSIMP作为一个开源项目,设计了一套可扩展的架构,为模型的导入导出提供了良好的支持。这里说的导入是把模型文件,解析成ASSIMP自身定义的一套模型,而导出即是把自身建立的模型结构导出为模型文件。
ASSIMP默认提供了网络上比较流行的多种模型文件格式的导入和导出,如果我们仍需要对一下特殊的文件格式做这些操作,可以自己扩展。
Importer :
//1.注册模型导入实现类,BaseImporter对应的是一种具体的格式的模型文件 aiReturn RegisterLoader(BaseImporter* pImp); aiReturn UnregisterLoader(BaseImporter* pImp); //2.注册处理过程,BaseProcess对应一个加载后的处理过程,例如把所有的非三角形网格变成三角形网格之类的,功能很强大 aiReturn RegisterPPStep(BaseProcess* pImp); aiReturn UnregisterPPStep(BaseProcess* pImp); //3.各种属性 void SetPropertyInteger(const char* szName, int iValue, bool* bWasExisting = NULL); void SetPropertyBool(const char* szName, bool value, bool* bWasExisting = NULL) { SetPropertyInteger(szName,value,bWasExisting); } void SetPropertyFloat(const char* szName, float fValue, bool* bWasExisting = NULL); void SetPropertyString(const char* szName, const std::string& sValue, bool* bWasExisting = NULL); int GetPropertyInteger(const char* szName, int iErrorReturn = 0xffffffff) const; bool GetPropertyBool(const char* szName, bool bErrorReturn = false) const { return GetPropertyInteger(szName,bErrorReturn)!=0; } float GetPropertyFloat(const char* szName, float fErrorReturn = 10e10f) const; const std::string& GetPropertyString(const char* szName,const std::string& sErrorReturn = "") const; //4.IOSystem只有一个,这是ASSIMP封住好的读写文件的类,这功能不需要扩展。 void SetIOHandler( IOSystem* pIOHandler); IOSystem* GetIOHandler() const; bool IsDefaultIOHandler() const; //5.这个是意淫中的功能至少我的用的3.0版本里面,它还不是一个功能。 void SetProgressHandler ( ProgressHandler* pHandler ); ProgressHandler* GetProgressHandler() const; bool IsDefaultProgressHandler() const; bool ValidateFlags(unsigned int pFlags) const; //6.读文件最终获得一个aiScene对象 const aiScene* ReadFile(const char* pFile, unsigned int pFlags); const aiScene* ReadFileFromMemory( const void* pBuffer,size_t pLength,unsigned int pFlags,const char* pHint = ""); const aiScene* ApplyPostProcessing(unsigned int pFlags); const aiScene* ReadFile(const std::string& pFile, unsigned int pFlags); void FreeScene( ); const char* GetErrorString() const; const aiScene* GetScene() const; aiScene* GetOrphanedScene();
由于导入模型和导出模型是两个不相干的事情,所以在ASSIMP里面它们是没有任何联系的,Importer 作为导入模型的入口类,其管理的角色比较明显。以这种服务注册的模式,使代码的结构更松散。另外,上述对象都保存在ImporterPimpl里面而不是Importer 里面。
读取模型的过程:
//把完整的方法的主要内容提取出来 const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) { // 1. 释放掉旧的内容 if (pimpl->mScene){ FreeScene(); } // 2.找出可以读当前文件的插件,这里存在一个隐患,如果列表里面有多个满足此文件,第一个优先采纳。 BaseImporter* imp = NULL; for( unsigned int a = 0; a < pimpl->mImporter.size(); a++){ if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, false)) { imp = pimpl->mImporter[a]; break; } } //... 这里略过一堆蛋疼的代码,不知道编程的人怎么想的,我觉得上面就够了。 // 3.先更新Progress,然后再解析模型 pimpl->mProgressHandler->Update(); pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler); pimpl->mProgressHandler->Update(); // 这里为啥又要调一次?不懂 // 4. 这个地方还有一个后处理步骤 if( pimpl->mScene){ ScenePreprocessor pre(pimpl->mScene); pre.ProcessScene(); pimpl->mProgressHandler->Update(); ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure)); } pimpl->mPPShared->Clean(); return pimpl->mScene; }
浓缩版的ReadFile方法,描述了整个解析过程,细节不再关注,这个插件主要强调的是用而不是修改。
支持的数据格式:
Assimp(Open Asset Import Library)是一个支持读取多种模型资源的开源库,当前最新的版本是3.0版,支持读取以下类型的3D模型:
支持以下格式的导出:
此外还支持对导入的模型做一些常用的处理,如把四边形转换为三角形、计算切线和副法线、将大模型分割为小模型等。