X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=skeleton.h;h=2be9a9aaff7ac7bd8945f9b5e37ed5d0ca0ff7ff;hb=8a31c1ffcb632b9b6d1702332f0d75d609c0a87b;hp=2fcfb5023f171540b517c989a918914061a8c754;hpb=a1056ed8198f0f5be0e0f341da8bd49aa6c47198;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/skeleton.h b/skeleton.h index 2fcfb50..2be9a9a 100644 --- a/skeleton.h +++ b/skeleton.h @@ -61,7 +61,7 @@ VG_STATIC u32 skeleton_bone_id( struct skeleton *skele, const char *name ) } vg_error( "skeleton_bone_id( *, \"%s\" );\n", name ); - vg_fatal_exit_loop( "Bone does not exist\n" ); + vg_fatal_error( "Bone does not exist\n" ); return 0; } @@ -73,6 +73,22 @@ VG_STATIC void keyframe_copy_pose( mdl_keyframe *kfa, mdl_keyframe *kfb, kfb[i] = kfa[i]; } + +/* apply a rotation from the perspective of root */ +VG_STATIC void keyframe_rotate_around( mdl_keyframe *kf, + v3f origin, v3f offset, v4f q ) +{ + v3f v0, co; + v3_add( kf->co, offset, co ); + v3_sub( co, origin, v0 ); + q_mulv( q, v0, v0 ); + v3_add( v0, origin, co ); + v3_sub( co, offset, kf->co ); + + q_mul( q, kf->q, kf->q ); + q_normalize( kf->q ); +} + /* * Lerp between two sets of keyframes and store in dest. Rotations use Nlerp. */ @@ -118,13 +134,13 @@ VG_STATIC void skeleton_sample_anim( struct skeleton *skele, float time, mdl_keyframe *output ) { - float animtime = time*anim->rate; + f32 animtime = fmodf( time*anim->rate, anim->length ), + animframe = floorf( animtime ), + t = animtime - animframe; - u32 frame = ((u32)animtime) % anim->length, + u32 frame = (u32)animframe % anim->length, next = (frame+1) % anim->length; - float t = vg_fractf( animtime ); - mdl_keyframe *base = anim->anim_data + (skele->bone_count-1)*frame, *nbase = anim->anim_data + (skele->bone_count-1)*next; @@ -149,7 +165,8 @@ typedef enum anim_apply { k_anim_apply_always, k_anim_apply_defer_ik, - k_anim_apply_deffered_only + k_anim_apply_deffered_only, + k_anim_apply_absolute } anim_apply; @@ -187,6 +204,18 @@ int should_apply_bone( struct skeleton *skele, u32 id, anim_apply type ) VG_STATIC void skeleton_apply_pose( struct skeleton *skele, mdl_keyframe *pose, anim_apply passtype ) { + if( passtype == k_anim_apply_absolute ){ + for( u32 i=1; ibone_count; i++ ){ + mdl_keyframe *kf = &pose[i-1]; + + v3f *posemtx = skele->final_mtx[i]; + + q_m3x3( kf->q, posemtx ); + v3_copy( kf->co, posemtx[3] ); + } + return; + } + m4x3_identity( skele->final_mtx[0] ); skele->bones[0].defer = 0; skele->bones[0].flags &= ~k_bone_flag_ik; @@ -217,6 +246,18 @@ VG_STATIC void skeleton_apply_pose( struct skeleton *skele, mdl_keyframe *pose, } } +/* + * Take the final matrices and decompose it into an absolute positioned anim + */ +VG_STATIC void skeleton_decompose_mtx_absolute( struct skeleton *skele, + mdl_keyframe *anim ){ + for( u32 i=1; ibone_count; i++ ){ + struct skeleton_bone *sb = &skele->bones[i]; + mdl_keyframe *kf = &anim[i-1]; + m4x3_decompose( skele->final_mtx[i], kf->co, kf->q, kf->s ); + } +} + /* * creates the reference inverse matrix for an IK bone, as it has an initial * intrisic rotation based on the direction that the IK is setup.. @@ -387,7 +428,7 @@ VG_STATIC struct skeleton_anim *skeleton_get_anim( struct skeleton *skele, } vg_error( "skeleton_get_anim( *, \"%s\" )\n", name ); - vg_fatal_exit_loop( "Invalid animation name\n" ); + vg_fatal_error( "Invalid animation name\n" ); return NULL; } @@ -421,11 +462,16 @@ VG_STATIC void skeleton_alloc_from( struct skeleton *skele, skele->ik = vg_linear_alloc( lin_alloc, ik_size ); skele->final_mtx = vg_linear_alloc( lin_alloc, mtx_size ); skele->anims = vg_linear_alloc( lin_alloc, anim_size ); + + memset( skele->bones, 0, bone_size ); + memset( skele->ik, 0, ik_size ); + memset( skele->final_mtx, 0, mtx_size ); + memset( skele->anims, 0, anim_size ); } VG_STATIC void skeleton_fatal_err(void) { - vg_fatal_exit_loop( "Skeleton setup failed" ); + vg_fatal_error( "Skeleton setup failed" ); } /* Setup an animated skeleton from model. mdl's metadata should stick around */