Autodesk FBX SDK Program 中文 (二)

來源:互聯網
上載者:User

這是Autodesk FBX SDK學習筆記第二篇,以下部分漢字翻譯自Autodesk FBX SDK Program,翻譯人:有道翻譯。

上一篇講了一些FBX SDK的基本操作,建立FbxManager這些,也寫了我們第一個FBX SDK 的例子。

今天是FBX SDK指南的第二篇,建立一個FBX檔案轉換器,有什麼用?

把FBX轉換成DAE、Obj這些格式啦!把FBX 二進位轉換成文本格式啦!


這一篇的知識點:

1、FbxManager的建立與銷毀

2、FbxImporter的建立與使用

3、FbxExporter的使用

4、Fbx SDK支援的檔案格式


Autodesk FBX SDK document本篇地址:

http://docs.autodesk.com/FBX/2014/ENU/FBX-SDK-Documentation/index.html

官方例子運行效果:


不知道是MFC還是.Net 。這兩個我一個都不會,所以還是寫個控制台的……



#include<fbxsdk.h>#pragma comment(lib,"libfbxsdk.lib")FbxManager *g_pFbxManager=NULL;/*** 初始化FbxSDK ***/bool InitializeFbxSDK(){bool bRet=false;do{//建立FbxManagerg_pFbxManager=FbxManager::Create();//建立FbxIOSettingFbxIOSettings *pFbxIOSettings=FbxIOSettings::Create(g_pFbxManager,IOSROOT);//綁定關係g_pFbxManager->SetIOSettings(pFbxIOSettings);bRet=true;} while (0);return bRet;}/***  擷取Fbx SDK 支援讀入的格式  ***/void GetFileCanImport(){int count= g_pFbxManager->GetIOPluginRegistry()->GetReaderFormatCount();printf("支援匯入以下 %d 種檔案格式:\n",count);FbxString s;int i=0;for (int i = 0; i <count; i++){s+=g_pFbxManager->GetIOPluginRegistry()->GetReaderFormatDescription(i); //擷取描述//s+=g_pFbxManager->GetIOPluginRegistry()->GetReaderFormatExtension(i); //擷取檔案尾碼s="%d : "+s+" \n";printf(s.Buffer(),i);s.Clear();}}/***  擷取Fbx SDK 可以匯出的格式 ***/void GetFileCanExport(){int count=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatCount();printf("支援匯出以下 %d 種檔案格式:\n",count);FbxString s;int i=0;for (int i = 0; i < count; i++){s+=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatDescription(i); //擷取描述//s+=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatExtension(i);//擷取檔案尾碼s="%d : "+s+" \n";printf(s.Buffer(),i);s.Clear();}}/***  讀入一個Fbx檔案到FbxScene  ***/bool ImportFbxModel(FbxScene* scene,const char* importfilename){int fileVerMajorNum,fileVerMinorNum,fileVerRevision; //檔案大版本號碼、小版本號碼,修正版本號碼int sdkVerMajorNum,sdkVerMinorNum,sdkVerRevision; //FBX SDK版本號碼int animStackCount; //動畫的數量bool bRet=false;char password[1024]; //密碼,檔案可能加密了需要輸入密碼//擷取FBX SDK的版本號碼FbxManager::GetFileFormatVersion(sdkVerMajorNum,sdkVerMinorNum,sdkVerRevision);//建立FbxImporterFbxImporter *pFbxImporter=FbxImporter::Create(g_pFbxManager,importfilename);//初始化FbxImporterconst bool importret= pFbxImporter->Initialize(importfilename,-1,g_pFbxManager->GetIOSettings());//擷取Fbx檔案的版本號碼pFbxImporter->GetFileVersion(fileVerMajorNum,fileVerMinorNum,fileVerRevision);if(!importret) //匯入出錯{FbxString errorStr=pFbxImporter->GetStatus().GetErrorString();printf("\nFbxImporter初始化失敗,錯誤原因:%s\n",errorStr.Buffer());if(pFbxImporter->GetStatus().GetCode() == FbxStatus::eInvalidFileVersion) //如果是檔案版本不正確{printf("\n FBX SDK 版本:%d.%d.%d\n",sdkVerMajorNum,sdkVerMinorNum,sdkVerRevision);printf("\nFBX 檔案版本 :%d.%d.%d\n",fileVerMajorNum,fileVerMinorNum,fileVerRevision);}return false;}printf("\n ***** 匯入檔案成功****** \n");printf("\n FBX SDK 版本:%d.%d.%d \n",sdkVerMajorNum,sdkVerMinorNum,sdkVerRevision);if(pFbxImporter->IsFBX() ) //如果匯入的檔案是FBX格式{printf("\n FBX 檔案版本 :%d.%d.%d \n",fileVerMajorNum,fileVerMinorNum,fileVerRevision);//在FBX檔案中,一個Scene中可能有一個或多個 "animation stack",一個"animation stack"裡面存放一個動畫資料,如果想擷取"animation stack"的資訊,不必要載入全部的Sceneprintf("\n Animation stack 資訊:\n");animStackCount=pFbxImporter->GetAnimStackCount();printf("數量:%d\n ",animStackCount);printf("名稱:%s\n ",pFbxImporter->GetActiveAnimStackName());for (int i = 0; i < animStackCount; i++){FbxTakeInfo *pFbxTakeInfo=pFbxImporter->GetTakeInfo(i);printf("Animation Stack %d\n",i);printf("Name: %s\n",pFbxTakeInfo->mName.Buffer());printf("Description: %s\n",pFbxTakeInfo->mDescription.Buffer());printf("Import Name: %s\n",pFbxTakeInfo->mImportName.Buffer()); //匯入進來的名字printf("Import State: %s\n",pFbxTakeInfo->mSelect ?"true":"false");}//匯入內容設定,預設匯入所有內容g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_MATERIAL,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_TEXTURE,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_LINK,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_SHAPE,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_GOBO,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_ANIMATION,true);g_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_GLOBAL_SETTINGS,true);}//如果匯入的檔案不是FBX格式,那就沒有上面的邏輯//匯入Fbx到情境bRet =  pFbxImporter->Import(scene);//如果檔案匯入出錯,並且是返回錯誤Code是密碼錯誤if(bRet==false && pFbxImporter->GetStatus().GetCode()==FbxStatus::ePasswordError){printf("請輸入密碼:\n");password[0]='\0';FBXSDK_CRT_SECURE_NO_WARNING_BEGIN//這個宏是用來關閉4996警告。類似scanf strcpy strcat在vs下都會有警告scanf("%s",password);FBXSDK_CRT_SECURE_NO_WARNING_ENDFbxString passwdStr(password);g_pFbxManager->GetIOSettings()->SetStringProp(IMP_FBX_PASSWORD,passwdStr);//對FbxIOSetting設定StringProp,即字串屬性,Prop這裡指propertyg_pFbxManager->GetIOSettings()->SetBoolProp(IMP_FBX_PASSWORD_ENABLE,true);//設定Bool屬性,是否使用密碼bRet=pFbxImporter->Import(scene); //輸入密碼後重新Importif(bRet==false && pFbxImporter->GetStatus().GetCode()==FbxStatus::ePasswordError){printf("檔案匯入錯誤,密碼錯誤!");}}pFbxImporter->Destroy();return bRet;}/***  匯出FbxScene到模型檔案  ***/bool ExportFbxSceneToModel(FbxScene* scene,const char* exportfilename,int exportformat,bool pexportmedia){bool bRet=false;//建立FbxExporterFbxExporter *pFbxExport = FbxExporter::Create(g_pFbxManager,"");if(exportformat<0 || exportformat>=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatCount()){//如果選擇匯出的檔案格式不支援printf(" 不支援匯出該種格式!!  \n");return false;exportformat=g_pFbxManager->GetIOPluginRegistry()->GetNativeWriterFormat();printf("  嘗試預設的格式(FBX)匯出:%d  ",exportformat);if(!pexportmedia) //如果不匯出多媒體{int formatcount=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatCount();//嘗試匯出FBX的 ascii檔案,即能看到內容能夠的for (int i = 0; i < formatcount; i++){if(g_pFbxManager->GetIOPluginRegistry()->WriterIsFBX(i)){FbxString desStr=g_pFbxManager->GetIOPluginRegistry()->GetWriterFormatDescription(i);if(desStr.Find("ascii")>=0){exportformat=i;break;}}}}}//選擇匯出格式正確的話就沒有上面的邏輯if(!pFbxExport->Initialize(exportfilename,-1,g_pFbxManager->GetIOSettings())){printf("FbxExport->Initialize Faild \n");printf("FbxExport 初始化失敗原因:%s",pFbxExport->GetStatus().GetErrorString());return false;}if(g_pFbxManager->GetIOPluginRegistry()->WriterIsFBX(exportformat)){g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_MATERIAL,true);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_TEXTURE,true);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_EMBEDDED,pexportmedia);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_SHAPE,true);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_GOBO,true);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_ANIMATION,true);g_pFbxManager->GetIOSettings()->SetBoolProp(EXP_FBX_GLOBAL_SETTINGS,true);}bRet=pFbxExport->Export(scene);pFbxExport->Destroy();return bRet;}/***  轉換一個模型檔案  ***/void ConvertModelFile(const char *importfilename,const char *exportfilename,int writefileformat){printf("匯入檔案路徑:%s\n 匯出檔案路徑:%s \n 匯出檔案格式:%d\n",importfilename,exportfilename,writefileformat);//建立FbxScene,名字就叫做寶箱吧 BaoxiangFbxScene *pFbxScene=FbxScene::Create(g_pFbxManager,"Baoxiang");printf("\n  ******   轉換開始  ******  \n");bool b=ImportFbxModel(pFbxScene,importfilename);if(b){printf("\n**  模型檔案載入成功  ****\n");}else{printf("\n**  模型檔案載入失敗  ****\n");pFbxScene->Destroy();return;}printf("\n**  開始匯出 ****\n");b=ExportFbxSceneToModel(pFbxScene,exportfilename,writefileformat,false);if(b){printf("\n**  匯出模型檔案成功  ****\n");}else{printf("\n**  匯出模型檔案失敗  ****\n");pFbxScene->Destroy();return;}}int main(int argc,char **argv){InitializeFbxSDK();GetFileCanImport();GetFileCanExport();char importfilename[1024];int exportformat=0;char exportfilename[1024];printf("\n請輸入匯入檔案路徑:");scanf("%s",importfilename);printf("\n請輸入匯出格式:");scanf("%d",&exportformat);printf("\n請輸入匯出檔案路徑:");scanf("%s",exportfilename);ConvertModelFile(importfilename,exportfilename,exportformat);system("pause");return 0;}


項目打包下載:

http://code.taobao.org/svn/xgameengine/trunk/OtherProject/FbxDemo


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.