Simplified FBX import pipeline. Resources are now immediately stored in their associated manager classes. You can access the imported resources through KRContext::getResources

This commit is contained in:
2013-05-06 14:06:29 -07:00
parent 8be1aff1ba
commit 9e605863ea
6 changed files with 70 additions and 131 deletions

View File

@@ -160,7 +160,7 @@ std::vector<KRResource *> KRContext::getResources()
unordered_map<std::string, unordered_map<std::string, KRUnknown *> > unknowns = m_pUnknownManager->getUnknowns();
for(unordered_map<std::string, unordered_map<std::string, KRUnknown *> >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) {
for(unordered_map<std::string, KRUnknown *>::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr++) {
for(unordered_map<std::string, KRUnknown *>::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) {
resources.push_back((*itr2).second);
}
}
@@ -202,6 +202,14 @@ void KRContext::loadResource(const std::string &file_name, KRDataBlock *data) {
m_pSoundManager->load(name.c_str(), extension, data);
} else if(extension.compare("aac") == 0) {
m_pSoundManager->load(name.c_str(), extension, data);
} else if(extension.compare("obj") == 0) {
KRResource::LoadObj(*this, file_name);
#if TARGET_OS_MAC
} else if(extension.compare("fbx") == 0) {
KRResource::LoadFbx(*this, file_name);
} else if(extension.compare("blend") == 0) {
KRResource::LoadBlenderScene(*this, file_name);
#endif
} else {
m_pUnknownManager->load(name, extension, data);
}

View File

@@ -39,15 +39,14 @@ bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pF
KRAnimation *LoadAnimation(KRContext &context, FbxAnimStack* pAnimStack);
KRAnimationCurve *LoadAnimationCurve(KRContext &context, FbxAnimCurve* pAnimCurve);
KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLayer);
void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
//void BakeNode(KFbxNode* pNode);
void LoadMaterial(KRContext &context, std::vector<KRResource *> &resources, FbxSurfaceMaterial *pMaterial);
void LoadMesh(KRContext &context, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxMesh* pSourceMesh);
KRNode *LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
KRNode *LoadLight(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode);
KRNode *LoadSkeleton(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode);
KRNode *LoadCamera(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode);
void LoadLOD(KRContext &context, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, FbxLODGroup* pSourceLodGroup);
void LoadMaterial(KRContext &context, FbxSurfaceMaterial *pMaterial);
void LoadMesh(KRContext &context, FbxGeometryConverter *pGeometryConverter, KFbxMesh* pSourceMesh);
KRNode *LoadMesh(KRNode *parent_node, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode);
KRNode *LoadLight(KRNode *parent_node, KFbxNode* pNode);
KRNode *LoadSkeleton(KRNode *parent_node, KFbxNode* pNode);
KRNode *LoadCamera(KRNode *parent_node, KFbxNode* pNode);
std::string GetFbxObjectName(FbxObject *obj);
const float KRAKEN_FBX_ANIMATION_FRAMERATE = 30.0f; // FINDME - This should be configurable
@@ -67,12 +66,10 @@ std::string GetFbxObjectName(FbxObject *obj)
return st.str();
}
std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::string& path)
void KRResource::LoadFbx(KRContext &context, const std::string& path)
{
std::vector<KRResource *> resources;
KRScene *pScene = new KRScene(context, KRResource::GetFileBase(path));
resources.push_back(pScene);
context.getSceneManager()->add(pScene);
KFbxSdkManager* lSdkManager = NULL;
KFbxScene* pFbxScene = NULL;
@@ -105,7 +102,6 @@ std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::str
KRAnimation *new_animation = LoadAnimation(context, animation);
if(new_animation) {
context.getAnimationManager()->addAnimation(new_animation);
resources.push_back(new_animation);
}
}
@@ -118,7 +114,6 @@ std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::str
KRAnimationCurve *new_curve = LoadAnimationCurve(context, curve);
if(new_curve) {
context.getAnimationCurveManager()->addAnimationCurve(new_curve);
resources.push_back(new_curve);
}
}
@@ -128,7 +123,7 @@ std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::str
for(int i=0; i < material_count; i++) {
FbxSurfaceMaterial *material = pFbxScene->GetSrcObject<FbxSurfaceMaterial>(i);
printf(" Material %i of %i: %s\n", i+1, material_count, material->GetName());
LoadMaterial(context, resources, material);
LoadMaterial(context, material);
}
// ----====---- Import Meshes ----====----
@@ -138,7 +133,7 @@ std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::str
FbxMesh *mesh = pFbxScene->GetSrcObject<FbxMesh>(i);
printf(" Mesh %i of %i: %s\n", i+1, mesh_count, mesh->GetNode()->GetName());
LoadMesh(context, resources, pGeometryConverter, mesh);
LoadMesh(context, pGeometryConverter, mesh);
}
// ----====---- Import Textures ----====----
@@ -158,61 +153,16 @@ std::vector<KRResource *> KRResource::LoadFbx(KRContext &context, const std::str
{
for(int i = 0; i < pNode->GetChildCount(); i++)
{
LoadNode(pFbxScene, pScene->getRootNode(), resources, pGeometryConverter, pNode->GetChild(i));
LoadNode(pFbxScene, pScene->getRootNode(), pGeometryConverter, pNode->GetChild(i));
}
}
std::vector<KRResource *> resources2 = context.getResources();
resources.insert(resources.begin(), resources2.begin(), resources2.end());
DestroySdkObjects(lSdkManager);
// Compress textures to PVR format
context.getTextureManager()->compress(); // TODO, HACK, FINDME - This should be configurable and exposed through the World Builder GUI
std::string base_name = KRResource::GetFileBase(path);
std::vector<KRResource *> output_resources;
// KRBundle *main_bundle = new KRBundle(context, base_name);
KRBundle *texture_bundle = new KRBundle(context, base_name + "_textures");
KRBundle *animation_bundle = new KRBundle(context, base_name + "_animations");
KRBundle *material_bundle = new KRBundle(context, base_name + "_materials");
KRBundle *meshes_bundle = new KRBundle(context, base_name + "_meshes");
for(std::vector<KRResource *>::iterator resource_itr=resources.begin(); resource_itr != resources.end(); resource_itr++) {
KRResource *resource = *resource_itr;
if(dynamic_cast<KRTexture *>(resource) != NULL) {
texture_bundle->append(*resource);
} else if(dynamic_cast<KRAnimation *>(resource) != NULL) {
animation_bundle->append(*resource);
} else if(dynamic_cast<KRAnimationCurve *>(resource) != NULL) {
animation_bundle->append(*resource);
} else if(dynamic_cast<KRMaterial *>(resource) != NULL) {
material_bundle->append(*resource);
} else if(dynamic_cast<KRMesh *>(resource) != NULL) {
meshes_bundle->append(*resource);
} else {
output_resources.push_back(resource);
// main_bundle->append(*resource);
}
}
output_resources.push_back(texture_bundle);
output_resources.push_back(animation_bundle);
output_resources.push_back(material_bundle);
output_resources.push_back(meshes_bundle);
// main_bundle->append(texture_bundle);
// main_bundle->append(animation_bundle);
// main_bundle->append(material_bundle);
// main_bundle->append(meshes_bundle);
// output_resources.push_back(main_bundle);
return output_resources;
}
void InitializeSdkObjects(KFbxSdkManager*& pSdkManager, KFbxScene*& pScene)
{
// The first thing to do is to create the FBX SDK manager which is the
@@ -585,7 +535,7 @@ KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLaye
// }
//}
void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
KFbxVector4 lTmpVector;
pNode->UpdatePropertiesFromPivotsAndLimits();
@@ -793,7 +743,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
new_node->setUseWorldUnits(use_world_space_units);
parent_node->addChild(new_node);
LoadNode(pFbxScene, new_node, resources, pGeometryConverter, pNode->GetChild(i));
LoadNode(pFbxScene, new_node, pGeometryConverter, pNode->GetChild(i));
if(i == 0) {
// Calculate reference point using the bounding box center from the highest quality LOD level
@@ -806,16 +756,16 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRNode *new_node = NULL;
switch(attribute_type) {
case KFbxNodeAttribute::eMesh:
new_node = LoadMesh(parent_node, resources, pGeometryConverter, pNode);
new_node = LoadMesh(parent_node, pGeometryConverter, pNode);
break;
case KFbxNodeAttribute::eLight:
new_node = LoadLight(parent_node, resources, pNode);
new_node = LoadLight(parent_node, pNode);
break;
case KFbxNodeAttribute::eSkeleton:
new_node = LoadSkeleton(parent_node, resources, pNode);
new_node = LoadSkeleton(parent_node, pNode);
break;
case KFbxNodeAttribute::eCamera:
new_node = LoadCamera(parent_node, resources, pNode);
new_node = LoadCamera(parent_node, pNode);
break;
default:
{
@@ -877,17 +827,13 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
// Load child nodes
for(int i = 0; i < pNode->GetChildCount(); i++)
{
LoadNode(pFbxScene, new_node, resources, pGeometryConverter, pNode->GetChild(i));
LoadNode(pFbxScene, new_node, pGeometryConverter, pNode->GetChild(i));
}
}
}
}
void LoadLOD(KRContext &context, std::vector<KRResource *> &resources, FbxLODGroup* pSourceLodGroup) {
}
void LoadMaterial(KRContext &context, std::vector<KRResource *> &resources, FbxSurfaceMaterial *pMaterial) {
void LoadMaterial(KRContext &context, FbxSurfaceMaterial *pMaterial) {
//printf(" %s: %i - %i\n", pMaterial->GetName(), mat_vertex_start, mat_vertex_count + mat_vertex_start - 1);
// ----====---- Output Material File ----====----
@@ -1039,22 +985,6 @@ void LoadMaterial(KRContext &context, std::vector<KRResource *> &resources, FbxS
}
}
/*
bool bFound = false;
for(vector<KRResource *>::iterator resource_itr = resources.begin(); resource_itr != resources.end(); resource_itr++) {
KRResource *pResource = (*resource_itr);
if(pResource->getName() == new_material->getName() && pResource->getExtension() == new_material->getExtension()) {
bFound = true;
}
}
if(bFound) {
delete new_material;
} else {
resources.push_back(new_material);
}
*/
// Only save unique materials
KRMaterial *found_material = context.getMaterialManager()->getMaterial(new_material->getName());
if(found_material == NULL) {
@@ -1064,7 +994,7 @@ void LoadMaterial(KRContext &context, std::vector<KRResource *> &resources, FbxS
}
}
void LoadMesh(KRContext &context, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxMesh* pSourceMesh) {
void LoadMesh(KRContext &context, FbxGeometryConverter *pGeometryConverter, KFbxMesh* pSourceMesh) {
KFbxMesh* pMesh = pGeometryConverter->TriangulateMesh(pSourceMesh);
int control_point_count = pMesh->GetControlPointsCount();
@@ -1314,7 +1244,7 @@ void LoadMesh(KRContext &context, std::vector<KRResource *> &resources, FbxGeome
context.getModelManager()->addModel(new_mesh);
}
KRNode *LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
KRNode *LoadMesh(KRNode *parent_node, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) {
std::string name = GetFbxObjectName(pNode);
KFbxMesh* pSourceMesh = (KFbxMesh*) pNode->GetNodeAttribute();
@@ -1342,13 +1272,13 @@ KRNode *LoadMesh(KRNode *parent_node, std::vector<KRResource *> &resources, FbxG
}
KRNode *LoadSkeleton(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode) {
KRNode *LoadSkeleton(KRNode *parent_node, KFbxNode* pNode) {
std::string name = GetFbxObjectName(pNode);
KRBone *new_bone = new KRBone(parent_node->getScene(), name.c_str());
return new_bone;
}
KRNode *LoadCamera(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode) {
KRNode *LoadCamera(KRNode *parent_node, KFbxNode* pNode) {
FbxCamera *camera = (FbxCamera *)pNode->GetNodeAttribute();
const char *szName = pNode->GetName();
@@ -1356,7 +1286,7 @@ KRNode *LoadCamera(KRNode *parent_node, std::vector<KRResource *> &resources, KF
return new_camera;
}
KRNode *LoadLight(KRNode *parent_node, std::vector<KRResource *> &resources, KFbxNode* pNode) {
KRNode *LoadLight(KRNode *parent_node, KFbxNode* pNode) {
const GLfloat PI = 3.14159265;
const GLfloat d2r = PI * 2 / 360;

View File

@@ -55,23 +55,6 @@ std::string KRResource::GetFilePath(const std::string& name)
}
}
std::vector<KRResource *> KRResource::Load(KRContext &context, const std::string& path)
{
std::vector<KRResource *> resources;
std::string extension = GetFileExtension(path);
if(extension.compare("obj") == 0) {
return LoadObj(context, path);
#if TARGET_OS_MAC
} else if(extension.compare("fbx") == 0) {
return LoadFbx(context, path);
} else if(extension.compare("blend") == 0) {
return LoadBlenderScene(context, path);
#endif
}
return resources;
}
bool KRResource::save(const std::string& path)
{
KRDataBlock data;

View File

@@ -25,9 +25,14 @@ public:
static std::string GetFileBase(const std::string& name);
static std::string GetFilePath(const std::string& name);
static std::vector<KRResource *> Load(KRContext &context, const std::string& path);
virtual ~KRResource();
static std::vector<KRResource *> LoadObj(KRContext &context, const std::string& path);
#if TARGET_OS_MAC
static void LoadFbx(KRContext &context, const std::string& path);
static std::vector<KRResource *> LoadBlenderScene(KRContext &context, const std::string& path);
#endif
protected:
KRResource(KRContext &context, std::string name);
@@ -36,11 +41,7 @@ protected:
private:
std::string m_name;
static std::vector<KRResource *> LoadObj(KRContext &context, const std::string& path);
#if TARGET_OS_MAC
static std::vector<KRResource *> LoadFbx(KRContext &context, const std::string& path);
static std::vector<KRResource *> LoadBlenderScene(KRContext &context, const std::string& path);
#endif
};
#endif

View File

@@ -42,14 +42,30 @@ KRSceneManager::~KRSceneManager() {
m_scenes.empty();
}
KRScene *KRSceneManager::loadScene(const char *szName, KRDataBlock *data) {
KRScene *pScene = KRScene::Load(*m_pContext, szName, data);
m_scenes[szName] = pScene;
KRScene *KRSceneManager::loadScene(const std::string &name, KRDataBlock *data) {
std::string lowerName = name;
std::transform(lowerName.begin(), lowerName.end(),
lowerName.begin(), ::tolower);
KRScene *pScene = KRScene::Load(*m_pContext, name, data);
m_scenes[lowerName] = pScene;
return pScene;
}
KRScene *KRSceneManager::getScene(const char *szName) {
return m_scenes[szName];
void KRSceneManager::add(KRScene *scene)
{
std::string lowerName = scene->getName();
std::transform(lowerName.begin(), lowerName.end(),
lowerName.begin(), ::tolower);
m_scenes[lowerName] = scene;
}
KRScene *KRSceneManager::getScene(const std::string &name) {
std::string lowerName = name;
std::transform(lowerName.begin(), lowerName.end(),
lowerName.begin(), ::tolower);
return m_scenes[lowerName];
}
KRScene *KRSceneManager::getFirstScene() {
@@ -61,7 +77,7 @@ KRScene *KRSceneManager::getFirstScene() {
}
}
unordered_map<std::string, KRScene *> KRSceneManager::getScenes() {
unordered_map<std::string, KRScene *> &KRSceneManager::getScenes() {
return m_scenes;
}

View File

@@ -44,13 +44,14 @@ public:
KRSceneManager(KRContext &context);
virtual ~KRSceneManager();
KRScene *loadScene(const char *szName, KRDataBlock *data);
void add(KRScene *scene);
KRScene *loadScene(const std::string &name, KRDataBlock *data);
KRScene *getScene(const char *szName);
KRScene *getScene(const std::string &name);
KRScene *getFirstScene();
std::vector<std::string> getSceneNames();
unordered_map<std::string, KRScene *> getScenes();
unordered_map<std::string, KRScene *> &getScenes();
private: