Fix major overstep with last commit
[carveJwlIkooP6JGAAIwe30JlM.git] / player_model.h
index 1673581a98aedb719d7c9dcc455328281d9202bb..97b476897c3500a1bfde5e17e1f7340717887b4f 100644 (file)
@@ -5,70 +5,32 @@
 #ifndef CHARACTER_H 
 #define CHARACTER_H
 
-#include "common.h"
-#include "model.h"
-#include "rigidbody.h"
-#include "render.h"
-#include "skeleton.h"
-#include "world.h"
-#include "skeleton_animator.h"
+#include "player.h"
+#include "player_ragdoll.h"
 #include "shaders/viewchar.h"
 
-static float k_ragdoll_floatyiness = 10.0f,
-             k_ragdoll_floatydrag  = 1.0f;
-
 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
 
-static void character_register(void)
+static void player_model_init(void)
 {
    shader_viewchar_register();
+   vg_acquire_thread_sync();
+   {
+      vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
+   }
+   vg_release_thread_sync();
 }
 
-static void character_init(void)
+static void player_model_free(void *_)
 {
-   vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
+   mesh_free( &player.mdl.mesh );
+   vg_tex2d_free( (vg_tex2d *[]){ &tex_characters }, 1 );
 }
 
-struct character
-{
-   glmesh mesh;
-   struct skeleton sk;
-   struct skeleton_anim *anim_stand,
-                        *anim_highg,
-                        *anim_slide,
-                        *anim_air,
-                        *anim_push, *anim_push_reverse,
-                        *anim_ollie, *anim_ollie_reverse,
-                        *anim_grabs, *anim_stop,
-                        *anim_walk, *anim_run, *anim_idle;
-
-   u32 id_hip,
-       id_ik_hand_l,
-       id_ik_hand_r,
-       id_ik_elbow_l,
-       id_ik_elbow_r,
-       id_head;
-
-   v3f cam_pos;
-
-   struct ragdoll_part
-   {
-      u32 bone_id;
-      v3f offset;
-
-      u32 use_limits;
-      v3f limits[2];
-
-      rigidbody  rb;
-      u32 parent;
-   }
-   *ragdoll;
-   u32 ragdoll_count;
-
-   int shoes[2];
-};
-
-static int character_load( struct character *ch, const char *name )
+/*
+ * Load model from file (.mdl)
+ */
+static void player_load_model( const char *name )
 {
    char buf[64];
 
@@ -76,217 +38,87 @@ static int character_load( struct character *ch, const char *name )
    mdl_header *src = mdl_load( buf );
 
    if( !src )
-      return 0;
-   
-   mdl_unpack_glmesh( src, &ch->mesh );
-
-   skeleton_setup( &ch->sk, src );
-   ch->anim_stand = skeleton_get_anim( &ch->sk, "pose_stand" );
-   ch->anim_highg = skeleton_get_anim( &ch->sk, "pose_highg" );
-   ch->anim_slide = skeleton_get_anim( &ch->sk, "pose_slide" );
-   ch->anim_air   = skeleton_get_anim( &ch->sk, "pose_air" );
-   ch->anim_push  = skeleton_get_anim( &ch->sk, "push" );
-   ch->anim_push_reverse = skeleton_get_anim( &ch->sk, "push_reverse" );
-   ch->anim_ollie = skeleton_get_anim( &ch->sk, "ollie" );
-   ch->anim_ollie_reverse = skeleton_get_anim( &ch->sk, "ollie_reverse" );
-   ch->anim_grabs = skeleton_get_anim( &ch->sk, "grabs" );
-   ch->anim_walk  = skeleton_get_anim( &ch->sk, "walk" );
-   ch->anim_run   = skeleton_get_anim( &ch->sk, "run" );
-   ch->anim_idle  = skeleton_get_anim( &ch->sk, "idle_cycle" );
-
-   ch->id_hip = skeleton_bone_id( &ch->sk, "hips" );
-   ch->id_ik_hand_l = skeleton_bone_id( &ch->sk, "hand.IK.L" );
-   ch->id_ik_hand_r = skeleton_bone_id( &ch->sk, "hand.IK.R" );
-   ch->id_ik_elbow_l = skeleton_bone_id( &ch->sk, "elbow.L" );
-   ch->id_ik_elbow_r = skeleton_bone_id( &ch->sk, "elbow.R" );
-   ch->id_head = skeleton_bone_id( &ch->sk, "head" );
-
-   /* setup ragdoll */
-   
-   if( ch->sk.collider_count )
    {
-      vg_info( "Alloc: %d\n", ch->sk.collider_count );
-      ch->ragdoll = malloc(sizeof(struct ragdoll_part) * ch->sk.collider_count);
-      ch->ragdoll_count = 0;
-
-      for( u32 i=0; i<ch->sk.bone_count; i ++ )
-      {
-         struct skeleton_bone *bone = &ch->sk.bones[i];
-         
-         if( bone->collider )
-         {
-            struct ragdoll_part *rp = &ch->ragdoll[ ch->ragdoll_count ++ ];
-            rp->bone_id = i;
-            
-            v3f delta;
-            v3_sub( bone->hitbox[1], bone->hitbox[0], delta );
-            v3_muls( delta, 0.5f, delta );
-
-            v3_add( bone->hitbox[0], delta, rp->offset );
-
-            v3_copy( delta, rp->rb.bbx[1] );
-            v3_muls( delta, -1.0f, rp->rb.bbx[0] );
-
-            q_identity( rp->rb.q );
-            v3_add( bone->co, rp->offset, rp->rb.co );
-            rp->rb.type = k_rb_shape_box;
-            rp->rb.is_world = 0;
-            rp->parent = 0xffffffff;
-
-            if( bone->parent )
-            {
-               for( u32 j=0; j<ch->ragdoll_count; j++ )
-               {
-                  if( ch->ragdoll[ j ].bone_id == bone->parent )
-                  {
-                     rp->parent = j;
-                     break;
-                  }
-               }
-            }
-            
-            /* TODO: refactor to use this style elswhere */
-            struct mdl_node *pnode = mdl_node_from_id( src, bone->orig_node );
-            struct classtype_bone *bone_inf = mdl_get_entdata( src, pnode );
-
-            rp->use_limits = bone_inf->use_limits;
-            v3_copy( bone_inf->angle_limits[0], rp->limits[0] );
-            v3_copy( bone_inf->angle_limits[1], rp->limits[1] );
-
-            rb_init( &rp->rb );
-         }
-      }
+      vg_error( "Could not load model\n" );
+      return;
    }
 
-   free( src );
-   return 1;
-}
-
-static void character_eval( struct character *ch ){}
-static void character_draw( struct character *ch, float temp, m4x3f camera ){}
-static void character_init_ragdoll_joints( struct character *ch ){}
-static void character_init_ragdoll( struct character *ch ){}
-static void character_ragdoll_go( struct character *ch, v3f pos ){}
-
-static void character_mimic_ragdoll( struct character *ch )
-{
-   for( int i=0; i<ch->ragdoll_count; i++ )
-   {
-      struct ragdoll_part *part = &ch->ragdoll[i];
-      m4x3f offset;
-      m3x3_identity(offset);
-      v3_negate( part->offset, offset[3] );
-      m4x3_mul( part->rb.to_world, offset, ch->sk.final_mtx[part->bone_id] );
-   }
+   struct player_model temp;
 
-   skeleton_apply_inverses( &ch->sk );
-}
+   mdl_unpack_glmesh( src, &temp.mesh );
+   skeleton_setup( &temp.sk, src );
 
-static void character_ragdoll_copypose( struct character *ch, v3f v )
-{
-   for( int i=0; i<ch->ragdoll_count; i++ )
+   /* 
+    * Link animations
+    */
+   struct _load_anim
    {
-      struct ragdoll_part *part = &ch->ragdoll[i];
-
-      v3f pos, offset;
-      u32 bone = part->bone_id;
-      
-      m4x3_mulv( ch->sk.final_mtx[bone], ch->sk.bones[bone].co, pos );
-      m3x3_mulv( ch->sk.final_mtx[bone], part->offset, offset );
-      v3_add( pos, offset, part->rb.co );
-      m3x3_q( ch->sk.final_mtx[bone], part->rb.q );
-      v3_copy( v, part->rb.v );
-      v3_zero( part->rb.w );
-      
-      rb_update_transform( &part->rb );
+      const char *name;
+      struct skeleton_anim **anim;
    }
-}
-
-static void character_debug_ragdoll( struct character *ch )
-{
-   for( u32 i=0; i<ch->ragdoll_count; i ++ )
-      rb_debug( &ch->ragdoll[i].rb, 0xff00ff00 );
-}
-
-static void character_ragdoll_iter( struct character *ch )
-{
-   rb_solver_reset();
-
-   for( int i=0; i<ch->ragdoll_count; i ++ )
-      rb_collide( &ch->ragdoll[i].rb, &world.rb_geo );
-
-   rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
-
-   v3f rv;
-
-   float shoe_vel[2] = {0.0f,0.0f};
-   for( int i=0; i<2; i++ )
-      if( ch->shoes[i] )
-         shoe_vel[i] = v3_length( ch->ragdoll[i].rb.v );
+   anims[] = {
+      { "pose_stand",   &temp.anim_stand },
+      { "pose_highg",   &temp.anim_highg },
+      { "pose_slide",   &temp.anim_slide },
+      { "pose_air",     &temp.anim_air   },
+      { "push",         &temp.anim_push  },
+      { "push_reverse", &temp.anim_push_reverse },
+      { "ollie",        &temp.anim_ollie },
+      { "ollie_reverse",&temp.anim_ollie_reverse },
+      { "grabs",        &temp.anim_grabs },
+      { "walk",         &temp.anim_walk  },
+      { "run",          &temp.anim_run   },
+      { "idle_cycle",   &temp.anim_idle  }
+   };
    
-   for( int j=0; j<ch->ragdoll_count; j++ )
+   for( int i=0; i<vg_list_size(anims); i++ )
    {
-      struct ragdoll_part *pj = &ch->ragdoll[j];
-      struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
-
-      if( pj->parent != 0xffffffff )
+      *anims[i].anim = skeleton_get_anim( &temp.sk, anims[i].name );
+      
+      if( !(*anims[i].anim) )
       {
-         struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
-         struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
-
-         v3f lca, lcb;
-         v3_negate( pj->offset, lca );
-         v3_add( bp->co, pp->offset, lcb );
-         v3_sub( bj->co, lcb, lcb );
-
-         rb_debug_constraint_position( &pj->rb, lca, &pp->rb, lcb );
-
-         if( pj->use_limits )
-         {
-            rb_debug_constraint_limits( &pj->rb, &pp->rb, lca, pj->limits );
-         }
+         vg_error( "Animation '%s' is missing from character '%s'\n",
+                     anims[i].name, name );
+         vg_free( src );
+         return;
       }
-
-      v4f plane = {0.0f,1.0f,0.0f,0.0f};
-      rb_effect_simple_bouyency( &pj->rb, plane, k_ragdoll_floatyiness,
-                                                 k_ragdoll_floatydrag );
    }
 
-   /* CONSTRAINTS */
-   for( int i=0; i<10; i++ )
+   /* 
+    * Link bones
+    */
+   struct _load_bone
    {
-      rb_solve_contacts( rb_contact_buffer, rb_contact_count );
+      const char *name;
+      u32 *bone_id;
+   }
+   bones[] = {
+      { "hips",      &temp.id_hip },
+      { "hand.IK.L", &temp.id_ik_hand_l },
+      { "hand.IK.R", &temp.id_ik_hand_r },
+      { "elbow.L",   &temp.id_ik_elbow_l },
+      { "elbow.R",   &temp.id_ik_elbow_r },
+      { "head",      &temp.id_head }
+   };
+
+   for( int i=0; i<vg_list_size(bones); i++ )
+   {
+      *bones[i].bone_id = skeleton_bone_id( &temp.sk, bones[i].name );
 
-      for( int j=0; j<ch->ragdoll_count; j++ )
+      if( !(*bones[i].bone_id) )
       {
-         struct ragdoll_part *pj = &ch->ragdoll[j];
-         struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
-
-         if( pj->parent != 0xffffffff && pj->use_limits )
-         {
-            struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
-            struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
-
-            v3f lca, lcb;
-            v3_negate( pj->offset, lca );
-            v3_add( bp->co, pp->offset, lcb );
-            v3_sub( bj->co, lcb, lcb );
-
-            rb_constraint_position( &pj->rb, lca, &pp->rb, lcb );
-
-            rb_constraint_limits( &pj->rb, lca, &pp->rb, lcb, pj->limits );
-         }
+         vg_error( "Required bone '%s' is missing from character '%s'\n",
+                     bones[i].name, name );
+         vg_free( src );
+         return;
       }
    }
 
-   /* INTEGRATION */
-   for( int i=0; i<ch->ragdoll_count; i++ )
-      rb_iter( &ch->ragdoll[i].rb );
-   
-   /* SHOES */
-
-   for( int i=0; i<ch->ragdoll_count; i++ )
-      rb_update_transform( &ch->ragdoll[i].rb );
+   /* swap temp into actual model */
+   mesh_free( &player.mdl.mesh );
+   player.mdl = temp;
+   player_init_ragdoll( src );
+   vg_free( src );
 }
 
 #endif