@@ -335,17 +335,14 @@ namespace MDL {
335335 0 , 0 , 0 , 1
336336 };
337337 mMatrixTable [i] = glm::inverseTranspose (mMatrixTable [i]);
338+ mSkeleton [i].Transform = mMatrixTable [i];
338339 }
339340
340341 BuildScenegraphSkeleton (0 , -1 );
341342
342343 for (auto & bone : mSkeleton ){
343- glm::mat4 transform = bone.Transform () * glm::inverse (bone.ParentTransform ());
344- glm::vec3 skew;
345- glm::vec4 persp;
346- glm::decompose (transform, bone.Scale , bone.Rotation , bone.Translation , skew, persp);
347- bone.Rotation = glm::conjugate (bone.Rotation );
348- // std::cout << std::format("Bone Translation [{}, {}, {}], Rotation [{}, {}, {}]", bone.Translation.x, bone.Translation.y, bone.Translation.z, bone.Rotation.x, bone.Rotation.y, bone.Rotation.z) << std::endl;
344+ bone.InverseTransform = glm::inverse (bone.ParentTransform ());
345+ bone.Transform *= bone.InverseTransform ;
349346 }
350347
351348 InitSkeletonRenderer (0 , -1 );
@@ -547,10 +544,6 @@ namespace MDL {
547544 void Model::BuildScenegraphSkeleton (uint32_t index, uint32_t parentIndex){
548545 auto node = mGraphNodes [index];
549546
550- glm::vec3 skew;
551- glm::vec4 persp;
552- glm::decompose (mMatrixTable [index], mSkeleton [index].Scale , mSkeleton [index].Rotation , mSkeleton [index].Translation , skew, persp);
553- mSkeleton [index].Rotation = glm::conjugate (mSkeleton [index].Rotation );
554547 mSkeleton [index].ParentIndex = parentIndex;
555548 if (parentIndex != -1 ){
556549 mSkeleton [index].Parent = &mSkeleton [parentIndex];
@@ -594,11 +587,10 @@ namespace MDL {
594587 std::vector<glm::mat4> skeleton;
595588 skeleton.resize (mSkeleton .size ());
596589 for (int i = 0 ; i < mSkeleton .size (); i++){
597- glm::mat4 bone = mSkeleton [i].Transform ();
598590 if (skeletalAnimation != nullptr ){
599- skeleton[i] = bone * skeletalAnimation->GetJoint (i);
591+ skeleton[i] = skeletalAnimation->GetJoint (i) * mSkeleton [i]. InverseTransform ;
600592 } else {
601- skeleton[i] = bone ;
593+ skeleton[i] = mSkeleton [i]. Transform ;
602594 }
603595 }
604596
@@ -692,7 +684,7 @@ namespace MDL {
692684 float py = joint.PositionY .mKeys .size () > 0 ? MixTrack (joint.PositionY , mTime , joint.mPreviousPosKeyY , joint.mNextPosKeyY ) : 0 .0f ;
693685 float pz = joint.PositionZ .mKeys .size () > 0 ? MixTrack (joint.PositionZ , mTime , joint.mPreviousPosKeyZ , joint.mNextPosKeyZ ) : 0 .0f ;
694686
695- // std::cout << std::format("Frame Data: [{}, {}, {}][{}, {}, {}][{}, {}, {}]", pz, py, px, rz, ry, rx, sz, sy, sx) << std::endl;
687+ std::cout << std::format (" Frame Data: [{}, {}, {}][{}, {}, {}][{}, {}, {}]" , pz, py, px, rz, ry, rx, sz, sy, sx) << std::endl;
696688
697689 keyframe = glm::scale (keyframe, glm::vec3 (sz, sy, sx));
698690 keyframe = glm::rotate (keyframe, glm::radians (-rz), glm::vec3 (1 , 0 , 0 ));
@@ -722,7 +714,7 @@ namespace MDL {
722714
723715
724716 std::vector<std::array<uint32_t , 9 >> beginIndices;
725- std::vector<std::array<std::tuple <uint8_t , uint8_t >, 9 >> trackFlags;
717+ std::vector<std::array<std::pair <uint8_t , uint8_t >, 9 >> trackFlags;
726718
727719 stream->seek (keyStartOffset);
728720 for (uint32_t i = 0 ; i < jointCount; i++){
@@ -732,7 +724,7 @@ namespace MDL {
732724 }
733725 stream->seek (keyCountOffset);
734726 for (uint32_t i = 0 ; i < jointCount; i++){
735- std::array<std::tuple <uint8_t , uint8_t >, 9 > flags;
727+ std::array<std::pair <uint8_t , uint8_t >, 9 > flags;
736728 for (uint32_t j = 0 ; j < 9 ; j++){
737729 std::get<0 >(flags[j]) = stream->readUInt8 ();
738730 std::get<1 >(flags[j]) = stream->readUInt8 ();
@@ -746,17 +738,17 @@ namespace MDL {
746738 for (int j = 0 ; j < jointCount; j++){
747739 JointTrack& joint = mJointAnimations [j];
748740
749- joint.ScaleX .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][0 ], std::get< 1 >( trackFlags[j][0 ]) , true , std::get< 0 >( trackFlags[j][0 ]) == 0x80 );
750- joint.ScaleY .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][1 ], std::get< 1 >( trackFlags[j][1 ]) , true , std::get< 0 >( trackFlags[j][1 ]) == 0x80 );
751- joint.ScaleZ .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][2 ], std::get< 1 >( trackFlags[j][2 ]) , true , std::get< 0 >( trackFlags[j][2 ]) == 0x80 );
741+ joint.ScaleX .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][0 ], trackFlags[j][0 ]. second , true , trackFlags[j][0 ]. first == 0x80 );
742+ joint.ScaleY .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][1 ], trackFlags[j][1 ]. second , true , trackFlags[j][1 ]. first == 0x80 );
743+ joint.ScaleZ .LoadTrackEx (stream, scaleKeyframeOffset, beginIndices[j][2 ], trackFlags[j][2 ]. second , true , trackFlags[j][2 ]. first == 0x80 );
752744
753- joint.RotationX .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][3 ], std::get< 1 >( trackFlags[j][3 ]) , true , std::get< 0 >( trackFlags[j][3 ]) == 0x80 , 2 );
754- joint.RotationY .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][4 ], std::get< 1 >( trackFlags[j][4 ]) , true , std::get< 0 >( trackFlags[j][4 ]) == 0x80 , 2 );
755- joint.RotationZ .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][5 ], std::get< 1 >( trackFlags[j][5 ]) , true , std::get< 0 >( trackFlags[j][5 ]) == 0x80 , 2 );
745+ joint.RotationX .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][3 ], trackFlags[j][3 ]. second , true , trackFlags[j][3 ]. first == 0x80 , 2 );
746+ joint.RotationY .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][4 ], trackFlags[j][4 ]. second , true , trackFlags[j][4 ]. first == 0x80 , 2 );
747+ joint.RotationZ .LoadTrackEx (stream, rotationKeyframeOffset, beginIndices[j][5 ], trackFlags[j][5 ]. second , true , trackFlags[j][5 ]. first == 0x80 , 2 );
756748
757- joint.PositionX .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][6 ], std::get< 1 >( trackFlags[j][6 ]) , true , std::get< 0 >( trackFlags[j][6 ]) == 0x80 );
758- joint.PositionY .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][7 ], std::get< 1 >( trackFlags[j][7 ]) , true , std::get< 0 >( trackFlags[j][7 ]) == 0x80 );
759- joint.PositionZ .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][8 ], std::get< 1 >( trackFlags[j][8 ]) , true , std::get< 0 >( trackFlags[j][8 ]) == 0x80 );
749+ joint.PositionX .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][6 ], trackFlags[j][6 ]. second , true , trackFlags[j][6 ]. first == 0x80 );
750+ joint.PositionY .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][7 ], trackFlags[j][7 ]. second , true , trackFlags[j][7 ]. first == 0x80 );
751+ joint.PositionZ .LoadTrackEx (stream, positionKeyframeOffset, beginIndices[j][8 ], trackFlags[j][8 ]. second , true , trackFlags[j][8 ]. first == 0x80 );
760752 }
761753 }
762754 }
0 commit comments