basic npc
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_npc.c
1 #include "vg/vg_mem.h"
2 #include "ent_npc.h"
3 #include "shaders/model_character_view.h"
4
5 struct npc npc_gumpa;
6 static struct skeleton_anim *gumpa_idle;
7
8 void npc_load_model( struct npc *npc, const char *path )
9 {
10 vg_linear_clear( vg_mem.scratch );
11
12 mdl_context *meta = &npc->meta;
13 mdl_open( meta, path, vg_mem.rtmemory );
14 mdl_load_metadata_block( meta, vg_mem.rtmemory );
15 mdl_load_animation_block( meta, vg_mem.rtmemory );
16
17 struct skeleton *sk = &npc->skeleton;
18 skeleton_setup( sk, vg_mem.rtmemory, meta );
19
20 u32 mtx_size = sizeof(m4x3f)*sk->bone_count;
21 npc->final_mtx = vg_linear_alloc( vg_mem.rtmemory, mtx_size );
22
23 if( !mdl_arrcount( &meta->textures ) )
24 vg_fatal_error( "No texture in model" );
25
26 mdl_texture *tex0 = mdl_arritm( &meta->textures, 0 );
27 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
28 mdl_fread_pack_file( meta, &tex0->file, data );
29
30 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
31 VG_TEX2D_NEAREST|VG_TEX2D_CLAMP,
32 &npc->texture );
33
34 mdl_async_load_glmesh( meta, &npc->mesh, NULL );
35 mdl_close( meta );
36 }
37
38 void npc_init(void)
39 {
40 npc_load_model( &npc_gumpa, "models/gumpa.mdl" );
41 gumpa_idle = skeleton_get_anim( &npc_gumpa.skeleton, "gumpa_idle" );
42 }
43
44 static struct npc *npc_resolve( u32 id )
45 {
46 if( id == 1 ) return &npc_gumpa;
47 else return NULL;
48 }
49
50 void npc_update( ent_npc *ent )
51 {
52 struct npc *npc_def = npc_resolve( ent->id );
53 VG_ASSERT( npc_def );
54
55 player_pose pose;
56 struct skeleton *sk = &npc_def->skeleton;
57 pose.type = k_player_pose_type_ik;
58 pose.board.lean = 0.0f;
59
60 skeleton_sample_anim( sk, gumpa_idle, vg.time, pose.keyframes );
61
62 v3_copy( ent->transform.co, pose.root_co );
63 v4_copy( ent->transform.q, pose.root_q );
64 apply_full_skeleton_pose( &npc_def->skeleton, &pose, npc_def->final_mtx );
65 }
66
67 void npc_render( ent_npc *ent, world_instance *world, vg_camera *cam )
68 {
69 struct npc *npc_def = npc_resolve( ent->id );
70 VG_ASSERT( npc_def );
71
72 shader_model_character_view_use();
73
74 glActiveTexture( GL_TEXTURE0 );
75 glBindTexture( GL_TEXTURE_2D, npc_def->texture );
76 shader_model_character_view_uTexMain( 0 );
77 shader_model_character_view_uCamera( cam->transform[3] );
78 shader_model_character_view_uPv( cam->mtx.pv );
79 shader_model_character_view_uDepthCompare( 0 );
80
81 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_character_view );
82
83 glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
84 npc_def->skeleton.bone_count,
85 0,
86 (const GLfloat *)npc_def->final_mtx );
87
88 mesh_bind( &npc_def->mesh );
89 mesh_draw( &npc_def->mesh );
90 }