Repository navigation
Expand file tree
/
Copy pathgltfPackage.cpp
More file actions
256 lines (224 loc) · 9.53 KB
/
Copy pathgltfPackage.cpp
File metadata and controls
256 lines (224 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// Copyright (c) Autodesk, Inc. All rights reserved
//
// C++ glTF FBX converter
// by Cyrille Fauvel - Autodesk Developer Network (ADN)
// January 2015
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
#include "StdAfx.h"
#include "glTF.h"
#include "gltfPackage.h"
//-----------------------------------------------------------------------------
/*static*/ FbxAutoPtr<fbxSdkMgr> fbxSdkMgr::_singleton ;
fbxSdkMgr::fbxSdkMgr () : _sdkManager(FbxManager::Create ()) {
_ASSERTE( _sdkManager ) ;
FbxIOSettings *pIOSettings =FbxIOSettings::Create (_sdkManager, IOSROOT) ;
//pIOSettings->SetBoolProp (IMP_FBX_CONSTRAINT, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_CONSTRAINT_COUNT, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_MATERIAL, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_TEXTURE, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_LINK, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_SHAPE, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_GOBO, false) ;
//pIOSettings->SetBoolProp (IMP_FBX_ANIMATION, true) ;
//pIOSettings->SetBoolProp (IMP_FBX_GLOBAL_SETTINGS, true) ;
pIOSettings->SetBoolProp (EXP_FBX_EMBEDDED, false) ;
_sdkManager->SetIOSettings (pIOSettings) ;
// Load plug-ins from the executable directory
FbxString path =FbxGetApplicationDirectory () ;
#if defined(_WIN64) || defined(_WIN32)
FbxString extension ("dll") ;
#elif defined(__APPLE__)
FbxString extension ("dylib") ;
#else // __linux
FbxString extension ("so") ;
#endif
_sdkManager->LoadPluginsDirectory (path.Buffer ()/*, extension.Buffer ()*/) ;
_sdkManager->FillIOSettingsForReadersRegistered (*pIOSettings) ;
_sdkManager->FillIOSettingsForWritersRegistered (*pIOSettings) ;
}
fbxSdkMgr::~fbxSdkMgr () {
}
/*static*/ FbxAutoPtr<fbxSdkMgr> &fbxSdkMgr::Instance () {
if ( !fbxSdkMgr::_singleton )
fbxSdkMgr::_singleton.Reset (new fbxSdkMgr ()) ;
return (fbxSdkMgr::_singleton) ;
}
FbxSharedDestroyPtr<FbxManager> &fbxSdkMgr::fbxMgr () {
_ASSERTE( _sdkManager ) ;
return (_sdkManager) ;
}
//-----------------------------------------------------------------------------
gltfPackage::gltfPackage () : _scene(nullptr), _ioSettings ({ U("") }) {
}
gltfPackage::~gltfPackage () {
}
void gltfPackage::ioSettings (
const utility::char_t *name /*=nullptr*/,
bool angleInDegree /*=false*/,
bool invertTransparency /*=false*/,
bool defaultLighting /*=false*/,
bool copyMedia /*=false*/,
bool embedMedia /*=false*/
) {
FbxIOSettings *pIOSettings =fbxSdkMgr::Instance ()->fbxMgr ()->GetIOSettings () ;
_ioSettings._name =name == nullptr ? U("") : name ;
pIOSettings->SetBoolProp (IOSN_FBX_GLTF_ANGLEINDEGREE, angleInDegree) ;
pIOSettings->SetBoolProp (IOSN_FBX_GLTF_INVERTTRANSPARENCY, invertTransparency) ;
pIOSettings->SetBoolProp (IOSN_FBX_GLTF_DEFAULTLIGHTING, defaultLighting) ;
pIOSettings->SetBoolProp (IOSN_FBX_GLTF_COPYMEDIA, copyMedia) ;
pIOSettings->SetBoolProp (IOSN_FBX_GLTF_EMBEDMEDIA, embedMedia) ;
//fbxSdkMgr::Instance ()->fbxMgr ()->SetIOSettings (pIOSettings) ;
}
bool gltfPackage::load (const utility::string_t &fn) {
_ASSERTE( !_scene ) ;
auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ;
_scene.Reset (FbxScene::Create (pMgr, utility::conversions::to_utf8string (gltfPackage::filename (fn)).c_str ())) ;
_ASSERTE( !!_scene ) ;
bool bRet =LoadScene (fn) ;
return (bRet) ;
}
bool gltfPackage::import (const utility::string_t &fn) {
_ASSERTE( !_scene ) ;
auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ;
_scene.Reset (FbxScene::Create (pMgr, utility::conversions::to_utf8string (gltfPackage::filename (fn)).c_str ())) ;
_ASSERTE( !!_scene ) ;
bool bRet =LoadScene (fn) ;
return (bRet) ;
}
bool gltfPackage::save (const utility::string_t &outdir) {
_ASSERTE( !!_scene ) ;
bool bRet =WriteScene (outdir) ;
return (bRet) ;
}
/*static*/ utility::string_t gltfPackage::filename (const utility::string_t &path) {
utility::string_t filename ;
size_t pos =path.find_last_of (U("/\\")) ;
if ( pos != utility::string_t::npos ) {
size_t pos2 =path.find (U("."), pos) ;
if ( pos2 != utility::string_t::npos )
filename.assign (path.begin () + pos + 1, path.begin () + pos2) ;
else
filename.assign (path.begin () + pos + 1, path.end ()) ;
} else {
filename =path ;
}
return (filename) ;
}
/*static*/ utility::string_t gltfPackage::pathname (const utility::string_t &filename) {
utility::string_t path ;
size_t pos =filename.find_last_of (U ("/\\")) ;
if ( pos != utility::string_t::npos )
path.assign (filename.begin (), filename.begin () + pos + 1) ;
else
path =U("") ;
return (path) ;
}
bool gltfPackage::LoadScene (const utility::string_t &fn) {
auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ;
FbxAutoDestroyPtr<FbxImporter> pImporter (FbxImporter::Create (pMgr, "")) ;
if ( !pImporter->Initialize (utility::conversions::to_utf8string (fn).c_str (), -1, pMgr->GetIOSettings ()) )
return (false) ;
if ( pImporter->IsFBX () ) {
// From this point, it is possible to access animation stack information without
// the expense of loading the entire file.
// Set the import states. By default, the import states are always set to true.
}
bool bStatus =pImporter->Import (_scene) ;
if ( _ioSettings._name.length () )
_scene->SetName (utility::conversions::to_utf8string (_ioSettings._name).c_str ()) ;
else if ( _scene->GetName () == FbxString ("") )
//_scene->SetName ("untitled") ;
_scene->SetName (utility::conversions::to_utf8string (gltfPackage::filename (fn)).c_str ()) ;
//if ( bStatus == false && pImporter->GetStatus ().GetCode () == FbxStatus::ePasswordError ) {
//}
// Get current UpAxis of the FBX file.
// This have to be done before ConvertiAxisSystem(), cause the function will always change SceneAxisSystem to Y-up.
// First, however, if we have the ForcedFileAxis activated, we need to overwrite the global settings.
//switch ( IOS_REF.GetEnumProp (IMP_FILE_UP_AXIS, FbxMayaUtility::eUPAXIS_AUTO) ) {
// default:
// case FbxMayaUtility::eUPAXIS_AUTO:
// break ;
// case FbxMayaUtility::eUPAXIS_Y:
// _scene->GetGlobalSettings ().SetAxisSystem (FbxAxisSystem::MayaYUp) ;
// break ;
// case FbxMayaUtility::eUPAXIS_Z:
// _scene->GetGlobalSettings ().SetAxisSystem (FbxAxisSystem::MayaZUp) ;
// break ;
//}
//FbxAxisSystem sceneAxisSystem =_scene->GetGlobalSettings ().GetAxisSystem () ;
//int lSign =0 ;
//FbxAxisSystem::EUpVector upVectorFromFile =sceneAxisSystem.GetUpVector (lSign) ;
FbxAxisSystem::MayaYUp.ConvertScene (_scene) ; // We want the Y up axis for glTF
FbxSystemUnit sceneSystemUnit =_scene->GetGlobalSettings ().GetSystemUnit () ; // We want meter as default unit for gltTF
if ( sceneSystemUnit != FbxSystemUnit::m ) {
//const FbxSystemUnit::ConversionOptions conversionOptions ={
// false, // mConvertRrsNodes
// true, // mConvertAllLimits
// true, // mConvertClusters
// false, // mConvertLightIntensity
// true, // mConvertPhotometricLProperties
// false // mConvertCameraClipPlanes
//} ;
//FbxSystemUnit::m.ConvertScene (_scene, conversionOptions) ;
FbxSystemUnit::m.ConvertScene (_scene) ;
}
FbxGeometryConverter converter (fbxSdkMgr::Instance ()->fbxMgr ()) ;
converter.Triangulate (_scene, true) ; // glTF supports triangles only
converter.SplitMeshesPerMaterial (_scene, true) ; // Split meshes per material, so we only have one material per mesh (VBO support)
// Set the current peripheral to be the NULL so FBX geometries that have been imported can be flushed
_scene->SetPeripheral (NULL_PERIPHERAL) ;
//int nb =_scene->GetSrcObjectCount<FbxNodeAttribute> () ;
//int nbTot =5 * nb ;
//int nbRest =4 * nb ;
//int nbSteps =nbRest / 8 ;
//FbxArray<FbxNode *> pBadMeshes =RemoveBadPolygonsFromMeshes (_scene) ;
return (true) ;
}
//FbxArray<FbxNode *> RemoveBadPolygonsFromMeshes (FbxScene *pScene) {
// FbxArray<FbxNode *> pAffectedNodes ;
// FbxNode *pNode =pScene->GetRootNode () ;
// RemoveBadPolygonsFromMeshesRecursive (pNode, pAffectedNodes) ;
// return (pAffectedNodes) ;
//}
//
//FbxArray<FbxNode *> RemoveBadPolygonsFromMeshesRecursive (FbxNode *pNode, FbxArray<FbxNode *> &pAffectedNodes) {
// FbxMesh *pMesh =pNode->GetMesh () ;
// if ( pMesh ) {
// if ( pMesh->RemoveBadPolygons () > 0 )
// pAffectedNodes.Add (pNode) ;
// }
// for ( int nChildIndex =0 ; nChildIndex < pNode->GetChildCount () ; nChildIndex++ ) {
// FbxNode *pChild =pNode->GetChild (nChildIndex) ;
// RemoveBadPolygonsFromMeshesRecursive (pChild, pAffectedNodes) ;
// }
// return (pAffectedNodes) ;
//}
bool gltfPackage::WriteScene (const utility::string_t &outdir) {
auto pMgr =fbxSdkMgr::Instance ()->fbxMgr () ;
int iFormat =pMgr->GetIOPluginRegistry ()->FindWriterIDByExtension ("gltf") ;
if ( iFormat == -1 )
return (false) ;
FbxAutoDestroyPtr<FbxExporter> pExporter (FbxExporter::Create (pMgr, "")) ;
utility::string_t newFn =outdir + utility::conversions::to_string_t (_scene->GetName ()) + U(".gltf") ;
bool bRet =pExporter->Initialize (utility::conversions::to_utf8string (newFn).c_str (), iFormat, pMgr->GetIOSettings ()) ;
_ASSERTE( bRet ) ;
if ( !bRet )
return (false) ;
// The next line will call the exporter
bRet =pExporter->Export (_scene) ;
return (bRet) ;
}