-
Notifications
You must be signed in to change notification settings - Fork 0
/
FbxFileReader.cpp
99 lines (79 loc) · 2.95 KB
/
FbxFileReader.cpp
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
// This file is part of FbxReader.
//
// FbxReader is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FbxReader is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with FbxReader. If not, see <http://www.gnu.org/licenses/>.
#include "FbxFileReader.h"
#include <algorithm>
using namespace std;
unique_ptr<FbxManager> FbxFileReader::s_pSdkManager = nullptr;
FbxFileReader::FbxFileReader(const string& filename) : m_pScene(nullptr)
{
// Create an SDK manager
if(!s_pSdkManager)
s_pSdkManager = unique_ptr<FbxManager>( FbxManager::Create() );
//Configure I/O settings
FbxIOSettings* pIOSettings = FbxIOSettings::Create(s_pSdkManager.get(), IOSROOT);
s_pSdkManager->SetIOSettings(pIOSettings);
// Create an importer
FbxImporter* pImporter = FbxImporter::Create(s_pSdkManager.get(), "");
if( !pImporter->Initialize(filename.c_str(), -1, pIOSettings) )
throw exception("Failed to initialize FbxImporter.");
// Add fbx data to scene using the importer
m_pScene = FbxScene::Create( s_pSdkManager.get(), filename.c_str() );
pImporter->Import(m_pScene);
// Done importing
pImporter->Destroy();
//Check if the scene contains a root node
if( !m_pScene->GetRootNode() )
throw std::exception("No RootNode could be found.");
}
FbxFileReader::~FbxFileReader(void)
{
m_pScene->Destroy();
}
//Returns the first mesh encountered in the fbx file
Mesh& FbxFileReader::GetMesh(void)
{
if(m_Meshes.empty())
ReadMeshes();
return m_Meshes[0];
}
//Returns a mesh with a specific name
Mesh& FbxFileReader::GetMesh(const std::string& nodeName)
{
if(m_Meshes.empty())
ReadMeshes();
auto it = find_if(m_Meshes.begin(), m_Meshes.end(), [&](Mesh& meshRef){
return meshRef.GetName() == nodeName;
});
if(it == m_Meshes.end())
throw exception( ("Failed to find a mesh with the name " + nodeName).c_str() );
return *it;
}
void FbxFileReader::ReadMeshes(void)
{
//Get the scene's rootnode
FbxNode* pRootNode = m_pScene->GetRootNode();
// Loop through all of the child nodes in the scene
int nrOfChildren = pRootNode->GetChildCount();
for(int i=0; i<nrOfChildren; ++i){
FbxNode* pChild = pRootNode->GetChild(i);
FbxNodeAttribute* pAttribute = pChild->GetNodeAttribute();
//If this node is a mesh, add it to our collection
if(pAttribute && pAttribute->GetAttributeType() == FbxNodeAttribute::eMesh)
m_Meshes.push_back( Mesh( pChild->GetMesh() ) );
}
//Throw when no meshes are found
if(m_Meshes.empty())
throw exception("Failed to find mesh data.");
}