@@ -336,14 +336,18 @@ namespace MDL {
336336 0 , 0 , 0 , 1
337337 };
338338 mMatrixTable [i] = glm::inverseTranspose (mMatrixTable [i]);
339- mSkeleton [i].Transform = mMatrixTable [i];
339+ mSkeleton [i].Model = mMatrixTable [i];
340+ mSkeleton [i].InverseModel = glm::inverse (mMatrixTable [i]);
340341 }
341342
342343 BuildScenegraphSkeleton (0 , -1 );
343344
344- for (auto & bone : mSkeleton ){
345- bone.InverseTransform = glm::inverse (bone.ParentTransform ());
346- bone.Transform *= bone.InverseTransform ;
345+ for (auto & bone : mSkeleton ){ // Generate local matrices for each bone in the
346+ if (bone.Parent != nullptr ){
347+ bone.Local = glm::inverse (bone.Parent ->Transform ()) * bone.Model ;
348+ } else {
349+ bone.Local = glm::mat4 (1 .0f );
350+ }
347351 }
348352
349353 InitSkeletonRenderer (0 , -1 );
@@ -585,15 +589,17 @@ namespace MDL {
585589
586590 void Model::Draw (glm::mat4* transform, int32_t id, bool selected, TXP::Animation* materialAnimtion, Animation* skeletalAnimation){
587591
588- std::vector<glm::mat4> skeleton;
589- skeleton.resize (mSkeleton .size ());
590- for (int i = 0 ; i < mSkeleton .size (); i++){
591- if (skeletalAnimation != nullptr ){
592- skeleton[i] = (mSkeleton [i].Transform * skeletalAnimation->GetJoint (i)) * mSkeleton [i].InverseTransform ;
592+ std::vector<glm::mat4> skeleton (mSkeleton .size ());
593+ for (int i = 0 ; i < skeleton.size (); i++){
594+ if (mSkeleton [i].ParentIndex != -1 ){
595+ skeleton[i] = skeleton[mSkeleton [i].ParentIndex ] * (skeletalAnimation != nullptr && skeletalAnimation->mJointAnimations .size () >= i ? skeletalAnimation->GetJoint (mSkeleton [i].Local , i) : mSkeleton [i].Local );
593596 } else {
594- skeleton[i] = mSkeleton [i].Transform ;
597+ skeleton[i] = mSkeleton [i].Local ;
595598 }
596599 }
600+ for (int i = 0 ; i < skeleton.size (); i++){
601+ skeleton[i] = skeleton[i] * mSkeleton [i].InverseModel ;
602+ }
597603
598604 glUseProgram (mProgram );
599605 glUniformMatrix4fv (glGetUniformLocation (mProgram , " transform" ), 1 , 0 , &(*transform)[0 ][0 ]);
@@ -667,31 +673,37 @@ namespace MDL {
667673 }
668674 }
669675
670- glm::mat4 Animation::GetJoint (uint32_t id){
671- if (id >= mJointAnimations .size ()) return glm::mat4 ( 1 . 0f ) ;
676+ glm::mat4 Animation::GetJoint (glm::mat4 local, uint32_t id){
677+ if (id >= mJointAnimations .size ()) return local ;
672678 // std::cout << "Id is " << id << std::endl;
673679 JointTrack& joint = mJointAnimations [id];
674680 glm::mat4 keyframe (1 .0f );
675-
676- float sx = joint.ScaleX .mKeys .size () > 0 ? MixTrack (joint.ScaleX , mTime , joint.mPreviousScaleKeyX , joint.mNextScaleKeyX ) : 1 .0f ;
677- float sy = joint.ScaleY .mKeys .size () > 0 ? MixTrack (joint.ScaleY , mTime , joint.mPreviousScaleKeyY , joint.mNextScaleKeyY ) : 1 .0f ;
678- float sz = joint.ScaleZ .mKeys .size () > 0 ? MixTrack (joint.ScaleZ , mTime , joint.mPreviousScaleKeyZ , joint.mNextScaleKeyZ ) : 1 .0f ;
679-
680- float rz = joint.RotationX .mKeys .size () > 0 ? MixTrack (joint.RotationX , mTime , joint.mPreviousRotKeyX , joint.mNextRotKeyX ) : 0 .0f ;
681- float ry = joint.RotationY .mKeys .size () > 0 ? MixTrack (joint.RotationY , mTime , joint.mPreviousRotKeyY , joint.mNextRotKeyY ) : 0 .0f ;
682- float rx = joint.RotationZ .mKeys .size () > 0 ? MixTrack (joint.RotationZ , mTime , joint.mPreviousRotKeyZ , joint.mNextRotKeyZ ) : 0 .0f ;
683681
684- float px = joint. PositionX . mKeys . size () > 0 ? MixTrack (joint. PositionX , mTime , joint. mPreviousPosKeyX , joint. mNextPosKeyX ) : 0 . 0f ;
685- float py = joint. PositionY . mKeys . size () > 0 ? MixTrack (joint. PositionY , mTime , joint. mPreviousPosKeyY , joint. mNextPosKeyY ) : 0 . 0f ;
686- float pz = joint. PositionZ . mKeys . size () > 0 ? MixTrack (joint. PositionZ , mTime , joint. mPreviousPosKeyZ , joint. mNextPosKeyZ ) : 0 . 0f ;
682+ glm::vec3 translation, scale, skew ;
683+ glm::vec4 persp ;
684+ glm::quat rot ;
687685
688- std::cout << std::format ( " Frame Data: [{}, {}, {}][{}, {}, {}][{}, {}, {}] " , pz, py, px, rz, ry, rx, sz, sy, sx) << std::endl ;
686+ glm::decompose (local, scale, rot, translation, skew, persp) ;
689687
690- keyframe = glm::scale (keyframe, glm::vec3 (sz, sy, sx));
691- keyframe = glm::rotate (keyframe, glm::radians (-rz), glm::vec3 (1 , 0 , 0 ));
692- keyframe = glm::rotate (keyframe, glm::radians (-ry), glm::vec3 (0 , 1 , 0 ));
693- keyframe = glm::rotate (keyframe, glm::radians (-rx), glm::vec3 (0 , 0 , 1 ));
694- keyframe = glm::translate (keyframe, glm::vec3 (sz, sy, sx));
688+ glm::vec3 rotEuler = glm::eulerAngles (rot);
689+
690+ if (joint.ScaleX .mKeys .size () > 0 ) scale.z = MixTrack (joint.ScaleX , mTime , joint.mPreviousScaleKeyX , joint.mNextScaleKeyX );
691+ if (joint.ScaleY .mKeys .size () > 0 ) scale.y = MixTrack (joint.ScaleY , mTime , joint.mPreviousScaleKeyY , joint.mNextScaleKeyY );
692+ if (joint.ScaleZ .mKeys .size () > 0 ) scale.x = MixTrack (joint.ScaleZ , mTime , joint.mPreviousScaleKeyZ , joint.mNextScaleKeyZ );
693+
694+ if (joint.RotationX .mKeys .size () > 0 ) rotEuler.z = glm::radians (MixTrack (joint.RotationX , mTime , joint.mPreviousRotKeyX , joint.mNextRotKeyX ));
695+ if (joint.RotationY .mKeys .size () > 0 ) rotEuler.y = glm::radians (MixTrack (joint.RotationY , mTime , joint.mPreviousRotKeyY , joint.mNextRotKeyY ));
696+ if (joint.RotationZ .mKeys .size () > 0 ) rotEuler.x = glm::radians (MixTrack (joint.RotationZ , mTime , joint.mPreviousRotKeyZ , joint.mNextRotKeyZ ));
697+
698+ if (joint.PositionX .mKeys .size () > 0 ) translation.z = MixTrack (joint.PositionX , mTime , joint.mPreviousPosKeyX , joint.mNextPosKeyX );
699+ if (joint.PositionY .mKeys .size () > 0 ) translation.y = MixTrack (joint.PositionY , mTime , joint.mPreviousPosKeyY , joint.mNextPosKeyY );
700+ if (joint.PositionZ .mKeys .size () > 0 ) translation.x = MixTrack (joint.PositionZ , mTime , joint.mPreviousPosKeyZ , joint.mNextPosKeyZ );
701+
702+ keyframe = glm::scale (keyframe, scale);
703+ keyframe = glm::rotate (keyframe, rotEuler.x , glm::vec3 (1 , 0 , 0 ));
704+ keyframe = glm::rotate (keyframe, rotEuler.y , glm::vec3 (0 , 1 , 0 ));
705+ keyframe = glm::rotate (keyframe, rotEuler.z , glm::vec3 (0 , 0 , 1 ));
706+ keyframe = glm::translate (keyframe, translation);
695707
696708 return keyframe;
697709 }
0 commit comments