Animation system in progress

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40177
This commit is contained in:
kearwood
2012-12-07 03:13:10 +00:00
parent accc9ce05a
commit 34fd415b49
12 changed files with 123 additions and 41 deletions

View File

@@ -32,6 +32,9 @@
#include "KRAnimation.h" #include "KRAnimation.h"
#include "KRAnimationManager.h" #include "KRAnimationManager.h"
#include "KRContext.h" #include "KRContext.h"
#include "KRNode.h"
#include "KRAnimationCurve.h"
#import "tinyxml2.h" #import "tinyxml2.h"
KRAnimation::KRAnimation(KRContext &context, std::string name) : KRResource(context, name) KRAnimation::KRAnimation(KRContext &context, std::string name) : KRResource(context, name)
@@ -130,9 +133,27 @@ void KRAnimation::update(float deltaTime)
if(m_playing) { if(m_playing) {
m_local_time += deltaTime; m_local_time += deltaTime;
} }
if(m_local_time > m_duration) { while(m_local_time > m_duration) {
m_local_time -= m_duration; m_local_time -= m_duration;
} }
for(std::map<std::string, KRAnimationLayer *>::iterator layer_itr = m_layers.begin(); layer_itr != m_layers.end(); layer_itr++) {
KRAnimationLayer *layer = (*layer_itr).second;
for(std::vector<KRAnimationAttribute *>::iterator attribute_itr = layer->getAttributes().begin(); attribute_itr != layer->getAttributes().end(); attribute_itr++) {
KRAnimationAttribute *attribute = *attribute_itr;
// TODO - Currently only a single layer supported per animation -- need to either implement combining of multiple layers or ask the FBX sdk to bake all layers into one
KRAnimationCurve *curve = attribute->getCurve();
KRNode *target = attribute->getTarget();
KRNode::node_attribute_type attribute_type = attribute->getTargetAttribute();
if(curve != NULL && target != NULL) {
target->SetAttribute(attribute_type, curve->getValue(m_local_time));
}
}
}
} }
void KRAnimation::Play() void KRAnimation::Play()

View File

@@ -30,11 +30,16 @@
// //
#include "KRAnimationAttribute.h" #include "KRAnimationAttribute.h"
#include "KRContext.h"
#include "KRAnimationManager.h"
#include "KRAnimationCurveManager.h"
KRAnimationAttribute::KRAnimationAttribute(KRContext &context) : KRContextObject(context) KRAnimationAttribute::KRAnimationAttribute(KRContext &context) : KRContextObject(context)
{ {
m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE;
m_target = NULL;
m_curve = NULL;
} }
KRAnimationAttribute::~KRAnimationAttribute() KRAnimationAttribute::~KRAnimationAttribute()
@@ -89,36 +94,48 @@ tinyxml2::XMLElement *KRAnimationAttribute::saveXML( tinyxml2::XMLNode *parent)
void KRAnimationAttribute::loadXML(tinyxml2::XMLElement *e) void KRAnimationAttribute::loadXML(tinyxml2::XMLElement *e)
{ {
m_target = NULL;
m_curve = NULL;
m_curve_name = e->Attribute("curve"); m_curve_name = e->Attribute("curve");
m_target_name = e->Attribute("target"); m_target_name = e->Attribute("target");
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE;
const char *szAttribute = e->Attribute("attribute"); const char *szAttribute = e->Attribute("attribute");
if(strcmp(szAttribute, "none") == 0) { if(strcmp(szAttribute, "none") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE;
} else if(strcmp(szAttribute, "translate_x") == 0) { } else if(strcmp(szAttribute, "translate_x") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_X; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_X;
} else if(strcmp(szAttribute, "translate_y") == 0) { } else if(strcmp(szAttribute, "translate_y") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Y; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Y;
} else if(strcmp(szAttribute, "translate_z") == 0) { } else if(strcmp(szAttribute, "translate_z") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Z; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Z;
} else if(strcmp(szAttribute, "rotate_x") == 0) { } else if(strcmp(szAttribute, "rotate_x") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_X; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_X;
} else if(strcmp(szAttribute, "rotate_y") == 0) { } else if(strcmp(szAttribute, "rotate_y") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Y; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Y;
} else if(strcmp(szAttribute, "rotate_z") == 0) { } else if(strcmp(szAttribute, "rotate_z") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Z; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Z;
} else if(strcmp(szAttribute, "scale_x") == 0) { } else if(strcmp(szAttribute, "scale_x") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_X; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_X;
} else if(strcmp(szAttribute, "scale_y") == 0) { } else if(strcmp(szAttribute, "scale_y") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Y; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Y;
} else if(strcmp(szAttribute, "scale_z") == 0) { } else if(strcmp(szAttribute, "scale_z") == 0) {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Z; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Z;
} else { } else {
m_target_attribute_name = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE; m_node_attribute = KRNode::KRENGINE_NODE_ATTRIBUTE_NONE;
} }
} }
KRNode::node_attribute_type KRAnimationAttribute::getTargetAttribute() const
{
return m_node_attribute;
}
void KRAnimationAttribute::setTargetAttribute(KRNode::node_attribute_type target_attribute)
{
m_node_attribute = target_attribute;
}
std::string KRAnimationAttribute::getTargetName() const std::string KRAnimationAttribute::getTargetName() const
{ {
return m_target_name; return m_target_name;
@@ -127,19 +144,9 @@ std::string KRAnimationAttribute::getTargetName() const
void KRAnimationAttribute::setTargetName(const std::string &target_name) void KRAnimationAttribute::setTargetName(const std::string &target_name)
{ {
m_target_name = target_name; m_target_name = target_name;
m_target = NULL;
} }
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 std::string KRAnimationAttribute::getCurveName() const
{ {
return m_curve_name; return m_curve_name;
@@ -148,6 +155,23 @@ std::string KRAnimationAttribute::getCurveName() const
void KRAnimationAttribute::setCurveName(const std::string &curve_name) void KRAnimationAttribute::setCurveName(const std::string &curve_name)
{ {
m_curve_name = curve_name; m_curve_name = curve_name;
m_curve = NULL;
}
KRNode *KRAnimationAttribute::getTarget()
{
if(m_target == NULL) {
m_target = getContext().getSceneManager()->getFirstScene()->getRootNode()->findChild(m_target_name); // FINDME, HACK! - This won't work with multiple scenes in a context; we should move the animations out of KRAnimationManager and attach them to the parent nodes of the animated KRNode's
}
return m_target;
}
KRAnimationCurve *KRAnimationAttribute::getCurve()
{
if(m_curve == NULL) {
m_curve = getContext().getAnimationCurveManager()->getAnimationCurve(m_curve_name.c_str());
}
return m_curve;
} }

View File

@@ -36,6 +36,7 @@
#import "KREngine-common.h" #import "KREngine-common.h"
#import "tinyxml2.h" #import "tinyxml2.h"
#import "KRNode.h" #import "KRNode.h"
#import "KRAnimationCurve.h"
class KRAnimationAttribute : public KRContextObject { class KRAnimationAttribute : public KRContextObject {
public: public:
@@ -51,14 +52,19 @@ public:
std::string getTargetName() const; std::string getTargetName() const;
void setTargetName(const std::string &target_name); void setTargetName(const std::string &target_name);
std::string getTargetAttributeName() const; KRNode::node_attribute_type getTargetAttribute() const;
void setTargetAttributeName(const std::string &target_attribute_name); void setTargetAttribute(KRNode::node_attribute_type target_attribute);
KRNode *getTarget();
KRAnimationCurve *getCurve();
private: private:
std::string m_target_name; std::string m_target_name;
std::string m_curve_name; std::string m_curve_name;
KRNode::node_attribute_type m_node_attribute; KRNode::node_attribute_type m_node_attribute;
std::string m_target_attribute_name;
KRNode *m_target;
KRAnimationCurve *m_curve;
}; };
#endif #endif

View File

@@ -139,3 +139,10 @@ void KRAnimationCurve::setValue(int frame_number, float value)
} }
} }
float KRAnimationCurve::getValue(float local_time)
{
// TODO - Need to add interpolation for time values between frames.
// Must consider looping animations when determining which two frames to interpolate between.
return getValue((int)(local_time * getFrameRate()));
}

View File

@@ -54,6 +54,7 @@ public:
void setFrameStart(int frame_number); void setFrameStart(int frame_number);
int getFrameCount(); int getFrameCount();
void setFrameCount(int frame_count); void setFrameCount(int frame_count);
float getValue(float local_time);
float getValue(int frame_number); float getValue(int frame_number);
void setValue(int frame_number, float value); void setValue(int frame_number, float value);

View File

@@ -191,3 +191,8 @@ void KRAnimationLayer::addAttribute(KRAnimationAttribute *attribute)
{ {
m_attributes.push_back(attribute); m_attributes.push_back(attribute);
} }
std::vector<KRAnimationAttribute *> &KRAnimationLayer::getAttributes()
{
return m_attributes;
}

View File

@@ -77,6 +77,7 @@ public:
void setScaleAccumulationMode(const scale_accumulation_mode_t &scale_accumulation_mode); void setScaleAccumulationMode(const scale_accumulation_mode_t &scale_accumulation_mode);
void addAttribute(KRAnimationAttribute *attribute); void addAttribute(KRAnimationAttribute *attribute);
std::vector<KRAnimationAttribute *> &getAttributes();
private: private:
std::string m_name; std::string m_name;

View File

@@ -59,7 +59,7 @@ void KRAnimationManager::endFrame(float deltaTime)
KRAnimation *KRAnimationManager::loadAnimation(const char *szName, KRDataBlock *data) { KRAnimation *KRAnimationManager::loadAnimation(const char *szName, KRDataBlock *data) {
KRAnimation *pAnimation = KRAnimation::Load(*m_pContext, szName, data); KRAnimation *pAnimation = KRAnimation::Load(*m_pContext, szName, data);
m_animations[szName] = pAnimation; addAnimation(pAnimation);
return pAnimation; return pAnimation;
} }

View File

@@ -123,7 +123,7 @@ void KRContext::loadResource(const std::string &file_name, KRDataBlock *data) {
m_pSceneManager->loadScene(name.c_str(), data); m_pSceneManager->loadScene(name.c_str(), data);
} else if(extension.compare("kranimation") == 0) { } else if(extension.compare("kranimation") == 0) {
m_pAnimationManager->loadAnimation(name.c_str(), data); m_pAnimationManager->loadAnimation(name.c_str(), data);
} else if(extension.compare("kranimatinocurve") == 0) { } else if(extension.compare("kranimationcurve") == 0) {
m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data); m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data);
} else if(extension.compare("pvr") == 0) { } else if(extension.compare("pvr") == 0) {
m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data);

View File

@@ -272,13 +272,28 @@ void KRNode::SetAttribute(node_attribute_type attrib, float v)
setLocalScale(KRVector3(m_localScale.x, m_localScale.y, v)); setLocalScale(KRVector3(m_localScale.x, m_localScale.y, v));
break; break;
case KRENGINE_NODE_ATTRIBUTE_ROTATE_X: case KRENGINE_NODE_ATTRIBUTE_ROTATE_X:
setLocalRotation(KRVector3(v, m_localRotation.y, m_localRotation.z)); setLocalRotation(KRVector3(v / M_PI_2, m_localRotation.y, m_localRotation.z));
break; break;
case KRENGINE_NODE_ATTRIBUTE_ROTATE_Y: case KRENGINE_NODE_ATTRIBUTE_ROTATE_Y:
setLocalRotation(KRVector3(m_localRotation.x, v, m_localRotation.z)); setLocalRotation(KRVector3(m_localRotation.x, v / M_PI_2, m_localRotation.z));
break; break;
case KRENGINE_NODE_ATTRIBUTE_ROTATE_Z: case KRENGINE_NODE_ATTRIBUTE_ROTATE_Z:
setLocalRotation(KRVector3(m_localRotation.x, m_localRotation.y, v)); setLocalRotation(KRVector3(m_localRotation.x, m_localRotation.y, v / M_PI_2));
break; break;
} }
} }
KRNode *KRNode::findChild(const std::string &name)
{
if(m_name == name) {
return this;
} else {
for(std::vector<KRNode *>::iterator child_itr = m_childNodes.begin(); child_itr != m_childNodes.end(); child_itr++) {
KRNode *match = (*child_itr)->findChild(name);
if(match) return match;
}
}
return NULL;
}

View File

@@ -53,6 +53,8 @@ public:
void addChild(KRNode *child); void addChild(KRNode *child);
const std::vector<KRNode *> &getChildren(); const std::vector<KRNode *> &getChildren();
KRNode *findChild(const std::string &name);
void setLocalTranslation(const KRVector3 &v); void setLocalTranslation(const KRVector3 &v);
void setLocalScale(const KRVector3 &v); void setLocalScale(const KRVector3 &v);
void setLocalRotation(const KRVector3 &v); void setLocalRotation(const KRVector3 &v);

View File

@@ -538,7 +538,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("rotate_x"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_X);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -547,7 +547,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("rotate_y"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Y);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -556,7 +556,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("rotate_z"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_ROTATE_Z);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -565,7 +565,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("translate_x"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_X);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -574,7 +574,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("translate_y"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Y);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -583,7 +583,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("translate_z"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_TRANSLATE_Z);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -592,7 +592,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("scale_x"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_X);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -601,7 +601,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("scale_y"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Y);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
@@ -610,7 +610,7 @@ void LoadNode(KFbxScene* pFbxScene, KRNode *parent_node, std::vector<KRResource
KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext()); KRAnimationAttribute *new_attribute = new KRAnimationAttribute(parent_node->getContext());
new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve")); new_attribute->setCurveName(GetFbxObjectName(pAnimCurve, "fbx_curve"));
new_attribute->setTargetName(pNode->GetName()); new_attribute->setTargetName(pNode->GetName());
new_attribute->setTargetAttributeName("scale_z"); new_attribute->setTargetAttribute(KRNode::KRENGINE_NODE_ATTRIBUTE_SCALE_Z);
pAnimationLayer->addAttribute(new_attribute); pAnimationLayer->addAttribute(new_attribute);
} }
} }