Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 49 additions & 29 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -5375,16 +5375,18 @@ static BoneInfo *LoadBoneInfoGLTF(cgltf_skin skin, int *boneCount)
cgltf_node node = *skin.joints[i];
if (node.name != NULL) strncpy(bones[i].name, node.name, sizeof(bones[i].name) - 1);

// Find parent bone index
// Find parent bone index by walking up the node tree past any
// non-joint ancestors (intermediate transform nodes used by some
// DCC exporters), until we hit a node that is also in skin.joints.
int parentIndex = -1;

for (unsigned int j = 0; j < skin.joints_count; j++)
cgltf_node *ancestor = node.parent;
while (ancestor != NULL && parentIndex == -1)
Comment thread
raysan5 marked this conversation as resolved.
{
if (skin.joints[j] == node.parent)
for (unsigned int j = 0; j < skin.joints_count; j++)
{
parentIndex = (int)j;
break;
if (skin.joints[j] == ancestor) { parentIndex = (int)j; break; }
}
if (parentIndex == -1) ancestor = ancestor->parent;
Comment thread
raysan5 marked this conversation as resolved.
}

bones[i].parent = parentIndex;
Expand Down Expand Up @@ -6518,17 +6520,33 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo
*animCount = (int)data->animations_count;
animations = (ModelAnimation *)RL_CALLOC(data->animations_count, sizeof(ModelAnimation));

Transform worldTransform = { 0 };
cgltf_float cgltf_worldTransform[16] = { 0 };
cgltf_node *node = skin.joints[0];
cgltf_node_transform_world(node->parent, cgltf_worldTransform);
Matrix worldMatrix = {
cgltf_worldTransform[0], cgltf_worldTransform[4], cgltf_worldTransform[8], cgltf_worldTransform[12],
cgltf_worldTransform[1], cgltf_worldTransform[5], cgltf_worldTransform[9], cgltf_worldTransform[13],
cgltf_worldTransform[2], cgltf_worldTransform[6], cgltf_worldTransform[10], cgltf_worldTransform[14],
cgltf_worldTransform[3], cgltf_worldTransform[7], cgltf_worldTransform[11], cgltf_worldTransform[15]
};
MatrixDecompose(worldMatrix, &worldTransform.translation, &worldTransform.rotation, &worldTransform.scale);
// Precompute, per joint, the static transform contributed by any
// intermediate non-joint nodes between the joint and its nearest
// joint ancestor. This handles exporters (e.g. wow.export) that
// store bone offsets on dummy parent nodes rather than on the
// joints themselves. Depends only on the skin, not the animation.
int jointCount = (int)skin.joints_count;
Matrix *extOffset = (Matrix *)RL_MALLOC(jointCount*sizeof(Matrix));
Comment thread
raysan5 marked this conversation as resolved.
for (int k = 0; k < jointCount; k++)
{
extOffset[k] = MatrixIdentity();
cgltf_node *n = skin.joints[k]->parent;
while (n != NULL)
{
bool isJoint = false;
for (int jj = 0; jj < jointCount; jj++)
{
if (skin.joints[jj] == n) { isJoint = true; break; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting

}
if (isJoint) break;
Matrix nm = MatrixMultiply(MatrixMultiply(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting or try to write this more readable.

MatrixScale(n->scale[0], n->scale[1], n->scale[2]),
QuaternionToMatrix((Quaternion){ n->rotation[0], n->rotation[1], n->rotation[2], n->rotation[3] })),
MatrixTranslate(n->translation[0], n->translation[1], n->translation[2]));
extOffset[k] = MatrixMultiply(extOffset[k], nm);
n = n->parent;
}
}

for (unsigned int a = 0; a < data->animations_count; a++)
{
Expand Down Expand Up @@ -6637,27 +6655,29 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo
}
}

animations[a].keyframePoses[j][k] = (Transform){
.translation = translation,
.rotation = rotation,
.scale = scale
};
// Compose joint local TRS, then prepend the static
// intermediate non-joint offsets so the final TRS is
// expressed relative to the joint's skeleton parent.
Matrix S = MatrixScale(scale.x, scale.y, scale.z);
Matrix R = QuaternionToMatrix(rotation);
Matrix T = MatrixTranslate(translation.x, translation.y, translation.z);
Matrix jointLocal = MatrixMultiply(MatrixMultiply(S, R), T);
Matrix combined = MatrixMultiply(jointLocal, extOffset[k]);

Transform tr;
MatrixDecompose(combined, &tr.translation, &tr.rotation, &tr.scale);
animations[a].keyframePoses[j][k] = tr;
}

Transform *root = &animations[a].keyframePoses[j][0];
root->rotation = QuaternionMultiply(worldTransform.rotation, root->rotation);
root->scale = Vector3Multiply(root->scale, worldTransform.scale);
root->translation = Vector3Multiply(root->translation, worldTransform.scale);
root->translation = Vector3RotateByQuaternion(root->translation, worldTransform.rotation);
root->translation = Vector3Add(root->translation, worldTransform.translation);

BuildPoseFromParentJoints(bones, animations[a].boneCount, animations[a].keyframePoses[j]);
}

TRACELOG(LOG_INFO, "MODEL: [%s] Loaded animation: %s | Frames: %d | Duration: %fs", fileName, (animData.name != NULL)? animData.name : "NULL", animations[a].keyframeCount, animDuration);
RL_FREE(boneChannels);
RL_FREE(bones);
}

RL_FREE(extOffset);
}

if (data->skins_count > 1)
Expand Down
Loading