Changed the structure of the Animation Data in both Exporter & Importer.

This commit is contained in:
antc13
2016-02-04 11:25:31 +01:00
parent d99bf60510
commit 356cf72b0f
7 changed files with 251 additions and 166 deletions
+41 -21
View File
@@ -11,6 +11,7 @@ RawModelCustom::RawModelCustom(std::string fileName)
ReadMeshFile(fileName);
ReadMaterialFile(fileName);
ReadAnimationFile(fileName);
int k = 0;
}
void RawModelCustom::ReadMeshFile(std::string filePath)
@@ -40,8 +41,8 @@ void RawModelCustom::ReadMeshFile(std::string filePath)
void RawModelCustom::ReadMeshFileHeader(unsigned int& offset, char* fileData, unsigned int& fileByteSize)
{
#ifdef BOOST_LITTLE_ENDIAN
isSkined = *(unsigned int*)(fileData + offset);
offset += sizeof(bool);
isSkined = true;//*(unsigned int*)(fileData + offset);
//offset += sizeof(bool);
if (isSkined) {
m_SkinedVertices.resize(*(unsigned int*)(fileData + offset));
}
@@ -331,26 +332,42 @@ void RawModelCustom::ReadAnimationClipSingle(unsigned int &offset, char* fileDat
if (offset + sizeof(float) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationClip duration failed");
}
newAnimation.Duration = *(float*)(fileData + offset);
offset += sizeof(float);
if (offset + sizeof(unsigned int) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationClip NrOfKeyframes failed");
throw Resource::FailedLoadingException("Reading AnimationClip numberOfJointFrames failed");
}
unsigned int nrOfKeyframes = *(unsigned int*)(fileData + offset);
unsigned int numberOfJointFrames = *(unsigned int*)(fileData + offset);
offset += sizeof(unsigned int);
newAnimation.Keyframes.reserve(nrOfKeyframes);
for (unsigned int i = 0; i < nrOfKeyframes; i++) {
ReadAnimationKeyFrame(offset, fileData, fileByteSize, newAnimation);
for (unsigned int i = 0; i < numberOfJointFrames; i++) {
if (offset + sizeof(unsigned int) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationClip JointID failed");
}
int jointID = *(unsigned int*)(fileData + offset);
offset += sizeof(unsigned int);
if (offset + sizeof(unsigned int) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationClip numberOFKeyFrames failed");
}
unsigned int numberOFKeyFrames = *(unsigned int*)(fileData + offset);
offset += sizeof(unsigned int);
if (numberOFKeyFrames > 0) {
newAnimation.JointAnimations[jointID].reserve(numberOFKeyFrames);
for (unsigned int j = 0; j < numberOFKeyFrames; j++) {
ReadAnimationKeyFrame(offset, fileData, fileByteSize, newAnimation.JointAnimations[jointID]);
}
}
}
m_Skeleton->Animations[newAnimation.Name] = newAnimation;
#else
#endif
}
void RawModelCustom::ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, Skeleton::Animation& animation)
void RawModelCustom::ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, std::vector<Skeleton::Animation::Keyframe>& animation)
{
Skeleton::Animation::Keyframe newKeyFrame;
@@ -366,23 +383,26 @@ void RawModelCustom::ReadAnimationKeyFrame(unsigned int &offset, char* fileData,
newKeyFrame.Time = *(float*)(fileData + offset);
offset += sizeof(float);
if (offset + sizeof(unsigned int) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame NrOfJoints failed");
if (offset + sizeof(float) * 3 > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame Position failed");
}
unsigned int nrOfJoints = *(unsigned int*)(fileData + offset);
offset += sizeof(unsigned int);
memcpy(&newKeyFrame.BoneProperties.Position[0], fileData + offset, sizeof(float) * 3);
offset += sizeof(float) * 3;
if (offset + sizeof(Skeleton::Animation::Keyframe::BoneProperty) * nrOfJoints> fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame joints failed");
if (offset + sizeof(float) * 4 > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame Rotation failed");
}
memcpy(&newKeyFrame.BoneProperties.Rotation[0], fileData + offset, sizeof(float) * 4);
offset += sizeof(float) * 4;
Skeleton::Animation::Keyframe::BoneProperty newBone;
for (unsigned int i = 0; i < nrOfJoints; i++) {
memcpy(&newBone, (fileData + offset), sizeof(Skeleton::Animation::Keyframe::BoneProperty));
offset += sizeof(Skeleton::Animation::Keyframe::BoneProperty);
newKeyFrame.BoneProperties[newBone.ID] = newBone;
if (offset + sizeof(float) * 3 > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame Scale failed");
}
animation.Keyframes.push_back(newKeyFrame);
memcpy(&newKeyFrame.BoneProperties.Scale[0], fileData + offset, sizeof(float) * 3);
offset += sizeof(float) * 3;
animation.push_back(newKeyFrame);
}
RawModelCustom::~RawModelCustom()
+41 -41
View File
@@ -49,15 +49,15 @@ std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation& animation, doubl
time -= animation.Duration;
}
int currentKeyframeIndex = GetKeyframe(animation, time);
//int currentKeyframeIndex = GetKeyframe(animation, time);
const Animation::Keyframe& currentFrame = animation.Keyframes[currentKeyframeIndex];
const Animation::Keyframe& nextFrame = animation.Keyframes[(currentKeyframeIndex + 1) % animation.Keyframes.size()];
float alpha = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time);
//const Animation::Keyframe& currentFrame = animation.Keyframes[currentKeyframeIndex];
//const Animation::Keyframe& nextFrame = animation.Keyframes[(currentKeyframeIndex + 1) % animation.Keyframes.size()];
//float alpha = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time);
//auto animationFrame = Animations[""].Keyframes[frame];
////auto animationFrame = Animations[""].Keyframes[frame];
std::map<int, glm::mat4> frameBones;
AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, alpha, frameBones, RootBone, glm::mat4(1));
//AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, alpha, frameBones, RootBone, glm::mat4(1));
std::vector<glm::mat4> finalMatrices;
for (auto &kv : frameBones) {
@@ -68,36 +68,36 @@ std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation& animation, doubl
void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation::Keyframe &currentFrame, const Animation::Keyframe &nextFrame, float progress, std::map<int, glm::mat4> &boneMatrices, const Bone* bone, glm::mat4 parentMatrix)
{
glm::mat4 boneMatrix;
// glm::mat4 boneMatrix;
if (currentFrame.BoneProperties.find(bone->ID) != currentFrame.BoneProperties.end() || nextFrame.BoneProperties.find(bone->ID) != nextFrame.BoneProperties.end()) {
Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties.at(bone->ID);
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties.at(bone->ID);
//if (currentFrame.BoneProperties.find(bone->ID) != currentFrame.BoneProperties.end() || nextFrame.BoneProperties.find(bone->ID) != nextFrame.BoneProperties.end()) {
// Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties.at(bone->ID);
// Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties.at(bone->ID);
glm::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress;
// glm::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
// glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
// glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress;
// Flag for no root motion
if (bone == RootBone && noRootMotion) {
positionInterp.x = 0;
positionInterp.z = 0;
}
// // Flag for no root motion
// if (bone == RootBone && noRootMotion) {
// positionInterp.x = 0;
// positionInterp.z = 0;
// }
boneMatrix = parentMatrix * (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp));
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
} else {
if (bone->Parent) {
boneMatrix = parentMatrix; // * glm::inverse(bone->OffsetMatrix);
}
boneMatrices[bone->ID] = boneMatrix; // * bone->OffsetMatrix;
}
// boneMatrix = parentMatrix * (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp));
// boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
//} else {
// if (bone->Parent) {
// boneMatrix = parentMatrix; // * glm::inverse(bone->OffsetMatrix);
// }
// boneMatrices[bone->ID] = boneMatrix; // * bone->OffsetMatrix;
//}
for (auto &child : bone->Children) {
std::string name = child->Name;
AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, progress, boneMatrices, child, boneMatrix);
}
//for (auto &child : bone->Children) {
// std::string name = child->Name;
// AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, progress, boneMatrices, child, boneMatrix);
//}
}
@@ -134,18 +134,18 @@ void Skeleton::PrintSkeleton(const Bone* bone, int depthCount)
int Skeleton::GetKeyframe(const Animation& animation, double time)
{
if (time < 0) {
time = 0;
}
if (time >= animation.Duration) {
return animation.Keyframes.size() - 1;
}
//if (time < 0) {
// time = 0;
//}
//if (time >= animation.Duration) {
// return animation.Keyframes.size() - 1;
//}
for (int keyframe = 0; keyframe < animation.Keyframes.size(); ++keyframe) {
if (animation.Keyframes[keyframe].Time > time) {
return (keyframe - 1) % animation.Keyframes.size();
}
}
//for (int keyframe = 0; keyframe < animation.Keyframes.size(); ++keyframe) {
// if (animation.Keyframes[keyframe].Time > time) {
// return (keyframe - 1) % animation.Keyframes.size();
// }
//}
return 0;
}