allow armature fixups on model load
[carveJwlIkooP6JGAAIwe30JlM.git] / player_render.c
index b0416f5f3e927c946ebbe7a7ea6b46719dea1c61..ad56c241ec10f11dff072567cb1537a564b3a72b 100644 (file)
@@ -42,6 +42,7 @@ static void player_load_animation_reference( const char *path ){
    localplayer.id_wheel_r    = skeleton_bone_id( sk, "wheel.R" );
    localplayer.id_ik_knee_l  = skeleton_bone_id( sk, "knee.L" );
    localplayer.id_ik_knee_r  = skeleton_bone_id( sk, "knee.R" );
+   localplayer.id_eyes       = skeleton_bone_id( sk, "eyes" );
 
    setup_ragdoll_from_skeleton( sk, &localplayer.ragdoll );
 
@@ -55,8 +56,8 @@ static void player_load_animation_reference( const char *path ){
 /* TODO: Standard model load */
 
 static void dynamic_model_load( mdl_context *ctx,
-                                   struct dynamic_model_1texture *mdl, 
-                                   const char *path ){
+                                struct dynamic_model_1texture *mdl, 
+                                const char *path, u32 *fixup_table ){
    if( !mdl_arrcount( &ctx->textures ) )
       vg_fatal_error( "No texture in model" );
 
@@ -68,7 +69,7 @@ static void dynamic_model_load( mdl_context *ctx,
                             VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
                             &mdl->texture );
 
-   mdl_async_load_glmesh( ctx, &mdl->mesh );
+   mdl_async_load_glmesh( ctx, &mdl->mesh, fixup_table );
 }
 
 static void dynamic_model_unload( struct dynamic_model_1texture *mdl ){
@@ -86,7 +87,7 @@ static void player_board_load( struct player_board *board,
    mdl_open( &ctx, path, vg_mem.scratch );
    mdl_load_metadata_block( &ctx, vg_mem.scratch );
 
-   dynamic_model_load( &ctx, &board->mdl, path );
+   dynamic_model_load( &ctx, &board->mdl, path, NULL );
 
    mdl_array_ptr markers;
    MDL_LOAD_ARRAY( &ctx, &markers, ent_marker, vg_mem.scratch );
@@ -142,8 +143,30 @@ static void player_model_load( struct player_model *board, const char *path){
    mdl_open( &ctx, path, vg_mem.scratch );
    mdl_load_metadata_block( &ctx, vg_mem.scratch );
 
-   dynamic_model_load( &ctx, &board->mdl, path );
+   if( !ctx.armatures.count )
+      vg_fatal_error( "No armature in playermodel\n" );
 
+   mdl_armature *armature = mdl_arritm( &ctx.armatures, 0 );
+
+   u32 fixup_table[ armature->bone_count+1 ];
+   for( u32 i=0; i<armature->bone_count+1; i ++ )
+      fixup_table[i] = 0;
+
+   for( u32 i=1; i<localplayer.skeleton.bone_count; i ++ ){
+      struct skeleton_bone *sb = &localplayer.skeleton.bones[i];
+      u32 hash = vg_strdjb2( sb->name );
+
+      for( u32 j=1; j<armature->bone_count; j ++ ){
+         mdl_bone *bone = mdl_arritm( &ctx.bones, armature->bone_start+j );
+
+         if( mdl_pstreq( &ctx, bone->pstr_name, sb->name, hash ) ){
+            fixup_table[j+1] = i;
+            break;
+         }
+      }
+   }
+
+   dynamic_model_load( &ctx, &board->mdl, path, fixup_table );
    mdl_close( &ctx );
 }
 
@@ -202,6 +225,8 @@ static void player__animate(void){
       localplayer.holdout_time -= vg.time_frame_delta * 2.0f;
    }
 
+   v3_fill( pose->keyframes[ localplayer.id_eyes-1 ].co, sinf(vg.time) );
+
    apply_full_skeleton_pose( sk, &localplayer.pose, localplayer.final_mtx );
    skeleton_debug( sk, localplayer.final_mtx );