1+ #pragma once
2+
3+ class qFBXModel
4+ {
5+ public:
6+ fbxsdk::FbxScene* mScene = 0 ;
7+
8+ qFBXModel (fbxsdk::FbxManager* mgr)
9+ {
10+ mScene = fbxsdk::FbxScene::Create (mgr, " " );
11+
12+ mScene ->GetGlobalSettings ().SetSystemUnit (FbxSystemUnit::m);
13+ }
14+
15+ ~qFBXModel ()
16+ {
17+ mScene ->Destroy (1 );
18+ }
19+
20+ bool Export (fbxsdk::FbxManager* mgr, const char * filename)
21+ {
22+ fbxsdk::FbxAxisSystem targetSystem (fbxsdk::FbxAxisSystem::EUpVector::eYAxis, fbxsdk::FbxAxisSystem::EFrontVector::eParityOdd, fbxsdk::FbxAxisSystem::eRightHanded);
23+ targetSystem.DeepConvertScene (mScene );
24+
25+ FbxExporter* exporter = FbxExporter::Create (mgr, " " );
26+
27+ bool exported = exporter->Initialize (filename, -1 , mgr->GetIOSettings ());
28+ if (!exporter->Export (mScene )) {
29+ exported = 0 ;
30+ }
31+
32+ exporter->Destroy ();
33+ return exported;
34+ }
35+
36+ fbxsdk::FbxMesh* CreateMesh (const char * name, int num_cp)
37+ {
38+ auto node = fbxsdk::FbxNode::Create (mScene , name);
39+ auto mesh = fbxsdk::FbxMesh::Create (mScene , name);
40+
41+ node->SetNodeAttribute (mesh);
42+ mesh->InitControlPoints (num_cp);
43+
44+ mScene ->GetRootNode ()->AddChild (node);
45+
46+ return mesh;
47+ }
48+
49+ fbxsdk::FbxSurfacePhong* CreateDiffuseMaterial (const char * name, const char * filename, const char * uvset)
50+ {
51+ auto fileTexture = fbxsdk::FbxFileTexture::Create (mScene , name);
52+ fileTexture->SetFileName (filename);
53+ fileTexture->SetTextureUse (fbxsdk::FbxTexture::eStandard);
54+ fileTexture->SetMappingType (fbxsdk::FbxTexture::eUV);
55+ fileTexture->SetMaterialUse (fbxsdk::FbxFileTexture::eModelMaterial);
56+ fileTexture->SetSwapUV (0 );
57+ fileTexture->UVSet .Set (uvset);
58+
59+ auto material = fbxsdk::FbxSurfacePhong::Create (mScene , name);
60+ material->Diffuse .Set (fbxsdk::FbxDouble3 (1 , 1 , 1 ));
61+ material->DiffuseFactor .Set (1.0 );
62+ material->Diffuse .ConnectSrcObject (fileTexture);
63+
64+ return material;
65+ }
66+
67+ fbxsdk::FbxSurfacePhong* CreateBumpMaterial (const char * name, const char * filename, const char * uvset)
68+ {
69+ auto fileTexture = fbxsdk::FbxFileTexture::Create (mScene , name);
70+ fileTexture->SetFileName (filename);
71+ fileTexture->SetTextureUse (fbxsdk::FbxTexture::eBumpNormalMap);
72+ fileTexture->SetMappingType (fbxsdk::FbxTexture::eUV);
73+ fileTexture->SetMaterialUse (fbxsdk::FbxFileTexture::eModelMaterial);
74+ fileTexture->SetSwapUV (0 );
75+ fileTexture->UVSet .Set (uvset);
76+
77+ auto layeredTexture = fbxsdk::FbxLayeredTexture::Create (mScene , " " );
78+ layeredTexture->SetTextureUse (fbxsdk::FbxLayeredTexture::eBumpNormalMap);
79+ layeredTexture->ConnectSrcObject (fileTexture);
80+
81+ auto material = fbxsdk::FbxSurfacePhong::Create (mScene , name);
82+ material->Bump .Set (fbxsdk::FbxDouble3 (1 , 1 , 1 ));
83+ material->Bump .ConnectSrcObject (fileTexture);
84+
85+ return material;
86+ }
87+
88+ fbxsdk::FbxSkin* CreateSkin (const char * name)
89+ {
90+ return fbxsdk::FbxSkin::Create (mScene , name);
91+ }
92+
93+ fbxsdk::FbxNode* CreateLimbNode (const char * name, const fbxsdk::FbxAMatrix& matrix)
94+ {
95+ auto skel = fbxsdk::FbxSkeleton::Create (mScene , name);
96+ skel->SetSkeletonType (fbxsdk::FbxSkeleton::eLimbNode);
97+
98+ auto node = fbxsdk::FbxNode::Create (mScene , name);
99+ node->SetNodeAttribute (skel);
100+
101+ node->LclTranslation .Set (matrix.GetT ());
102+ node->LclRotation .Set (matrix.GetR ());
103+ node->LclScaling .Set (matrix.GetS ());
104+
105+ return node;
106+ }
107+
108+ fbxsdk::FbxCluster* CreateCluster (fbxsdk::FbxSkin* skin, fbxsdk::FbxNode* node, const fbxsdk::FbxAMatrix& matrix)
109+ {
110+ auto cluster = fbxsdk::FbxCluster::Create (mScene , node->GetName ());
111+ cluster->SetLink (node);
112+ cluster->SetLinkMode (fbxsdk::FbxCluster::eNormalize);
113+
114+ cluster->SetTransformMatrix (matrix);
115+ cluster->SetTransformLinkMatrix (node->EvaluateGlobalTransform ());
116+
117+ skin->AddCluster (cluster);
118+
119+ return cluster;
120+ }
121+ };
0 commit comments