2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
36 mdl_keyframe
*anim_data
;
47 u32 lower
, upper
, target
, pole
;
58 static u32
skeleton_bone_id( struct skeleton
*skele
, const char *name
)
60 for( u32 i
=1; i
<skele
->bone_count
; i
++ ){
61 if( !strcmp( skele
->bones
[i
].name
, name
))
65 vg_error( "skeleton_bone_id( *, \"%s\" );\n", name
);
66 vg_fatal_error( "Bone does not exist\n" );
71 static void keyframe_copy_pose( mdl_keyframe
*kfa
, mdl_keyframe
*kfb
,
74 for( int i
=0; i
<num
; i
++ )
79 /* apply a rotation from the perspective of root */
80 static void keyframe_rotate_around( mdl_keyframe
*kf
,
81 v3f origin
, v3f offset
, v4f q
)
84 v3_add( kf
->co
, offset
, co
);
85 v3_sub( co
, origin
, v0
);
87 v3_add( v0
, origin
, co
);
88 v3_sub( co
, offset
, kf
->co
);
90 q_mul( q
, kf
->q
, kf
->q
);
95 * Lerp between two sets of keyframes and store in dest. Rotations use Nlerp.
97 static void keyframe_lerp_pose( mdl_keyframe
*kfa
, mdl_keyframe
*kfb
,
98 float t
, mdl_keyframe
*kfd
, int count
)
101 keyframe_copy_pose( kfa
, kfd
, count
);
104 else if( t
>= 0.9999f
){
105 keyframe_copy_pose( kfb
, kfd
, count
);
109 for( int i
=0; i
<count
; i
++ ){
110 v3_lerp( kfa
[i
].co
, kfb
[i
].co
, t
, kfd
[i
].co
);
111 q_nlerp( kfa
[i
].q
, kfb
[i
].q
, t
, kfd
[i
].q
);
112 v3_lerp( kfa
[i
].s
, kfb
[i
].s
, t
, kfd
[i
].s
);
117 void skeleton_lerp_pose( struct skeleton
*skele
,
118 mdl_keyframe
*kfa
, mdl_keyframe
*kfb
, float t
,
121 keyframe_lerp_pose( kfa
, kfb
, t
, kfd
, skele
->bone_count
-1 );
124 static void skeleton_copy_pose( struct skeleton
*skele
,
125 mdl_keyframe
*kfa
, mdl_keyframe
*kfd
)
127 keyframe_copy_pose( kfa
, kfd
, skele
->bone_count
-1 );
131 * Sample animation between 2 closest frames using time value. Output is a
132 * keyframe buffer that is allocated with an appropriate size
134 static void skeleton_sample_anim( struct skeleton
*skele
,
135 struct skeleton_anim
*anim
,
137 mdl_keyframe
*output
)
139 f32 animtime
= fmodf( time
*anim
->rate
, anim
->length
),
140 animframe
= floorf( animtime
),
141 t
= animtime
- animframe
;
143 u32 frame
= (u32
)animframe
% anim
->length
,
144 next
= (frame
+1) % anim
->length
;
146 mdl_keyframe
*base
= anim
->anim_data
+ (skele
->bone_count
-1)*frame
,
147 *nbase
= anim
->anim_data
+ (skele
->bone_count
-1)*next
;
149 skeleton_lerp_pose( skele
, base
, nbase
, t
, output
);
152 static int skeleton_sample_anim_clamped( struct skeleton
*skele
,
153 struct skeleton_anim
*anim
,
155 mdl_keyframe
*output
)
157 float end
= (float)(anim
->length
-1) / anim
->rate
;
158 skeleton_sample_anim( skele
, anim
, vg_minf( end
, time
), output
);
166 typedef enum anim_apply
169 k_anim_apply_defer_ik
,
170 k_anim_apply_deffered_only
,
171 k_anim_apply_absolute
176 int should_apply_bone( struct skeleton
*skele
, u32 id
, anim_apply type
)
178 struct skeleton_bone
*sb
= &skele
->bones
[ id
],
179 *sp
= &skele
->bones
[ sb
->parent
];
181 if( type
== k_anim_apply_defer_ik
){
182 if( ((sp
->flags
& k_bone_flag_ik
) && !(sb
->flags
& k_bone_flag_ik
))
193 else if( type
== k_anim_apply_deffered_only
){
204 * Apply block of keyframes to skeletons final pose
206 static void skeleton_apply_pose( struct skeleton
*skele
, mdl_keyframe
*pose
,
207 anim_apply passtype
, m4x3f
*final_mtx
){
208 if( passtype
== k_anim_apply_absolute
){
209 for( u32 i
=1; i
<skele
->bone_count
; i
++ ){
210 mdl_keyframe
*kf
= &pose
[i
-1];
212 v3f
*posemtx
= final_mtx
[i
];
214 q_m3x3( kf
->q
, posemtx
);
215 v3_copy( kf
->co
, posemtx
[3] );
220 m4x3_identity( final_mtx
[0] );
221 skele
->bones
[0].defer
= 0;
222 skele
->bones
[0].flags
&= ~k_bone_flag_ik
;
224 for( u32 i
=1; i
<skele
->bone_count
; i
++ ){
225 struct skeleton_bone
*sb
= &skele
->bones
[i
],
226 *sp
= &skele
->bones
[sb
->parent
];
228 if( !should_apply_bone( skele
, i
, passtype
) )
237 v3_sub( skele
->bones
[i
].co
, skele
->bones
[sb
->parent
].co
, temp_delta
);
240 mdl_keyframe
*kf
= &pose
[i
-1];
241 q_m3x3( kf
->q
, posemtx
);
242 v3_copy( kf
->co
, posemtx
[3] );
243 v3_add( temp_delta
, posemtx
[3], posemtx
[3] );
246 m4x3_mul( final_mtx
[ sb
->parent
], posemtx
, final_mtx
[i
] );
251 * Take the final matrices and decompose it into an absolute positioned anim
253 static void skeleton_decompose_mtx_absolute( struct skeleton
*skele
,
256 for( u32 i
=1; i
<skele
->bone_count
; i
++ ){
257 struct skeleton_bone
*sb
= &skele
->bones
[i
];
258 mdl_keyframe
*kf
= &anim
[i
-1];
259 m4x3_decompose( final_mtx
[i
], kf
->co
, kf
->q
, kf
->s
);
264 * creates the reference inverse matrix for an IK bone, as it has an initial
265 * intrisic rotation based on the direction that the IK is setup..
267 static void skeleton_inverse_for_ik( struct skeleton
*skele
,
269 u32 id
, m3x3f inverse
)
271 v3_copy( ivaxis
, inverse
[0] );
272 v3_copy( skele
->bones
[id
].end
, inverse
[1] );
273 v3_normalize( inverse
[1] );
274 v3_cross( inverse
[0], inverse
[1], inverse
[2] );
275 m3x3_transpose( inverse
, inverse
);
279 * Creates inverse rotation matrices which the IK system uses.
281 static void skeleton_create_inverses( struct skeleton
*skele
)
283 /* IK: inverse 'plane-bone space' axis '(^axis,^bone,...)[base] */
284 for( u32 i
=0; i
<skele
->ik_count
; i
++ ){
285 struct skeleton_ik
*ik
= &skele
->ik
[i
];
288 v3f iv0
, iv1
, ivaxis
;
289 v3_sub( skele
->bones
[ik
->target
].co
, skele
->bones
[ik
->lower
].co
, iv0
);
290 v3_sub( skele
->bones
[ik
->pole
].co
, skele
->bones
[ik
->lower
].co
, iv1
);
291 v3_cross( iv0
, iv1
, ivaxis
);
292 v3_normalize( ivaxis
);
294 skeleton_inverse_for_ik( skele
, ivaxis
, ik
->lower
, ik
->ia
);
295 skeleton_inverse_for_ik( skele
, ivaxis
, ik
->upper
, ik
->ib
);
300 * Apply a model matrix to all bones, should be done last
303 void skeleton_apply_transform( struct skeleton
*skele
, m4x3f transform
,
306 for( u32 i
=0; i
<skele
->bone_count
; i
++ ){
307 struct skeleton_bone
*sb
= &skele
->bones
[i
];
308 m4x3_mul( transform
, final_mtx
[i
], final_mtx
[i
] );
313 * Apply an inverse matrix to all bones which maps vertices from bind space into
314 * bone relative positions
316 static void skeleton_apply_inverses( struct skeleton
*skele
, m4x3f
*final_mtx
){
317 for( u32 i
=0; i
<skele
->bone_count
; i
++ ){
318 struct skeleton_bone
*sb
= &skele
->bones
[i
];
320 m3x3_identity( inverse
);
321 v3_negate( sb
->co
, inverse
[3] );
323 m4x3_mul( final_mtx
[i
], inverse
, final_mtx
[i
] );
328 * Apply all IK modifiers (2 bone ik reference from blender is supported)
330 static void skeleton_apply_ik_pass( struct skeleton
*skele
, m4x3f
*final_mtx
){
331 for( u32 i
=0; i
<skele
->ik_count
; i
++ ){
332 struct skeleton_ik
*ik
= &skele
->ik
[i
];
334 v3f v0
, /* base -> target */
335 v1
, /* base -> pole */
342 v3_copy( final_mtx
[ik
->lower
][3], co_base
);
343 v3_copy( final_mtx
[ik
->target
][3], co_target
);
344 v3_copy( final_mtx
[ik
->pole
][3], co_pole
);
346 v3_sub( co_target
, co_base
, v0
);
347 v3_sub( co_pole
, co_base
, v1
);
348 v3_cross( v0
, v1
, vaxis
);
349 v3_normalize( vaxis
);
351 v3_cross( vaxis
, v0
, v1
);
353 /* localize problem into [x:v0,y:v1] 2d plane */
354 v2f base
= { v3_dot( v0
, co_base
), v3_dot( v1
, co_base
) },
355 end
= { v3_dot( v0
, co_target
), v3_dot( v1
, co_target
) },
358 /* Compute angles (basic trig)*/
360 v2_sub( end
, base
, delta
);
363 l1
= v3_length( skele
->bones
[ik
->lower
].end
),
364 l2
= v3_length( skele
->bones
[ik
->upper
].end
),
365 d
= vg_clampf( v2_length(delta
), fabsf(l1
- l2
), l1
+l2
-0.00001f
),
366 c
= acosf( (l1
*l1
+ d
*d
- l2
*l2
) / (2.0f
*l1
*d
) ),
367 rot
= atan2f( delta
[1], delta
[0] ) + c
- VG_PIf
/2.0f
;
369 knee
[0] = sinf(-rot
) * l1
;
370 knee
[1] = cosf(-rot
) * l1
;
372 m4x3_identity( final_mtx
[ik
->lower
] );
373 m4x3_identity( final_mtx
[ik
->upper
] );
375 /* create rotation matrix */
377 v3_muladds( co_base
, v0
, knee
[0], co_knee
);
378 v3_muladds( co_knee
, v1
, knee
[1], co_knee
);
379 vg_line( co_base
, co_knee
, 0xff00ff00 );
382 v3_copy( vaxis
, transform
[0] );
383 v3_muls( v0
, knee
[0], transform
[1] );
384 v3_muladds( transform
[1], v1
, knee
[1], transform
[1] );
385 v3_normalize( transform
[1] );
386 v3_cross( transform
[0], transform
[1], transform
[2] );
387 v3_copy( co_base
, transform
[3] );
389 m3x3_mul( transform
, ik
->ia
, transform
);
390 m4x3_copy( transform
, final_mtx
[ik
->lower
] );
392 /* upper/knee bone */
393 v3_copy( vaxis
, transform
[0] );
394 v3_sub( co_target
, co_knee
, transform
[1] );
395 v3_normalize( transform
[1] );
396 v3_cross( transform
[0], transform
[1], transform
[2] );
397 v3_copy( co_knee
, transform
[3] );
399 m3x3_mul( transform
, ik
->ib
, transform
);
400 m4x3_copy( transform
, final_mtx
[ik
->upper
] );
405 * Applies the typical operations that you want for an IK rig:
406 * Pose, IK, Pose(deferred), Inverses, Transform
408 static void skeleton_apply_standard( struct skeleton
*skele
, mdl_keyframe
*pose
,
409 m4x3f transform
, m4x3f
*final_mtx
){
410 skeleton_apply_pose( skele
, pose
, k_anim_apply_defer_ik
, final_mtx
);
411 skeleton_apply_ik_pass( skele
, final_mtx
);
412 skeleton_apply_pose( skele
, pose
, k_anim_apply_deffered_only
, final_mtx
);
413 skeleton_apply_inverses( skele
, final_mtx
);
414 skeleton_apply_transform( skele
, transform
, final_mtx
);
418 * Get an animation by name
420 static struct skeleton_anim
*skeleton_get_anim( struct skeleton
*skele
,
422 for( u32 i
=0; i
<skele
->anim_count
; i
++ ){
423 struct skeleton_anim
*anim
= &skele
->anims
[i
];
425 if( !strcmp( anim
->name
, name
) )
429 vg_error( "skeleton_get_anim( *, \"%s\" )\n", name
);
430 vg_fatal_error( "Invalid animation name\n" );
435 static void skeleton_alloc_from( struct skeleton
*skele
,
438 mdl_armature
*armature
){
439 skele
->bone_count
= armature
->bone_count
+1;
440 skele
->anim_count
= armature
->anim_count
;
442 skele
->collider_count
= 0;
444 for( u32 i
=0; i
<armature
->bone_count
; i
++ ){
445 mdl_bone
*bone
= mdl_arritm( &mdl
->bones
, armature
->bone_start
+i
);
447 if( bone
->flags
& k_bone_flag_ik
)
451 skele
->collider_count
++;
454 u32 bone_size
= sizeof(struct skeleton_bone
) * skele
->bone_count
,
455 ik_size
= sizeof(struct skeleton_ik
) * skele
->ik_count
,
456 mtx_size
= sizeof(m4x3f
) * skele
->bone_count
,
457 anim_size
= sizeof(struct skeleton_anim
) * skele
->anim_count
;
459 skele
->bones
= vg_linear_alloc( lin_alloc
, bone_size
);
460 skele
->ik
= vg_linear_alloc( lin_alloc
, ik_size
);
461 //skele->final_mtx = vg_linear_alloc( lin_alloc, mtx_size );
462 skele
->anims
= vg_linear_alloc( lin_alloc
, anim_size
);
464 memset( skele
->bones
, 0, bone_size
);
465 memset( skele
->ik
, 0, ik_size
);
466 //memset( skele->final_mtx, 0, mtx_size );
467 memset( skele
->anims
, 0, anim_size
);
470 static void skeleton_fatal_err(void){
471 vg_fatal_error( "Skeleton setup failed" );
474 /* Setup an animated skeleton from model. mdl's metadata should stick around */
475 static void skeleton_setup( struct skeleton
*skele
,
476 void *lin_alloc
, mdl_context
*mdl
){
477 u32 ik_count
= 0, collider_count
= 0;
478 skele
->bone_count
= 0;
480 //skele->final_mtx = NULL;
483 if( !mdl
->armatures
.count
){
484 vg_error( "No skeleton in model\n" );
485 skeleton_fatal_err();
488 mdl_armature
*armature
= mdl_arritm( &mdl
->armatures
, 0 );
489 skeleton_alloc_from( skele
, lin_alloc
, mdl
, armature
);
491 for( u32 i
=0; i
<armature
->bone_count
; i
++ ){
492 mdl_bone
*bone
= mdl_arritm( &mdl
->bones
, armature
->bone_start
+i
);
493 struct skeleton_bone
*sb
= &skele
->bones
[i
+1];
495 v3_copy( bone
->co
, sb
->co
);
496 v3_copy( bone
->end
, sb
->end
);
498 sb
->parent
= bone
->parent
;
499 sb
->name
= mdl_pstr( mdl
, bone
->pstr_name
);
500 sb
->flags
= bone
->flags
;
501 sb
->collider
= bone
->collider
;
502 sb
->orig_bone
= bone
;
504 if( sb
->flags
& k_bone_flag_ik
){
505 skele
->bones
[ sb
->parent
].flags
|= k_bone_flag_ik
;
507 if( ik_count
== skele
->ik_count
){
508 vg_error( "Too many ik bones, corrupt model file\n" );
509 skeleton_fatal_err();
512 struct skeleton_ik
*ik
= &skele
->ik
[ ik_count
++ ];
514 ik
->lower
= bone
->parent
;
515 ik
->target
= bone
->ik_target
;
516 ik
->pole
= bone
->ik_pole
;
519 box_copy( bone
->hitbox
, sb
->hitbox
);
521 if( bone
->collider
){
522 if( collider_count
== skele
->collider_count
){
523 vg_error( "Too many collider bones\n" );
524 skeleton_fatal_err();
531 /* fill in implicit root bone */
532 v3_zero( skele
->bones
[0].co
);
533 v3_copy( (v3f
){0.0f
,1.0f
,0.0f
}, skele
->bones
[0].end
);
534 skele
->bones
[0].parent
= 0xffffffff;
535 skele
->bones
[0].flags
= 0;
536 skele
->bones
[0].name
= "[root]";
538 /* process animation quick refs */
539 for( u32 i
=0; i
<skele
->anim_count
; i
++ ){
540 mdl_animation
*anim
=
541 mdl_arritm( &mdl
->animations
, armature
->anim_start
+i
);
543 skele
->anims
[i
].rate
= anim
->rate
;
544 skele
->anims
[i
].length
= anim
->length
;
545 skele
->anims
[i
].name
= mdl_pstr(mdl
, anim
->pstr_name
);
546 skele
->anims
[i
].anim_data
=
547 mdl_arritm( &mdl
->keyframes
, anim
->offset
);
549 vg_info( "animation[ %f, %u ] '%s'\n", anim
->rate
,
551 skele
->anims
[i
].name
);
554 skeleton_create_inverses( skele
);
555 vg_success( "Loaded skeleton with %u bones\n", skele
->bone_count
);
556 vg_success( " %u colliders\n", skele
->collider_count
);
559 static void skeleton_debug( struct skeleton
*skele
, m4x3f
*final_mtx
){
560 for( u32 i
=1; i
<skele
->bone_count
; i
++ ){
561 struct skeleton_bone
*sb
= &skele
->bones
[i
];
564 v3_copy( sb
->co
, p0
);
565 v3_add( p0
, sb
->end
, p1
);
567 m4x3_mulv( final_mtx
[i
], p0
, p0
);
568 m4x3_mulv( final_mtx
[i
], p1
, p1
);
570 if( sb
->flags
& k_bone_flag_deform
){
571 if( sb
->flags
& k_bone_flag_ik
){
572 vg_line( p0
, p1
, 0xff0000ff );
575 vg_line( p0
, p1
, 0xffcccccc );
579 vg_line( p0
, p1
, 0xff00ffff );
583 #endif /* SKELETON_H */