cleanup frame saving
[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
16 VG_STATIC void player_avatar_load( struct player_avatar *av, const char *path )
17 {
18 mdl_open( &av->meta, path, vg_mem.rtmemory );
19 mdl_load_metadata_block( &av->meta, vg_mem.rtmemory );
20 mdl_load_animation_block( &av->meta, vg_mem.rtmemory );
21 mdl_close( &av->meta );
22
23 struct skeleton *sk = &av->sk;
24 skeleton_setup( sk, vg_mem.rtmemory, &av->meta );
25
26 av->id_hip = skeleton_bone_id( sk, "hips" );
27 av->id_ik_hand_l = skeleton_bone_id( sk, "hand.IK.L" );
28 av->id_ik_hand_r = skeleton_bone_id( sk, "hand.IK.R" );
29 av->id_ik_elbow_l = skeleton_bone_id( sk, "elbow.L" );
30 av->id_ik_elbow_r = skeleton_bone_id( sk, "elbow.R" );
31 av->id_head = skeleton_bone_id( sk, "head" );
32 av->id_ik_foot_l = skeleton_bone_id( sk, "foot.IK.L" );
33 av->id_ik_foot_r = skeleton_bone_id( sk, "foot.IK.R" );
34 av->id_board = skeleton_bone_id( sk, "board" );
35 av->id_wheel_l = skeleton_bone_id( sk, "wheel.L" );
36 av->id_wheel_r = skeleton_bone_id( sk, "wheel.R" );
37 av->id_ik_knee_l = skeleton_bone_id( sk, "knee.L" );
38 av->id_ik_knee_r = skeleton_bone_id( sk, "knee.R" );
39 }
40
41 /* TODO: Standard model load */
42
43 VG_STATIC void dynamic_model_load( mdl_context *ctx,
44 struct dynamic_model_1texture *mdl,
45 const char *path )
46 {
47 if( !mdl_arrcount( &ctx->textures ) )
48 vg_fatal_error( "No texture in model" );
49
50 mdl_texture *tex0 = mdl_arritm( &ctx->textures, 0 );
51 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
52 mdl_fread_pack_file( ctx, &tex0->file, data );
53
54 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
55 VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
56 &mdl->texture );
57
58 mdl_async_load_glmesh( ctx, &mdl->mesh );
59 }
60
61 VG_STATIC void dynamic_model_unload( struct dynamic_model_1texture *mdl ){
62 mesh_free( &mdl->mesh );
63 glDeleteTextures( 1, &mdl->texture );
64 }
65
66 /* TODO: allow error handling */
67 VG_STATIC void player_board_load( struct player_board *board,
68 const char *path ){
69
70 vg_linear_clear( vg_mem.scratch );
71
72 mdl_context ctx;
73 mdl_open( &ctx, path, vg_mem.scratch );
74 mdl_load_metadata_block( &ctx, vg_mem.scratch );
75
76 dynamic_model_load( &ctx, &board->mdl, path );
77
78 mdl_array_ptr markers;
79 mdl_load_array( &ctx, &markers, "ent_marker", vg_mem.scratch );
80
81 /* TODO: you get put into a new section, the above is standard mdl loads. */
82 for( int i=0; i<4; i++ )
83 board->wheels[i].indice_count = 0;
84 for( int i=0; i<2; i++ )
85 board->trucks[i].indice_count = 0;
86 board->board.indice_count = 0;
87
88 for( u32 i=0; i<mdl_arrcount(&ctx.meshs); i++ ){
89 mdl_mesh *mesh = mdl_arritm( &ctx.meshs, i );
90
91 if( mdl_entity_id_type( mesh->entity_id ) != k_ent_marker )
92 continue;
93
94 u32 index = mdl_entity_id_id( mesh->entity_id );
95 ent_marker *marker = mdl_arritm( &markers, index );
96
97 mdl_submesh *sm0 = mdl_arritm( &ctx.submeshs, mesh->submesh_start );
98
99 const char *alias = mdl_pstr( &ctx, marker->pstr_alias );
100 u32 lr = marker->transform.co[0] > 0.0f? 1: 0,
101 fb = marker->transform.co[2] > 0.0f? 0: 1;
102
103 if( !strcmp( alias, "wheel" ) ){
104 u32 id = fb<<1 | lr;
105 board->wheels[ id ] = *sm0;
106 v3_copy( marker->transform.co, board->wheel_positions[ id ] );
107 }
108 else if( !strcmp( alias, "board" ) ){
109 board->board = *sm0;
110 v3_copy( marker->transform.co, board->board_position );
111 }
112 else if( !strcmp( alias, "truck" ) ){
113 board->trucks[ fb ] = *sm0;
114 v3_copy( marker->transform.co, board->truck_positions[ fb ] );
115 }
116 }
117
118 mdl_close( &ctx );
119 }
120
121 VG_STATIC void player_board_unload( struct player_board *board ){
122 dynamic_model_unload( &board->mdl );
123 }
124
125 VG_STATIC void player_model_load( struct player_model *board, const char *path){
126 vg_linear_clear( vg_mem.scratch );
127
128 mdl_context ctx;
129 mdl_open( &ctx, path, vg_mem.scratch );
130 mdl_load_metadata_block( &ctx, vg_mem.scratch );
131
132 dynamic_model_load( &ctx, &board->mdl, path );
133
134 mdl_close( &ctx );
135 }
136
137 VG_STATIC void player_model_unload( struct player_model *board ){
138 dynamic_model_unload( &board->mdl );
139 }
140
141 VG_STATIC void player__animate( player_instance *player ){
142 if( _player_animate[ player->subsystem ] ){
143 player_animation res;
144 res.type = k_player_animation_type_fk;
145
146 _player_animate[ player->subsystem ]( player, &res );
147
148 m4x3f transform;
149 q_m3x3( res.root_q, transform );
150 v3_copy( res.root_co, transform[3] );
151
152 struct skeleton *sk = &player->playeravatar->sk;
153
154 if( player->holdout_time > 0.0f ){
155 skeleton_lerp_pose( sk, res.pose, player->holdout_pose,
156 player->holdout_time, res.pose );
157 player->holdout_time -= vg.time_frame_delta * 2.0f;
158 }
159
160 if( res.type == k_player_animation_type_fk ){
161 skeleton_apply_pose( sk, res.pose, k_anim_apply_defer_ik );
162 skeleton_apply_ik_pass( sk );
163 skeleton_apply_pose( sk, res.pose, k_anim_apply_deffered_only );
164 skeleton_apply_inverses( sk );
165 skeleton_apply_transform( sk, transform );
166 }
167 else {
168 skeleton_apply_pose( sk, res.pose, k_anim_apply_absolute );
169 }
170
171 skeleton_debug( sk );
172 }
173
174 if( _player_post_animate[ player->subsystem ] )
175 _player_post_animate[ player->subsystem ]( player );
176
177 player__cam_iterate( player );
178 }
179
180 VG_STATIC void player__animate_from_replay( player_instance *player,
181 replay_buffer *replay ){
182 /* TODO: holdout blending (from when the game has to slow down) */
183
184 player_animation res;
185 replay_frame *frame = replay->cursor_frame,
186 *next = NULL;
187
188 if( frame ){
189 next = frame->r;
190
191 if( next ){
192 f32 t = replay_subframe_time( replay );
193
194 struct skeleton *sk = &player->playeravatar->sk;
195 skeleton_lerp_pose(sk, frame->anim.pose, next->anim.pose, t, res.pose);
196 v3_lerp( frame->anim.root_co, next->anim.root_co, t, res.root_co );
197 q_nlerp( frame->anim.root_q, next->anim.root_q, t, res.root_q );
198 res.type = k_player_animation_type_absolute;
199 player->board_pose.lean = vg_lerpf( frame->board_pose.lean,
200 next->board_pose.lean, t );
201 }
202 else {
203 memcpy( &res, &frame->anim, sizeof(frame->anim) );
204 memcpy( &frame->board_pose, &player->board_pose,
205 sizeof(player->board_pose) );
206 }
207 }
208 else return;
209
210 struct skeleton *sk = &player->playeravatar->sk;
211 skeleton_apply_pose( sk, res.pose, k_anim_apply_absolute );
212 }
213
214 VG_STATIC void player__pre_render( player_instance *player ){
215 /* shadowing/ao info */
216 struct player_avatar *av = player->playeravatar;
217 struct player_board *board =
218 addon_cache_item_if_loaded( k_addon_type_board,
219 player->board_view_slot );
220 v3f vp0, vp1;
221 if( board ){
222 v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
223 v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
224 }
225 else{
226 v3_zero( vp0 );
227 v3_zero( vp1 );
228 }
229
230 struct ub_world_lighting *ubo = &world_current_instance()->ub_lighting;
231 m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp0, ubo->g_board_0 );
232 m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp1, ubo->g_board_1 );
233 }
234
235 VG_STATIC void render_board( camera *cam, world_instance *world,
236 struct player_board *board, m4x3f root,
237 struct board_pose *pose,
238 enum board_shader shader )
239 {
240 if( !board ) return;
241
242 v3f inverse;
243
244 glActiveTexture( GL_TEXTURE0 );
245 glBindTexture( GL_TEXTURE_2D, board->mdl.texture );
246
247 if( shader == k_board_shader_player ){
248 shader_model_board_view_use();
249 shader_model_board_view_uTexMain( 0 );
250 shader_model_board_view_uCamera( cam->transform[3] );
251 shader_model_board_view_uPv( cam->mtx.pv );
252 shader_model_board_view_uTexSceneDepth( 1 );
253 render_fb_bind_texture( gpipeline.fb_main, 2, 1 );
254
255 render_fb_inverse_ratio( gpipeline.fb_main, inverse );
256
257 inverse[2] = skaterift.cam.farz-skaterift.cam.nearz;
258
259 shader_model_board_view_uInverseRatioDepth( inverse );
260 render_fb_inverse_ratio( NULL, inverse );
261 inverse[2] = cam->farz-cam->nearz;
262 shader_model_board_view_uInverseRatioMain( inverse );
263
264 world_link_lighting_ub( world, _shader_model_board_view.id );
265 world_bind_position_texture( world, _shader_model_board_view.id,
266 _uniform_model_board_view_g_world_depth, 2 );
267 world_bind_light_array( world, _shader_model_board_view.id,
268 _uniform_model_board_view_uLightsArray, 3 );
269 world_bind_light_index( world, _shader_model_board_view.id,
270 _uniform_model_board_view_uLightsIndex, 4 );
271 }
272 else if( shader == k_board_shader_entity ){
273 shader_model_entity_use();
274 shader_model_entity_uTexMain( 0 );
275 shader_model_entity_uCamera( cam->transform[3] );
276 shader_model_entity_uPv( cam->mtx.pv );
277
278 world_link_lighting_ub( world, _shader_model_entity.id );
279 world_bind_position_texture( world, _shader_model_entity.id,
280 _uniform_model_entity_g_world_depth, 2 );
281 world_bind_light_array( world, _shader_model_entity.id,
282 _uniform_model_entity_uLightsArray, 3 );
283 world_bind_light_index( world, _shader_model_entity.id,
284 _uniform_model_entity_uLightsIndex, 4 );
285 }
286
287 mesh_bind( &board->mdl.mesh );
288
289 m4x4f m4mdl;
290
291 if( board->board.indice_count ){
292 m4x3f mlocal;
293 m3x3_identity( mlocal );
294
295 mdl_keyframe kf;
296 v3_zero( kf.co );
297 q_identity( kf.q );
298 v3_zero( kf.s );
299
300 v4f qroll;
301 q_axis_angle( qroll, (v3f){0.0f,0.0f,1.0f}, pose->lean * 0.6f );
302 keyframe_rotate_around( &kf, (v3f){0.0f,0.11f,0.0f},
303 (v3f){0.0f,0.0f,0.0f}, qroll );
304
305 v3_add( board->board_position, kf.co, mlocal[3] );
306 q_m3x3( kf.q, mlocal );
307
308 m4x3_mul( root, mlocal, mlocal );
309
310 if( shader == k_board_shader_entity ){
311 /* TODO: provide a way to supply previous mdl mtx? */
312 m4x3_expand( mlocal, m4mdl );
313 m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
314 shader_model_entity_uPvmPrev( m4mdl );
315 shader_model_entity_uMdl( mlocal );
316 }
317 else
318 shader_model_board_view_uMdl( mlocal );
319
320 mdl_draw_submesh( &board->board );
321 }
322
323 for( int i=0; i<2; i++ ){
324 if( !board->trucks[i].indice_count )
325 continue;
326
327 m4x3f mlocal;
328 m3x3_identity( mlocal );
329 v3_copy( board->truck_positions[i], mlocal[3] );
330 m4x3_mul( root, mlocal, mlocal );
331
332 if( shader == k_board_shader_entity ){
333 m4x3_expand( mlocal, m4mdl );
334 m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
335 shader_model_entity_uPvmPrev( m4mdl );
336 shader_model_entity_uMdl( mlocal );
337 }
338 else
339 shader_model_board_view_uMdl( mlocal );
340
341 mdl_draw_submesh( &board->trucks[i] );
342 }
343
344 for( int i=0; i<4; i++ ){
345 if( !board->wheels[i].indice_count )
346 continue;
347
348 m4x3f mlocal;
349 m3x3_identity( mlocal );
350 v3_copy( board->wheel_positions[i], mlocal[3] );
351 m4x3_mul( root, mlocal, mlocal );
352
353 if( shader == k_board_shader_entity ){
354 m4x3_expand( mlocal, m4mdl );
355 m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
356 shader_model_entity_uPvmPrev( m4mdl );
357 shader_model_entity_uMdl( mlocal );
358 }
359 else
360 shader_model_board_view_uMdl( mlocal );
361
362 mdl_draw_submesh( &board->wheels[i] );
363 }
364 }
365
366 VG_STATIC void render_playermodel( camera *cam, world_instance *world,
367 struct player_model *model,
368 struct skeleton *skeleton ){
369 if( !model ) return;
370
371 shader_model_character_view_use();
372
373 glActiveTexture( GL_TEXTURE0 );
374 glBindTexture( GL_TEXTURE_2D, model->mdl.texture );
375 shader_model_character_view_uTexMain( 0 );
376 shader_model_character_view_uCamera( cam->transform[3] );
377 shader_model_character_view_uPv( cam->mtx.pv );
378 shader_model_character_view_uTexSceneDepth( 1 );
379 render_fb_bind_texture( gpipeline.fb_main, 2, 1 );
380 v3f inverse;
381 render_fb_inverse_ratio( gpipeline.fb_main, inverse );
382 inverse[2] = skaterift.cam.farz-skaterift.cam.nearz;
383
384 shader_model_character_view_uInverseRatioDepth( inverse );
385 render_fb_inverse_ratio( NULL, inverse );
386 inverse[2] = cam->farz-cam->nearz;
387 shader_model_character_view_uInverseRatioMain( inverse );
388
389 world_link_lighting_ub( world, _shader_model_character_view.id );
390 world_bind_position_texture( world, _shader_model_character_view.id,
391 _uniform_model_character_view_g_world_depth, 2 );
392 world_bind_light_array( world, _shader_model_character_view.id,
393 _uniform_model_character_view_uLightsArray, 3 );
394 world_bind_light_index( world, _shader_model_character_view.id,
395 _uniform_model_character_view_uLightsIndex, 4 );
396
397 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
398 skeleton->bone_count,
399 0,
400 (float *)skeleton->final_mtx );
401
402 mesh_bind( &model->mdl.mesh );
403 mesh_draw( &model->mdl.mesh );
404 }
405
406 PLAYER_API void player__render( camera *cam, player_instance *player )
407 {
408 world_instance *world = world_current_instance();
409 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
410
411 struct player_model *model =
412 addon_cache_item_if_loaded( k_addon_type_player,
413 player->playermodel_view_slot );
414
415 if( !model ) model = &player->fallback_model;
416 render_playermodel( cam, world, model, &player->playeravatar->sk );
417
418 struct player_board *board =
419 addon_cache_item_if_loaded( k_addon_type_board,
420 player->board_view_slot );
421
422 render_board( cam, world, board, player->playeravatar->sk.final_mtx[
423 player->playeravatar->id_board],
424 &player->board_pose,
425 k_board_shader_player );
426
427 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
428 }
429
430 #endif /* PLAYER_RENDER_C */