c++jsonanimationgraphicsgltf

gltf how are the animation matrices supposed to be computed?


I am trying to get animations to work for complex glTF models I am trying the RiggedFigure.

In particular I am trying to calculate the globalTransformOfJointNode as described here.

For that I have this function:

std::vector<Eigen::Matrix4f> GetGlobalJointMatrices(
    const std::vector<Eigen::Matrix4f>& local_matrices,
    const std::vector<Eigen::Matrix4f>& animation_matrices,
    const std::map<int, int>& skeleton,
    const int start_joint)
{
    std::vector<Eigen::Matrix4f> skeleton_matrices(skeleton.size());
    skeleton_matrices[0] = Eigen::Matrix4f::Identity();

    for(auto [child, parent]: skeleton)
    {
        skeleton_matrices[child - start_joint] = skeleton_matrices[parent - start_joint] *
            local_matrices[child - start_joint] * animation_matrices[child - start_joint];
    }

    return skeleton_matrices;
}

In here local_matrices refers to the local transform of each node in the skeleton as described in the nodes array.

animation_matrices are only the animations that target nodes in the skin.

For both arrays, since the nodes are contiguous, the index i contains the value corresponding to node i + start_joint. I have verified that the animations are loaded correctly and in the correct order.

This is the result of doing:

skeleton_matrices[child - start_joint] = skeleton_matrices[parent - start_joint] *
            local_matrices[child - start_joint];

enter image description here

If I try to apply the animation matrices Instead I get:

enter image description here

Putting the animation matrices at other points results in similar behaviour.

I am sure I am not loading the martices incorrectly, for example these are the matrices that target nodes 6 and 10:

animation targetting node:10
rotation: 0.0245355 -0.319997    0.9446 0.0687827
translation: -0.00234646  -0.0661734   0.0278567
scales: 1 1 1
node 6:
animation targetting node:6
rotation: -0.0341418  -0.319178   0.946171 -0.0414678
translation: -0.00145852  -0.0661988   0.0278567

I went and verified that those values are correct manually, they are.

I don't understand what is wrong.


Solution

  • I feel the spec should be more clear about this. So as it turns out you should NOT apply the transform of thr nodes and only calculate the animation matrix, the animation matrices already contain the node matrix premultiplied.

    TL;DR: Do this:

    skeleton_matrices[child - start_joint] = skeleton_matrices[parent - start_joint] *
                animation_matrices[child - start_joint];
    

    enter image description here