Search code examples
openglopentkskinningassimp

Skinning with Assimp.Net and OpenTK


I'm trying to implement skeletal animation using Assimp.net and OpenTK and have been following this tutorial but I cannot get it to work.

The model appears fine with identity matrices but is terribly garbled when using the transforms I generate from Assimp.

I suspect the issue is the way I am combining all of the matrices or that there is a difference in OpenTK that I am not realising. I have making similar adjustments from the tutorial as suggested here: Matrix calculations for gpu skinning

but it is still garbled, just differently, I have also tried converting all Assimp matrices to OpenTK matrices before performing any multiplication. These are the areas of the code related to the matrices, I can provide more if needed:

Matrix Conversion

public static OpenTK.Matrix4 TKMatrix(Assimp.Matrix4x4 input)
     {
        return new OpenTK.Matrix4(input.A1, input.B1, input.C1, input.D1,
                                   input.A2, input.B2, input.C2, input.D2,
                                   input.A3, input.B3, input.C3, input.D3,
                                   input.A4, input.B4, input.C4, input.D4);
     }

Storing the GLobal Inverse

public class LoaderMesh
  {
     public Scene mScene;
     public Mesh mMesh;

     public OpenTK.Matrix4 GlobalInverseTransform { get; set; }

     public LoaderMesh(Scene aiScene, Mesh aiMesh)
     {
        mScene = aiScene;
        mMesh = aiMesh;

        GlobalInverseTransform = Util.TKMatrix(mScene.RootNode.Transform);
        GlobalInverseTransform.Invert();
     }

Loading the bones

public void LoadBones(List<VBO.Vtx_BoneWeight.Vtx> boneData)
     {
        for (uint iBone = 0; iBone < mMesh.BoneCount; ++iBone)
        {
           uint boneIndex = 0;
           String bonename = mMesh.Bones[iBone].Name;

           if (!BoneMapping.ContainsKey(bonename))
           {
              boneIndex = (uint)NumBones;
              NumBones++;
              BoneInfo bi = new BoneInfo();
              BoneInfos.Add(bi);
           }
           else
           {
              boneIndex = BoneMapping[bonename];
           }

           BoneMapping[bonename] = boneIndex;
           BoneInfos[(int)boneIndex].OffsetMatrix = Util.TKMatrix(mMesh.Bones[iBone].OffsetMatrix);

           for (uint iWeight = 0; iWeight < mMesh.Bones[iBone].VertexWeightCount; iWeight++)
           {
              uint VertexID = /*m_Entries[MeshIndex].BaseVertex*/ mMesh.Bones[iBone].VertexWeights[iWeight].VertexID;
              float Weight = mMesh.Bones[iBone].VertexWeights[iWeight].Weight;

              VBO.Vtx_BoneWeight.Vtx vtx = boneData[(int)VertexID];

              VBO.Vtx_BoneWeight.AddWeight(ref vtx, boneIndex, Weight);

              boneData[(int)VertexID] = vtx;
           }
        }
     }

Calculating the Transforms

public void ReadNodeHierarchy(float animationTime, Node aiNode, ref OpenTK.Matrix4 parentTransform)
     {
        String NodeName = aiNode.Name;

        Animation animation = mScene.Animations[0];

        OpenTK.Matrix4 NodeTransformation = Util.TKMatrix(aiNode.Transform);

        NodeAnimationChannel nodeAnim = FindNodeAnim(animation, NodeName);

        OpenTK.Matrix4 localTransform = OpenTK.Matrix4.Identity;

        if (nodeAnim != null)
        {
           // Interpolate scaling and generate scaling transformation matrix
           Vector3D Scaling = new Vector3D();
           CalcInterpolatedScaling(ref Scaling, animationTime, nodeAnim);
           Console.WriteLine("Scaling: " + Scaling.ToString());
           OpenTK.Matrix4 ScalingM = Util.TKMatrix(Matrix4x4.FromScaling(Scaling));

           // Interpolate rotation and generate rotation transformation matrix
           Quaternion RotationQ = new Quaternion();
           CalcInterpolatedRotation(ref RotationQ, animationTime, nodeAnim);
           Console.WriteLine("Rotation: " + RotationQ.ToString());
           OpenTK.Matrix4 RotationM = Util.TKMatrix(RotationQ.GetMatrix());

           // Interpolate translation and generate translation transformation matrix
           Vector3D Translation = new Vector3D();
           CalcInterpolatedPosition(ref Translation, animationTime, nodeAnim);
           Console.WriteLine("Transform: " + Translation.ToString());
           OpenTK.Matrix4 TranslationM = Util.TKMatrix(Matrix4x4.FromTranslation(Translation));

           // Combine the above transformations
           NodeTransformation = TranslationM * RotationM * ScalingM;

           localTransform = TranslationM * RotationM * ScalingM;
        }

        OpenTK.Matrix4 GlobalTransformation = parentTransform * NodeTransformation;

        OpenTK.Matrix4 parentPass = OpenTK.Matrix4.Identity;
        if (BoneMapping.ContainsKey(NodeName) == true)
        {
           uint BoneIndex = BoneMapping[NodeName];
           //BoneInfos[(int)BoneIndex].FinalTransformation = GlobalInverseTransform * BoneInfos[(int)BoneIndex].OffsetMatrix * GlobalTransformation;
           BoneInfos[(int)BoneIndex].NodeTransformation = parentTransform * Util.TKMatrix(aiNode.Transform) * localTransform;
           parentPass = BoneInfos[(int)BoneIndex].NodeTransformation;

           BoneInfos[(int)BoneIndex].FinalTransformation = GlobalInverseTransform * BoneInfos[(int)BoneIndex].NodeTransformation * BoneInfos[(int)BoneIndex].OffsetMatrix;
        }

        for (uint i = 0; i < aiNode.ChildCount; i++)
        {
           ReadNodeHierarchy(animationTime, aiNode.Children[i], ref parentPass);
        }
     }

And this is the vertex shader code

#version 400

layout(location = 0)in vec4 vert;
layout(location = 1)in vec4 normal;
layout(location = 2)in vec4 texCoord;
layout(location = 3)in vec4 tanCoord;

layout(location = 4)in ivec4 boneIDs;
layout(location = 5)in vec4 boneWeights;

uniform mat4 projectionMtx;
uniform mat4 viewMtx;
uniform mat4 modelMtx;

const int MAX_BONES = 100;

uniform mat4 bones[MAX_BONES];

out vec3 positionFrg_CS;
out vec3 normalFrg_CS;
out vec3 tanCoordFrg_CS;
out vec3 bitCoordFrg_CS;
out vec4 texCoordFrg;

void main()
{
   mat4 BoneTransform = bones[boneIDs[0]] * boneWeights[0];
    BoneTransform += bones[boneIDs[1]] * boneWeights[1];
    BoneTransform += bones[boneIDs[2]] * boneWeights[2];
    BoneTransform += bones[boneIDs[3]] * boneWeights[3];

    gl_Position = projectionMtx * viewMtx * modelMtx * BoneTransform * vert;


}

Is there anything I am doing wrong multiplying the matrices together?


Solution

  • In reply to livin_amuk, I have got this working, at least well enough for my needs, however I fixed this 6 months ago and my memory is vague...

    If I remember correctly my main issue was the bone/vertex indices, I think I messed up the BaseVertex because I was in a rush. Here is my current working LoadBones function.

    public void LoadBones(List<VBO.Vtx_BoneWeight.Vtx> boneData, SubMesh mesh)
      {
         for (int iBone = 0; iBone < mesh.mMesh.BoneCount; ++iBone)
         {
            uint boneIndex = 0;
            String bonename = mesh.mMesh.Bones[iBone].Name;
    
            if (!BoneMapping.ContainsKey(bonename))
            {
               boneIndex = (uint)NumBones;
               NumBones++;
               BoneInfo bi = new BoneInfo();
               BoneInfos.Add(bi);
    
    //Note, I have these two lines included inside the if statement, the original tut does not. Not sure if it makes a difference.
               BoneMapping[bonename] = boneIndex;
               BoneInfos[(int)boneIndex].OffsetMatrix = AssimpToOpenTK.TKMatrix(mesh.mMesh.Bones[iBone].OffsetMatrix);
            }
            else
            {
               boneIndex = BoneMapping[bonename];
            }
    
            for (int iWeight = 0; iWeight < mesh.mMesh.Bones[iBone].VertexWeightCount; iWeight++)
            {
    //My question has the mesh.BaseVertex commented out. it is important!
               long VertexID = mesh.BaseVertex + mesh.mMesh.Bones[iBone].VertexWeights[iWeight].VertexID;
               float Weight = mesh.mMesh.Bones[iBone].VertexWeights[iWeight].Weight;
    
               VBO.Vtx_BoneWeight.Vtx vtx = boneData[(int)VertexID];
    
               VBO.Vtx_BoneWeight.AddWeight(ref vtx, boneIndex, Weight);
    
               boneData[(int)VertexID] = vtx;
            }
         }
      }
    

    I also had the transforms backwards. Read node hierarchy function.

    public void ReadNodeHierarchy(float animationTime, Node aiNode, ref OpenTK.Matrix4 parentTransform)
      {
         String NodeName = aiNode.Name;
    
         Animation animation = mScene.Animations[0];
    
         OpenTK.Matrix4 NodeTransformation = AssimpToOpenTK.TKMatrix(aiNode.Transform);
    
         NodeAnimationChannel nodeAnim = FindNodeAnim(animation, NodeName);
    
         if (nodeAnim != null)
         {
            // Interpolate scaling and generate scaling transformation matrix
            Vector3D Scaling = new Vector3D();
            CalcInterpolatedScaling(ref Scaling, animationTime, nodeAnim);
            OpenTK.Matrix4 ScalingM = AssimpToOpenTK.TKMatrix(Matrix4x4.FromScaling(Scaling));
    
            // Interpolate rotation and generate rotation transformation matrix
            Quaternion RotationQ = new Quaternion();
            CalcInterpolatedRotation(ref RotationQ, animationTime, nodeAnim);
            OpenTK.Matrix4 RotationM = AssimpToOpenTK.TKMatrix(RotationQ.GetMatrix());
    
            // Interpolate translation and generate translation transformation matrix
            Vector3D Translation = new Vector3D();
            CalcInterpolatedPosition(ref Translation, animationTime, nodeAnim);
            OpenTK.Matrix4 TranslationM = AssimpToOpenTK.TKMatrix(Matrix4x4.FromTranslation(Translation));
    
            // Combine the above transformations
            //All that local transform stuff is gone. The order of the transforms is reversed from my question AND the original tut.
            NodeTransformation = ScalingM * RotationM * TranslationM;
         }
    //Also reversed.
         OpenTK.Matrix4 GlobalTransformation = NodeTransformation * parentTransform;
    
         //GlobalTransformation = OpenTK.Matrix4.Identity;
         if (BoneMapping.ContainsKey(NodeName) == true)
         {
            uint BoneIndex = BoneMapping[NodeName];
    //Also, Also, reversed.
            BoneInfos[(int)BoneIndex].FinalTransformation = BoneInfos[(int)BoneIndex].OffsetMatrix * GlobalTransformation * GlobalInverseTransform;
         }
    
         for (int i = 0; i < aiNode.ChildCount; i++)
         {
            ReadNodeHierarchy(animationTime, aiNode.Children[i], ref GlobalTransformation);
         }
      }
    

    The Matrix conversion at the top is also correct, as is the Shader code.