add one shots to replay buffer
[carveJwlIkooP6JGAAIwe30JlM.git] / player_render.c
1 #ifndef PLAYER_RENDER_C
2 #define PLAYER_RENDER_C
3
4 #include "player.h"
5 #include "player_render.h"
6 #include "camera.h"
7 #include "player_model.h"
8 #include "ent_skateshop.h"
9 #include "audio.h"
10 #include "input.h"
11
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"
17
18 #include "network.h"
19 #include "player_remote.h"
20
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 );
26 mdl_close( meta );
27
28 struct skeleton *sk = &localplayer.skeleton;
29 skeleton_setup( sk, vg_mem.rtmemory, meta );
30
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" );
49
50 for( i32 i=0; i<sk->bone_count; i ++ ){
51 localplayer.skeleton_mirror[i] = 0;
52 }
53
54 for( i32 i=1; i<sk->bone_count-1; i ++ ){
55 struct skeleton_bone *si = &sk->bones[i];
56
57 char tmp[64];
58 vg_str str;
59 vg_strnull( &str, tmp, 64 );
60 vg_strcat( &str, si->name );
61
62 char *L = vg_strch( &str, 'L' );
63 if( !L ) continue;
64 u32 len = L-tmp;
65
66 for( i32 j=i+1; j<sk->bone_count; j ++ ){
67 struct skeleton_bone *sj = &sk->bones[j];
68
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;
73 break;
74 }
75 }
76 }
77 }
78
79 setup_ragdoll_from_skeleton( sk, &localplayer.ragdoll );
80
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 );
86 }
87
88 /* TODO: Standard model load */
89
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" );
95
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 );
99
100 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
101 VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
102 &mdl->texture );
103
104 mdl_async_load_glmesh( ctx, &mdl->mesh, fixup_table );
105 }
106
107 static void dynamic_model_unload( struct dynamic_model_1texture *mdl ){
108 mesh_free( &mdl->mesh );
109 glDeleteTextures( 1, &mdl->texture );
110 }
111
112 /* TODO: allow error handling */
113 static void player_board_load( struct player_board *board,
114 const char *path ){
115
116 vg_linear_clear( vg_mem.scratch );
117
118 mdl_context ctx;
119 mdl_open( &ctx, path, vg_mem.scratch );
120 mdl_load_metadata_block( &ctx, vg_mem.scratch );
121
122 dynamic_model_load( &ctx, &board->mdl, path, NULL );
123
124 mdl_array_ptr markers;
125 MDL_LOAD_ARRAY( &ctx, &markers, ent_marker, vg_mem.scratch );
126
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;
133
134 for( u32 i=0; i<mdl_arrcount(&ctx.meshs); i++ ){
135 mdl_mesh *mesh = mdl_arritm( &ctx.meshs, i );
136
137 if( mdl_entity_id_type( mesh->entity_id ) != k_ent_marker )
138 continue;
139
140 u32 index = mdl_entity_id_id( mesh->entity_id );
141 ent_marker *marker = mdl_arritm( &markers, index );
142
143 mdl_submesh *sm0 = mdl_arritm( &ctx.submeshs, mesh->submesh_start );
144
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;
148
149 if( !strcmp( alias, "wheel" ) ){
150 u32 id = fb<<1 | lr;
151 board->wheels[ id ] = *sm0;
152 v3_copy( marker->transform.co, board->wheel_positions[ id ] );
153 }
154 else if( !strcmp( alias, "board" ) ){
155 board->board = *sm0;
156 v3_copy( marker->transform.co, board->board_position );
157 }
158 else if( !strcmp( alias, "truck" ) ){
159 board->trucks[ fb ] = *sm0;
160 v3_copy( marker->transform.co, board->truck_positions[ fb ] );
161 }
162 }
163
164 mdl_close( &ctx );
165 }
166
167 static void player_board_unload( struct player_board *board ){
168 dynamic_model_unload( &board->mdl );
169 }
170
171 static void player_model_load( struct player_model *board, const char *path){
172 vg_linear_clear( vg_mem.scratch );
173
174 mdl_context ctx;
175 mdl_open( &ctx, path, vg_mem.scratch );
176 mdl_load_metadata_block( &ctx, vg_mem.scratch );
177
178 if( !ctx.armatures.count )
179 vg_fatal_error( "No armature in playermodel\n" );
180
181 mdl_armature *armature = mdl_arritm( &ctx.armatures, 0 );
182
183 u32 fixup_table[ armature->bone_count+1 ];
184 for( u32 i=0; i<armature->bone_count+1; i ++ )
185 fixup_table[i] = 0;
186
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 );
190
191 for( u32 j=1; j<armature->bone_count; j ++ ){
192 mdl_bone *bone = mdl_arritm( &ctx.bones, armature->bone_start+j );
193
194 if( mdl_pstreq( &ctx, bone->pstr_name, sb->name, hash ) ){
195 fixup_table[j+1] = i;
196 break;
197 }
198 }
199 }
200
201 dynamic_model_load( &ctx, &board->mdl, path, fixup_table );
202 mdl_close( &ctx );
203 }
204
205 static void player_model_unload( struct player_model *board ){
206 dynamic_model_unload( &board->mdl );
207 }
208
209 static void apply_full_skeleton_pose( struct skeleton *sk, player_pose *pose,
210 m4x3f *final_mtx ){
211 m4x3f transform;
212 q_m3x3( pose->root_q, transform );
213 v3_copy( pose->root_co, transform[3] );
214
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 );
223 }
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 );
229 }
230 }
231
232 static void player__animate(void){
233 struct player_subsystem_interface *sys =
234 player_subsystems[localplayer.subsystem];
235
236 struct player_board *board =
237 addon_cache_item_if_loaded( k_addon_type_board,
238 localplayer.board_view_slot );
239
240 assert( sys->animate );
241 assert( sys->pose );
242 assert( sys->animator_data );
243
244 sys->animate();
245
246 player_pose *pose = &localplayer.pose;
247 sys->pose( sys->animator_data, pose );
248
249 struct skeleton *sk = &localplayer.skeleton;
250
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 );
255
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 );
260
261 localplayer.holdout_time -= vg.time_frame_delta / 0.25f;
262 }
263
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 );
267
268 if( sys->effects ){
269 sys->effects( sys->animator_data, localplayer.final_mtx, board,
270 &localplayer.effect_data );
271 }
272
273 skeleton_debug( sk, localplayer.final_mtx );
274
275 if( sys->post_animate )
276 sys->post_animate();
277
278 player__observe_system( localplayer.subsystem );
279 if( sys->sfx_comp )
280 sys->sfx_comp( sys->animator_data );
281
282 player__cam_iterate();
283 }
284
285 static void player_copy_frame_animator( replay_frame *frame ){
286 struct player_subsystem_interface *sys =
287 player_subsystems[localplayer.subsystem];
288
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 );
292 }
293 }
294
295 static void lerp_player_pose( player_pose *pose0, player_pose *pose1, f32 t,
296 player_pose *posed ){
297 struct skeleton *sk = &localplayer.skeleton;
298
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 );
303
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 );
307 }
308 else {
309 skeleton_lerp_pose( sk, pose0->keyframes, pose1->keyframes, t,
310 posed->keyframes );
311 }
312 }
313
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 ];
318
319 if( sysm1->sfx_kill ) sysm1->sfx_kill();
320 localplayer.observing_system = id;
321 }
322 }
323
324 static void player__animate_from_replay( replay_buffer *replay ){
325 replay_frame *frame = replay->cursor_frame,
326 *next = NULL;
327 if( frame ){
328 next = frame->r;
329
330 struct player_subsystem_interface
331 *sys0 = player_subsystems[frame->system];
332 void *a0 = replay_frame_data( frame, k_replay_framedata_animator );
333
334 if( next ){
335 f32 t = replay_subframe_time( replay );
336
337 player_pose pose0, pose1;
338
339 struct player_subsystem_interface
340 *sys1 = player_subsystems[next->system];
341 void *a1 = replay_frame_data( next, k_replay_framedata_animator );
342
343 sys0->pose( a0, &pose0 );
344 sys1->pose( a1, &pose1 );
345
346 lerp_player_pose( &pose0, &pose1, t, &localplayer.pose );
347 }
348 else{
349 sys0->pose( a0, &localplayer.pose );
350 }
351
352 player__observe_system( frame->system );
353 if( sys0->sfx_comp )
354 sys0->sfx_comp( a0 );
355 }
356 else return;
357
358 apply_full_skeleton_pose( &localplayer.skeleton, &localplayer.pose,
359 localplayer.final_mtx );
360 }
361
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 );
367 v3f vp0, vp1;
368 if( board ){
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 );
371 }
372 else{
373 v3_zero( vp0 );
374 v3_zero( vp1 );
375 }
376
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 );
381 }
382
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 )
387 {
388 if( !board )
389 board = &localplayer.fallback_board;
390
391 /* TODO:
392 * adding depth compare to this shader
393 */
394
395 v3f inverse;
396
397 glActiveTexture( GL_TEXTURE0 );
398 glBindTexture( GL_TEXTURE_2D, board->mdl.texture );
399
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 );
405
406 shader_model_board_view_uDepthCompare(1);
407 depth_compare_bind(
408 shader_model_board_view_uTexSceneDepth,
409 shader_model_board_view_uInverseRatioDepth,
410 shader_model_board_view_uInverseRatioMain,
411 cam );
412
413 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_board_view );
414 }
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 );
420
421 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
422 }
423
424 mesh_bind( &board->mdl.mesh );
425
426 m4x4f m4mdl;
427
428 if( board->board.indice_count ){
429 m4x3f mlocal;
430 m3x3_identity( mlocal );
431
432 mdl_keyframe kf;
433 v3_zero( kf.co );
434 q_identity( kf.q );
435 v3_zero( kf.s );
436
437 v4f qroll;
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 );
441
442 v3_add( board->board_position, kf.co, mlocal[3] );
443 q_m3x3( kf.q, mlocal );
444
445 m4x3_mul( root, mlocal, mlocal );
446
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 );
453 }
454 else
455 shader_model_board_view_uMdl( mlocal );
456
457 mdl_draw_submesh( &board->board );
458 }
459
460 for( int i=0; i<2; i++ ){
461 if( !board->trucks[i].indice_count )
462 continue;
463
464 m4x3f mlocal;
465 m3x3_identity( mlocal );
466 v3_copy( board->truck_positions[i], mlocal[3] );
467 m4x3_mul( root, mlocal, mlocal );
468
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 );
474 }
475 else
476 shader_model_board_view_uMdl( mlocal );
477
478 mdl_draw_submesh( &board->trucks[i] );
479 }
480
481 for( int i=0; i<4; i++ ){
482 if( !board->wheels[i].indice_count )
483 continue;
484
485 m4x3f mlocal;
486 m3x3_identity( mlocal );
487 v3_copy( board->wheel_positions[i], mlocal[3] );
488 m4x3_mul( root, mlocal, mlocal );
489
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 );
495 }
496 else
497 shader_model_board_view_uMdl( mlocal );
498
499 mdl_draw_submesh( &board->wheels[i] );
500 }
501 }
502
503 static void render_playermodel( camera *cam, world_instance *world,
504 int depth_compare,
505 struct player_model *model,
506 struct skeleton *skeleton,
507 m4x3f *final_mtx ){
508 if( !model ) return;
509
510 shader_model_character_view_use();
511
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 );
517
518 shader_model_character_view_uDepthCompare( depth_compare );
519 if( depth_compare ){
520 depth_compare_bind(
521 shader_model_character_view_uTexSceneDepth,
522 shader_model_character_view_uInverseRatioDepth,
523 shader_model_character_view_uInverseRatioMain,
524 cam );
525 }
526
527 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_character_view );
528
529 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
530 skeleton->bone_count,
531 0,
532 (const GLfloat *)final_mtx );
533
534 mesh_bind( &model->mdl.mesh );
535 mesh_draw( &model->mdl.mesh );
536 }
537
538 static void player__render( camera *cam ){
539 world_instance *world = world_current_instance();
540 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
541
542 struct player_model *model =
543 addon_cache_item_if_loaded( k_addon_type_player,
544 localplayer.playermodel_view_slot );
545
546 if( !model ) model = &localplayer.fallback_model;
547 render_playermodel( cam, world, 1, model, &localplayer.skeleton,
548 localplayer.final_mtx );
549
550 struct player_board *board =
551 addon_cache_item_if_loaded( k_addon_type_board,
552 localplayer.board_view_slot );
553
554 render_board( cam, world, board, localplayer.final_mtx[localplayer.id_board],
555 &localplayer.pose.board, k_board_shader_player );
556
557 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
558 }
559
560 static void player_mirror_pose( mdl_keyframe pose[32],
561 mdl_keyframe mirrored[32] ){
562 mdl_keyframe temp[32];
563
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];
568
569 if( mapping ) *dest = pose[mapping-1]; /* R */
570 else *dest = pose[i-1]; /* L */
571
572 dest->co[2] *= -1.0f;
573 dest->q[0] *= -1.0f;
574 dest->q[1] *= -1.0f;
575 }
576
577 for( u32 i=0; i<sk->bone_count-1; i ++ ){
578 mirrored[i] = temp[i];
579 }
580 }
581 #endif /* PLAYER_RENDER_C */