diff --git a/KREngine/KREngine/Classes/KRAnimation.cpp b/KREngine/KREngine/Classes/KRAnimation.cpp index f034740..1d88e8b 100644 --- a/KREngine/KREngine/Classes/KRAnimation.cpp +++ b/KREngine/KREngine/Classes/KRAnimation.cpp @@ -89,3 +89,13 @@ KRAnimation *KRAnimation::Load(KRContext &context, const std::string &name, KRDa return new_animation; } + +std::map &KRAnimation::getLayers() +{ + return m_layers; +} + +KRAnimationLayer *KRAnimation::getLayer(const char *szName) +{ + return m_layers[szName]; +} diff --git a/KREngine/KREngine/Classes/KRAnimation.h b/KREngine/KREngine/Classes/KRAnimation.h index 0b5b200..778d154 100644 --- a/KREngine/KREngine/Classes/KRAnimation.h +++ b/KREngine/KREngine/Classes/KRAnimation.h @@ -51,6 +51,9 @@ public: static KRAnimation *Load(KRContext &context, const std::string &name, KRDataBlock *data); void addLayer(KRAnimationLayer *layer); + std::map &getLayers(); + KRAnimationLayer *getLayer(const char *szName); + private: std::map m_layers; diff --git a/KREngine/KREngine/Classes/KRAnimationAttribute.cpp b/KREngine/KREngine/Classes/KRAnimationAttribute.cpp index 7135a8c..8a03351 100644 --- a/KREngine/KREngine/Classes/KRAnimationAttribute.cpp +++ b/KREngine/KREngine/Classes/KRAnimationAttribute.cpp @@ -47,10 +47,48 @@ tinyxml2::XMLElement *KRAnimationAttribute::saveXML( tinyxml2::XMLNode *parent) tinyxml2::XMLDocument *doc = parent->GetDocument(); tinyxml2::XMLElement *e = doc->NewElement("attribute"); tinyxml2::XMLNode *n = parent->InsertEndChild(e); - + e->SetAttribute("curve", m_curve_name.c_str()); + e->SetAttribute("target", m_target_name.c_str()); + e->SetAttribute("attribute", m_target_attribute_name.c_str()); return e; } void KRAnimationAttribute::loadXML(tinyxml2::XMLElement *e) { + m_curve_name = e->Attribute("curve"); + m_target_name = e->Attribute("target"); + m_target_attribute_name = e->Attribute("attribute"); } + +std::string KRAnimationAttribute::getTargetName() const +{ + return m_target_name; +} + +void KRAnimationAttribute::setTargetName(const std::string &target_name) +{ + m_target_name = target_name; +} + +std::string KRAnimationAttribute::getTargetAttributeName() const +{ + return m_target_attribute_name; +} + +void KRAnimationAttribute::setTargetAttributeName(const std::string &target_attribute_name) +{ + m_target_attribute_name = target_attribute_name; +} + + +std::string KRAnimationAttribute::getCurveName() const +{ + return m_curve_name; +} + +void KRAnimationAttribute::setCurveName(const std::string &curve_name) +{ + m_curve_name = curve_name; +} + + diff --git a/KREngine/KREngine/Classes/KRAnimationAttribute.h b/KREngine/KREngine/Classes/KRAnimationAttribute.h index b29c5d2..2eb0f60 100644 --- a/KREngine/KREngine/Classes/KRAnimationAttribute.h +++ b/KREngine/KREngine/Classes/KRAnimationAttribute.h @@ -44,11 +44,19 @@ public: tinyxml2::XMLElement *saveXML( tinyxml2::XMLNode *parent); void loadXML(tinyxml2::XMLElement *e); + std::string getCurveName() const; + void setCurveName(const std::string &curve_name); + std::string getTargetName() const; void setTargetName(const std::string &target_name); + std::string getTargetAttributeName() const; + void setTargetAttributeName(const std::string &target_attribute_name); + private: std::string m_target_name; + std::string m_curve_name; + std::string m_target_attribute_name; }; #endif diff --git a/KREngine/KREngine/Classes/KRAnimationManager.cpp b/KREngine/KREngine/Classes/KRAnimationManager.cpp index df01a74..d9db90a 100644 --- a/KREngine/KREngine/Classes/KRAnimationManager.cpp +++ b/KREngine/KREngine/Classes/KRAnimationManager.cpp @@ -59,7 +59,12 @@ KRAnimation *KRAnimationManager::getAnimation(const char *szName) { return m_animations[szName]; } -std::map KRAnimationManager::getAnimations() { +std::map &KRAnimationManager::getAnimations() { return m_animations; } +void KRAnimationManager::addAnimation(KRAnimation *new_animation) +{ + m_animations[new_animation->getName()] = new_animation; +} + diff --git a/KREngine/KREngine/Classes/KRAnimationManager.h b/KREngine/KREngine/Classes/KRAnimationManager.h index ba1449d..09bee38 100644 --- a/KREngine/KREngine/Classes/KRAnimationManager.h +++ b/KREngine/KREngine/Classes/KRAnimationManager.h @@ -50,7 +50,8 @@ public: KRAnimation *loadAnimation(const char *szName, KRDataBlock *data); KRAnimation *getAnimation(const char *szName); - std::map getAnimations(); + void addAnimation(KRAnimation *new_animation); + std::map &getAnimations(); void startFrame(); diff --git a/KREngine/KREngine/Classes/KRResource+fbx.cpp b/KREngine/KREngine/Classes/KRResource+fbx.cpp index 12a4234..0dd5e46 100644 --- a/KREngine/KREngine/Classes/KRResource+fbx.cpp +++ b/KREngine/KREngine/Classes/KRResource+fbx.cpp @@ -42,7 +42,7 @@ void DestroySdkObjects(KFbxSdkManager* pSdkManager); bool LoadScene(KFbxSdkManager* pSdkManager, KFbxDocument* pScene, const char* pFilename); KRAnimation *LoadAnimation(KRContext &context, FbxAnimStack* pAnimStack); KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLayer); -void LoadNode(KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); +void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); //void BakeNode(KFbxNode* pNode); KRNode *LoadMesh(KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode); KRNode *LoadLight(KRNode *parent_node, std::vector &resources, KFbxNode* pNode); @@ -83,6 +83,8 @@ std::vector KRResource::LoadFbx(KRContext &context, const std::str int animation_count = pFbxScene->GetSrcObjectCount(FBX_TYPE(FbxAnimStack)); for(int i = 0; i < animation_count; i++) { // FbxAnimStack* pAnimStack = FbxCast(pFbxScene->GetSrcObject(FBX_TYPE(FbxAnimStack), i)); + KRAnimation *new_animation = LoadAnimation(context, pFbxScene->GetSrcObject(i)); + context.getAnimationManager()->addAnimation(new_animation); resources.push_back(LoadAnimation(context, pFbxScene->GetSrcObject(i))); } @@ -91,7 +93,7 @@ std::vector KRResource::LoadFbx(KRContext &context, const std::str { for(int i = 0; i < pNode->GetChildCount(); i++) { - LoadNode(pScene->getRootNode(), resources, pGeometryConverter, pNode->GetChild(i)); + LoadNode(pFbxScene, pScene->getRootNode(), resources, pGeometryConverter, pNode->GetChild(i)); } } @@ -445,12 +447,104 @@ KRAnimationLayer *LoadAnimationLayer(KRContext &context, FbxAnimLayer *pAnimLaye // } //} -void LoadNode(KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { +void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector &resources, FbxGeometryConverter *pGeometryConverter, KFbxNode* pNode) { KFbxVector4 lTmpVector; pNode->UpdatePropertiesFromPivotsAndLimits(); // Transform = T * Roff * Rp * Rpre * R * Rpost * inverse(Rp) * Soff * Sp * S * inverse(Sp) - + // Import animated properties + int animation_count = pFbxScene->GetSrcObjectCount(); + for(int i = 0; i < animation_count; i++) { + // FbxAnimStack* pAnimStack = FbxCast(pFbxScene->GetSrcObject(FBX_TYPE(FbxAnimStack), i)); + FbxAnimStack* pAnimStack = pFbxScene->GetSrcObject(i); + KRAnimation *pAnimation = parent_node->getContext().getAnimationManager()->getAnimation(pAnimStack->GetName()); + int cLayers = pAnimStack->GetMemberCount(); + for(int iLayer=0; iLayer < cLayers; iLayer++) { + FbxAnimLayer *pFbxAnimLayer = pAnimStack->GetMember(iLayer); + KRAnimationLayer *pAnimationLayer = pAnimation->getLayer(pFbxAnimLayer->GetName()); + + FbxAnimCurve *pAnimCurve = pNode->LclRotation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("rotate_x"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclRotation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("rotate_y"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclRotation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("rotate_z"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclTranslation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("translate_x"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclTranslation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("translate_y"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclTranslation.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("translate_z"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclScaling.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("scale_x"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclScaling.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("scale_y"); + pAnimationLayer->addAttribute(new_attribute); + } + + pAnimCurve = pNode->LclScaling.GetCurve(pFbxAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if(pAnimCurve) { + KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); + new_attribute->setCurveName(pAnimCurve->GetName()); + new_attribute->setTargetName(pNode->GetName()); + new_attribute->setTargetAttributeName("scale_z"); + pAnimationLayer->addAttribute(new_attribute); + } + } + } fbxDouble3 local_rotation = pNode->LclRotation.Get(); // pNode->GetGeometricRotation(KFbxNode::eSourcePivot); fbxDouble3 local_translation = pNode->LclTranslation.Get(); // pNode->GetGeometricTranslation(KFbxNode::eSourcePivot); @@ -481,18 +575,9 @@ void LoadNode(KRNode *parent_node, std::vector &resources, FbxGeom assert(geometric_translation == lZero); assert(geometric_scaling == lOne); assert(rotation_order == eEulerXYZ); -// -// bool rotation_active = pNode->RotationActive.Get(); - -// KRVector3 node_translation = KRVector3(local_translation[0] + rotation_offset[0] + rotation_pivot[0], local_translation[1] + rotation_offset[1] + rotation_pivot[1], local_translation[2] + rotation_offset[2] + rotation_pivot[2]); // T * Roff * Rp + KRVector3 node_translation = KRVector3(local_translation[0], local_translation[1], local_translation[2]); // T * Roff * Rp - -// KRVector3 node_rotation = KRQuaternion(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI).euler(); // R KRVector3 node_rotation = KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI; -// if(rotation_active) { -// node_rotation = (KRQuaternion(KRVector3(post_rotation[0], post_rotation[1], post_rotation[2]) / 180.0 * M_PI) * KRQuaternion(KRVector3(local_rotation[0], local_rotation[1], local_rotation[2]) / 180.0 * M_PI) * KRQuaternion(KRVector3(pre_rotation[0], pre_rotation[1], pre_rotation[2]) / 180.0 * M_PI)).euler(); // Rpre * R * Rpost -// } - KRVector3 node_scale = KRVector3(local_scale[0], local_scale[1], local_scale[2]); // printf(" Local Translation: %f %f %f\n", local_translation[0], local_translation[1], local_translation[2]); @@ -529,7 +614,7 @@ void LoadNode(KRNode *parent_node, std::vector &resources, FbxGeom // Load child nodes for(int i = 0; i < pNode->GetChildCount(); i++) { - LoadNode(new_node, resources, pGeometryConverter, pNode->GetChild(i)); + LoadNode(pFbxScene, new_node, resources, pGeometryConverter, pNode->GetChild(i)); } } }