Autodesk fbx SDK Program (2)

Source: Internet
Author: User

This is the second learning Note of the Autodesk fbx SDK. The following Chinese characters are translated from the Autodesk fbx SDK program.

In the previous article, we talked about some basic operations of the fbx SDK, creating fbxmanager, and writing our first fbx SDK example.

Today is the second part of the fbx SDK guide. How can I create an fbx file converter?

Convert fbx to the Dae and OBJ formats! Convert fbx binary to text format!


Knowledge points of this article:

1. fbxmanager creation and destruction

2. Create and use fbximporter

3. Use of fbxexporter

4. file formats supported by fbx SDK


Autodesk fbx SDK document address:

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

Official example running effect:


I don't know whether it is MFC or. NET. I don't know either of them, so I still write a console ......

# Include <fbxsdk. h> # pragma comment (Lib, "libfbxsdk. lib ") fbxmanager * g_pfbxmanager = NULL;/*** initialize fbxsdk ***/bool initializefbxsdk () {bool Bret = false; do {// create fbxmanagerg_pfbxmanager = fbxmanager :: create (); // create authorization * pfbxiosettings = fbxiosettings: Create (g_pfbxmanager, iosroot); // bind the relationship g_pfbxmanager-> setiosettings (pfbxiosettings); Bret = true ;} while (0); Return Bret;}/*** get the format that fbx SDK supports reading ***/V Oid getfilecanimport () {int COUNT = g_pfbxmanager-> getiopluginregistry ()-> getreaderformatcount (); printf ("the following % d file formats can be imported: \ n", count ); fbxstring s; int I = 0; For (INT I = 0; I <count; I ++) {S + = g_pfbxmanager-> getiopluginregistry ()-> getreaderformatdescription (I ); // obtain the description // s + = g_pfbxmanager-> getiopluginregistry ()-> getreaderformatextension (I); // obtain the file suffix S = "% d: "+ S +" \ n "; printf (S. buffer (), I); S. clear () ;}/ *** get fbx SDK export format: ***/void getfilecanexport () {int COUNT = g_pfbxmanager-> getiopluginregistry ()-> getwriterformatcount (); printf ("supports exporting the following % d file formats: \ n", count); fbxstring s; int I = 0; For (INT I = 0; I <count; I ++) {S + = g_pfbxmanager-> getiopluginregistry ()-> getwriterformatdescription (I); // get the description // s + = g_pfbxmanager-> getiopluginregistry () -> getwriterformatextension (I); // obtain the file suffix S = "% d:" + S + "\ n"; printf (S. buffer (), I); S. CLE AR () ;}}/*** read an fbx file to fbxscene ***/bool importfbxmodel (fbxscene * scene, const char * importfilename) {int filevermajornum, fileverminornum, fileverrevision; // file version number and minor version number, corrected version number: int sdkvermajornum, sdkverminornum, sdkverrevision; // fbx SDK version number: int animstackcount; // Number of animations: bool Bret = false; char password [1024]; // password. The file may be encrypted. enter a password // obtain the fbx SDK version number fbxmanager: getfileformatversion (sdkvermajornum, sdkverminornum, sdkverr Evision); // create a condition * pfbximporter = fbximporter: Create (g_pfbxmanager, importfilename); // initialize required bool importret = pfbximporter-> initialize (importfilename,-1, g_pfbxmanager-> getiosettings (); // obtain the fbx file version pfbximporter-> getfileversion (filevermajornum, fileverminornum, fileverrevision); If (! Importret) // import error {fbxstring errorstr = pfbximporter-> getstatus (). geterrorstring (); printf ("\ nfbximporter initialization failed, error cause: % s \ n", errorstr. buffer (); If (pfbximporter-> getstatus (). getcode () = fbxstatus: einvalidfileversion) // if the file version is incorrect {printf ("\ n fbx SDK version: % d. % d. % d \ n ", sdkvermajornum, sdkverminornum, sdkverrevision); printf (" \ nfbx file version: % d. % d. % d \ n ", filevermajornum, fileverminornum, fileverrevision);} return false;} p Rintf ("\ n ****** imported file ******* \ n"); printf ("\ n fbx SDK version: % d. % d. % d \ n ", sdkvermajornum, sdkverminornum, sdkverrevision); If (pfbximporter-> isfbx ()) // If the imported file is in fbx format {printf ("\ n fbx file version: % d. % d. % d \ n ", filevermajornum, fileverminornum, fileverrevision); // In the fbx file, one or more" animation stacks "in a scene may exist ", an "animation stack" stores an animation data. If you want to obtain "animation stack" information, you do not need to load all sceneprintf ("\ n animation stack information: \ n "); animstackc Ount = pfbximporter-> getanimstackcount (); printf ("Quantity: % d \ n", animstackcount); printf ("Name: % 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 (); // The imported name printf ("import State: % s \ n", pfbxtakeinfo-> mselect? "True": "false");} // sets the imported content. By default, all content is imported into g_pfbxmanager-> getiosettings ()-> setboolprop (imp_fbx_material, true ); g_pfbxmanager-> getiosettings ()-> setboolprop (enabled, 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);} // If the imported file is not in fbx format, then there is no above logic // import fbx to the scenario Bret = pfbximporter-> Import (scene); // If the File Import fails, the returned error code is a Password error if (Bret = false & pfbximporter-> getstatus (). getcode () = fbxstatus: epassworderror) {printf ("Enter Password: \ n"); password [0] = '\ 0 '; fbxsdk_crt_secure_no_warning_begin // This macro is used to disable the 4996 warning. Similar to scanf strcpy strcat, scanf ("% s", password), passwdstr (password), g_pfbxmanager-> getiosettings ()-> setstringprop (imp_fbx_password, passwdstr); // set stringprop for fbxiosetting, that is, string attribute. Prop indicates propertyg_pfbxmanager-> getiosettings ()-> setboolprop (imp_fbx_password_enable, true); // set the bool attribute, use password Bret = pfbximporter-> Import (scene); // enter the password and re-importif (Bret = false & pfbxim Porter-> getstatus (). getcode () = fbxstatus: epassworderror) {printf ("File Import error, incorrect password! ") ;}} Pfbximporter-> destroy (); Return Bret;}/*** export fbxscene to model file ***/bool exportfbxscenetomodel (fbxscene * scene, const char * exportfilename, int exportformat, bool pexportmedia) {bool Bret = false; // create fbxexporterfbxexporter * pfbxexport = fbxexporter: Create (g_pfbxmanager ,""); if (exportformat <0 | exportformat> = g_pfbxmanager-> getiopluginregistry ()-> getwriterformatcount ()) {// if you select the exported file format, printf is not supported ("this format cannot be exported !! \ N "); Return false; exportformat = g_pfbxmanager-> getiopluginregistry ()-> getnativewriterformat (); printf (" try to export the default format (fbx): % d ", exportformat); If (! Pexportmedia) // if you do not export multimedia {int formatcount = g_pfbxmanager-> getiopluginregistry ()-> getwriterformatcount (); // try to export the fbx ASCII file, you can see the available 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 the export format is correct, the preceding logic if (! Pfbxexport-> initialize (exportfilename,-1, g_pfbxmanager-> getiosettings () {printf ("fbxexport-> initialize faild \ n"); printf ("fbxexport initialization failure cause: % s ", pfbxexport-> getstatus (). geterrorstring (); Return false;} If (g_pfbxmanager-> getiopluginregistry ()-> writerisfbx (exportformat) {g_pfbxmanager-> getiosettings ()-> setboolprop (accept, true ); g_pfbxmanager-> getiosettings ()-> setboolprop (exp_fbx_texture, true); g_pfbxmanager-> getiosettings ()-> setboolprop (batch, pexportmedia); g_pfbxmanager-> getiosettings () -> setboolprop (exp_fbx_shape, true); g_pfbxmanager-> getiosettings ()-> setboolprop (partial, true); g_pfbxmanager-> getiosettings ()-> setboolprop (partial, true ); g_pfbxmanager-> getiosettings ()-> setboolprop (exp_fbx_global_settings, true);} BRET = pfbxexport-> Export (scene); pfbxexport-> destroy (); Return Bret ;} /*** convert a model file ***/void convertmodelfile (const char * importfilename, const char * exportfilename, int writefileformat) {printf ("Import file path: % s \ n export file path: % s \ n Export File Format: % d \ n ", importfilename, exportfilename, writefileformat); // create fbxscene, the name is baoxiangfbxscene * pfbxscene = fbxscene: Create (g_pfbxmanager, "baoxiang "); printf ("\ n ****** conversion start ******* \ n"); bool B = importfbxmodel (pfbxscene, importfilename); If (B) {printf ("\ n ** model file loaded successfully ***** \ n ");} else {printf ("\ n ** model file loading failed ***** \ n"); pfbxscene-> destroy (); return ;} printf ("\ n ** start to export *** \ n"); B = exportfbxscenetomodel (pfbxscene, exportfilename, writefileformat, false); If (B) {printf ("\ n ** model file exported successfully ***** \ n ");} else {printf ("\ n ** failed to export model file ***** \ n"); pfbxscene-> destroy (); Return ;}} int main (INT argc, char ** argv) {initializefbxsdk (); getfilecanimport (); getfilecanexport (); char importfilename [1024]; int exportformat = 0; char exportfilename [1024]; printf ("\ n enter the import file path:"); scanf ("% s", importfilename); printf ("\ n enter the export format :"); scanf ("% d", & exportformat); printf ("\ n enter the export file path:"); scanf ("% s", exportfilename); convertmodelfile (importfilename, exportfilename, exportformat); System ("pause"); Return 0 ;}


Package and download the project:

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.