fix regression with gate flipping
[carveJwlIkooP6JGAAIwe30JlM.git] / player_render.c
1 #include "player.h"
2 #include "player_render.h"
3 #include "vg/vg_camera.h"
4 #include "player_model.h"
5 #include "ent_skateshop.h"
6 #include "audio.h"
7 #include "input.h"
8
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"
14
15 #include "network.h"
16 #include "player_remote.h"
17 #include "player_glide.h"
18
19 void player_load_animation_reference( const char *path )
20 {
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 );
25 mdl_close( meta );
26
27 struct skeleton *sk = &localplayer.skeleton;
28 skeleton_setup( sk, vg_mem.rtmemory, meta );
29
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" );
48
49 for( i32 i=0; i<sk->bone_count; i ++ ){
50 localplayer.skeleton_mirror[i] = 0;
51 }
52
53 for( i32 i=1; i<sk->bone_count-1; i ++ ){
54 struct skeleton_bone *si = &sk->bones[i];
55
56 char tmp[64];
57 vg_str str;
58 vg_strnull( &str, tmp, 64 );
59 vg_strcat( &str, si->name );
60
61 char *L = vg_strch( &str, 'L' );
62 if( !L ) continue;
63 u32 len = L-tmp;
64
65 for( i32 j=i+1; j<sk->bone_count; j ++ ){
66 struct skeleton_bone *sj = &sk->bones[j];
67
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;
72 break;
73 }
74 }
75 }
76 }
77
78 setup_ragdoll_from_skeleton( sk, &localplayer.ragdoll );
79
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 );
87 }
88
89 /* TODO: Standard model load */
90
91 void dynamic_model_load( mdl_context *ctx,
92 struct dynamic_model_1texture *mdl,
93 const char *path, u32 *fixup_table )
94 {
95 if( !mdl_arrcount( &ctx->textures ) )
96 vg_fatal_error( "No texture in model" );
97
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 );
101
102 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
103 VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
104 &mdl->texture );
105
106 mdl_async_load_glmesh( ctx, &mdl->mesh, fixup_table );
107 }
108
109 void dynamic_model_unload( struct dynamic_model_1texture *mdl )
110 {
111 mesh_free( &mdl->mesh );
112 glDeleteTextures( 1, &mdl->texture );
113 }
114
115 /* TODO: allow error handling */
116 void player_board_load( struct player_board *board, const char *path )
117 {
118 vg_linear_clear( vg_mem.scratch );
119
120 mdl_context ctx;
121 mdl_open( &ctx, path, vg_mem.scratch );
122 mdl_load_metadata_block( &ctx, vg_mem.scratch );
123
124 dynamic_model_load( &ctx, &board->mdl, path, NULL );
125
126 mdl_array_ptr markers;
127 MDL_LOAD_ARRAY( &ctx, &markers, ent_marker, vg_mem.scratch );
128
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;
135
136 for( u32 i=0; i<mdl_arrcount(&ctx.meshs); i++ ){
137 mdl_mesh *mesh = mdl_arritm( &ctx.meshs, i );
138
139 if( mdl_entity_id_type( mesh->entity_id ) != k_ent_marker )
140 continue;
141
142 u32 index = mdl_entity_id_id( mesh->entity_id );
143 ent_marker *marker = mdl_arritm( &markers, index );
144
145 mdl_submesh *sm0 = mdl_arritm( &ctx.submeshs, mesh->submesh_start );
146
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;
150
151 if( !strcmp( alias, "wheel" ) ){
152 u32 id = fb<<1 | lr;
153 board->wheels[ id ] = *sm0;
154 v3_copy( marker->transform.co, board->wheel_positions[ id ] );
155 }
156 else if( !strcmp( alias, "board" ) ){
157 board->board = *sm0;
158 v3_copy( marker->transform.co, board->board_position );
159 }
160 else if( !strcmp( alias, "truck" ) ){
161 board->trucks[ fb ] = *sm0;
162 v3_copy( marker->transform.co, board->truck_positions[ fb ] );
163 }
164 }
165
166 mdl_close( &ctx );
167 }
168
169 void player_board_unload( struct player_board *board )
170 {
171 dynamic_model_unload( &board->mdl );
172 }
173
174 void player_model_load( struct player_model *board, const char *path)
175 {
176 vg_linear_clear( vg_mem.scratch );
177
178 mdl_context ctx;
179 mdl_open( &ctx, path, vg_mem.scratch );
180 mdl_load_metadata_block( &ctx, vg_mem.scratch );
181
182 if( !ctx.armatures.count )
183 vg_fatal_error( "No armature in playermodel\n" );
184
185 mdl_armature *armature = mdl_arritm( &ctx.armatures, 0 );
186
187 u32 fixup_table[ armature->bone_count+1 ];
188 for( u32 i=0; i<armature->bone_count+1; i ++ )
189 fixup_table[i] = 0;
190
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 );
194
195 for( u32 j=1; j<armature->bone_count; j ++ ){
196 mdl_bone *bone = mdl_arritm( &ctx.bones, armature->bone_start+j );
197
198 if( mdl_pstreq( &ctx, bone->pstr_name, sb->name, hash ) ){
199 fixup_table[j+1] = i;
200 break;
201 }
202 }
203 }
204
205 dynamic_model_load( &ctx, &board->mdl, path, fixup_table );
206 mdl_close( &ctx );
207 }
208
209 void player_model_unload( struct player_model *board )
210 {
211 dynamic_model_unload( &board->mdl );
212 }
213
214 void apply_full_skeleton_pose( struct skeleton *sk, player_pose *pose,
215 m4x3f *final_mtx ){
216 m4x3f transform;
217 q_m3x3( pose->root_q, transform );
218 v3_copy( pose->root_co, transform[3] );
219
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 );
228 }
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 );
234 }
235 }
236
237 void player__animate(void)
238 {
239 struct player_subsystem_interface *sys =
240 player_subsystems[localplayer.subsystem];
241
242 struct player_board *board =
243 addon_cache_item_if_loaded( k_addon_type_board,
244 localplayer.board_view_slot );
245
246 sys->animate();
247
248 player_pose *pose = &localplayer.pose;
249 sys->pose( sys->animator_data, pose );
250
251 struct skeleton *sk = &localplayer.skeleton;
252
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 );
257
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 );
262
263 localplayer.holdout_time -= vg.time_frame_delta / 0.25f;
264 }
265
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 );
269
270 if( sys->effects ){
271 sys->effects( sys->animator_data, localplayer.final_mtx, board,
272 &localplayer.effect_data );
273 }
274
275 skeleton_debug( sk, localplayer.final_mtx );
276
277 if( sys->post_animate )
278 sys->post_animate();
279
280 player__observe_system( localplayer.subsystem );
281 if( sys->sfx_comp )
282 sys->sfx_comp( sys->animator_data );
283
284 player__cam_iterate();
285 }
286
287 static void player_copy_frame_animator( replay_frame *frame ){
288 struct player_subsystem_interface *sys =
289 player_subsystems[localplayer.subsystem];
290
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 );
294 }
295 }
296
297 void lerp_player_pose( player_pose *pose0, player_pose *pose1, f32 t,
298 player_pose *posed ){
299 struct skeleton *sk = &localplayer.skeleton;
300
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 );
305
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 );
309 }
310 else {
311 skeleton_lerp_pose( sk, pose0->keyframes, pose1->keyframes, t,
312 posed->keyframes );
313 }
314 }
315
316 void player__observe_system( enum player_subsystem id )
317 {
318 if( id != localplayer.observing_system ){
319 struct player_subsystem_interface *sysm1 =
320 player_subsystems[ localplayer.observing_system ];
321
322 if( sysm1->sfx_kill ) sysm1->sfx_kill();
323 localplayer.observing_system = id;
324 }
325 }
326
327 void player__animate_from_replay( replay_buffer *replay )
328 {
329 replay_frame *frame = replay->cursor_frame,
330 *next = NULL;
331 if( frame ){
332 next = frame->r;
333
334 struct player_subsystem_interface
335 *sys0 = player_subsystems[frame->system];
336 void *a0 = replay_frame_data( frame, k_replay_framedata_animator );
337
338 struct replay_glider_data
339 *g0 = replay_frame_data( frame, k_replay_framedata_glider ),
340 *g1;
341
342 f32 t = 0.0f;
343
344 if( next ){
345 t = replay_subframe_time( replay );
346
347 player_pose pose0, pose1;
348
349 struct player_subsystem_interface
350 *sys1 = player_subsystems[next->system];
351 void *a1 = replay_frame_data( next, k_replay_framedata_animator );
352
353 sys0->pose( a0, &pose0 );
354 sys1->pose( a1, &pose1 );
355
356 lerp_player_pose( &pose0, &pose1, t, &localplayer.pose );
357 g1 = replay_frame_data( next, k_replay_framedata_glider );
358 }
359 else{
360 sys0->pose( a0, &localplayer.pose );
361 g1 = NULL;
362 }
363
364 player__observe_system( frame->system );
365 if( sys0->sfx_comp )
366 sys0->sfx_comp( a0 );
367
368 if( g0 ){
369 if( g0->glider_orphan ){
370 if( g1 ){
371 v3_lerp( g0->co, g1->co, t, player_glide.rb.co );
372 q_nlerp( g0->q, g1->q, t, player_glide.rb.q );
373 }
374 else {
375 v3_copy( g0->co, player_glide.rb.co );
376 v4_copy( g0->q, player_glide.rb.q );
377 }
378
379 rb_update_matrices( &player_glide.rb );
380 }
381
382 if( g1 )
383 player_glide.t = vg_lerpf( g0->t, g1->t, t );
384 else
385 player_glide.t = g0->t;
386
387 localplayer.have_glider = g0->have_glider;
388 localplayer.glider_orphan = g0->glider_orphan;
389 }
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;
394 }
395 }
396 else return;
397
398 apply_full_skeleton_pose( &localplayer.skeleton, &localplayer.pose,
399 localplayer.final_mtx );
400 }
401
402 void player__pre_render(void)
403 {
404 /* shadowing/ao info */
405 struct player_board *board =
406 addon_cache_item_if_loaded( k_addon_type_board,
407 localplayer.board_view_slot );
408 v3f vp0, vp1;
409 if( board ){
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 );
412 }
413 else{
414 v3_zero( vp0 );
415 v3_zero( vp1 );
416 }
417
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 );
422 }
423
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 )
428 {
429 if( !board )
430 board = &localplayer.fallback_board;
431
432 /* TODO:
433 * adding depth compare to this shader
434 */
435
436 v3f inverse;
437
438 glActiveTexture( GL_TEXTURE0 );
439 glBindTexture( GL_TEXTURE_2D, board->mdl.texture );
440
441 if( shader == k_board_shader_player )
442 {
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 );
447
448 shader_model_board_view_uDepthMode(1);
449 depth_compare_bind(
450 shader_model_board_view_uTexSceneDepth,
451 shader_model_board_view_uInverseRatioDepth,
452 shader_model_board_view_uInverseRatioMain,
453 cam );
454
455 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_board_view );
456 }
457 else if( shader == k_board_shader_entity )
458 {
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 );
463
464 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
465 }
466
467 mesh_bind( &board->mdl.mesh );
468
469 m4x4f m4mdl;
470
471 if( board->board.indice_count ){
472 m4x3f mlocal;
473 m3x3_identity( mlocal );
474
475 mdl_keyframe kf;
476 v3_zero( kf.co );
477 q_identity( kf.q );
478 v3_zero( kf.s );
479
480 v4f qroll;
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 );
484
485 v3_add( board->board_position, kf.co, mlocal[3] );
486 q_m3x3( kf.q, mlocal );
487
488 m4x3_mul( root, mlocal, mlocal );
489
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 );
496 }
497 else
498 shader_model_board_view_uMdl( mlocal );
499
500 mdl_draw_submesh( &board->board );
501 }
502
503 for( int i=0; i<2; i++ ){
504 if( !board->trucks[i].indice_count )
505 continue;
506
507 m4x3f mlocal;
508 m3x3_identity( mlocal );
509 v3_copy( board->truck_positions[i], mlocal[3] );
510 m4x3_mul( root, mlocal, mlocal );
511
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 );
517 }
518 else
519 shader_model_board_view_uMdl( mlocal );
520
521 mdl_draw_submesh( &board->trucks[i] );
522 }
523
524 for( int i=0; i<4; i++ ){
525 if( !board->wheels[i].indice_count )
526 continue;
527
528 m4x3f mlocal;
529 m3x3_identity( mlocal );
530 v3_copy( board->wheel_positions[i], mlocal[3] );
531 m4x3_mul( root, mlocal, mlocal );
532
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 );
538 }
539 else
540 shader_model_board_view_uMdl( mlocal );
541
542 mdl_draw_submesh( &board->wheels[i] );
543 }
544 }
545
546 void render_playermodel( vg_camera *cam, world_instance *world,
547 int depth_compare,
548 struct player_model *model,
549 struct skeleton *skeleton,
550 m4x3f *final_mtx )
551 {
552 if( !model ) return;
553
554 shader_model_character_view_use();
555
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 );
562 if( depth_compare )
563 {
564 depth_compare_bind(
565 shader_model_character_view_uTexSceneDepth,
566 shader_model_character_view_uInverseRatioDepth,
567 shader_model_character_view_uInverseRatioMain,
568 cam );
569 }
570
571 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_character_view );
572
573 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
574 skeleton->bone_count,
575 0,
576 (const GLfloat *)final_mtx );
577
578 mesh_bind( &model->mdl.mesh );
579 mesh_draw( &model->mdl.mesh );
580 }
581
582 void player__render( vg_camera *cam )
583 {
584 world_instance *world = world_current_instance();
585 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
586
587 struct player_model *model =
588 addon_cache_item_if_loaded( k_addon_type_player,
589 localplayer.playermodel_view_slot );
590
591 if( !model ) model = &localplayer.fallback_model;
592 render_playermodel( cam, world, 1, model, &localplayer.skeleton,
593 localplayer.final_mtx );
594
595 struct player_board *board =
596 addon_cache_item_if_loaded( k_addon_type_board,
597 localplayer.board_view_slot );
598
599 render_board( cam, world, board, localplayer.final_mtx[localplayer.id_board],
600 &localplayer.pose.board, k_board_shader_player );
601
602 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
603
604 glEnable( GL_CULL_FACE );
605 player_glide_render( cam, world, &localplayer.pose );
606 glDisable( GL_CULL_FACE );
607 }
608
609 void player_mirror_pose( mdl_keyframe pose[32], mdl_keyframe mirrored[32] )
610 {
611 mdl_keyframe temp[32];
612
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];
617
618 if( mapping ) *dest = pose[mapping-1]; /* R */
619 else *dest = pose[i-1]; /* L */
620
621 dest->co[2] *= -1.0f;
622 dest->q[0] *= -1.0f;
623 dest->q[1] *= -1.0f;
624 }
625
626 for( u32 i=0; i<sk->bone_count-1; i ++ ){
627 mirrored[i] = temp[i];
628 }
629 }