allow armature fixups on model load
authorhgn <hgodden00@gmail.com>
Sat, 25 Nov 2023 22:10:04 +0000 (22:10 +0000)
committerhgn <hgodden00@gmail.com>
Sat, 25 Nov 2023 22:10:04 +0000 (22:10 +0000)
12 files changed:
font.h
gui.h
menu.h
model.h
player.h
player_render.c
player_render.h
shaders/model_board_view.h
shaders/model_character_view.h
world_gate.c
world_render.c
world_sfd.c

diff --git a/font.h b/font.h
index a5449f906a6860225a1d31aa93ceff4859ed568f..0647b771a1457ce793466b66370c7a1b4911b77b 100644 (file)
--- a/font.h
+++ b/font.h
@@ -97,7 +97,7 @@ static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
    void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
    mdl_fread_pack_file( &font->mdl, &tex0->file, data );
 
-   mdl_async_load_glmesh( &font->mdl, &font->mesh );
+   mdl_async_load_glmesh( &font->mdl, &font->mesh, NULL );
    vg_tex2d_load_qoi_async( data, tex0->file.pack_size, 
                             VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
                             &font->texture );
diff --git a/gui.h b/gui.h
index 2d984f36082b797747a7367a532ea272590ce554..1414bd587f6edb2513dba20a2dc0dd7da24c05d0 100644 (file)
--- a/gui.h
+++ b/gui.h
@@ -344,7 +344,7 @@ static void gui_init(void){
                             VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
                             &gui.icons_texture );
 
-   mdl_async_load_glmesh( &gui.model_icons, &gui.icons_mesh );
+   mdl_async_load_glmesh( &gui.model_icons, &gui.icons_mesh, NULL );
    mdl_close( &gui.model_icons );
 }
 
diff --git a/menu.h b/menu.h
index 53cb52d30dc4010c007adb3665af42dbb491f07b..737c849e4b892c53e532694ba422259d26010684 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -154,7 +154,7 @@ static void menu_init(void){
    void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
    mdl_fread_pack_file( &menu.model, &tex0->file, data );
 
-   mdl_async_load_glmesh( &menu.model, &menu.mesh );
+   mdl_async_load_glmesh( &menu.model, &menu.mesh, NULL );
    vg_tex2d_load_qoi_async( data, tex0->file.pack_size, 
                             VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
                             &menu.texture );
diff --git a/model.h b/model.h
index c054d18127e27e85113a7437cff74f3748ac465a..476079474aae4201fcf752a056b3cd6d86b207fe 100644 (file)
--- a/model.h
+++ b/model.h
@@ -608,8 +608,8 @@ static void async_mdl_load_glmesh( void *payload, u32 size )
                            job->indices, job->indice_count );
 }
 
-static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
-{
+static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh
+                                   u32 *fixup_table ){
    mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" );
    mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" );
 
@@ -635,6 +635,16 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
       mdl_load_array_file_buffer( mdl, arr_indices, job->indices, 
                                   sizeof(mdl_indice) );
 
+      if( fixup_table ){
+         for( u32 i=0; i<job->vertex_count; i ++ ){
+            mdl_vert *vert = &job->verts[i];
+            
+            for( u32 j=0; j<4; j++ ){
+               vert->groups[j] = fixup_table[vert->groups[j]];
+            }
+         }
+      }
+
       /*
        * Unpack the indices (if there are meshes)
        * ---------------------------------------------------------
@@ -669,7 +679,7 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
 
 /* uploads the glmesh, and textures. everything is saved into the mdl_context */
 static void mdl_async_full_load_std( mdl_context *mdl ){
-   mdl_async_load_glmesh( mdl, &mdl->mesh );
+   mdl_async_load_glmesh( mdl, &mdl->mesh, NULL );
    
    for( u32 i=0; i<mdl_arrcount( &mdl->textures ); i ++ ){
       vg_linear_clear( vg_mem.scratch );
index fe6d74c59795d6c89934a6f0dd540878768c73ba..1378ce86ac0e88f3c9acd9790a6ec73a2297d559 100644 (file)
--- a/player.h
+++ b/player.h
@@ -155,7 +155,8 @@ struct {
        id_ik_knee_r,
        id_wheel_l,
        id_wheel_r,
-       id_board;
+       id_board,
+       id_eyes;
 }
 static localplayer = {
    .rb = {
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 );
 
index f5c6de9a80d1ac8b961ec0436f9cacf5799db404..21a53b715439830c4627e61a1ad1d32788ba6942 100644 (file)
@@ -48,7 +48,7 @@ enum board_shader{
 
 static void dynamic_model_load( mdl_context *ctx,
                                    struct dynamic_model_1texture *mdl, 
-                                   const char *path );
+                                   const char *path, u32 *fixup_table );
 static void dynamic_model_unload( struct dynamic_model_1texture *mdl );
 
 static void player_board_load( struct player_board *mdl, const char *path );
index 1251f0b0d68f98e7f5ef3eb0ff901debc39f9ecd..a131124b6c08bb9d5184319a9c4687053aa54654 100644 (file)
@@ -462,7 +462,7 @@ static struct vg_shader _shader_model_board_view = {
 "\n"
 "vec3 character_clearskies_lighting( vec3 normal, float shadow, vec3 halfview )\n"
 "{\n"
-"   float fresnel = 0.0*step(0.5,1.0 - abs(dot(normal,halfview)));\n"
+"   float fresnel = 1.0 - abs(dot(normal,halfview));\n"
 "\n"
 "   vec3  reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb, \n"
 "                               g_sunset_phase );\n"
index c59737feb10f8feb55c5f46c20cab12416b5a08a..c9feae4655028357ba922985302670b0d02abf02 100644 (file)
@@ -470,7 +470,7 @@ static struct vg_shader _shader_model_character_view = {
 "\n"
 "vec3 character_clearskies_lighting( vec3 normal, float shadow, vec3 halfview )\n"
 "{\n"
-"   float fresnel = 0.0*step(0.5,1.0 - abs(dot(normal,halfview)));\n"
+"   float fresnel = 1.0 - abs(dot(normal,halfview));\n"
 "\n"
 "   vec3  reflect_colour = mix( g_daysky_colour.rgb, g_sunset_colour.rgb, \n"
 "                               g_sunset_phase );\n"
index be339cf587603482e124053b3134090aeb99cdd0..79ddbc9867a14adc77fbbac5987fcf26f633da39 100644 (file)
@@ -65,7 +65,7 @@ static void world_gates_init(void)
       world_gates.sm_marker[i] = *sm;
    }
 
-   mdl_async_load_glmesh( &mgate, &world_gates.mesh );
+   mdl_async_load_glmesh( &mgate, &world_gates.mesh, NULL );
    mdl_close( &mgate );
 }
 
index 4d5321bc2a2db50a019b8b141845b093a4603c56..2542ce3b19b1a48e450beb78479d212fed592a99 100644 (file)
@@ -95,7 +95,7 @@ static void world_render_init(void)
    mdl_context msky;
    mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
    mdl_load_metadata_block( &msky, vg_mem.scratch );
-   mdl_async_load_glmesh( &msky, &world_render.skydome );
+   mdl_async_load_glmesh( &msky, &world_render.skydome, NULL );
    mdl_close( &msky );
 
    vg_info( "Loading default world textures\n" );
index 6d663dec54dc348e569bc39b420e07d5c7fdbf2a..b247ba6e778f5f173627704e38427b4f153c4eb9 100644 (file)
@@ -298,7 +298,7 @@ static void world_sfd_init(void){
    mdl_context mscoreboard;
    mdl_open( &mscoreboard, "models/rs_scoretext.mdl", vg_mem.scratch );
    mdl_load_metadata_block( &mscoreboard, vg_mem.scratch );
-   mdl_async_load_glmesh( &mscoreboard, &world_sfd.mesh_base );
+   mdl_async_load_glmesh( &mscoreboard, &world_sfd.mesh_base, NULL );
 
    mdl_load_mesh_block( &mscoreboard, vg_mem.scratch );