1 #ifndef PLAYER_RENDER_C
2 #define PLAYER_RENDER_C
5 #include "player_render.h"
7 #include "player_model.h"
8 #include "ent_skateshop.h"
12 #include "shaders/model_character_view.h"
13 #include "shaders/model_board_view.h"
14 #include "shaders/model_entity.h"
15 #include "shaders/model_board_view.h"
16 #include "depth_compare.h"
19 #include "player_remote.h"
21 static void player_load_animation_reference( const char *path
){
22 mdl_context
*meta
= &localplayer
.skeleton_meta
;
23 mdl_open( meta
, path
, vg_mem
.rtmemory
);
24 mdl_load_metadata_block( meta
, vg_mem
.rtmemory
);
25 mdl_load_animation_block( meta
, vg_mem
.rtmemory
);
28 struct skeleton
*sk
= &localplayer
.skeleton
;
29 skeleton_setup( sk
, vg_mem
.rtmemory
, meta
);
31 localplayer
.id_world
= skeleton_bone_id( sk
, "world" );
32 localplayer
.id_hip
= skeleton_bone_id( sk
, "hips" );
33 localplayer
.id_chest
= skeleton_bone_id( sk
, "chest" );
34 localplayer
.id_ik_hand_l
= skeleton_bone_id( sk
, "hand.IK.L" );
35 localplayer
.id_ik_hand_r
= skeleton_bone_id( sk
, "hand.IK.R" );
36 localplayer
.id_ik_elbow_l
= skeleton_bone_id( sk
, "elbow.L" );
37 localplayer
.id_ik_elbow_r
= skeleton_bone_id( sk
, "elbow.R" );
38 localplayer
.id_head
= skeleton_bone_id( sk
, "head" );
39 localplayer
.id_foot_l
= skeleton_bone_id( sk
, "foot.L" );
40 localplayer
.id_foot_r
= skeleton_bone_id( sk
, "foot.R" );
41 localplayer
.id_ik_foot_l
= skeleton_bone_id( sk
, "foot.IK.L" );
42 localplayer
.id_ik_foot_r
= skeleton_bone_id( sk
, "foot.IK.R" );
43 localplayer
.id_board
= skeleton_bone_id( sk
, "board" );
44 localplayer
.id_wheel_l
= skeleton_bone_id( sk
, "wheel.L" );
45 localplayer
.id_wheel_r
= skeleton_bone_id( sk
, "wheel.R" );
46 localplayer
.id_ik_knee_l
= skeleton_bone_id( sk
, "knee.L" );
47 localplayer
.id_ik_knee_r
= skeleton_bone_id( sk
, "knee.R" );
48 localplayer
.id_eyes
= skeleton_bone_id( sk
, "eyes" );
50 for( i32 i
=0; i
<sk
->bone_count
; i
++ ){
51 localplayer
.skeleton_mirror
[i
] = 0;
54 for( i32 i
=1; i
<sk
->bone_count
-1; i
++ ){
55 struct skeleton_bone
*si
= &sk
->bones
[i
];
59 vg_strnull( &str
, tmp
, 64 );
60 vg_strcat( &str
, si
->name
);
62 char *L
= vg_strch( &str
, 'L' );
66 for( i32 j
=i
+1; j
<sk
->bone_count
; j
++ ){
67 struct skeleton_bone
*sj
= &sk
->bones
[j
];
69 if( !strncmp( si
->name
, sj
->name
, len
) ){
70 if( sj
->name
[len
] == 'R' ){
71 localplayer
.skeleton_mirror
[i
] = j
;
72 localplayer
.skeleton_mirror
[j
] = i
;
79 setup_ragdoll_from_skeleton( sk
, &localplayer
.ragdoll
);
81 /* allocate matrix buffers for localplayer and remote players */
82 u32 mtx_size
= sizeof(m4x3f
)*sk
->bone_count
;
83 localplayer
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
, mtx_size
);
84 netplayers
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
,
85 mtx_size
*NETWORK_MAX_PLAYERS
);
88 /* TODO: Standard model load */
90 static void dynamic_model_load( mdl_context
*ctx
,
91 struct dynamic_model_1texture
*mdl
,
92 const char *path
, u32
*fixup_table
){
93 if( !mdl_arrcount( &ctx
->textures
) )
94 vg_fatal_error( "No texture in model" );
96 mdl_texture
*tex0
= mdl_arritm( &ctx
->textures
, 0 );
97 void *data
= vg_linear_alloc( vg_mem
.scratch
, tex0
->file
.pack_size
);
98 mdl_fread_pack_file( ctx
, &tex0
->file
, data
);
100 vg_tex2d_load_qoi_async( data
, tex0
->file
.pack_size
,
101 VG_TEX2D_NEAREST
|VG_TEX2D_CLAMP
,
104 mdl_async_load_glmesh( ctx
, &mdl
->mesh
, fixup_table
);
107 static void dynamic_model_unload( struct dynamic_model_1texture
*mdl
){
108 mesh_free( &mdl
->mesh
);
109 glDeleteTextures( 1, &mdl
->texture
);
112 /* TODO: allow error handling */
113 static void player_board_load( struct player_board
*board
,
116 vg_linear_clear( vg_mem
.scratch
);
119 mdl_open( &ctx
, path
, vg_mem
.scratch
);
120 mdl_load_metadata_block( &ctx
, vg_mem
.scratch
);
122 dynamic_model_load( &ctx
, &board
->mdl
, path
, NULL
);
124 mdl_array_ptr markers
;
125 MDL_LOAD_ARRAY( &ctx
, &markers
, ent_marker
, vg_mem
.scratch
);
127 /* TODO: you get put into a new section, the above is standard mdl loads. */
128 for( int i
=0; i
<4; i
++ )
129 board
->wheels
[i
].indice_count
= 0;
130 for( int i
=0; i
<2; i
++ )
131 board
->trucks
[i
].indice_count
= 0;
132 board
->board
.indice_count
= 0;
134 for( u32 i
=0; i
<mdl_arrcount(&ctx
.meshs
); i
++ ){
135 mdl_mesh
*mesh
= mdl_arritm( &ctx
.meshs
, i
);
137 if( mdl_entity_id_type( mesh
->entity_id
) != k_ent_marker
)
140 u32 index
= mdl_entity_id_id( mesh
->entity_id
);
141 ent_marker
*marker
= mdl_arritm( &markers
, index
);
143 mdl_submesh
*sm0
= mdl_arritm( &ctx
.submeshs
, mesh
->submesh_start
);
145 const char *alias
= mdl_pstr( &ctx
, marker
->pstr_alias
);
146 u32 lr
= marker
->transform
.co
[0] > 0.0f
? 1: 0,
147 fb
= marker
->transform
.co
[2] > 0.0f
? 0: 1;
149 if( !strcmp( alias
, "wheel" ) ){
151 board
->wheels
[ id
] = *sm0
;
152 v3_copy( marker
->transform
.co
, board
->wheel_positions
[ id
] );
154 else if( !strcmp( alias
, "board" ) ){
156 v3_copy( marker
->transform
.co
, board
->board_position
);
158 else if( !strcmp( alias
, "truck" ) ){
159 board
->trucks
[ fb
] = *sm0
;
160 v3_copy( marker
->transform
.co
, board
->truck_positions
[ fb
] );
167 static void player_board_unload( struct player_board
*board
){
168 dynamic_model_unload( &board
->mdl
);
171 static void player_model_load( struct player_model
*board
, const char *path
){
172 vg_linear_clear( vg_mem
.scratch
);
175 mdl_open( &ctx
, path
, vg_mem
.scratch
);
176 mdl_load_metadata_block( &ctx
, vg_mem
.scratch
);
178 if( !ctx
.armatures
.count
)
179 vg_fatal_error( "No armature in playermodel\n" );
181 mdl_armature
*armature
= mdl_arritm( &ctx
.armatures
, 0 );
183 u32 fixup_table
[ armature
->bone_count
+1 ];
184 for( u32 i
=0; i
<armature
->bone_count
+1; i
++ )
187 for( u32 i
=1; i
<localplayer
.skeleton
.bone_count
; i
++ ){
188 struct skeleton_bone
*sb
= &localplayer
.skeleton
.bones
[i
];
189 u32 hash
= vg_strdjb2( sb
->name
);
191 for( u32 j
=1; j
<armature
->bone_count
; j
++ ){
192 mdl_bone
*bone
= mdl_arritm( &ctx
.bones
, armature
->bone_start
+j
);
194 if( mdl_pstreq( &ctx
, bone
->pstr_name
, sb
->name
, hash
) ){
195 fixup_table
[j
+1] = i
;
201 dynamic_model_load( &ctx
, &board
->mdl
, path
, fixup_table
);
205 static void player_model_unload( struct player_model
*board
){
206 dynamic_model_unload( &board
->mdl
);
209 static void apply_full_skeleton_pose( struct skeleton
*sk
, player_pose
*pose
,
212 q_m3x3( pose
->root_q
, transform
);
213 v3_copy( pose
->root_co
, transform
[3] );
215 if( pose
->type
== k_player_pose_type_ik
){
216 skeleton_apply_pose( sk
, pose
->keyframes
,
217 k_anim_apply_defer_ik
, final_mtx
);
218 skeleton_apply_ik_pass( sk
, final_mtx
);
219 skeleton_apply_pose( sk
, pose
->keyframes
,
220 k_anim_apply_deffered_only
, final_mtx
);
221 skeleton_apply_inverses( sk
, final_mtx
);
222 skeleton_apply_transform( sk
, transform
, final_mtx
);
224 else if( pose
->type
== k_player_pose_type_fk_2
){
225 skeleton_apply_pose( sk
, pose
->keyframes
,
226 k_anim_apply_always
, final_mtx
);
227 skeleton_apply_inverses( sk
, final_mtx
);
228 skeleton_apply_transform( sk
, transform
, final_mtx
);
232 static void player__animate(void){
233 struct player_subsystem_interface
*sys
=
234 player_subsystems
[localplayer
.subsystem
];
236 struct player_board
*board
=
237 addon_cache_item_if_loaded( k_addon_type_board
,
238 localplayer
.board_view_slot
);
240 assert( sys
->animate
);
242 assert( sys
->animator_data
);
246 player_pose
*pose
= &localplayer
.pose
;
247 sys
->pose( sys
->animator_data
, pose
);
249 struct skeleton
*sk
= &localplayer
.skeleton
;
251 if( localplayer
.holdout_time
> 0.0f
){
252 skeleton_lerp_pose( sk
,
253 pose
->keyframes
,localplayer
.holdout_pose
.keyframes
,
254 localplayer
.holdout_time
, pose
->keyframes
);
256 v3_muladds( pose
->root_co
, localplayer
.holdout_pose
.root_co
,
257 localplayer
.holdout_time
, pose
->root_co
);
258 q_nlerp( pose
->root_q
, localplayer
.holdout_pose
.root_q
,
259 localplayer
.holdout_time
, pose
->root_q
);
261 localplayer
.holdout_time
-= vg
.time_frame_delta
/ 0.25f
;
264 effect_blink_apply( &localplayer
.effect_data
.blink
,
265 &localplayer
.pose
, vg
.time_delta
);
266 apply_full_skeleton_pose( sk
, &localplayer
.pose
, localplayer
.final_mtx
);
269 sys
->effects( sys
->animator_data
, localplayer
.final_mtx
, board
,
270 &localplayer
.effect_data
);
273 skeleton_debug( sk
, localplayer
.final_mtx
);
275 if( sys
->post_animate
)
278 player__observe_system( localplayer
.subsystem
);
280 sys
->sfx_comp( sys
->animator_data
);
282 player__cam_iterate();
285 static void player_copy_frame_animator( replay_frame
*frame
){
286 struct player_subsystem_interface
*sys
=
287 player_subsystems
[localplayer
.subsystem
];
289 if( sys
->animator_size
){
290 void *src
= replay_frame_data( frame
, k_replay_framedata_animator
);
291 memcpy( sys
->animator_data
, src
, sys
->animator_size
);
295 static void lerp_player_pose( player_pose
*pose0
, player_pose
*pose1
, f32 t
,
296 player_pose
*posed
){
297 struct skeleton
*sk
= &localplayer
.skeleton
;
299 v3_lerp( pose0
->root_co
, pose1
->root_co
, t
, posed
->root_co
);
300 q_nlerp( pose0
->root_q
, pose1
->root_q
, t
, posed
->root_q
);
301 posed
->type
= pose0
->type
;
302 posed
->board
.lean
= vg_lerpf( pose0
->board
.lean
, pose1
->board
.lean
, t
);
304 if( pose0
->type
!= pose1
->type
){
305 /* it would be nice to apply IK pass in-keyframes. TOO BAD! */
306 skeleton_copy_pose( sk
, pose0
->keyframes
, posed
->keyframes
);
309 skeleton_lerp_pose( sk
, pose0
->keyframes
, pose1
->keyframes
, t
,
314 static void player__observe_system( enum player_subsystem id
){
315 if( id
!= localplayer
.observing_system
){
316 struct player_subsystem_interface
*sysm1
=
317 player_subsystems
[ localplayer
.observing_system
];
319 if( sysm1
->sfx_kill
) sysm1
->sfx_kill();
320 localplayer
.observing_system
= id
;
324 static void player__animate_from_replay( replay_buffer
*replay
){
325 replay_frame
*frame
= replay
->cursor_frame
,
330 struct player_subsystem_interface
331 *sys0
= player_subsystems
[frame
->system
];
332 void *a0
= replay_frame_data( frame
, k_replay_framedata_animator
);
335 f32 t
= replay_subframe_time( replay
);
337 player_pose pose0
, pose1
;
339 struct player_subsystem_interface
340 *sys1
= player_subsystems
[next
->system
];
341 void *a1
= replay_frame_data( next
, k_replay_framedata_animator
);
343 sys0
->pose( a0
, &pose0
);
344 sys1
->pose( a1
, &pose1
);
346 lerp_player_pose( &pose0
, &pose1
, t
, &localplayer
.pose
);
349 sys0
->pose( a0
, &localplayer
.pose
);
352 player__observe_system( frame
->system
);
354 sys0
->sfx_comp( a0
);
358 apply_full_skeleton_pose( &localplayer
.skeleton
, &localplayer
.pose
,
359 localplayer
.final_mtx
);
362 static void player__pre_render(void){
363 /* shadowing/ao info */
364 struct player_board
*board
=
365 addon_cache_item_if_loaded( k_addon_type_board
,
366 localplayer
.board_view_slot
);
369 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[0][2]}, vp0
);
370 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[1][2]}, vp1
);
377 struct ub_world_lighting
*ubo
= &world_current_instance()->ub_lighting
;
378 v3f
*board_mtx
= localplayer
.final_mtx
[ localplayer
.id_board
];
379 m4x3_mulv( board_mtx
, vp0
, ubo
->g_board_0
);
380 m4x3_mulv( board_mtx
, vp1
, ubo
->g_board_1
);
383 static void render_board( camera
*cam
, world_instance
*world
,
384 struct player_board
*board
, m4x3f root
,
385 struct player_board_pose
*pose
,
386 enum board_shader shader
)
389 board
= &localplayer
.fallback_board
;
392 * adding depth compare to this shader
397 glActiveTexture( GL_TEXTURE0
);
398 glBindTexture( GL_TEXTURE_2D
, board
->mdl
.texture
);
400 if( shader
== k_board_shader_player
){
401 shader_model_board_view_use();
402 shader_model_board_view_uTexMain( 0 );
403 shader_model_board_view_uCamera( cam
->transform
[3] );
404 shader_model_board_view_uPv( cam
->mtx
.pv
);
406 shader_model_board_view_uDepthCompare(1);
408 shader_model_board_view_uTexSceneDepth
,
409 shader_model_board_view_uInverseRatioDepth
,
410 shader_model_board_view_uInverseRatioMain
,
413 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_board_view
);
415 else if( shader
== k_board_shader_entity
){
416 shader_model_entity_use();
417 shader_model_entity_uTexMain( 0 );
418 shader_model_entity_uCamera( cam
->transform
[3] );
419 shader_model_entity_uPv( cam
->mtx
.pv
);
421 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
424 mesh_bind( &board
->mdl
.mesh
);
428 if( board
->board
.indice_count
){
430 m3x3_identity( mlocal
);
438 q_axis_angle( qroll
, (v3f
){0.0f
,0.0f
,1.0f
}, pose
->lean
* 0.6f
);
439 keyframe_rotate_around( &kf
, (v3f
){0.0f
,0.11f
,0.0f
},
440 (v3f
){0.0f
,0.0f
,0.0f
}, qroll
);
442 v3_add( board
->board_position
, kf
.co
, mlocal
[3] );
443 q_m3x3( kf
.q
, mlocal
);
445 m4x3_mul( root
, mlocal
, mlocal
);
447 if( shader
== k_board_shader_entity
){
448 /* TODO: provide a way to supply previous mdl mtx? */
449 m4x3_expand( mlocal
, m4mdl
);
450 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
451 shader_model_entity_uPvmPrev( m4mdl
);
452 shader_model_entity_uMdl( mlocal
);
455 shader_model_board_view_uMdl( mlocal
);
457 mdl_draw_submesh( &board
->board
);
460 for( int i
=0; i
<2; i
++ ){
461 if( !board
->trucks
[i
].indice_count
)
465 m3x3_identity( mlocal
);
466 v3_copy( board
->truck_positions
[i
], mlocal
[3] );
467 m4x3_mul( root
, mlocal
, mlocal
);
469 if( shader
== k_board_shader_entity
){
470 m4x3_expand( mlocal
, m4mdl
);
471 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
472 shader_model_entity_uPvmPrev( m4mdl
);
473 shader_model_entity_uMdl( mlocal
);
476 shader_model_board_view_uMdl( mlocal
);
478 mdl_draw_submesh( &board
->trucks
[i
] );
481 for( int i
=0; i
<4; i
++ ){
482 if( !board
->wheels
[i
].indice_count
)
486 m3x3_identity( mlocal
);
487 v3_copy( board
->wheel_positions
[i
], mlocal
[3] );
488 m4x3_mul( root
, mlocal
, mlocal
);
490 if( shader
== k_board_shader_entity
){
491 m4x3_expand( mlocal
, m4mdl
);
492 m4x4_mul( cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
493 shader_model_entity_uPvmPrev( m4mdl
);
494 shader_model_entity_uMdl( mlocal
);
497 shader_model_board_view_uMdl( mlocal
);
499 mdl_draw_submesh( &board
->wheels
[i
] );
503 static void render_playermodel( camera
*cam
, world_instance
*world
,
505 struct player_model
*model
,
506 struct skeleton
*skeleton
,
510 shader_model_character_view_use();
512 glActiveTexture( GL_TEXTURE0
);
513 glBindTexture( GL_TEXTURE_2D
, model
->mdl
.texture
);
514 shader_model_character_view_uTexMain( 0 );
515 shader_model_character_view_uCamera( cam
->transform
[3] );
516 shader_model_character_view_uPv( cam
->mtx
.pv
);
518 shader_model_character_view_uDepthCompare( depth_compare
);
521 shader_model_character_view_uTexSceneDepth
,
522 shader_model_character_view_uInverseRatioDepth
,
523 shader_model_character_view_uInverseRatioMain
,
527 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_character_view
);
529 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms
,
530 skeleton
->bone_count
,
532 (const GLfloat
*)final_mtx
);
534 mesh_bind( &model
->mdl
.mesh
);
535 mesh_draw( &model
->mdl
.mesh
);
538 static void player__render( camera
*cam
){
539 world_instance
*world
= world_current_instance();
540 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
542 struct player_model
*model
=
543 addon_cache_item_if_loaded( k_addon_type_player
,
544 localplayer
.playermodel_view_slot
);
546 if( !model
) model
= &localplayer
.fallback_model
;
547 render_playermodel( cam
, world
, 1, model
, &localplayer
.skeleton
,
548 localplayer
.final_mtx
);
550 struct player_board
*board
=
551 addon_cache_item_if_loaded( k_addon_type_board
,
552 localplayer
.board_view_slot
);
554 render_board( cam
, world
, board
, localplayer
.final_mtx
[localplayer
.id_board
],
555 &localplayer
.pose
.board
, k_board_shader_player
);
557 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
560 static void player_mirror_pose( mdl_keyframe pose
[32],
561 mdl_keyframe mirrored
[32] ){
562 mdl_keyframe temp
[32];
564 struct skeleton
*sk
= &localplayer
.skeleton
;
565 for( u32 i
=1; i
<sk
->bone_count
; i
++ ){
566 mdl_keyframe
*dest
= &temp
[i
-1];
567 u8 mapping
= localplayer
.skeleton_mirror
[i
];
569 if( mapping
) *dest
= pose
[mapping
-1]; /* R */
570 else *dest
= pose
[i
-1]; /* L */
572 dest
->co
[2] *= -1.0f
;
577 for( u32 i
=0; i
<sk
->bone_count
-1; i
++ ){
578 mirrored
[i
] = temp
[i
];
581 #endif /* PLAYER_RENDER_C */