animation channel test

This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 33d077bc94
commit b2ee272327
8 changed files with 133 additions and 12 deletions
+2 -1
View File
@@ -29,7 +29,8 @@ target_link_libraries(vb01 freetype)
set(renderTest nodeTest.cpp cameraTest.cpp)
set(armatureTest boneTest.cpp)
set(modelTest vbModelReaderTest.cpp)
set(test ${library} ${renderTest} ${armatureTest} ${modelTest})
set(animationTest animationChannelTest.cpp)
set(test ${library} ${renderTest} ${armatureTest} ${modelTest} ${animationTest})
add_executable(vb01Tests main.cpp ${test})
target_link_libraries(vb01Tests cppunit)
+6 -8
View File
@@ -14,14 +14,12 @@ namespace vb01{
void AnimationChannel::update(){
if(canUpdate()){
if(firstFrame < currentFrame && currentFrame < numFrames)
currentFrame += (forward? 1 : -1);
else if(loop){
if(!forward && currentFrame <= firstFrame)
currentFrame = numFrames - 1;
else if(forward && currentFrame >= numFrames)
currentFrame = firstFrame + 1;
}
if(firstFrame <= currentFrame && currentFrame <= numFrames)
currentFrame += (forward ? 1 : -1);
if(currentFrame < firstFrame)
currentFrame = (loop ? numFrames : firstFrame);
else if(currentFrame > numFrames)
currentFrame = (loop ? firstFrame : numFrames);
lastUpdateTime = getTime();
}
+4 -1
View File
@@ -20,9 +20,10 @@ namespace vb01{
inline void removeAllBones(){bones.clear();}
inline void setUpdateRate(float updateRate){this->updateRate = updateRate;}
inline int getUpdateRate(){return updateRate;}
inline int getFirstFrame(){return firstFrame;}
inline int getCurrentFrame(){return currentFrame;}
inline void setCurrentFrame(int frame){this->currentFrame = frame;}
inline int getNumnFrames(){return numFrames;}
inline int getNumFrames(){return numFrames;}
inline void setLoop(bool loop){this->loop = loop;}
inline bool isLoop(){return loop;}
inline void setForward(bool forward){this->forward = forward;}
@@ -39,6 +40,8 @@ namespace vb01{
bool loop = false, forward = true;
std::string animationName;
std::vector<Bone*> bones;
friend class AnimationChannelTest;
};
}
+85
View File
@@ -0,0 +1,85 @@
#include "animationChannelTest.h"
#include "skeleton.h"
#include "animationController.h"
#include "animationChannel.h"
#include "animation.h"
#include "bone.h"
#include <vector>
using namespace std;
namespace vb01{
void AnimationChannelTest::setUp(){
Bone *bone = new Bone("bone", 1, Vector3(0, 0, 0), Quaternion::QUAT_I, Vector3::VEC_IJK);
skeleton = new Skeleton();
skeleton->addBone(bone, nullptr);
AnimationController *controller = skeleton->getAnimationController();
Keyframe k1, k2;
k1.frame = 0;
k2.frame = 10;
KeyframeChannel kc;
kc.type = KeyframeChannel::Type::POS_X;
kc.keyframes = vector<Keyframe>({k1, k2});
KeyframeGroup kg;
kg.bone = bone;
kg.keyframeChannels = vector<KeyframeChannel>({kc});
Animation *anim = new Animation("anim");
anim->addKeyframeGroup(kg);
channel = new AnimationChannel(controller);
controller->addAnimationChannel(channel);
controller->addAnimation(anim);
channel->addBone(bone);
channel->setAnimationName("anim");
}
void AnimationChannelTest::tearDown(){
}
void AnimationChannelTest::testUpdate(){
int numFrames = 10;
int firstFrame = channel->getFirstFrame();
channel->numFrames = numFrames;
channel->lastUpdateTime = 0;
channel->setLoop(false);
channel->setForward(true);
channel->update();
CPPUNIT_ASSERT(channel->getCurrentFrame() == 2);
channel->lastUpdateTime = 0;
channel->setCurrentFrame(numFrames - 1);
channel->setLoop(false);
channel->setForward(true);
channel->update();
CPPUNIT_ASSERT(channel->getCurrentFrame() == 10);
channel->lastUpdateTime = 0;
channel->setCurrentFrame(1);
channel->setLoop(false);
channel->setForward(false);
channel->update();
CPPUNIT_ASSERT(channel->getCurrentFrame() == firstFrame);
channel->lastUpdateTime = 0;
channel->setCurrentFrame(numFrames);
channel->setLoop(true);
channel->setForward(true);
channel->update();
CPPUNIT_ASSERT(channel->getCurrentFrame() == firstFrame);
channel->lastUpdateTime = 0;
channel->setCurrentFrame(firstFrame);
channel->setLoop(true);
channel->setForward(false);
channel->update();
CPPUNIT_ASSERT(channel->getCurrentFrame() == numFrames);
}
void AnimationChannelTest::testGetMaxKeyframeNum(){
int numFrames = channel->getMaxKeyframeNum("anim");
CPPUNIT_ASSERT(numFrames == 10);
}
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef ANIMATION_CHANNEL_TEST
#define ANIMATION_CHANNEL_TES
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
namespace vb01{
class Skeleton;
class AnimationChannel;
class AnimationChannelTest : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(AnimationChannelTest);
CPPUNIT_TEST(testUpdate);
CPPUNIT_TEST(testGetMaxKeyframeNum);
CPPUNIT_TEST_SUITE_END();
public:
AnimationChannelTest(){}
~AnimationChannelTest(){}
void setUp();
void tearDown();
private:
Skeleton *skeleton = nullptr;
AnimationChannel *channel = nullptr;
void testUpdate();
void testGetMaxKeyframeNum();
};
}
#endif
+1 -1
View File
@@ -72,7 +72,7 @@ namespace vb01{
Keyframe nextKeyframe = animChannel.keyframes[pastKeyframeId + 1];
int pastFrame = pastKeyframe.frame;
int nextFrame = nextKeyframe.frame;
ratio = (float)(channel->getCurrentFrame() - pastFrame) / (nextFrame - pastFrame);
ratio = (float)(max(0, channel->getCurrentFrame() - pastFrame)) / (nextFrame - pastFrame);
interpMode = pastKeyframe.interpolation;
switch(animChannel.type){
+1 -1
View File
@@ -4,7 +4,7 @@
using namespace std;
namespace vb01{
Bone::Bone(string name, float length, Vector3 pos,Quaternion rot, Vector3 scale) : Node(pos,rot,scale,name){
Bone::Bone(string name, float length, Vector3 pos, Quaternion rot, Vector3 scale) : Node(pos, rot, scale, name){
this->name = name;
this->length = length;
}
+2
View File
@@ -7,6 +7,7 @@
#include "nodeTest.h"
#include "boneTest.h"
#include "vbModelReaderTest.h"
#include "animationChannelTest.h"
using namespace CppUnit;
using namespace vb01;
@@ -17,6 +18,7 @@ int main(){
runner.addTest(NodeTest::suite());
runner.addTest(BoneTest::suite());
runner.addTest(VbModelReaderTest::suite());
runner.addTest(AnimationChannelTest::suite());
runner.run();
return 0;
}