I am currently trying to load a skeletal animation from a collada file (.dae). But there seems to be a bug in my code which I can not find myself.
The bug visualizes in not showing the last child's of the hierarchy at all. They are loaded but transformed in a way they disappear.
I try to give as dense code spinets as needed to understand what I am doing. If you need more context please request it!
The code I am using for loading the bone transformations:
Where bone_map
is the map that translate from assimp joint name to my joint array.
mesh.joint_local_transformations = std::make_unique<glm::mat4[]>(ai_mesh->mNumBones);
mesh.joint_global_inverse_transformations = std::make_unique<glm::mat4[]>(ai_mesh->mNumBones);
mesh.joint_count = ai_mesh->mNumBones;
for (uint32_t i = 0; i < ai_mesh->mNumBones; ++i)
{
const auto *joint = ai_mesh->mBones[i];
if (bone_map.contains(joint->mName.data))
{
const auto joint_index = bone_map.at(joint->mName.data);
auto joint_transformation_invert = fromAiMat4(joint->mOffsetMatrix);
const auto *node = find_node(joint->mName, ai_scene);
auto joint_transformation = fromAiMat4(node->mTransformation);
mesh.joint_global_inverse_transformations[joint_index] = joint_transformation_invert;
mesh.joint_local_transformations[joint_index] = joint_transformation;
}
}
The code I am using for loading animation transformations:
anim.joint_transformations = std::make_unique<Animation::JointTransformation[]>(anim.key_frame_count * anim.joint_count);
for (size_t i = 0; i < anim.key_frame_count; ++i)
{
for (size_t j = 0; j < ai_anim->mNumChannels; ++j)
{
if (bone_map.contains(ai_anim->mChannels[j]->mNodeName.data))
{
const auto bone_index = bone_map.at(ai_anim->mChannels[j]->mNodeName.data);
const auto r = ai_anim->mChannels[j]->mRotationKeys[i].mValue;
const auto s = ai_anim->mChannels[j]->mScalingKeys[i].mValue;
const auto p = ai_anim->mChannels[j]->mPositionKeys[i].mValue;
Animation::JointTransformation t{};
t.rotation = {r.w, r.x, r.y, r.z};
t.scale = {s.x, s.y, s.z};
t.translation = {p.x, p.y, p.z};
anim.joint_transformations[anim.joint_count * i + bone_index] = t;
}
}
}
The code I am using to animate:
Where i
is the current key frame index and j
is the current joint index.
Where joints
is the array of transformations that is passed into the shader. (the result)
Notice that a parent has always a lower index than is child. Therefor a parent is always processed first.
const auto *f0 = &(anim->joint_transformations[(i - 1) * anim->joint_count]);
const auto *f1 = &(anim->joint_transformations[(i - 0) * anim->joint_count]);
joints[0] = Animation::mix(f0[0], f1[0], current_time);
for (uint32_t j = 1; j < mesh->joint_count; ++j)
{
const auto &parent = joints[mesh->joint_parent_indices[j]];
joints[j] = parent * Animation::mix(f0[j], f1[j], current_time);
}
for (uint32_t j = 0; j < mesh->joint_count; ++j)
{
joints[j] = joints[j] * mesh->joint_global_inverse_transformations[j];
}
The data structure used (probably not relevant for the problem):
struct Mesh
{
std::unique_ptr<glm::vec3[]> vertices;
std::unique_ptr<uint32_t[]> indices;
std::unique_ptr<glm::vec3[]> normals;
std::unique_ptr<glm::vec2[]> tex_coords;
uint32_t vertex_count;
uint32_t indices_count;
};
struct AnimatedMesh
: public Mesh
{
std::unique_ptr<uint32_t[]> joint_indices;
std::unique_ptr<float[]> joint_weights;
std::unique_ptr<glm::mat4[]> joint_local_transformations;
std::unique_ptr<glm::mat4[]> joint_global_inverse_transformations;
std::unique_ptr<uint32_t[]> joint_parent_indices;
uint32_t joint_count;
uint32_t joints_per_vertex = 3;
};
struct Animation
{
struct JointTransformation
{
glm::vec3 translation;
glm::quat rotation;
glm::vec3 scale;
};
std::unique_ptr<JointTransformation[]> joint_transformations;
std::unique_ptr<float[]> joint_timestamps;
uint32_t joint_count;
uint32_t key_frame_count;
static glm::mat4 mix (const JointTransformation &t0, const JointTransformation &t1, float delta)
{
const auto t = glm::translate(glm::identity<glm::mat4>(), glm::mix(t0.translation, t1.translation, delta));
const auto r = glm::toMat4(glm::slerp(t0.rotation, t1.rotation, delta));
const auto s = glm::scale(glm::identity<glm::mat4>(), glm::mix(t0.scale, t1.scale, delta));
return t * r * s;
}
};
Thanks to everyone who tries to help!