medfall

A super great game engine
Log | Files | Refs

commit 5b8c06a724b3d487bde991c14faeed5ef7777928
parent dd642aa15d1ddd48a4c24b30a4194d74406317ae
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu, 30 May 2019 23:46:26 +0300

Handle multiple roots in skin

Diffstat:
Mgltf.cc | 23++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/gltf.cc b/gltf.cc @@ -141,11 +141,15 @@ void SampleAnimationClip( const AnimationReel & reel, u32 clip_idx, float t, flo void ComputeMatrixPalette( const AnimationReel & animation, const SQT * sqts, Mat4 * joint_poses, Mat4 * skinning_matrices ) { u8 joint_idx = animation.root_joint; - joint_poses[ joint_idx ] = SQTToMat4( sqts[ joint_idx ] ); - for( u32 i = 1; i < animation.num_joints; i++ ) { + for( u32 i = 0; i < animation.num_joints; i++ ) { joint_idx = animation.joints[ joint_idx ].next; u8 parent = animation.joints[ joint_idx ].parent; - joint_poses[ joint_idx ] = joint_poses[ parent ] * SQTToMat4( sqts[ joint_idx ] ); + if( parent != U8_MAX ) { + joint_poses[ joint_idx ] = joint_poses[ parent ] * SQTToMat4( sqts[ joint_idx ] ); + } + else { + joint_poses[ joint_idx ] = SQTToMat4( sqts[ joint_idx ] ); + } } for( u32 i = 0; i < animation.num_joints; i++ ) { @@ -198,19 +202,16 @@ static void LoadSkin( const cgltf_skin * skin ) { model.animation.joints = ( AnimationReel::Joint * ) malloc( sizeof( AnimationReel::Joint ) * skin->joints_count ); model.animation.num_joints = skin->joints_count; - // we want to find the root of the subtree representing the skeleton - // mark all subtree nodes and then walk up the tree to find the root for( size_t i = 0; i < skin->joints_count; i++ ) { skin->joints[ i ]->camera = ( cgltf_camera * ) ( i + 1 ); } - cgltf_node * root = skin->joints[ 0 ]; - while( root->parent != NULL && root->parent->camera != NULL ) { - root = root->parent; - } - u8 * prev_ptr = &model.animation.root_joint; - AddJoint( skin, root, &prev_ptr ); + for( size_t i = 0; i < skin->joints_count; i++ ) { + if( skin->joints[ i ]->parent == NULL || skin->joints[ i ]->parent->camera == NULL ) { + AddJoint( skin, skin->joints[ i ], &prev_ptr ); + } + } } const char * pathtype( cgltf_animation_path_type type ) {