From 61559a4d856351c40f5ad6be67b811ec01b037f9 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 25 Nov 2023 22:10:04 +0000 Subject: [PATCH] allow armature fixups on model load --- font.h | 2 +- gui.h | 2 +- menu.h | 2 +- model.h | 16 +++++++++++++--- player.h | 3 ++- player_render.c | 35 +++++++++++++++++++++++++++++----- player_render.h | 2 +- shaders/model_board_view.h | 2 +- shaders/model_character_view.h | 2 +- world_gate.c | 2 +- world_render.c | 2 +- world_sfd.c | 2 +- 12 files changed, 54 insertions(+), 18 deletions(-) diff --git a/font.h b/font.h index a5449f9..0647b77 100644 --- 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 2d984f3..1414bd5 100644 --- 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 53cb52d..737c849 100644 --- 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 c054d18..4760794 100644 --- 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; ivertex_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; itextures ); i ++ ){ vg_linear_clear( vg_mem.scratch ); diff --git a/player.h b/player.h index fe6d74c..1378ce8 100644 --- 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 = { diff --git a/player_render.c b/player_render.c index b0416f5..ad56c24 100644 --- a/player_render.c +++ b/player_render.c @@ -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; ibone_count+1; i ++ ) + fixup_table[i] = 0; + + for( u32 i=1; iname ); + + for( u32 j=1; jbone_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 ); diff --git a/player_render.h b/player_render.h index f5c6de9..21a53b7 100644 --- a/player_render.h +++ b/player_render.h @@ -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 ); diff --git a/shaders/model_board_view.h b/shaders/model_board_view.h index 1251f0b..a131124 100644 --- a/shaders/model_board_view.h +++ b/shaders/model_board_view.h @@ -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" diff --git a/shaders/model_character_view.h b/shaders/model_character_view.h index c59737f..c9feae4 100644 --- a/shaders/model_character_view.h +++ b/shaders/model_character_view.h @@ -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" diff --git a/world_gate.c b/world_gate.c index be339cf..79ddbc9 100644 --- a/world_gate.c +++ b/world_gate.c @@ -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 ); } diff --git a/world_render.c b/world_render.c index 4d5321b..2542ce3 100644 --- a/world_render.c +++ b/world_render.c @@ -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" ); diff --git a/world_sfd.c b/world_sfd.c index 6d663de..b247ba6 100644 --- a/world_sfd.c +++ b/world_sfd.c @@ -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 ); -- 2.25.1