my fucking fingers
[carveJwlIkooP6JGAAIwe30JlM.git] / player_render.c
index ab8942207366aa30d30a435e009b997d0c5c62d5..8f2521538ca9719b7c83b93dccf3a27553f49e0c 100644 (file)
@@ -5,9 +5,11 @@
 #include "player_render.h"
 #include "camera.h"
 #include "player_model.h"
+#include "ent_skateshop.h"
 
 #include "shaders/model_character_view.h"
 #include "shaders/model_board_view.h"
+#include "shaders/model_entity.h"
 
 VG_STATIC void player_avatar_load( struct player_avatar *av, const char *path )
 {
@@ -43,33 +45,23 @@ VG_STATIC void player_model_load( struct player_model *mdl, const char *path )
    mdl_context ctx;
    mdl_open( &ctx, path, vg_mem.scratch );
    mdl_load_metadata_block( &ctx, vg_mem.scratch );
-   mdl_load_mesh_block( &ctx, vg_mem.scratch );
 
    if( !mdl_arrcount( &ctx.textures ) )
-      vg_fatal_exit_loop( "No texture in player model" );
+      vg_fatal_error( "No texture in player model" );
 
    mdl_texture *tex0 = mdl_arritm( &ctx.textures, 0 );
    void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
    mdl_fread_pack_file( &ctx, &tex0->file, data );
 
-   vg_acquire_thread_sync();
-   {
-      mdl_unpack_glmesh( &ctx, &mdl->mesh );
-
-      /* upload first texture */
-      mdl->texture = vg_tex2d_new();
-
-      vg_tex2d_set_error();
-      vg_tex2d_qoi( data, tex0->file.pack_size,
-                    mdl_pstr( &ctx, tex0->file.pstr_path ));
-      vg_tex2d_nearest();
-      vg_tex2d_clamp();
-   }
-   vg_release_thread_sync();
+   vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
+                            VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
+                            &mdl->texture );
 
+   mdl_async_load_glmesh( &ctx, &mdl->mesh );
    mdl_close( &ctx );
 }
 
+/* TODO: allow error handling */
 VG_STATIC void player_board_load( struct player_board *mdl, const char *path )
 {
    vg_linear_clear( vg_mem.scratch );
@@ -77,33 +69,22 @@ VG_STATIC void player_board_load( struct player_board *mdl, const char *path )
    mdl_context ctx;
    mdl_open( &ctx, path, vg_mem.scratch );
    mdl_load_metadata_block( &ctx, vg_mem.scratch );
-   mdl_load_mesh_block( &ctx, vg_mem.scratch );
 
    mdl_array_ptr markers;
    mdl_load_array( &ctx, &markers, "ent_marker", vg_mem.scratch );
 
    if( !mdl_arrcount( &ctx.textures ) )
-      vg_fatal_exit_loop( "No texture in board model" );
+      vg_fatal_error( "No texture in board model" );
 
    mdl_texture *tex0 = mdl_arritm( &ctx.textures, 0 );
    void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
    mdl_fread_pack_file( &ctx, &tex0->file, data );
 
-   vg_acquire_thread_sync();
-   {
-      mdl_unpack_glmesh( &ctx, &mdl->mesh );
-
-      /* upload first texture */
-      mdl->texture = vg_tex2d_new();
-
-      vg_tex2d_set_error();
-      vg_tex2d_qoi( data, tex0->file.pack_size,
-                    mdl_pstr( &ctx, tex0->file.pstr_path ));
-      vg_tex2d_nearest();
-      vg_tex2d_clamp();
-   }
-   vg_release_thread_sync();
+   vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
+                            VG_TEX2D_CLAMP|VG_TEX2D_NEAREST,
+                            &mdl->texture );
 
+   mdl_async_load_glmesh( &ctx, &mdl->mesh );
    mdl_close( &ctx );
 
    for( int i=0; i<4; i++ )
@@ -115,10 +96,6 @@ VG_STATIC void player_board_load( struct player_board *mdl, const char *path )
    for( u32 i=0; i<mdl_arrcount(&ctx.meshs); i++ ){
       mdl_mesh *mesh = mdl_arritm( &ctx.meshs, i );
 
-      vg_info( "[%u]=%u:%u\n", mesh->entity_id,
-                          mdl_entity_id_type( mesh->entity_id ),
-                          mdl_entity_id_id( mesh->entity_id ) );
-
       if( mdl_entity_id_type( mesh->entity_id ) != k_ent_marker )
          continue;
 
@@ -147,6 +124,12 @@ VG_STATIC void player_board_load( struct player_board *mdl, const char *path )
    }
 }
 
+VG_STATIC void player_board_unload( struct player_board *mdl )
+{
+   mesh_free( &mdl->mesh );
+   glDeleteTextures( 1, &mdl->texture );
+}
+
 VG_STATIC void player__pre_render( player_instance *player )
 {
    if( _player_animate[ player->subsystem ] ){
@@ -178,14 +161,28 @@ VG_STATIC void player__pre_render( player_instance *player )
       _player_post_animate[ player->subsystem ]( player );
 
    struct player_avatar *av = player->playeravatar;
+   struct player_board *board = player_get_player_board( player );
 
-   v3f vp0 = {0.0f,0.1f, player->playerboard->truck_positions[0][2]},
-       vp1 = {0.0f,0.1f, player->playerboard->truck_positions[1][2]};
+   v3f vp0, vp1;
+
+   if( board ){
+      v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
+      v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
+   }
+   else{
+      v3_zero( vp0 );
+      v3_zero( vp1 );
+   }
 
    struct ub_world_lighting *ubo = &get_active_world()->ub_lighting;
    m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp0, ubo->g_board_0 );
    m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp1, ubo->g_board_1 );
 
+   if( player->immobile ){
+      player__cam_iterate( player );
+      return;
+   }
+
    if( player->rewinding ){
       if( player->rewind_time <= 0.0f ){
          double taken = vg.time - player->rewind_start;
@@ -274,6 +271,7 @@ VG_STATIC void player__pre_render( player_instance *player )
             vg_alerpf( fr->ang[0], fr1->ang[0], sub );
          player->cam_override_angles[1] = 
             vg_lerpf ( fr->ang[1], fr1->ang[1], sub );
+         player->cam_override_fov = player->cam.fov;
 
          float blend = player->rewind_time * 0.25f;
          player->cam_override_strength = vg_clampf( blend, 0.0f, 1.0f );
@@ -284,82 +282,81 @@ VG_STATIC void player__pre_render( player_instance *player )
    player__cam_iterate( player );
 }
 
-PLAYER_API void player__render( camera *cam, player_instance *player )
+enum board_shader{
+   k_board_shader_player,
+   k_board_shader_entity
+};
+
+VG_STATIC void render_board( camera *cam, world_instance *world,
+                             struct player_board *board, m4x3f root,
+                             enum board_shader shader )
 {
-   shader_model_character_view_use();
+   if( !board ) return;
 
-       glActiveTexture( GL_TEXTURE0 );
-       glBindTexture( GL_TEXTURE_2D, player->playermodel->texture );
-   shader_model_character_view_uTexMain( 0 );
-   shader_model_character_view_uCamera( cam->transform[3] );
-   shader_model_character_view_uPv( cam->mtx.pv );
-   shader_model_character_view_uTexSceneDepth( 1 );
-   render_fb_bind_texture( gpipeline.fb_main, 2, 1 );
    v3f inverse;
-   render_fb_inverse_ratio( gpipeline.fb_main, inverse );
-   inverse[2] = main_camera.farz-main_camera.nearz;
 
-   shader_model_character_view_uInverseRatioDepth( inverse );
-   render_fb_inverse_ratio( NULL, inverse );
-   inverse[2] = cam->farz-cam->nearz;
-   shader_model_character_view_uInverseRatioMain( inverse );
-
-   world_instance *world = get_active_world();
-   world_link_lighting_ub( world, _shader_model_character_view.id );
-   world_bind_position_texture( world, _shader_model_character_view.id,
-                              _uniform_model_character_view_g_world_depth, 2 );
-   world_bind_light_array( world, _shader_model_character_view.id,
-                              _uniform_model_character_view_uLightsArray, 3 );
-   world_bind_light_index( world, _shader_model_character_view.id,
-                              _uniform_model_character_view_uLightsIndex, 4 );
+   glActiveTexture( GL_TEXTURE0 );
+   glBindTexture( GL_TEXTURE_2D, board->texture );
 
-   glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
-                         player->playeravatar->sk.bone_count,
-                         0,
-                         (float *)player->playeravatar->sk.final_mtx );
-   
-   mesh_bind( &player->playermodel->mesh );
-   mesh_draw( &player->playermodel->mesh );
+   if( shader == k_board_shader_player ){
+      shader_model_board_view_use();
+      shader_model_board_view_uTexMain( 0 );
+      shader_model_board_view_uCamera( cam->transform[3] );
+      shader_model_board_view_uPv( cam->mtx.pv );
+      shader_model_board_view_uTexSceneDepth( 1 );
 
-   /* draw skateboard */
-   shader_model_board_view_use();
-       glActiveTexture( GL_TEXTURE0 );
-   glBindTexture( GL_TEXTURE_2D, player->playerboard->texture );
-   shader_model_board_view_uTexMain( 0 );
-   shader_model_board_view_uCamera( cam->transform[3] );
-   shader_model_board_view_uPv( cam->mtx.pv );
-   shader_model_board_view_uTexSceneDepth( 1 );
+      render_fb_inverse_ratio( gpipeline.fb_main, inverse );
 
-   render_fb_inverse_ratio( gpipeline.fb_main, inverse );
-   inverse[2] = main_camera.farz-main_camera.nearz;
+      inverse[2] = main_camera.farz-main_camera.nearz;
 
-   shader_model_board_view_uInverseRatioDepth( inverse );
-   render_fb_inverse_ratio( NULL, inverse );
-   inverse[2] = cam->farz-cam->nearz;
-   shader_model_board_view_uInverseRatioMain( inverse );
+      shader_model_board_view_uInverseRatioDepth( inverse );
+      render_fb_inverse_ratio( NULL, inverse );
+      inverse[2] = cam->farz-cam->nearz;
+      shader_model_board_view_uInverseRatioMain( inverse );
 
-   world_link_lighting_ub( world, _shader_model_board_view.id );
-   world_bind_position_texture( world, _shader_model_board_view.id,
-                              _uniform_model_board_view_g_world_depth, 2 );
-   world_bind_light_array( world, _shader_model_board_view.id,
-                              _uniform_model_board_view_uLightsArray, 3 );
-   world_bind_light_index( world, _shader_model_board_view.id,
-                              _uniform_model_board_view_uLightsIndex, 4 );
-
-   m4x3f root;
-   m4x3_copy( player->playeravatar->sk.final_mtx[player->playeravatar->id_board]
-              , root );
+      world_link_lighting_ub( world, _shader_model_board_view.id );
+      world_bind_position_texture( world, _shader_model_board_view.id,
+                                 _uniform_model_board_view_g_world_depth, 2 );
+      world_bind_light_array( world, _shader_model_board_view.id,
+                                 _uniform_model_board_view_uLightsArray, 3 );
+      world_bind_light_index( world, _shader_model_board_view.id,
+                                 _uniform_model_board_view_uLightsIndex, 4 );
+   }
+   else if( shader == k_board_shader_entity ){
+      shader_model_entity_use();
+      shader_model_entity_uTexMain( 0 );
+      shader_model_entity_uCamera( cam->transform[3] );
+      shader_model_entity_uPv( cam->mtx.pv );
+      
+      world_link_lighting_ub( world, _shader_model_entity.id );
+      world_bind_position_texture( world, _shader_model_entity.id,
+                                 _uniform_model_entity_g_world_depth, 2 );
+      world_bind_light_array( world, _shader_model_entity.id,
+                                 _uniform_model_entity_uLightsArray, 3 );
+      world_bind_light_index( world, _shader_model_entity.id,
+                                 _uniform_model_entity_uLightsIndex, 4 );
+   }
 
-   struct player_board *board = player->playerboard;
    mesh_bind( &board->mesh );
 
+   m4x4f m4mdl;
+
    if( board->board.indice_count ){
       m4x3f mlocal;
       m3x3_identity( mlocal );
       v3_copy( board->board_position, mlocal[3] );
       m4x3_mul( root, mlocal, mlocal );
 
-      shader_model_board_view_uMdl( mlocal );
+      if( shader == k_board_shader_entity ){
+         /* TODO: provide a way to supply previous mdl mtx? */
+         m4x3_expand( mlocal, m4mdl );
+         m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
+         shader_model_entity_uPvmPrev( m4mdl );
+         shader_model_entity_uMdl( mlocal );
+      }
+      else
+         shader_model_board_view_uMdl( mlocal );
+
       mdl_draw_submesh( &board->board );
    }
 
@@ -372,7 +369,15 @@ PLAYER_API void player__render( camera *cam, player_instance *player )
       v3_copy( board->truck_positions[i], mlocal[3] );
       m4x3_mul( root, mlocal, mlocal );
 
-      shader_model_board_view_uMdl( mlocal );
+      if( shader == k_board_shader_entity ){
+         m4x3_expand( mlocal, m4mdl );
+         m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
+         shader_model_entity_uPvmPrev( m4mdl );
+         shader_model_entity_uMdl( mlocal );
+      }
+      else
+         shader_model_board_view_uMdl( mlocal );
+
       mdl_draw_submesh( &board->trucks[i] );
    }
 
@@ -385,9 +390,61 @@ PLAYER_API void player__render( camera *cam, player_instance *player )
       v3_copy( board->wheel_positions[i], mlocal[3] );
       m4x3_mul( root, mlocal, mlocal );
 
-      shader_model_board_view_uMdl( mlocal );
+      if( shader == k_board_shader_entity ){
+         m4x3_expand( mlocal, m4mdl );
+         m4x4_mul( cam->mtx_prev.pv, m4mdl, m4mdl );
+         shader_model_entity_uPvmPrev( m4mdl );
+         shader_model_entity_uMdl( mlocal );
+      }
+      else
+         shader_model_board_view_uMdl( mlocal );
+
       mdl_draw_submesh( &board->wheels[i] );
    }
 }
 
+PLAYER_API void player__render( camera *cam, player_instance *player )
+{
+   shader_model_character_view_use();
+
+       glActiveTexture( GL_TEXTURE0 );
+       glBindTexture( GL_TEXTURE_2D, player->playermodel->texture );
+   shader_model_character_view_uTexMain( 0 );
+   shader_model_character_view_uCamera( cam->transform[3] );
+   shader_model_character_view_uPv( cam->mtx.pv );
+   shader_model_character_view_uTexSceneDepth( 1 );
+   render_fb_bind_texture( gpipeline.fb_main, 2, 1 );
+   v3f inverse;
+   render_fb_inverse_ratio( gpipeline.fb_main, inverse );
+   inverse[2] = main_camera.farz-main_camera.nearz;
+
+   shader_model_character_view_uInverseRatioDepth( inverse );
+   render_fb_inverse_ratio( NULL, inverse );
+   inverse[2] = cam->farz-cam->nearz;
+   shader_model_character_view_uInverseRatioMain( inverse );
+
+   world_instance *world = get_active_world();
+   world_link_lighting_ub( world, _shader_model_character_view.id );
+   world_bind_position_texture( world, _shader_model_character_view.id,
+                              _uniform_model_character_view_g_world_depth, 2 );
+   world_bind_light_array( world, _shader_model_character_view.id,
+                              _uniform_model_character_view_uLightsArray, 3 );
+   world_bind_light_index( world, _shader_model_character_view.id,
+                              _uniform_model_character_view_uLightsIndex, 4 );
+
+   glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
+                         player->playeravatar->sk.bone_count,
+                         0,
+                         (float *)player->playeravatar->sk.final_mtx );
+   
+   mesh_bind( &player->playermodel->mesh );
+   mesh_draw( &player->playermodel->mesh );
+
+   struct player_board *board = player_get_player_board( player );
+
+   render_board( cam, world, board, player->playeravatar->sk.final_mtx[
+                                       player->playeravatar->id_board],
+                                       k_board_shader_player );
+}
+
 #endif /* PLAYER_RENDER_C */