技术经验分享:ASSIMP概述

本文涉及的产品
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 技术经验分享:ASSIMP概述

阅读更多


在3D渲染的时候,工作量比较大且比较麻烦的一件事就是建模,如果想降低这种麻烦就需要借用网络上已经存在的一些模型素材,至少这是非商用渲染程序常用手段(咱们自己写例子的时候经常这样做)。但是,由于网络中的模型格式众多,如果自己一一去解析代价也挺高的。ASSIMP作为一个开源项目,设计了一套可扩展的架构,为模型的导入导出提供了良好的支持。这里说的导入是把模型文件,解析成ASSIMP自身定义的一套模型,而导出即是把自身建立的模型结构导出为模型文件。


ASSIMP默认提供了网络上比较流行的多种模型文件格式的导入和导出,如果我们仍需要对一下特殊的文件格式做这些操作,可以自己扩展。


Importer :


Cpp代码


//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();


//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 //代码效果参考:http://www.jhylw.com.cn/210633659.html

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 里面。


读取模型的过程:


Cpp代码


//把完整的方法的主要内容提取出来 const aiScene Importer::ReadFile( const char _pFile, unsigned int pFlags) { // 1. 释放掉旧的内容 //代码效果参考:http://www.jhylw.com.cn/470929351.html

if (pimpl->mScene){ FreeScene(); } // 2.找出可以读当前文件的插件,这里存在一个隐患,如果列表里面有多个满足此文件,第一个优先采纳。 BaseImporter imp = NULL; for( unsigned int a = 0; a 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; }

//把完整的方法的主要内容提取出来


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 [/span> pimpl-

if( pimpl-

imp = pimpl-

break;


}


}


//... 这里略过一堆蛋疼的代码,不知道编程的人怎么想的,我觉得上面就够了。


// 3.先更新Progress,然后再解析模型


pimpl-

pimpl-

pimpl-

// 4. 这个地方还有一个后处理步骤


if( pimpl-

ScenePreprocessor pre(pimpl-

pre.ProcessScene();


pimpl-

ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));


}


pimpl-

return pimpl-

// 2.找出可以读当前文件的插件,这里存在一个隐患,如果列表里面有多个满足此文件,第一个优先采纳。


BaseImporter imp = NULL;


for( unsigned int a = 0; a [/span> pimpl-

if( pimpl-

imp = pimpl-

break;


}


}


//... 这里略过一堆蛋疼的代码,不知道编程的人怎么想的,我觉得上面就够了。


// 3.先更新Progress,然后再解析模型


pimpl-

pimpl-

pimpl-

// 4. 这个地方还有一个后处理步骤


if( pimpl-

ScenePreprocessor pre(pimpl-

pre.ProcessScene();


pimpl-

ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));


}


pimpl-

return pimpl-

}


浓缩版的ReadFile方法,描述了整个解析过程,细节不再关注,这个插件主要强调的是用而不是修改。


支持的数据格式:


Assimp(Open Asset Import Library)是一个支持读取多种模型资源的开源库,当前最新的版本是3.0版,支持读取以下类型的3D模型:


Collada ( .dae )Blender 3D ( .blend )3ds Max 3DS ( .3ds )3ds Max ASE ( .ase )Wavefront Object ( .obj )Industry Foundation Classes (IFC/Step) ( .ifc )XGL ( .xgl,.zgl )Stanford Polygon Library ( .ply )AutoCAD DXF ( .dxf )LightWave ( .lwo )LightWave Scene ( .lws )Modo ( .lxo )Stereolithography ( .stl )DirectX X ( .x )AC3D ( .ac )Milkshape 3D ( .ms3d ) TrueSpace ( .cob,.scn )Biovision BVH ( .bvh ) CharacterStudio Motion ( .csm )Ogre XML ( .xml )Irrlicht Mesh ( .irrmesh ) Irrlicht Scene ( .irr )Quake I ( .mdl )Quake II ( .md2 )Quake III Mesh ( .md3 )Quake III Map/BSP ( .pk3 ) Return to Castle Wolfenstein ( .mdc )Doom 3 ( .md5 )Valve Model ( .smd,.vta )Starcraft II M3 ( .m3 )Unreal ( .3d )BlitzBasic 3D ( .b3d )Quick3D ( .q3d,.q3s )Neutral File Format ( .nff )Sense8 WorldToolKit ( .nff )Object File Format ( .off )PovRAY Raw ( .raw )Terragen Terrain ( .ter )3D GameStudio (3DGS) ( .mdl )3D GameStudio (3DGS) Terrain ( .hmp )Izware Nendo ( .ndo )


支持以下格式的导出:


Collada ( .dae )Wavefront Object ( .obj )Stereolithography ( .stl )Stanford Polygon Library ( .ply )


此外还支持对导入的模型做一些常用的处理,如把四边形转换为三角形、计算切线和副法线、将大模型分割为小模型等。


[span class="hljs-name">a href="/blog/1756120" class="next" title="ASSIMP的数据结构"

相关文章
|
1天前
|
数据可视化 BI OLAP
技术经验分享:DundasBI简介
技术经验分享:DundasBI简介
|
2天前
|
存储 安全 物联网
技术经验分享:FIDO标准简介
技术经验分享:FIDO标准简介
|
2天前
|
存储 C# 索引
技术经验分享:C#入门详解(8)
技术经验分享:C#入门详解(8)
|
3天前
|
C++ 开发者
技术经验分享:dumpbin的使用方法_dumpbin的基础使用
技术经验分享:dumpbin的使用方法_dumpbin的基础使用
|
3天前
|
前端开发 关系型数据库 MySQL
技术好文:R基础学习(三)
技术好文:R基础学习(三)
|
9月前
|
架构师 测试技术 API
深入浅出聊一聊自动化架构!
深入浅出聊一聊自动化架构!
131 1
|
1月前
|
云栖大会 数据库
电子书阅读分享《2022云栖大会:云时代数据库应用最佳实践》
电子书阅读分享《2022云栖大会:云时代数据库应用最佳实践》
44 5
|
JSON 缓存 运维
上篇:运维人员不得不看的K8S API入门实战,呕心沥血整理得又臭又长,有人看吗?
上篇:运维人员不得不看的K8S API入门实战,呕心沥血整理得又臭又长,有人看吗?
120 0
|
人工智能 开发者 Python
模型社区实战训练营首开,开源让代码更有趣!
阿里云开发者学堂联合modelscope社区推出了模型实战系列训练营,培养AI开发者从各种模态的模型选择,只需要懂得python,也可以玩转人工智能模型。
|
机器学习/深度学习 文字识别 搜索推荐
机器学习基础实践教学开发 | 开发者社区精选文章合集(十九)
当今机器学习正带来一场全新的技术变革,实践应用更是重中之重,零基础入门机器学习,宝藏分享来啦!
机器学习基础实践教学开发 | 开发者社区精选文章合集(十九)