2 #include "player_render.h"
3 #include "vg/vg_camera.h"
4 #include "player_model.h"
5 #include "ent_skateshop.h"
9 #include "shaders/model_character_view.h"
10 #include "shaders/model_board_view.h"
11 #include "shaders/model_entity.h"
12 #include "shaders/model_board_view.h"
13 #include "depth_compare.h"
16 #include "player_remote.h"
17 #include "player_glide.h"
19 void player_load_animation_reference( const char *path
)
21 mdl_context
*meta
= &localplayer
.skeleton_meta
;
22 mdl_open( meta
, path
, vg_mem
.rtmemory
);
23 mdl_load_metadata_block( meta
, vg_mem
.rtmemory
);
24 mdl_load_animation_block( meta
, vg_mem
.rtmemory
);
27 struct skeleton
*sk
= &localplayer
.skeleton
;
28 skeleton_setup( sk
, vg_mem
.rtmemory
, meta
);
30 localplayer
.id_world
= skeleton_bone_id( sk
, "world" );
31 localplayer
.id_hip
= skeleton_bone_id( sk
, "hips" );
32 localplayer
.id_chest
= skeleton_bone_id( sk
, "chest" );
33 localplayer
.id_ik_hand_l
= skeleton_bone_id( sk
, "hand.IK.L" );
34 localplayer
.id_ik_hand_r
= skeleton_bone_id( sk
, "hand.IK.R" );
35 localplayer
.id_ik_elbow_l
= skeleton_bone_id( sk
, "elbow.L" );
36 localplayer
.id_ik_elbow_r
= skeleton_bone_id( sk
, "elbow.R" );
37 localplayer
.id_head
= skeleton_bone_id( sk
, "head" );
38 localplayer
.id_foot_l
= skeleton_bone_id( sk
, "foot.L" );
39 localplayer
.id_foot_r
= skeleton_bone_id( sk
, "foot.R" );
40 localplayer
.id_ik_foot_l
= skeleton_bone_id( sk
, "foot.IK.L" );
41 localplayer
.id_ik_foot_r
= skeleton_bone_id( sk
, "foot.IK.R" );
42 localplayer
.id_board
= skeleton_bone_id( sk
, "board" );
43 localplayer
.id_wheel_l
= skeleton_bone_id( sk
, "wheel.L" );
44 localplayer
.id_wheel_r
= skeleton_bone_id( sk
, "wheel.R" );
45 localplayer
.id_ik_knee_l
= skeleton_bone_id( sk
, "knee.L" );
46 localplayer
.id_ik_knee_r
= skeleton_bone_id( sk
, "knee.R" );
47 localplayer
.id_eyes
= skeleton_bone_id( sk
, "eyes" );
49 for( i32 i
=0; i
<sk
->bone_count
; i
++ ){
50 localplayer
.skeleton_mirror
[i
] = 0;
53 for( i32 i
=1; i
<sk
->bone_count
-1; i
++ ){
54 struct skeleton_bone
*si
= &sk
->bones
[i
];
58 vg_strnull( &str
, tmp
, 64 );
59 vg_strcat( &str
, si
->name
);
61 char *L
= vg_strch( &str
, 'L' );
65 for( i32 j
=i
+1; j
<sk
->bone_count
; j
++ ){
66 struct skeleton_bone
*sj
= &sk
->bones
[j
];
68 if( !strncmp( si
->name
, sj
->name
, len
) ){
69 if( sj
->name
[len
] == 'R' ){
70 localplayer
.skeleton_mirror
[i
] = j
;
71 localplayer
.skeleton_mirror
[j
] = i
;
78 setup_ragdoll_from_skeleton( sk
, &localplayer
.ragdoll
);
80 /* allocate matrix buffers for localplayer and remote players */
81 u32 mtx_size
= sizeof(m4x3f
)*sk
->bone_count
;
82 localplayer
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
, mtx_size
);
83 netplayers
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
,
84 mtx_size
*NETWORK_MAX_PLAYERS
);
85 netplayers
.glider_mtx
= vg_linear_alloc( vg_mem
.rtmemory
,
86 sizeof(m4x3f
)*NETWORK_MAX_PLAYERS
);
89 /* TODO: Standard model load */
91 void dynamic_model_load( mdl_context
*ctx
,
92 struct dynamic_model_1texture
*mdl
,
93 const char *path
, u32
*fixup_table
)
95 if( !mdl_arrcount( &ctx
->textures
) )
96 vg_fatal_error( "No texture in model" );
98 mdl_texture
*tex0
= mdl_arritm( &ctx
->textures
, 0 );
99 void *data
= vg_linear_alloc( vg_mem
.scratch
, tex0
->file
.pack_size
);
100 mdl_fread_pack_file( ctx
, &tex0
->file
, data
);
102 vg_tex2d_load_qoi_async( data
, tex0
->file
.pack_size
,
103 VG_TEX2D_NEAREST
|VG_TEX2D_CLAMP
,
106 mdl_async_load_glmesh( ctx
, &mdl
->mesh
, fixup_table
);
109 void dynamic_model_unload( struct dynamic_model_1texture
*mdl
)
111 mesh_free( &mdl
->mesh
);
112 glDeleteTextures( 1, &mdl
->texture
);
115 /* TODO: allow error handling */
116 void player_board_load( struct player_board
*board
, const char *path
)
118 vg_linear_clear( vg_mem
.scratch
);
121 mdl_open( &ctx
, path
, vg_mem
.scratch
);
122 mdl_load_metadata_block( &ctx
, vg_mem
.scratch
);
124 dynamic_model_load( &ctx
, &board
->mdl
, path
, NULL
);
126 mdl_array_ptr markers
;
127 MDL_LOAD_ARRAY( &ctx
, &markers
, ent_marker
, vg_mem
.scratch
);
129 /* TODO: you get put into a new section, the above is standard mdl loads. */
130 for( int i
=0; i
<4; i
++ )
131 board
->wheels
[i
].indice_count
= 0;
132 for( int i
=0; i
<2; i
++ )
133 board
->trucks
[i
].indice_count
= 0;
134 board
->board
.indice_count
= 0;
136 for( u32 i
=0; i
<mdl_arrcount(&ctx
.meshs
); i
++ ){
137 mdl_mesh
*mesh
= mdl_arritm( &ctx
.meshs
, i
);
139 if( mdl_entity_id_type( mesh
->entity_id
) != k_ent_marker
)
142 u32 index
= mdl_entity_id_id( mesh
->entity_id
);
143 ent_marker
*marker
= mdl_arritm( &markers
, index
);
145 mdl_submesh
*sm0
= mdl_arritm( &ctx
.submeshs
, mesh
->submesh_start
);
147 const char *alias
= mdl_pstr( &ctx
, marker
->pstr_alias
);
148 u32 lr
= marker
->transform
.co
[0] > 0.0f
? 1: 0,
149 fb
= marker
->transform
.co
[2] > 0.0f
? 0: 1;
151 if( !strcmp( alias
, "wheel" ) ){
153 board
->wheels
[ id
] = *sm0
;
154 v3_copy( marker
->transform
.co
, board
->wheel_positions
[ id
] );
156 else if( !strcmp( alias
, "board" ) ){
158 v3_copy( marker
->transform
.co
, board
->board_position
);
160 else if( !strcmp( alias
, "truck" ) ){
161 board
->trucks
[ fb
] = *sm0
;
162 v3_copy( marker
->transform
.co
, board
->truck_positions
[ fb
] );
169 void player_board_unload( struct player_board
*board
)
171 dynamic_model_unload( &board
->mdl
);
174 void player_model_load( struct player_model
*board
, const char *path
)
176 vg_linear_clear( vg_mem
.scratch
);
179 mdl_open( &ctx
, path
, vg_mem
.scratch
);
180 mdl_load_metadata_block( &ctx
, vg_mem
.scratch
);
182 if( !ctx
.armatures
.count
)
183 vg_fatal_error( "No armature in playermodel\n" );
185 mdl_armature
*armature
= mdl_arritm( &ctx
.armatures
, 0 );
187 u32 fixup_table
[ armature
->bone_count
+1 ];
188 for( u32 i
=0; i
<armature
->bone_count
+1; i
++ )
191 for( u32 i
=1; i
<localplayer
.skeleton
.bone_count
; i
++ ){
192 struct skeleton_bone
*sb
= &localplayer
.skeleton
.bones
[i
];
193 u32 hash
= vg_strdjb2( sb
->name
);
195 for( u32 j
=1; j
<armature
->bone_count
; j
++ ){
196 mdl_bone
*bone
= mdl_arritm( &ctx
.bones
, armature
->bone_start
+j
);
198 if( mdl_pstreq( &ctx
, bone
->pstr_name
, sb
->name
, hash
) ){
199 fixup_table
[j
+1] = i
;
205 dynamic_model_load( &ctx
, &board
->mdl
, path
, fixup_table
);
209 void player_model_unload( struct player_model
*board
)
211 dynamic_model_unload( &board
->mdl
);
214 void apply_full_skeleton_pose( struct skeleton
*sk
, player_pose
*pose
,
217 q_m3x3( pose
->root_q
, transform
);
218 v3_copy( pose
->root_co
, transform
[3] );
220 if( pose
->type
== k_player_pose_type_ik
){
221 skeleton_apply_pose( sk
, pose
->keyframes
,
222 k_anim_apply_defer_ik
, final_mtx
);
223 skeleton_apply_ik_pass( sk
, final_mtx
);
224 skeleton_apply_pose( sk
, pose
->keyframes
,
225 k_anim_apply_deffered_only
, final_mtx
);
226 skeleton_apply_inverses( sk
, final_mtx
);
227 skeleton_apply_transform( sk
, transform
, final_mtx
);
229 else if( pose
->type
== k_player_pose_type_fk_2
){
230 skeleton_apply_pose( sk
, pose
->keyframes
,
231 k_anim_apply_always
, final_mtx
);
232 skeleton_apply_inverses( sk
, final_mtx
);
233 skeleton_apply_transform( sk
, transform
, final_mtx
);
237 void player__animate(void)
239 struct player_subsystem_interface
*sys
=
240 player_subsystems
[localplayer
.subsystem
];
242 struct player_board
*board
=
243 addon_cache_item_if_loaded( k_addon_type_board
,
244 localplayer
.board_view_slot
);
248 player_pose
*pose
= &localplayer
.pose
;
249 sys
->pose( sys
->animator_data
, pose
);
251 struct skeleton
*sk
= &localplayer
.skeleton
;
253 if( localplayer
.holdout_time
> 0.0f
){
254 skeleton_lerp_pose( sk
,
255 pose
->keyframes
,localplayer
.holdout_pose
.keyframes
,
256 localplayer
.holdout_time
, pose
->keyframes
);
258 v3_muladds( pose
->root_co
, localplayer
.holdout_pose
.root_co
,
259 localplayer
.holdout_time
, pose
->root_co
);
260 q_nlerp( pose
->root_q
, localplayer
.holdout_pose
.root_q
,
261 localplayer
.holdout_time
, pose
->root_q
);
263 localplayer
.holdout_time
-= vg
.time_frame_delta
/ 0.25f
;
266 effect_blink_apply( &localplayer
.effect_data
.blink
,
267 &localplayer
.pose
, vg
.time_delta
);
268 apply_full_skeleton_pose( sk
, &localplayer
.pose
, localplayer
.final_mtx
);
271 sys
->effects( sys
->animator_data
, localplayer
.final_mtx
, board
,
272 &localplayer
.effect_data
);
275 skeleton_debug( sk
, localplayer
.final_mtx
);
277 if( sys
->post_animate
)
280 player__observe_system( localplayer
.subsystem
);
282 sys
->sfx_comp( sys
->animator_data
);
284 player__cam_iterate();
287 static void player_copy_frame_animator( replay_frame
*frame
){
288 struct player_subsystem_interface
*sys
=
289 player_subsystems
[localplayer
.subsystem
];
291 if( sys
->animator_size
){
292 void *src
= replay_frame_data( frame
, k_replay_framedata_animator
);
293 memcpy( sys
->animator_data
, src
, sys
->animator_size
);
297 void lerp_player_pose( player_pose
*pose0
, player_pose
*pose1
, f32 t
,
298 player_pose
*posed
){
299 struct skeleton
*sk
= &localplayer
.skeleton
;
301 v3_lerp( pose0
->root_co
, pose1
->root_co
, t
, posed
->root_co
);
302 q_nlerp( pose0
->root_q
, pose1
->root_q
, t
, posed
->root_q
);
303 posed
->type
= pose0
->type
;
304 posed
->board
.lean
= vg_lerpf( pose0
->board
.lean
, pose1
->board
.lean
, t
);
306 if( pose0
->type
!= pose1
->type
){
307 /* it would be nice to apply IK pass in-keyframes. TOO BAD! */
308 skeleton_copy_pose( sk
, pose0
->keyframes
, posed
->keyframes
);
311 skeleton_lerp_pose( sk
, pose0
->keyframes
, pose1
->keyframes
, t
,
316 void player__observe_system( enum player_subsystem id
)
318 if( id
!= localplayer
.observing_system
){
319 struct player_subsystem_interface
*sysm1
=
320 player_subsystems
[ localplayer
.observing_system
];
322 if( sysm1
->sfx_kill
) sysm1
->sfx_kill();
323 localplayer
.observing_system
= id
;
327 void player__animate_from_replay( replay_buffer
*replay
)
329 replay_frame
*frame
= replay
->cursor_frame
,
334 struct player_subsystem_interface
335 *sys0
= player_subsystems
[frame
->system
];
336 void *a0
= replay_frame_data( frame
, k_replay_framedata_animator
);
338 struct replay_glider_data
339 *g0
= replay_frame_data( frame
, k_replay_framedata_glider
),
345 t
= replay_subframe_time( replay
);
347 player_pose pose0
, pose1
;
349 struct player_subsystem_interface
350 *sys1
= player_subsystems
[next
->system
];
351 void *a1
= replay_frame_data( next
, k_replay_framedata_animator
);
353 sys0
->pose( a0
, &pose0
);
354 sys1
->pose( a1
, &pose1
);
356 lerp_player_pose( &pose0
, &pose1
, t
, &localplayer
.pose
);
357 g1
= replay_frame_data( next
, k_replay_framedata_glider
);
360 sys0
->pose( a0
, &localplayer
.pose
);
364 player__observe_system( frame
->system
);
366 sys0
->sfx_comp( a0
);
369 if( g0
->glider_orphan
){
371 v3_lerp( g0
->co
, g1
->co
, t
, player_glide
.rb
.co
);
372 q_nlerp( g0
->q
, g1
->q
, t
, player_glide
.rb
.q
);
375 v3_copy( g0
->co
, player_glide
.rb
.co
);
376 v4_copy( g0
->q
, player_glide
.rb
.q
);
379 rb_update_matrices( &player_glide
.rb
);
383 player_glide
.t
= vg_lerpf( g0
->t
, g1
->t
, t
);
385 player_glide
.t
= g0
->t
;
387 localplayer
.have_glider
= g0
->have_glider
;
388 localplayer
.glider_orphan
= g0
->glider_orphan
;
390 else /* no glider data in g1, or edge case we dont care about */ {
391 localplayer
.have_glider
= 0;
392 localplayer
.glider_orphan
= 0;
393 player_glide
.t
= 0.0f
;
398 apply_full_skeleton_pose( &localplayer
.skeleton
, &localplayer
.pose
,
399 localplayer
.final_mtx
);
402 void player__pre_render(void)
404 /* shadowing/ao info */
405 struct player_board
*board
=
406 addon_cache_item_if_loaded( k_addon_type_board
,
407 localplayer
.board_view_slot
);
410 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[0][2]}, vp0
);
411 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[1][2]}, vp1
);
418 struct ub_world_lighting
*ubo
= &world_current_instance()->ub_lighting
;
419 v3f
*board_mtx
= localplayer
.final_mtx
[ localplayer
.id_board
];
420 m4x3_mulv( board_mtx
, vp0
, ubo
->g_board_0
);
421 m4x3_mulv( board_mtx
, vp1
, ubo
->g_board_1
);
424 void render_board( vg_camera
*cam
, world_instance
*world
,
425 struct player_board
*board
, m4x3f root
,
426 struct player_board_pose
*pose
,
427 enum board_shader shader
)
430 board
= &localplayer
.fallback_board
;
433 * adding depth compare to this shader
438 glActiveTexture( GL_TEXTURE0
);
439 glBindTexture( GL_TEXTURE_2D
, board
->mdl
.texture
);
441 if( shader
== k_board_shader_player
)
443 shader_model_board_view_use();
444 shader_model_board_view_uTexMain( 0 );
445 shader_model_board_view_uCamera( cam
->transform
[3] );
446 shader_model_board_view_uPv( cam
->mtx
.pv
);
448 shader_model_board_view_uDepthMode(1);
450 shader_model_board_view_uTexSceneDepth
,
451 shader_model_board_view_uInverseRatioDepth
,
452 shader_model_board_view_uInverseRatioMain
,
455 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_board_view
);
457 else if( shader
== k_board_shader_entity
)
459 shader_model_entity_use();
460 shader_model_entity_uTexMain( 0 );
461 shader_model_entity_uCamera( cam
->transform
[3] );
462 shader_model_entity_uPv( cam
->mtx
.pv
);
464 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
467 mesh_bind( &board
->mdl
.mesh
);
471 if( board
->board
.indice_count
){
473 m3x3_identity( mlocal
);
481 q_axis_angle( qroll
, (v3f
){0.0f
,0.0f
,1.0f
}, pose
->lean
* 0.6f
);
482 keyframe_rotate_around( &kf
, (v3f
){0.0f
,0.11f
,0.0f
},
483 (v3f
){0.0f
,0.0f
,0.0f
}, qroll
);
485 v3_add( board
->board_position
, kf
.co
, mlocal
[3] );
486 q_m3x3( kf
.q
, mlocal
);
488 m4x3_mul( root
, mlocal
, mlocal
);
490 if( shader
== k_board_shader_entity
){
491 /* TODO: provide a way to supply previous mdl mtx? */
492 m4x3_expand( mlocal
, m4mdl
);
493 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
494 shader_model_entity_uPvmPrev( m4mdl
);
495 shader_model_entity_uMdl( mlocal
);
498 shader_model_board_view_uMdl( mlocal
);
500 mdl_draw_submesh( &board
->board
);
503 for( int i
=0; i
<2; i
++ ){
504 if( !board
->trucks
[i
].indice_count
)
508 m3x3_identity( mlocal
);
509 v3_copy( board
->truck_positions
[i
], mlocal
[3] );
510 m4x3_mul( root
, mlocal
, mlocal
);
512 if( shader
== k_board_shader_entity
){
513 m4x3_expand( mlocal
, m4mdl
);
514 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
515 shader_model_entity_uPvmPrev( m4mdl
);
516 shader_model_entity_uMdl( mlocal
);
519 shader_model_board_view_uMdl( mlocal
);
521 mdl_draw_submesh( &board
->trucks
[i
] );
524 for( int i
=0; i
<4; i
++ ){
525 if( !board
->wheels
[i
].indice_count
)
529 m3x3_identity( mlocal
);
530 v3_copy( board
->wheel_positions
[i
], mlocal
[3] );
531 m4x3_mul( root
, mlocal
, mlocal
);
533 if( shader
== k_board_shader_entity
){
534 m4x3_expand( mlocal
, m4mdl
);
535 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
536 shader_model_entity_uPvmPrev( m4mdl
);
537 shader_model_entity_uMdl( mlocal
);
540 shader_model_board_view_uMdl( mlocal
);
542 mdl_draw_submesh( &board
->wheels
[i
] );
546 void render_playermodel( vg_camera
*cam
, world_instance
*world
,
548 struct player_model
*model
,
549 struct skeleton
*skeleton
,
554 shader_model_character_view_use();
556 glActiveTexture( GL_TEXTURE0
);
557 glBindTexture( GL_TEXTURE_2D
, model
->mdl
.texture
);
558 shader_model_character_view_uTexMain( 0 );
559 shader_model_character_view_uCamera( cam
->transform
[3] );
560 shader_model_character_view_uPv( cam
->mtx
.pv
);
561 shader_model_character_view_uDepthMode( depth_compare
);
565 shader_model_character_view_uTexSceneDepth
,
566 shader_model_character_view_uInverseRatioDepth
,
567 shader_model_character_view_uInverseRatioMain
,
571 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_character_view
);
573 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms
,
574 skeleton
->bone_count
,
576 (const GLfloat
*)final_mtx
);
578 mesh_bind( &model
->mdl
.mesh
);
579 mesh_draw( &model
->mdl
.mesh
);
582 void player__render( vg_camera
*cam
)
584 world_instance
*world
= world_current_instance();
585 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
587 struct player_model
*model
=
588 addon_cache_item_if_loaded( k_addon_type_player
,
589 localplayer
.playermodel_view_slot
);
591 if( !model
) model
= &localplayer
.fallback_model
;
592 render_playermodel( cam
, world
, 1, model
, &localplayer
.skeleton
,
593 localplayer
.final_mtx
);
595 struct player_board
*board
=
596 addon_cache_item_if_loaded( k_addon_type_board
,
597 localplayer
.board_view_slot
);
599 render_board( cam
, world
, board
, localplayer
.final_mtx
[localplayer
.id_board
],
600 &localplayer
.pose
.board
, k_board_shader_player
);
602 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
604 glEnable( GL_CULL_FACE
);
605 player_glide_render( cam
, world
, &localplayer
.pose
);
606 glDisable( GL_CULL_FACE
);
609 void player_mirror_pose( mdl_keyframe pose
[32], mdl_keyframe mirrored
[32] )
611 mdl_keyframe temp
[32];
613 struct skeleton
*sk
= &localplayer
.skeleton
;
614 for( u32 i
=1; i
<sk
->bone_count
; i
++ ){
615 mdl_keyframe
*dest
= &temp
[i
-1];
616 u8 mapping
= localplayer
.skeleton_mirror
[i
];
618 if( mapping
) *dest
= pose
[mapping
-1]; /* R */
619 else *dest
= pose
[i
-1]; /* L */
621 dest
->co
[2] *= -1.0f
;
626 for( u32 i
=0; i
<sk
->bone_count
-1; i
++ ){
627 mirrored
[i
] = temp
[i
];