From fa1d0d277ca51503cae07007bb3921e340b30472 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 11 Mar 2022 10:17:00 +0200 Subject: [PATCH] animation keyframe channels now use animatable names --- CMakeLists.txt | 4 ++-- animation.cpp | 4 ++-- animation.h | 4 ++-- animationChannelTest.cpp | 2 +- animationController.cpp | 2 +- driver.cpp | 8 ++++++-- driver.h | 4 +++- driverSample.cpp | 10 +++++----- keyframeChannel.cpp | 2 +- keyframeChannel.h | 4 ++-- lightSample.cpp | 4 ++-- model.cpp | 3 --- textSample.cpp | 6 +++--- xmlModelReader.cpp | 12 ++---------- 14 files changed, 32 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cb5d69..81b64a6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ set(MATH quaternion.cpp vector.cpp matrix.cpp ray.cpp) set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp) set(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp) set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp) -set(ASSET_READERS abstractAssetReader.cpp imageReader.cpp fontReader.cpp modelReader.cpp assimpModelReader.cpp vbModelReader.cpp xmlModelReader.cpp) +set(ASSET_READERS abstractAssetReader.cpp imageReader.cpp fontReader.cpp modelReader.cpp assimpModelReader.cpp xmlModelReader.cpp) set(ASSET_MANAGER assetManager.cpp textAsset.h imageAsset.h modelAsset.h fontAsset.h) set(LIB_SRC ${RENDER} ${MATH} ${UTILS} ${GUI} ${ARMATURE} ${ASSET_MANAGER} ${ASSET_READERS} ${ANIM}) @@ -77,7 +77,7 @@ if(BUILD_TESTS) set(ARMATURE_TEST boneTest.cpp ikSolverTest.cpp) set(MODEL_TEST vbModelReaderTest.cpp) set(ANIMATION_TEST animationChannelTest.cpp) - set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${MODEL_TEST} ${ANIMATION_TEST}) + set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST}) add_executable(vb01Tests ${TEST_SRC}) target_link_libraries(vb01Tests ${LIB_NAME} ${DEPS} cppunit) diff --git a/animation.cpp b/animation.cpp index 331ae02..cc43540 100644 --- a/animation.cpp +++ b/animation.cpp @@ -7,7 +7,7 @@ namespace vb01{ this->name = name; } - KeyframeChannel* Animation::getKeyframeChannel(Animatable *animatable, KeyframeChannelType type){ + KeyframeChannel* Animation::getKeyframeChannel(string animatable, KeyframeChannelType type){ KeyframeChannel *k = nullptr; for(KeyframeChannel &channel : keyframeChannels) @@ -19,7 +19,7 @@ namespace vb01{ return k; } - vector Animation::getKeyframeChannelsByAnimatable(Animatable *animatable){ + vector Animation::getKeyframeChannelsByAnimatable(string animatable){ vector channels; for(KeyframeChannel channel : keyframeChannels) diff --git a/animation.h b/animation.h index 8bea814..cb9641c 100644 --- a/animation.h +++ b/animation.h @@ -13,8 +13,8 @@ namespace vb01{ public: Animation(std::string); ~Animation(){} - KeyframeChannel* getKeyframeChannel(Animatable*, KeyframeChannel::Type); - std::vector getKeyframeChannelsByAnimatable(Animatable*); + KeyframeChannel* getKeyframeChannel(std::string, KeyframeChannel::Type); + std::vector getKeyframeChannelsByAnimatable(std::string); inline void addKeyframeChannels(std::vector keyframeChannels){this->keyframeChannels.assign(keyframeChannels.begin(), keyframeChannels.end());} inline void addKeyframeChannel(KeyframeChannel channel){keyframeChannels.push_back(channel);} inline std::string getName(){return name;} diff --git a/animationChannelTest.cpp b/animationChannelTest.cpp index b32db68..60a423d 100644 --- a/animationChannelTest.cpp +++ b/animationChannelTest.cpp @@ -22,7 +22,7 @@ namespace vb01{ KeyframeChannel kc; kc.type = KeyframeChannel::Type::POS_X; kc.keyframes = vector({k1, k2}); - kc.animatable = bone; + kc.animatable = bone->getName(); Animation *anim = new Animation("anim"); anim->addKeyframeChannel(kc); diff --git a/animationController.cpp b/animationController.cpp index 2778836..285f7ac 100644 --- a/animationController.cpp +++ b/animationController.cpp @@ -25,7 +25,7 @@ namespace vb01{ Animation *animation = getAnimation(channel->getAnimationName()); for(Animatable *animatable : channel->getAnimatables()){ - vector keyframeChannels = animation->getKeyframeChannelsByAnimatable(animatable); + vector keyframeChannels = animation->getKeyframeChannelsByAnimatable(animatable->getName()); for(KeyframeChannel keyframeChannel : keyframeChannels){ int currentFrame = channel->getCurrentFrame(); diff --git a/driver.cpp b/driver.cpp index d616df6..156d1bc 100644 --- a/driver.cpp +++ b/driver.cpp @@ -6,7 +6,8 @@ using namespace std; namespace vb01{ - Driver::Driver(KeyframeChannel keyframeChannel, VariableType type){ + Driver::Driver(Animatable *animatable, KeyframeChannel keyframeChannel, VariableType type){ + this->animatable = animatable; this->keyframeChannel = keyframeChannel; this->type = type; } @@ -18,16 +19,18 @@ namespace vb01{ float nextFrame = nextKeyframe.frame; float ratio = (float)(driverValue - pastFrame) / (nextFrame - pastFrame); + if(ratio < 0) ratio = 0; Keyframe::Interpolation interp = pastKeyframe.interpolation; float value = KeyframeChannel::interpolate(pastKeyframe, nextKeyframe, ratio); - keyframeChannel.animatable->animate(value, keyframeChannel); + animatable->animate(value, keyframeChannel); } Driver::VariableType Driver::getDriverVariableType(string typeString){ VariableType type; + if(typeString == "LOC_X") type = Driver::POS_X; else if(typeString == "LOC_Y") @@ -48,6 +51,7 @@ namespace vb01{ type = Driver::SCALE_Y; else if(typeString == "SCALE_Z") type = Driver::SCALE_Z; + return type; } } diff --git a/driver.h b/driver.h index 474d14b..67f6064 100644 --- a/driver.h +++ b/driver.h @@ -23,14 +23,16 @@ namespace vb01{ SCALE_Z }; - Driver(KeyframeChannel, VariableType); + Driver(Animatable*, KeyframeChannel, VariableType); void drive(float); static VariableType getDriverVariableType(std::string); inline VariableType getType(){return type;} inline KeyframeChannel& getKeyframeChannel(){return keyframeChannel;} + inline Animatable* getAnimatable(){return animatable;} private: VariableType type; KeyframeChannel keyframeChannel; + Animatable *animatable = nullptr; }; } diff --git a/driverSample.cpp b/driverSample.cpp index 37d721d..82a2fef 100644 --- a/driverSample.cpp +++ b/driverSample.cpp @@ -82,7 +82,7 @@ int main(){ * and frame number. */ KeyframeChannel kcL; - kcL.animatable = leftMat->getUniform("diffuseColor"); + kcL.animatable = leftMat->getUniform("diffuseColor")->getName(); kcL.type = KeyframeChannel::UNIFORM_1; kcL.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, 1, 0), @@ -90,7 +90,7 @@ int main(){ }); KeyframeChannel kcR; - kcR.animatable = rightNode; + kcR.animatable = rightNode->getName(); kcR.type = KeyframeChannel::SCALE_Z; kcR.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, 1, 0), @@ -98,7 +98,7 @@ int main(){ }); KeyframeChannel kcD; - kcD.animatable = driver; + kcD.animatable = driver->getName(); kcD.type = KeyframeChannel::POS_Y; kcD.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, 0, 0), @@ -110,8 +110,8 @@ int main(){ * A Driver class is given a transform component, e.g, y coordinate of the position * and uses it to play the Keyframe. */ - driver->addDriver(new Driver(kcL, Driver::POS_Y)); - driver->addDriver(new Driver(kcR, Driver::POS_Y)); + driver->addDriver(new Driver(leftMat->getUniform("diffuseColor"), kcL, Driver::POS_Y)); + driver->addDriver(new Driver(rightNode, kcR, Driver::POS_Y)); Animation *anim = new Animation("anim"); anim->addKeyframeChannel(kcD); diff --git a/keyframeChannel.cpp b/keyframeChannel.cpp index b0ef84d..f4bd586 100644 --- a/keyframeChannel.cpp +++ b/keyframeChannel.cpp @@ -121,7 +121,7 @@ namespace vb01{ return keyframe; } - KeyframeChannel KeyframeChannel::createKeyframeChannel(KeyframeChannelType type, Animatable *animatable, vector keyframes){ + KeyframeChannel KeyframeChannel::createKeyframeChannel(KeyframeChannelType type, string animatable, vector keyframes){ KeyframeChannel keyframeChannel; keyframeChannel.type = type; keyframeChannel.animatable = animatable; diff --git a/keyframeChannel.h b/keyframeChannel.h index 47b046e..87cd055 100644 --- a/keyframeChannel.h +++ b/keyframeChannel.h @@ -64,7 +64,7 @@ namespace vb01{ }; Type type; - Animatable *animatable = nullptr; + std::string animatable = ""; std::vector keyframes; static float interpolateBezier(std::vector, float); @@ -73,7 +73,7 @@ namespace vb01{ static float interpolate(Keyframe, Keyframe, float); static Keyframe findKeyframe(float, KeyframeChannel, bool); static Keyframe createKeyframe(Keyframe::Interpolation, float, float, float = 0, float = 0, float = 0, float = 0); - static KeyframeChannel createKeyframeChannel(KeyframeChannel::Type, Animatable*, std::vector); + static KeyframeChannel createKeyframeChannel(KeyframeChannel::Type, std::string, std::vector); }; typedef KeyframeChannel::Type KeyframeChannelType; diff --git a/lightSample.cpp b/lightSample.cpp index 2ed6cc5..b9ae296 100644 --- a/lightSample.cpp +++ b/lightSample.cpp @@ -87,7 +87,7 @@ int main(){ * and frame number. */ KeyframeChannel kcA; - kcA.animatable = light; + kcA.animatable = light->getName(); kcA.type = KeyframeChannel::SPOTLIGHT_OUTER_ANGLE; kcA.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, .1, 1), @@ -96,7 +96,7 @@ int main(){ }); KeyframeChannel kcB; - kcA.animatable = light; + kcA.animatable = light->getName(); kcA.type = KeyframeChannel::SPOTLIGHT_INNER_ANGLE; kcA.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, .1, 1), diff --git a/model.cpp b/model.cpp index f380288..68decfe 100755 --- a/model.cpp +++ b/model.cpp @@ -7,7 +7,6 @@ #include "root.h" #include "skeleton.h" #include "animation.h" -#include "vbModelReader.h" #include "xmlModelReader.h" #include "assimpModelReader.h" @@ -28,8 +27,6 @@ namespace vb01{ if(extension == "xml") modelReader = new XmlModelReader(this, path); - else if(extension == "vb") - modelReader = new VbModelReader(this, path); else modelReader = new AssimpModelReader(this, path); } diff --git a/textSample.cpp b/textSample.cpp index 82e0e94..ef63d35 100644 --- a/textSample.cpp +++ b/textSample.cpp @@ -83,17 +83,17 @@ int main(){ * and frame number. */ KeyframeChannel kcA; - kcA.animatable = texture; + kcA.animatable = texture->getName(); kcA.type = KeyframeChannel::TEXTURE_FRAME_A; kcA.keyframes = vector({KeyframeChannel::createKeyframe(Keyframe::CONSTANT, 0, 1)}); KeyframeChannel kcB; - kcB.animatable = texture; + kcB.animatable = texture->getName(); kcB.type = KeyframeChannel::TEXTURE_FRAME_B; kcB.keyframes = vector({KeyframeChannel::createKeyframe(Keyframe::CONSTANT, 1, 1)}); KeyframeChannel kcC; - kcC.animatable = texture; + kcC.animatable = texture->getName(); kcC.type = KeyframeChannel::TEXTURE_MIX_RATIO; kcC.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, 0, 1), diff --git a/xmlModelReader.cpp b/xmlModelReader.cpp index b87ff20..de119f2 100644 --- a/xmlModelReader.cpp +++ b/xmlModelReader.cpp @@ -176,16 +176,8 @@ namespace vb01{ string typeStr = channelEl->Attribute("type"); KeyframeChannel::Type type = KeyframeChannel::getKeyframeChannelType(typeStr); - Animatable *animatable; - string name = channelEl->Attribute("name"); - - for(Animatable *an : animatables) - if(an->getName() == name){ - animatable = an; - break; - } - - KeyframeChannel channel = KeyframeChannel::createKeyframeChannel(type, animatable, keyframes); + string channelName = channelEl->Attribute("name"); + KeyframeChannel channel = KeyframeChannel::createKeyframeChannel(type, channelName, keyframes); animation->addKeyframeChannel(channel); }