From 9e605863eab05601bea3b4de797d169dfff0c5a8 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Mon, 6 May 2013 14:06:29 -0700 Subject: [PATCH] Simplified FBX import pipeline. Resources are now immediately stored in their associated manager classes. You can access the imported resources through KRContext::getResources --- KREngine/kraken/KRContext.cpp | 10 ++- KREngine/kraken/KRResource+fbx.cpp | 124 +++++++---------------------- KREngine/kraken/KRResource.cpp | 17 ---- KREngine/kraken/KRResource.h | 15 ++-- KREngine/kraken/KRSceneManager.cpp | 28 +++++-- KREngine/kraken/KRSceneManager.h | 7 +- 6 files changed, 70 insertions(+), 131 deletions(-) diff --git a/KREngine/kraken/KRContext.cpp b/KREngine/kraken/KRContext.cpp index ae815e1..52cfa7b 100644 --- a/KREngine/kraken/KRContext.cpp +++ b/KREngine/kraken/KRContext.cpp @@ -160,7 +160,7 @@ std::vector KRContext::getResources() unordered_map > unknowns = m_pUnknownManager->getUnknowns(); for(unordered_map >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) { - for(unordered_map::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr++) { + for(unordered_map::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); } diff --git a/KREngine/kraken/KRResource+fbx.cpp b/KREngine/kraken/KRResource+fbx.cpp index aa45d63..0038ad2 100644 --- a/KREngine/kraken/KRResource+fbx.cpp +++ b/KREngine/kraken/KRResource+fbx.cpp @@ -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 &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 &resources, FbxSurfaceMaterial *pMaterial); -void LoadMesh(KRContext &context, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxMesh* pSourceMesh); -KRNode *LoadMesh(KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); -KRNode *LoadLight(KRNode *parent_node, std::vector &resources, KFbxNode* pNode); -KRNode *LoadSkeleton(KRNode *parent_node, std::vector &resources, KFbxNode* pNode); -KRNode *LoadCamera(KRNode *parent_node, std::vector &resources, KFbxNode* pNode); -void LoadLOD(KRContext &context, std::vector &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::LoadFbx(KRContext &context, const std::string& path) +void KRResource::LoadFbx(KRContext &context, const std::string& path) { - - std::vector 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::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::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::LoadFbx(KRContext &context, const std::str for(int i=0; i < material_count; i++) { FbxSurfaceMaterial *material = pFbxScene->GetSrcObject(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::LoadFbx(KRContext &context, const std::str FbxMesh *mesh = pFbxScene->GetSrcObject(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::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 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 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::iterator resource_itr=resources.begin(); resource_itr != resources.end(); resource_itr++) { - KRResource *resource = *resource_itr; - if(dynamic_cast(resource) != NULL) { - texture_bundle->append(*resource); - } else if(dynamic_cast(resource) != NULL) { - animation_bundle->append(*resource); - } else if(dynamic_cast(resource) != NULL) { - animation_bundle->append(*resource); - } else if(dynamic_cast(resource) != NULL) { - material_bundle->append(*resource); - } else if(dynamic_cast(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 &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::vectorsetUseWorldUnits(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::vectorGetChildCount(); i++) { - LoadNode(pFbxScene, new_node, resources, pGeometryConverter, pNode->GetChild(i)); + LoadNode(pFbxScene, new_node, pGeometryConverter, pNode->GetChild(i)); } } } } -void LoadLOD(KRContext &context, std::vector &resources, FbxLODGroup* pSourceLodGroup) { - -} - -void LoadMaterial(KRContext &context, std::vector &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 &resources, FbxS } } - /* - bool bFound = false; - for(vector::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 &resources, FbxS } } -void LoadMesh(KRContext &context, std::vector &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 &resources, FbxGeome context.getModelManager()->addModel(new_mesh); } -KRNode *LoadMesh(KRNode *parent_node, std::vector &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 &resources, FbxG } -KRNode *LoadSkeleton(KRNode *parent_node, std::vector &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 &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 &resources, KF return new_camera; } -KRNode *LoadLight(KRNode *parent_node, std::vector &resources, KFbxNode* pNode) { +KRNode *LoadLight(KRNode *parent_node, KFbxNode* pNode) { const GLfloat PI = 3.14159265; const GLfloat d2r = PI * 2 / 360; diff --git a/KREngine/kraken/KRResource.cpp b/KREngine/kraken/KRResource.cpp index 4a18d9a..b72db4e 100644 --- a/KREngine/kraken/KRResource.cpp +++ b/KREngine/kraken/KRResource.cpp @@ -55,23 +55,6 @@ std::string KRResource::GetFilePath(const std::string& name) } } -std::vector KRResource::Load(KRContext &context, const std::string& path) -{ - std::vector 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; diff --git a/KREngine/kraken/KRResource.h b/KREngine/kraken/KRResource.h index 1401097..b6a1c5f 100644 --- a/KREngine/kraken/KRResource.h +++ b/KREngine/kraken/KRResource.h @@ -25,9 +25,14 @@ public: static std::string GetFileBase(const std::string& name); static std::string GetFilePath(const std::string& name); - static std::vector Load(KRContext &context, const std::string& path); - virtual ~KRResource(); + + + static std::vector LoadObj(KRContext &context, const std::string& path); +#if TARGET_OS_MAC + static void LoadFbx(KRContext &context, const std::string& path); + static std::vector 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 LoadObj(KRContext &context, const std::string& path); -#if TARGET_OS_MAC - static std::vector LoadFbx(KRContext &context, const std::string& path); - static std::vector LoadBlenderScene(KRContext &context, const std::string& path); -#endif + }; #endif diff --git a/KREngine/kraken/KRSceneManager.cpp b/KREngine/kraken/KRSceneManager.cpp index dbeb56d..ff0ded7 100644 --- a/KREngine/kraken/KRSceneManager.cpp +++ b/KREngine/kraken/KRSceneManager.cpp @@ -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 KRSceneManager::getScenes() { +unordered_map &KRSceneManager::getScenes() { return m_scenes; } diff --git a/KREngine/kraken/KRSceneManager.h b/KREngine/kraken/KRSceneManager.h index a812836..5bbdf8a 100644 --- a/KREngine/kraken/KRSceneManager.h +++ b/KREngine/kraken/KRSceneManager.h @@ -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 getSceneNames(); - unordered_map getScenes(); + unordered_map &getScenes(); private: