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
+1 -1
View File
@@ -110,7 +110,7 @@ private:
void ReadAnimationJoint(unsigned int &offset, char* fileData, unsigned int& fileByteSize); void ReadAnimationJoint(unsigned int &offset, char* fileData, unsigned int& fileByteSize);
void ReadAnimationClips(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int numberOfClips); void ReadAnimationClips(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int numberOfClips);
void ReadAnimationClipSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int clipIndex); void ReadAnimationClipSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int clipIndex);
void ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, Skeleton::Animation& animation); void ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, std::vector<Skeleton::Animation::Keyframe>& animation);
//void CreateSkeleton(std::vector<std::tuple<std::string, glm::mat4>> &boneInfo, std::map<std::string, int> &boneNameMapping, aiNode* node, int parentID); //void CreateSkeleton(std::vector<std::tuple<std::string, glm::mat4>> &boneInfo, std::map<std::string, int> &boneNameMapping, aiNode* node, int parentID);
}; };
+8 -7
View File
@@ -53,20 +53,21 @@ public:
{ {
struct BoneProperty struct BoneProperty
{ {
int ID;
glm::vec3 Position; glm::vec3 Position;
glm::quat Rotation; glm::quat Rotation;
glm::vec3 Scale = glm::vec3(1); glm::vec3 Scale = glm::vec3(1);
}; };
int Index = 0; int Index = 0;
double Time = 0.0; double Time = 0.0;
std::map<int, Keyframe::BoneProperty> BoneProperties; BoneProperty BoneProperties;
}; };
std::string Name;
double Duration;
std::map<int, std::vector<Keyframe>> JointAnimations;
std::string Name;
double Duration;
std::vector<Keyframe> Keyframes;
}; };
Skeleton() { } Skeleton() { }
+41 -21
View File
@@ -11,6 +11,7 @@ RawModelCustom::RawModelCustom(std::string fileName)
ReadMeshFile(fileName); ReadMeshFile(fileName);
ReadMaterialFile(fileName); ReadMaterialFile(fileName);
ReadAnimationFile(fileName); ReadAnimationFile(fileName);
int k = 0;
} }
void RawModelCustom::ReadMeshFile(std::string filePath) 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) void RawModelCustom::ReadMeshFileHeader(unsigned int& offset, char* fileData, unsigned int& fileByteSize)
{ {
#ifdef BOOST_LITTLE_ENDIAN #ifdef BOOST_LITTLE_ENDIAN
isSkined = *(unsigned int*)(fileData + offset); isSkined = true;//*(unsigned int*)(fileData + offset);
offset += sizeof(bool); //offset += sizeof(bool);
if (isSkined) { if (isSkined) {
m_SkinedVertices.resize(*(unsigned int*)(fileData + offset)); m_SkinedVertices.resize(*(unsigned int*)(fileData + offset));
} }
@@ -331,26 +332,42 @@ void RawModelCustom::ReadAnimationClipSingle(unsigned int &offset, char* fileDat
if (offset + sizeof(float) > fileByteSize) { if (offset + sizeof(float) > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationClip duration failed"); throw Resource::FailedLoadingException("Reading AnimationClip duration failed");
} }
newAnimation.Duration = *(float*)(fileData + offset); newAnimation.Duration = *(float*)(fileData + offset);
offset += sizeof(float); offset += sizeof(float);
if (offset + sizeof(unsigned int) > fileByteSize) { 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); offset += sizeof(unsigned int);
newAnimation.Keyframes.reserve(nrOfKeyframes); for (unsigned int i = 0; i < numberOfJointFrames; i++) {
for (unsigned int i = 0; i < nrOfKeyframes; i++) { if (offset + sizeof(unsigned int) > fileByteSize) {
ReadAnimationKeyFrame(offset, fileData, fileByteSize, newAnimation); 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; m_Skeleton->Animations[newAnimation.Name] = newAnimation;
#else #else
#endif #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; Skeleton::Animation::Keyframe newKeyFrame;
@@ -366,23 +383,26 @@ void RawModelCustom::ReadAnimationKeyFrame(unsigned int &offset, char* fileData,
newKeyFrame.Time = *(float*)(fileData + offset); newKeyFrame.Time = *(float*)(fileData + offset);
offset += sizeof(float); offset += sizeof(float);
if (offset + sizeof(unsigned int) > fileByteSize) { if (offset + sizeof(float) * 3 > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame NrOfJoints failed"); throw Resource::FailedLoadingException("Reading AnimationKeyFrame Position failed");
} }
unsigned int nrOfJoints = *(unsigned int*)(fileData + offset); memcpy(&newKeyFrame.BoneProperties.Position[0], fileData + offset, sizeof(float) * 3);
offset += sizeof(unsigned int); offset += sizeof(float) * 3;
if (offset + sizeof(Skeleton::Animation::Keyframe::BoneProperty) * nrOfJoints> fileByteSize) { if (offset + sizeof(float) * 4 > fileByteSize) {
throw Resource::FailedLoadingException("Reading AnimationKeyFrame joints failed"); 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; if (offset + sizeof(float) * 3 > fileByteSize) {
for (unsigned int i = 0; i < nrOfJoints; i++) { throw Resource::FailedLoadingException("Reading AnimationKeyFrame Scale failed");
memcpy(&newBone, (fileData + offset), sizeof(Skeleton::Animation::Keyframe::BoneProperty));
offset += sizeof(Skeleton::Animation::Keyframe::BoneProperty);
newKeyFrame.BoneProperties[newBone.ID] = newBone;
} }
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() RawModelCustom::~RawModelCustom()
+41 -41
View File
@@ -49,15 +49,15 @@ std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation& animation, doubl
time -= animation.Duration; time -= animation.Duration;
} }
int currentKeyframeIndex = GetKeyframe(animation, time); //int currentKeyframeIndex = GetKeyframe(animation, time);
const Animation::Keyframe& currentFrame = animation.Keyframes[currentKeyframeIndex]; //const Animation::Keyframe& currentFrame = animation.Keyframes[currentKeyframeIndex];
const Animation::Keyframe& nextFrame = animation.Keyframes[(currentKeyframeIndex + 1) % animation.Keyframes.size()]; //const Animation::Keyframe& nextFrame = animation.Keyframes[(currentKeyframeIndex + 1) % animation.Keyframes.size()];
float alpha = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time); //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; 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; std::vector<glm::mat4> finalMatrices;
for (auto &kv : frameBones) { 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) 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()) { //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 currentBoneProperty = currentFrame.BoneProperties.at(bone->ID);
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.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::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress); // glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress; // glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress;
// Flag for no root motion // // Flag for no root motion
if (bone == RootBone && noRootMotion) { // if (bone == RootBone && noRootMotion) {
positionInterp.x = 0; // positionInterp.x = 0;
positionInterp.z = 0; // positionInterp.z = 0;
} // }
boneMatrix = parentMatrix * (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp)); // boneMatrix = parentMatrix * (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp));
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix; // boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
} else { //} else {
if (bone->Parent) { // if (bone->Parent) {
boneMatrix = parentMatrix; // * glm::inverse(bone->OffsetMatrix); // boneMatrix = parentMatrix; // * glm::inverse(bone->OffsetMatrix);
} // }
boneMatrices[bone->ID] = boneMatrix; // * bone->OffsetMatrix; // boneMatrices[bone->ID] = boneMatrix; // * bone->OffsetMatrix;
} //}
for (auto &child : bone->Children) { //for (auto &child : bone->Children) {
std::string name = child->Name; // std::string name = child->Name;
AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, progress, boneMatrices, child, boneMatrix); // 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) int Skeleton::GetKeyframe(const Animation& animation, double time)
{ {
if (time < 0) { //if (time < 0) {
time = 0; // time = 0;
} //}
if (time >= animation.Duration) { //if (time >= animation.Duration) {
return animation.Keyframes.size() - 1; // return animation.Keyframes.size() - 1;
} //}
for (int keyframe = 0; keyframe < animation.Keyframes.size(); ++keyframe) { //for (int keyframe = 0; keyframe < animation.Keyframes.size(); ++keyframe) {
if (animation.Keyframes[keyframe].Time > time) { // if (animation.Keyframes[keyframe].Time > time) {
return (keyframe - 1) % animation.Keyframes.size(); // return (keyframe - 1) % animation.Keyframes.size();
} // }
} //}
return 0; return 0;
} }
+1 -1
View File
@@ -70,7 +70,7 @@ public:
virtual void WriteBinary(std::ostream& out) virtual void WriteBinary(std::ostream& out)
{ {
out.write((char*)&hasSkin, sizeof(bool)); //out.write((char*)&hasSkin, sizeof(bool));
out.write((char*)&NumVertices, sizeof(int)); out.write((char*)&NumVertices, sizeof(int));
out.write((char*)&NumIndices, sizeof(int)); out.write((char*)&NumIndices, sizeof(int));
for (auto aVertex : Vertices) { for (auto aVertex : Vertices) {
+109 -58
View File
@@ -214,26 +214,28 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e
jointIt.reset(); jointIt.reset();
}*/ }*/
MItDag jointIt(MItDag::TraversalType::kDepthFirst, MFn::kJoint);
unsigned int jointID = 0;
int currentFrame = startFrame; while (!jointIt.isDone()) {
while (currentFrame < endFrame) { // ANDREAS int currentFrame = startFrame;
Animation::Keyframe thisKeyFrame; Animation::JointAnimation thisJointAnimation;
thisKeyFrame.Index = currentFrame - startFrame; bool haxBool = false;
thisKeyFrame.Time = thisKeyFrame.Index * oneDivSixty; while (currentFrame < endFrame) {
Animation::JointAnimation::KeyFrame thisKeyFrame;
//thisKeyFrame.Index = currentFrame - startFrame;
//thisKeyFrame.Time = thisKeyFrame.Index * oneDivSixty;
MAnimControl::setCurrentTime(MTime(currentFrame, MTime::kNTSCField)); MAnimControl::setCurrentTime(MTime(currentFrame, MTime::kNTSCField));
MTime time = MAnimControl::currentTime(); MTime time = MAnimControl::currentTime();
MItDag jointIt(MItDag::TraversalType::kDepthFirst, MFn::kJoint);
unsigned int jointID = 0;
while (!jointIt.isDone()) {
MFnTransform thisJoint(jointIt.currentItem()); MFnTransform thisJoint(jointIt.currentItem());
MMatrix transformationMatrix = thisJoint.transformationMatrix(); MMatrix transformationMatrix = thisJoint.transformationMatrix();
double doubleMat[4][4]; double doubleMat[4][4];
if (currentFrame != startFrame){ if (currentFrame != startFrame) {
Animation::Keyframe::JointProperty joint; //Animation::JointAnimation joint;
doubleMat[0][0] = joinCheckMap[thisJoint.name().asChar()][0][0]; doubleMat[0][0] = joinCheckMap[thisJoint.name().asChar()][0][0];
doubleMat[0][1] = joinCheckMap[thisJoint.name().asChar()][0][1]; doubleMat[0][1] = joinCheckMap[thisJoint.name().asChar()][0][1];
@@ -255,9 +257,53 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e
MMatrix LastJointMatrix(doubleMat); MMatrix LastJointMatrix(doubleMat);
//Is same as last KeyFrame //Is same as last KeyFrame
if (LastJointMatrix.isEquivalent(transformationMatrix)) { if (LastJointMatrix.isEquivalent(transformationMatrix)) {
jointID++; //jointID++;
jointIt.next(); //jointIt.next();
currentFrame++;
haxBool = false;
continue; continue;
} else if(!haxBool){
haxBool = true;
MTransformationMatrix TransformationMatrix = LastJointMatrix;
MObject jointOrientObj = thisJoint.attribute("jointOrient");
MFnNumericAttribute jointOrient(jointOrientObj);
double jointOrientDouble[3];
jointOrient.getDefault(jointOrientDouble[0], jointOrientDouble[1], jointOrientDouble[2]);
//MGlobal::displayError(MString() + "Joint Matrix: ");
//MGlobal::displayError(MString() + Matrix.asMatrix()[0][0] + " " + Matrix.asMatrix()[0][1] + " " + Matrix.asMatrix()[0][2] + " " + Matrix.asMatrix()[0][3]);
//MGlobal::displayError(MString() + Matrix.asMatrix()[1][0] + " " + Matrix.asMatrix()[1][1] + " " + Matrix.asMatrix()[1][2] + " " + Matrix.asMatrix()[1][3]);
//MGlobal::displayError(MString() + Matrix.asMatrix()[2][0] + " " + Matrix.asMatrix()[2][1] + " " + Matrix.asMatrix()[2][2] + " " + Matrix.asMatrix()[2][3]);
//MGlobal::displayError(MString() + Matrix.asMatrix()[3][0] + " " + Matrix.asMatrix()[3][1] + " " + Matrix.asMatrix()[3][2] + " " + Matrix.asMatrix()[3][3]);
MEulerRotation joEuler(jointOrientDouble[0], jointOrientDouble[1], jointOrientDouble[2]);
MQuaternion jo = joEuler.asQuaternion();
double tmp[4];
TransformationMatrix.getRotationQuaternion(tmp[0], tmp[1], tmp[2], tmp[3]);
MQuaternion rotation(tmp);
rotation = rotation * jo;
rotation.get(tmp);
//Animation::JointAnimation::KeyFrame keyframe;
Animation::JointAnimation::KeyFrame previousKeyFrame;
previousKeyFrame.Index = currentFrame - startFrame - 1;
previousKeyFrame.Time = previousKeyFrame.Index * oneDivSixty;
previousKeyFrame.Rotation[0] = tmp[0];
previousKeyFrame.Rotation[1] = tmp[1];
previousKeyFrame.Rotation[2] = tmp[2];
previousKeyFrame.Rotation[3] = tmp[3];
TransformationMatrix.getTranslation(MSpace::kTransform).get(tmp);
previousKeyFrame.Position[0] = tmp[0];
previousKeyFrame.Position[1] = tmp[1];
previousKeyFrame.Position[2] = tmp[2];
TransformationMatrix.getScale(tmp, MSpace::kTransform);
previousKeyFrame.Scale[0] = tmp[0];
previousKeyFrame.Scale[1] = tmp[1];
previousKeyFrame.Scale[2] = tmp[2];
thisJointAnimation.m_KeyFrames.push_back(previousKeyFrame);
} }
} }
@@ -280,31 +326,35 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e
joinCheckMap[thisJoint.name().asChar()][3][2] = doubleMat[3][2]; joinCheckMap[thisJoint.name().asChar()][3][2] = doubleMat[3][2];
joinCheckMap[thisJoint.name().asChar()][3][3] = doubleMat[3][3]; joinCheckMap[thisJoint.name().asChar()][3][3] = doubleMat[3][3];
MPlug thisJointBindPose = thisJoint.findPlug("bindPose");
MDataHandle DataHandle;
thisJointBindPose.getValue(DataHandle);
MFnMatrixData MartixFn(DataHandle.data());
MMatrix thisJointBindPoseMatrix = MartixFn.matrix();
MFnTransform Parent(thisJoint.parent(0), &status);
if (status == MS::kSuccess && thisJoint.parent(0).apiType() == MFn::kJoint) {
MTransformationMatrix Matrix = Parent.transformation();
MPlug parentBindPose = Parent.findPlug("bindPose");
MDataHandle DataHandle;
parentBindPose.getValue(DataHandle);
MFnMatrixData MartixFn(DataHandle.data());
MMatrix parentBindPoseMatrix = MartixFn.matrix();
thisJointBindPoseMatrix = thisJointBindPoseMatrix * parentBindPoseMatrix.inverse();
}
MTransformationMatrix TransformationMatrix = thisJoint.transformation(); MTransformationMatrix TransformationMatrix = thisJoint.transformation();
if (thisJointBindPoseMatrix.isEquivalent(TransformationMatrix.asMatrix())) { if (currentFrame == startFrame) {
jointID++; MPlug thisJointBindPose = thisJoint.findPlug("bindPose");
jointIt.next(); MDataHandle DataHandle;
MGlobal::displayError(MString() + thisJoint.name() + " is in bindPose"); thisJointBindPose.getValue(DataHandle);
continue; MFnMatrixData MartixFn(DataHandle.data());
MMatrix thisJointBindPoseMatrix = MartixFn.matrix();
MFnTransform Parent(thisJoint.parent(0), &status);
if (status == MS::kSuccess && thisJoint.parent(0).apiType() == MFn::kJoint) {
MTransformationMatrix Matrix = Parent.transformation();
MPlug parentBindPose = Parent.findPlug("bindPose");
MDataHandle DataHandle;
parentBindPose.getValue(DataHandle);
MFnMatrixData MartixFn(DataHandle.data());
MMatrix parentBindPoseMatrix = MartixFn.matrix();
thisJointBindPoseMatrix = thisJointBindPoseMatrix * parentBindPoseMatrix.inverse();
}
if (thisJointBindPoseMatrix.isEquivalent(TransformationMatrix.asMatrix())) {
//jointID++;
//jointIt.next();
currentFrame++;
MGlobal::displayError(MString() + thisJoint.name() + " is in bindPose");
continue;
}
haxBool = true;
} }
MObject jointOrientObj = thisJoint.attribute("jointOrient"); MObject jointOrientObj = thisJoint.attribute("jointOrient");
@@ -327,34 +377,35 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e
rotation = rotation * jo; rotation = rotation * jo;
rotation.get(tmp); rotation.get(tmp);
Animation::Keyframe::JointProperty joint; //Animation::JointAnimation::KeyFrame keyframe;
joint.ID = jointID; thisKeyFrame.Index = currentFrame - startFrame;
thisKeyFrame.Time = thisKeyFrame.Index * oneDivSixty;
joint.Rotation[0] = tmp[0]; thisKeyFrame.Rotation[0] = tmp[0];
joint.Rotation[1] = tmp[1]; thisKeyFrame.Rotation[1] = tmp[1];
joint.Rotation[2] = tmp[2]; thisKeyFrame.Rotation[2] = tmp[2];
joint.Rotation[3] = tmp[3]; thisKeyFrame.Rotation[3] = tmp[3];
TransformationMatrix.getTranslation(MSpace::kTransform).get(tmp); TransformationMatrix.getTranslation(MSpace::kTransform).get(tmp);
joint.Position[0] = tmp[0]; thisKeyFrame.Position[0] = tmp[0];
joint.Position[1] = tmp[1]; thisKeyFrame.Position[1] = tmp[1];
joint.Position[2] = tmp[2]; thisKeyFrame.Position[2] = tmp[2];
TransformationMatrix.getScale(tmp, MSpace::kTransform); TransformationMatrix.getScale(tmp, MSpace::kTransform);
joint.Scale[0] = tmp[0]; thisKeyFrame.Scale[0] = tmp[0];
joint.Scale[1] = tmp[1]; thisKeyFrame.Scale[1] = tmp[1];
joint.Scale[2] = tmp[2]; thisKeyFrame.Scale[2] = tmp[2];
thisKeyFrame.JointProperties.push_back(joint); thisJointAnimation.m_KeyFrames.push_back(thisKeyFrame);
jointID++; currentFrame++;
jointIt.next();
} }
thisKeyFrame.NumberOfJoints = thisKeyFrame.JointProperties.size(); //thisKeyFrame.NumberOfJoints = thisKeyFrame.JointProperties.size();
returnData.Keyframes.push_back(thisKeyFrame); thisJointAnimation.numberOFKeyFrames = thisJointAnimation.m_KeyFrames.size();
currentFrame++; returnData.JointsFrameMap[jointID] = (thisJointAnimation);
jointID++;
jointIt.next();
} }
returnData.NumOfJointFrames = returnData.JointsFrameMap.size();
returnData.NumKeyFrames = returnData.Keyframes.size();
return returnData; return returnData;
} }
+47 -34
View File
@@ -10,44 +10,56 @@
class Animation : public OutputData { class Animation : public OutputData {
public: public:
struct Keyframe //struct Keyframe
{ //{
struct JointProperty // struct JointProperty
{ // {
int ID = 0; // int ID = 0;
// float Position[3]{ 0 };
// float Rotation[4]{ 0 };
// float Scale[3]{ 0 };
// };
// int Index = 0;
// float Time = 0;
// int NumberOfJoints;
// std::vector<JointProperty> JointProperties;
//};
struct JointAnimation {
struct KeyFrame {
int Index = 0;
float Time = 0;
float Position[3]{ 0 }; float Position[3]{ 0 };
float Rotation[4]{ 0 }; float Rotation[4]{ 0 };
float Scale[3]{ 0 }; float Scale[3]{ 0 };
}; };
unsigned int numberOFKeyFrames = 0;
int Index = 0; std::vector<KeyFrame> m_KeyFrames;
float Time = 0; };
int NumberOfJoints;
std::vector<JointProperty> JointProperties;
};
std::string Name; std::string Name;
int nameLength = 0; int nameLength = 0;
float Duration = 0; float Duration = 0;
int NumKeyFrames = 0; int NumOfJointFrames = 0;
std::vector<Keyframe> Keyframes; std::map<int, JointAnimation> JointsFrameMap;
virtual void WriteBinary(std::ostream& out) virtual void WriteBinary(std::ostream& out)
{ {
out.write((char*)&nameLength, sizeof(int)); out.write((char*)&nameLength, sizeof(int));
out.write(Name.c_str(), Name.size() + 1); out.write(Name.c_str(), Name.size() + 1);
out.write((char*)&Duration, sizeof(float)); out.write((char*)&Duration, sizeof(float));
out.write((char*)&NumKeyFrames, sizeof(int)); out.write((char*)&NumOfJointFrames, sizeof(int));
//Här under loopas alla key frames igenom //Här under loopas alla key frames igenom
for (auto aKeyframe : Keyframes) { for (auto aJointAnimation : JointsFrameMap) {
out.write((char*)&aKeyframe.Index, sizeof(int)); out.write((char*)&aJointAnimation.first, sizeof(int));
out.write((char*)&aKeyframe.Time, sizeof(float)); out.write((char*)&aJointAnimation.second.numberOFKeyFrames, sizeof(int));
out.write((char*)&aKeyframe.NumberOfJoints, sizeof(int)); for (auto aJointKeyFrame : aJointAnimation.second.m_KeyFrames) {
for (auto aJoint : aKeyframe.JointProperties) { out.write((char*)&aJointKeyFrame.Index, sizeof(int));
out.write((char*)&aJoint.ID, sizeof(int)); out.write((char*)&aJointKeyFrame.Time, sizeof(float));
out.write((char*)aJoint.Position, sizeof(float) * 3); out.write((char*)&aJointKeyFrame.Position, sizeof(float) * 3);
out.write((char*)aJoint.Rotation, sizeof(float) * 4); out.write((char*)&aJointKeyFrame.Rotation, sizeof(float) * 4);
out.write((char*)aJoint.Scale, sizeof(float) * 3); out.write((char*)&aJointKeyFrame.Scale, sizeof(float) * 3);
} }
} }
} }
@@ -56,16 +68,17 @@ public:
{ {
out << "Animation Name: " << Name << endl; out << "Animation Name: " << Name << endl;
out << "Duration: " << Duration << endl; out << "Duration: " << Duration << endl;
out << "Number of KeyFrames: " << NumKeyFrames << endl; out << "Number of KeyFrames: " << NumOfJointFrames << endl;
for (auto aKeyframe : Keyframes) { for (auto aJointAnimation : JointsFrameMap) {
out << "Frame: " << aKeyframe.Index << endl; out << "Bone: " << aJointAnimation.first << endl;
out << "Time: " << aKeyframe.Time << endl; //out << "Time: " << aJointAnimation.Time << endl;
out << "Number of Joints: " << aKeyframe.NumberOfJoints << endl; //out << "Number of Joints: " << aKeyframe.NumberOfJoints << endl;
for (auto aJoint : aKeyframe.JointProperties) { for (auto aJointKeyFrame : aJointAnimation.second.m_KeyFrames) {
out << "Joint ID: " << aJoint.ID << endl; //out << "Joint ID: " << aJoint.ID << endl;
out << aJoint.Position[0] << " " << aJoint.Position[1] << " " << aJoint.Position[2] << endl; out << "Time: " << aJointKeyFrame.Time << endl;
out << aJoint.Rotation[0] << " " << aJoint.Rotation[1] << " " << aJoint.Rotation[2] << " " << aJoint.Rotation[3] << endl; out << aJointKeyFrame.Position[0] << " " << aJointKeyFrame.Position[1] << " " << aJointKeyFrame.Position[2] << endl;
out << aJoint.Scale[0] << " " << aJoint.Scale[1] << " " << aJoint.Scale[2] << endl; out << aJointKeyFrame.Rotation[0] << " " << aJointKeyFrame.Rotation[1] << " " << aJointKeyFrame.Rotation[2] << " " << aJointKeyFrame.Rotation[3] << endl;
out << aJointKeyFrame.Scale[0] << " " << aJointKeyFrame.Scale[1] << " " << aJointKeyFrame.Scale[2] << endl;
} }
} }