remove some todos
[carveJwlIkooP6JGAAIwe30JlM.git] / model.h
diff --git a/model.h b/model.h
index 7c4e0d9cbade5266c18b85d45a2b3f09fe8d8e8a..ae19f6f090d57848ff97fdb9d8f9df2533377a6e 100644 (file)
--- a/model.h
+++ b/model.h
@@ -1,93 +1,51 @@
 /*
- * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
  */
 
-#ifndef MODEL_H
-#define MODEL_H
+#pragma once
 
-#include "common.h"
+#define MDL_VERSION_MIN 101
+#define MDL_VERSION_NR 105
 
-typedef struct glmesh glmesh;
-
-typedef struct mdl_vert mdl_vert;
-typedef struct mdl_submesh mdl_submesh;
-typedef struct mdl_material mdl_material;
-typedef struct mdl_node mdl_node;
-typedef struct mdl_file_header mdl_file_header;
-typedef struct mdl_animation mdl_animation;
-typedef struct mdl_keyframe mdl_keyframe;
-typedef struct mdl_texture mdl_texture;
-typedef struct mdl_context mdl_context;
-
-#define MDL_SIZE_MAX          0x1000000
-#define MDL_VERT_MAX          1000000
-#define MDL_INDICE_MAX        1000000
-#define MDL_MATERIAL_MAX      32
-#define MDL_NODE_MAX          4000
-#define MDL_SUBMESH_MAX       8000
-#define MDL_STRING_LENGTH_MAX 64
-
-enum classtype
-{
-   k_classtype_none                 = 0,
-   k_classtype_gate                 = 1,
-   k_classtype_spawn                = 3,
-   k_classtype_water                = 4,
-   k_classtype_route_node           = 8,
-   k_classtype_route                = 9,
-   k_classtype_bone                 = 10,
-   k_classtype_skeleton             = 11,
-   k_classtype_skin                 = 12,
-   k_classtype_audio                = 14,
-   k_classtype_trigger              = 100,
-   k_classtype_logic_achievement    = 101,
-   k_classtype_logic_relay          = 102,
-   k_classtype_world_light          = 200,
-   k_classtype_nonlocal_gate        = 300
-};
-
-enum mdl_shader
-{
+enum mdl_shader{
    k_shader_standard                = 0,
    k_shader_standard_cutout         = 1,
    k_shader_terrain_blend           = 2,
    k_shader_standard_vertex_blend   = 3,
-   k_shader_water                   = 4
-};
-
-enum mdl_surface_prop
-{
-   k_surface_prop_concrete          = 0,
-   k_surface_prop_wood              = 1,
-   k_surface_prop_grass             = 2
+   k_shader_water                   = 4,
+   k_shader_invisible               = 5,
+   k_shader_boundary                = 6,
+   k_shader_fxglow                  = 7,
+   k_shader_cubemap                 = 8,
+   k_shader_walking                 = 9,
+   k_shader_foliage                 = 10,
+   k_shader_override                = 30000
 };
 
-enum material_flag
-{
-   k_material_flag_skate_surface    = 0x1,
-   k_material_flag_collision        = 0x2,
-   k_material_flag_grow_grass       = 0x4,
-   k_material_flag_grind_surface    = 0x8
+enum mdl_surface_prop{
+   k_surface_prop_concrete = 0,
+   k_surface_prop_wood     = 1,
+   k_surface_prop_grass    = 2,
+   k_surface_prop_tiles    = 3,
+   k_surface_prop_metal    = 4,
+   k_surface_prop_snow     = 5,
+   k_surface_prop_sand     = 6
 };
 
-enum bone_flag
-{
-   k_bone_flag_deform               = 0x1,
-   k_bone_flag_ik                   = 0x2,
-   k_bone_flag_collider_box         = 0x4,
-   k_bone_flag_collider_capsule     = 0x8,
-   k_bone_flag_collider_reserved0   = 0x10,
-   k_bone_flag_collider_reserved1   = 0x20,
-   k_bone_flag_collider_reserved2   = 0x40,
-   k_bone_flag_collider_reserved3   = 0x80,
-   k_bone_flag_collider_any         = k_bone_flag_collider_box |
-                                      k_bone_flag_collider_capsule |
-                                      k_bone_flag_collider_reserved0 |
-                                      k_bone_flag_collider_reserved1 |
-                                      k_bone_flag_collider_reserved2 |
-                                      k_bone_flag_collider_reserved3,
-   k_bone_flag_cone_constraint      = 0x100,
-   k_bone_flag_force_u32            = 0xffffffff
+enum material_flag{
+   k_material_flag_skate_target     = 0x0001,
+   k_material_flag_collision        = 0x0002,
+   k_material_flag_grow_grass       = 0x0004,
+   k_material_flag_grindable        = 0x0008,
+   k_material_flag_invisible        = 0x0010,
+   k_material_flag_boundary         = 0x0020,
+   k_material_flag_preview_visibile = 0x0040,
+   k_material_flag_walking          = 0x0080,
+
+   k_material_flag_ghosts      =
+      k_material_flag_boundary|
+      k_material_flag_invisible|
+      k_material_flag_walking
 };
 
 #pragma pack(push,1)
@@ -104,623 +62,259 @@ struct mdl_vert
    u8  groups[4]; /* 4*8 */
 };
 
-struct mdl_submesh
-{
-   u32 indice_start,
-       indice_count,
-       vertex_start,
-       vertex_count;
-   
-   boxf bbx;
-   u32 material_id;
-};
-
-struct mdl_texture
-{
-   u32 pstr_name,
-       pack_offset,
-       pack_length;
-};
-
-struct mdl_material
-{
-   u32 pstr_name,
-       shader,
-       flags,
-       surface_prop;
+#pragma pack(pop)
 
-   v4f colour,
-       colour1;
+typedef u32 mdl_indice;
 
-   u32 tex_diffuse,
-       tex_decal,
-       tex_normal;
-};
+typedef struct mdl_context mdl_context;
+typedef struct mdl_array_ptr mdl_array_ptr;
+typedef struct mdl_vert mdl_vert;
+typedef struct mdl_transform mdl_transform;
+typedef struct mdl_submesh mdl_submesh;
+typedef struct mdl_material mdl_material;
+typedef struct mdl_bone mdl_bone;
+typedef struct mdl_armature mdl_armature;
+typedef struct mdl_animation mdl_animation;
+typedef struct mdl_transform mdl_keyframe;
+typedef struct mdl_mesh mdl_mesh;
+typedef struct mdl_file mdl_file;
+typedef struct mdl_texture mdl_texture;
+typedef struct mdl_array mdl_array;
+typedef struct mdl_header mdl_header;
 
-struct mdl_node
+typedef struct glmesh glmesh;
+struct glmesh
 {
-   v3f co;
-   v4f q;
-   v3f s;
-   
-   u32 sub_uid,         /* allocated in-file... too bad. */
-       submesh_start,
-       submesh_count,
-       classtype,
-       offset,
-       parent,
-       pstr_name;
+   GLuint vao, vbo, ebo;
+   u32 indice_count;
+   u32 loaded;
 };
 
-struct mdl_keyframe
+struct mdl_transform
 {
-   v3f co;
+   v3f co, s;
    v4f q;
-   v3f s;
 };
 
-struct mdl_animation
+static void transform_identity( mdl_transform *transform )
 {
-   u32 pstr_name,
-       length;
-
-   float rate;
-
-   u32 offset;
-};
+   v3_zero( transform->co );
+   q_identity( transform->q );
+   v3_fill( transform->s, 1.0f );
+}
 
-struct mdl_file_header
+static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest )
 {
-   u32 identifier, version, file_length, pad0;
-
-   u32 
-       node_count,      node_offset,
-       submesh_count,   submesh_offset,
-       material_count,  material_offset,
-       texture_count,   texture_offset,
-       anim_count,      anim_offset,
-       entdata_size,    entdata_offset, 
-       strings_size,    strings_offset, 
-
-       keyframe_count,  keyframe_offset,
-
-       vertex_count,    vertex_offset,
-       indice_count,    indice_offset,
-
-       pack_size,       pack_offset;
-};
-
-/* 
- * Entity data structures
- */
+   v3_mul( transform->s, vec, dest );
+   q_mulv( transform->q, dest, dest );
+}
 
-struct classtype_gate
+static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest )
 {
-   u32 target;
-   v3f dims;
-};
+   mdl_transform_vector( transform, co, dest );
+   v3_add( transform->co, dest, dest );
+}
 
-struct classtype_spawn
+static void mdl_transform_mul( mdl_transform *a, mdl_transform *b, 
+                               mdl_transform *d )
 {
-   u32 pstr_alias;
-};
+   mdl_transform_point( a, b->co, d->co );
+   q_mul( a->q, b->q, d->q );
+   q_normalize( d->q );
+   v3_mul( a->s, b->s, d->s );
+}
 
-struct classtype_water
+struct mdl_material
 {
-   u32 temp;
-};
+   u32 pstr_name,
+       shader,
+       flags,
+       surface_prop;
 
-struct classtype_route_node
-{
-   u32 target, target1;
-};
+   v4f colour,
+       colour1;
 
-struct classtype_route
-{
-   u32 id_start;
-   u32 pstr_name;
-   v3f colour;
+   u32 tex_diffuse, /* Indexes start from 1. 0 if missing. */
+       tex_none0,
+       tex_none1;
 };
 
-struct classtype_bone
+struct mdl_bone
 {
-   u32 flags,
+   v3f co, end;
+   u32 parent,
+       collider,
        ik_target,
-       ik_pole;
+       ik_pole,
+       flags,
+       pstr_name;
 
    boxf hitbox;
-
    v3f conevx, conevy, coneva;
    float conet;
 };
 
-struct classtype_skeleton
-{
-   u32 channels,
-       ik_count,
-       collider_count,
-       anim_start,
-       anim_count;
-};
-
-struct classtype_skin
+enum bone_flag
 {
-   u32 skeleton;
+   k_bone_flag_deform               = 0x00000001,
+   k_bone_flag_ik                   = 0x00000002,
+   k_bone_flag_cone_constraint      = 0x00000004
 };
 
-struct classtype_trigger
+enum bone_collider
 {
-   u32 target;
+   k_bone_collider_none = 0,
+   k_bone_collider_box = 1,
+   k_bone_collider_capsule = 2
 };
-
-struct classtype_logic_relay
+         
+struct mdl_armature
 {
-   u32 targets[4];
+   mdl_transform transform;
+   u32 bone_start,
+       bone_count,
+       anim_start,
+       anim_count;
 };
 
-struct classtype_logic_achievement
+struct mdl_animation
 {
-   u32 pstr_name;
+   u32 pstr_name,
+       length;
+   float rate;
+   u32 offset;
 };
 
-struct classtype_audio
+struct mdl_submesh
 {
-   u32 pstr_file,
-       flags;
+   u32 indice_start,
+       indice_count,
+       vertex_start,
+       vertex_count;
 
-   float volume;
+   boxf bbx;
+   u16 material_id, flags;
 };
 
-struct classtype_world_light
+enum esubmesh_flags
 {
-   enum light_type 
-   {
-      k_light_type_point,
-      k_light_type_spot,
-      k_light_type_point_nighttime_only,
-      k_light_type_spot_nighttime_only
-   } 
-   type;
-
-   v4f colour; /* RGB, Energy */
-   float angle;
+   k_submesh_flag_none     = 0x0000,
+   k_submesh_flag_consumed = 0x0001
 };
 
-#pragma pack(pop)
-
-
-struct mdl_context
+struct mdl_mesh
 {
-   FILE *file;
-   mdl_file_header info;
-
-   /* each buffer becomes availible after each _load function is called */
-   mdl_node       *node_buffer;     /* mdl_load_metadata() */
-   mdl_submesh    *submesh_buffer;
-   mdl_material   *material_buffer;
-   mdl_texture    *texture_buffer;
-   mdl_animation  *anim_buffer;
-   void           *entdata_buffer;
-   const char     *string_buffer;
-
-   mdl_keyframe   *keyframe_buffer; /* mdl_load_anim_data() */
-
-   mdl_vert       *vertex_buffer;   /* mdl_load_mesh_data() */
-   u32            *index_buffer;
-
-   void           *pack;            /* mdl_load_pack_data() */
+   mdl_transform transform;
+   u32 submesh_start,
+       submesh_count,
+       pstr_name,
+       entity_id,    /* upper 16 bits: type, lower 16 bits: index */
+       armature_id;
 };
 
-/*
- * Simple mesh interface for OpenGL
- */
-
-struct glmesh
+struct mdl_file
 {
-   GLuint vao, vbo, ebo;
-   u32 indice_count;
-   u32 loaded;
+   u32 pstr_path,
+       pack_offset,
+       pack_size;
 };
 
-VG_STATIC void mesh_upload( glmesh *mesh,
-                         mdl_vert *verts, u32 vert_count,
-                         u32 *indices, u32 indice_count )
-{
-   //assert( mesh->loaded == 0 );
-
-   glGenVertexArrays( 1, &mesh->vao );
-   glGenBuffers( 1, &mesh->vbo );
-   glGenBuffers( 1, &mesh->ebo );
-   glBindVertexArray( mesh->vao );
-
-   size_t stride = sizeof(mdl_vert);
-
-   glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
-   glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
-
-   glBindVertexArray( mesh->vao );
-   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
-   glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
-         indices, GL_STATIC_DRAW );
-   
-   /* 0: coordinates */
-   glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
-   glEnableVertexAttribArray( 0 );
-
-   /* 1: normal */
-   glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 
-         stride, (void *)offsetof(mdl_vert, norm) );
-   glEnableVertexAttribArray( 1 );
-
-   /* 2: uv */
-   glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, 
-         stride, (void *)offsetof(mdl_vert, uv) );
-   glEnableVertexAttribArray( 2 );
-
-   /* 3: colour */
-   glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE, 
-         stride, (void *)offsetof(mdl_vert, colour) );
-   glEnableVertexAttribArray( 3 );
-
-   /* 4: weights */
-   glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE, 
-         stride, (void *)offsetof(mdl_vert, weights) );
-   glEnableVertexAttribArray( 4 );
-
-   /* 5: groups */
-   glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
-         stride, (void *)offsetof(mdl_vert, groups) );
-   glEnableVertexAttribArray( 5 );
-
-   VG_CHECK_GL_ERR();
-
-   mesh->indice_count = indice_count;
-   mesh->loaded = 1;
-}
-
-VG_STATIC void mesh_bind( glmesh *mesh )
-{
-   glBindVertexArray( mesh->vao );
-}
-
-VG_STATIC void mesh_drawn( u32 start, u32 count )
-{
-   glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT, 
-         (void *)(start*sizeof(u32)) );
-}
-
-VG_STATIC void mesh_draw( glmesh *mesh )
-{
-   mesh_drawn( 0, mesh->indice_count );
-}
-
-VG_STATIC void mesh_free( glmesh *mesh )
-{
-   if( mesh->loaded )
-   {
-      glDeleteVertexArrays( 1, &mesh->vao );
-      glDeleteBuffers( 1, &mesh->ebo );
-      glDeleteBuffers( 1, &mesh->vbo );
-      mesh->loaded = 0;
-   }
-}
-
-VG_STATIC void mdl_load_fatal_corrupt( mdl_context *mdl )
-{
-   fclose( mdl->file );
-   vg_file_print_invalid( mdl->file );
-   vg_fatal_exit_loop( "Corrupt model" );
-}
-
-/*
- * Model implementation
- *
- * TODO.
- *
- * you have two api options for loading a model, first, the easy way:
- *       mdl_load ...
- *    will put the entire model straight into the linear_alloc
- *
- * or, to target different allocators:
- *
- *       mdl_open
- *       mdl_load_metadata
- *       mdl_load_vertex_data
- *       mdl_load_indice_data
- *       mdl_close
- *
- * these should ideally be called in quick succession to limit stalls.
- */
-
-/*
- * if calling mdl_open, and the file does not exist, the game will fatal quit
- */
-VG_STATIC void mdl_open( mdl_context *mdl, const char *path )
-{
-   memset( mdl, 0, sizeof( mdl_context ) );
-   mdl->file = fopen( path, "rb" );
-
-   if( !mdl->file )
-   {
-      vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
-      vg_fatal_exit_loop( "see above for details" );
-   }
-
-   u64 l = fread( &mdl->info, sizeof(mdl_file_header), 1, mdl->file );
-   if( l != 1 )
-      mdl_load_fatal_corrupt( mdl );
-}
-
-/*
- * Load all metadata (everything up until the large buffers). Probs at most 50k
- */
-VG_STATIC void mdl_load_metadata( mdl_context *mdl, void *lin_alloc )
-{
-   assert( mdl->file );
-   
-   u64 lheader  = sizeof(mdl_file_header),
-       ldata    = mdl->info.keyframe_offset - lheader;
-
-   void *all_data = vg_linear_alloc( lin_alloc, ldata );
-
-   fseek( mdl->file, lheader, SEEK_SET );
-   u64 l = fread( all_data, ldata, 1, mdl->file );
-
-   if( l != 1 )
-   {
-      vg_file_print_invalid( mdl->file );
-      vg_fatal_exit_loop( "Corrupt model" );
-   }
-
-   mdl->node_buffer     = all_data + (mdl->info.node_offset     - lheader);
-   mdl->submesh_buffer  = all_data + (mdl->info.submesh_offset  - lheader);
-   mdl->material_buffer = all_data + (mdl->info.material_offset - lheader);
-   mdl->texture_buffer  = all_data + (mdl->info.texture_offset  - lheader);
-   mdl->anim_buffer     = all_data + (mdl->info.anim_offset     - lheader);
-   mdl->entdata_buffer  = all_data + (mdl->info.entdata_offset  - lheader);
-   mdl->string_buffer   = all_data + (mdl->info.strings_offset  - lheader);
-}
-
-/*
- * Load just the mesh data
- */
-VG_STATIC void mdl_load_mesh_data( mdl_context *mdl, void *lin_alloc )
-{
-   assert( mdl->file );
-   
-   u64 size_verts = vg_align8( mdl->info.vertex_count * sizeof(mdl_vert) ),
-       size_index = vg_align8( mdl->info.indice_count * sizeof(u32) );
-
-   mdl->vertex_buffer = vg_linear_alloc( lin_alloc, size_verts );
-   mdl->index_buffer  = vg_linear_alloc( lin_alloc, size_index );
-                                                    
-   {
-      fseek( mdl->file, mdl->info.vertex_offset, SEEK_SET );
-      u64 l = fread( mdl->vertex_buffer, size_verts, 1, mdl->file );
-      if( l != 1 )
-         mdl_load_fatal_corrupt( mdl );
-   }
-   {
-      fseek( mdl->file, mdl->info.indice_offset, SEEK_SET );
-      u64 l = fread( mdl->index_buffer, size_index, 1, mdl->file );
-      if( l != 1 )
-         mdl_load_fatal_corrupt( mdl );
-   }
-}
-
-/* 
- * Load animation data
- */
-VG_STATIC void mdl_load_anim_data( mdl_context *mdl, void *lin_alloc )
-{
-   assert( mdl->file );
-
-   if( mdl->info.keyframe_count == 0 )
-      return;
-   
-   u64 size_kf = vg_align8( mdl->info.keyframe_count * sizeof(mdl_keyframe) );
-   mdl->keyframe_buffer = vg_linear_alloc( lin_alloc, size_kf );
-                                                    
-   fseek( mdl->file, mdl->info.keyframe_offset, SEEK_SET );
-   u64 l = fread( mdl->keyframe_buffer, size_kf, 1, mdl->file );
-   if( l != 1 )
-      mdl_load_fatal_corrupt( mdl );
-}
-
-/*
- * Load pack contents
- *
- * TODO request specific files (low)
- */
-VG_STATIC void mdl_load_pack_data( mdl_context *mdl, void *lin_alloc )
+struct mdl_texture
 {
-   assert( mdl->file );
-
-   if( mdl->info.pack_size == 0 )
-      return;
-   
-   mdl->pack = vg_linear_alloc( lin_alloc, vg_align8( mdl->info.pack_size ) );
-   fseek( mdl->file, mdl->info.pack_offset, SEEK_SET );
-
-   u64 l = fread( mdl->pack, mdl->info.pack_size, 1, mdl->file );
-   if( l != 1 )
-      mdl_load_fatal_corrupt( mdl );
-}
+   mdl_file file;
+   u32 glname;
+};
 
-/*
- * close file handle
- */
-VG_STATIC void mdl_close( mdl_context *mdl )
+struct mdl_array
 {
-   fclose( mdl->file );
-   mdl->file = NULL;
-}
+   u32 file_offset,
+       item_count,
+       item_size;
 
-/* open a model. TODO: make this flags ( ANIM_DATA|MESH_DATA ... ) */
-VG_STATIC mdl_context *mdl_load_full( void *lin_alloc, const char *path )
-{
-   /* Inspect the header by opening it, give us the size needed */
-   mdl_context temp_ctx;
-   mdl_open( &temp_ctx, path );
-
-   /* create allocator */
-   u32 tot_size = temp_ctx.info.file_length + sizeof( mdl_context ) + 64;
-   void *data = vg_create_linear_allocator( lin_alloc, tot_size, 
-                                            VG_MEMORY_SYSTEM );
-
-   /* copy context and load all other data */
-   mdl_context *ctx = vg_linear_alloc( data, sizeof(mdl_context) );
-   memcpy( ctx, &temp_ctx, sizeof(mdl_context) );
-
-   mdl_load_metadata( ctx, data );
-   mdl_load_anim_data( ctx, data );
-   mdl_load_mesh_data( ctx, data );
-   mdl_load_pack_data( ctx, data );
-   mdl_close( ctx );
-
-   return ctx;
-}
+   char name[16];
+};
 
-/*
- * Item getters
- * ----------------------------------------------------------------------------
- * TODO: Clamp access and oob errors
- */
-VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr )
+struct mdl_header
 {
-   return mdl->string_buffer + pstr;
-}
+   u32 version;
+   mdl_array index;
+};
 
-VG_STATIC mdl_node *mdl_node_from_id( mdl_context *mdl, u32 id )
-{
-   return &mdl->node_buffer[id];
-}
+struct mdl_context{
+   FILE *file;
+   mdl_header info;
 
-VG_STATIC mdl_node *mdl_node_from_name( mdl_context *mdl, const char *name )
-{
-   for( int i=0; i < mdl->info.node_count; i++ )
+   struct mdl_array_ptr
    {
-      mdl_node *pnode = mdl_node_from_id( mdl, i );
-      
-      if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) )
-         return pnode;
+      void *data;
+      u32 count, stride;
    }
-   
-   return NULL;
-}
-
-VG_STATIC mdl_submesh *mdl_node_submesh( mdl_context *mdl, 
-                                         mdl_node *node, u32 i )
-{
-   return &mdl->submesh_buffer[ node->submesh_start+i ];
-}
-
-VG_STATIC u32 *mdl_submesh_indices( mdl_context *mdl, mdl_submesh *sm )
-{
-   return &mdl->index_buffer[ sm->indice_start ];
-}
-
-VG_STATIC mdl_vert *mdl_submesh_vertices( mdl_context *mdl, mdl_submesh *sm )
-{
-   return &mdl->vertex_buffer[ sm->vertex_start ];
-}
+   index,
+
+   /* metadata */
+   strings,
+   meshs,
+   submeshs,
+   materials,
+   textures,
+   armatures,
+   bones,
+   animations,
+
+   /* animation buffers */
+   keyframes,
+
+   /* mesh buffers */
+   verts,
+   indices;
+   u32 pack_base_offset;
+
+   /* runtime */
+   glmesh mesh;
+};
 
-VG_STATIC void mdl_node_transform( mdl_node *pnode, m4x3f transform )
-{
-   q_m3x3( pnode->q, transform );
-   v3_muls( transform[0], pnode->s[0], transform[0] );
-   v3_muls( transform[1], pnode->s[1], transform[1] );
-   v3_muls( transform[2], pnode->s[2], transform[2] );
-   v3_copy( pnode->co, transform[3] );
-}
+void mesh_bind( glmesh *mesh );
+void mesh_drawn( u32 start, u32 count );
+void mesh_draw( glmesh *mesh );
+void mesh_free( glmesh *mesh );
 
-/* upload a mesh based on file submesh */
-VG_STATIC void mdl_unpack_submesh( mdl_context *mdl, glmesh *mesh, 
-                                mdl_submesh *sm )
-{
-   mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count,
-                      mdl_submesh_indices( mdl, sm ), sm->indice_count );
-}
+/* file context management */
+void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc );
+void mdl_close( mdl_context *mdl );
 
-/* upload entire mesh from model */
-VG_STATIC void mdl_unpack_glmesh( mdl_context *mdl, glmesh *mesh )
-{
-   u32 offset = mdl->submesh_buffer[0].vertex_count;
+/* array loading */
+int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
+                     const char *name, void *lin_alloc, u32 stride );
+#define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
+   _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
 
-   for( int i=1; i< mdl->info.submesh_count; i++ )
-   {
-      mdl_submesh *sm = &mdl->submesh_buffer[i];
-      u32 *indices    =  mdl_submesh_indices( mdl, sm );
+/* array access */
+void *mdl_arritm( mdl_array_ptr *arr, u32 index );
+u32 mdl_arrcount( mdl_array_ptr *arr );
 
-      for( u32 j=0; j<sm->indice_count; j++ )
-         indices[j] += offset;
+/* pack access */
+void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst );
 
-      offset += sm->vertex_count;
-   }
+/* standard array groups */
+int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc );
+int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc );
+int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc );
 
-   mesh_upload( mesh, mdl->vertex_buffer, mdl->info.vertex_count,
-                      mdl->index_buffer,  mdl->info.indice_count ); 
-}
+/* load mesh */
+void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table );
 
-VG_STATIC void mdl_draw_submesh( mdl_submesh *sm )
-{
-   mesh_drawn( sm->indice_start, sm->indice_count );
-}
+/* load textures and mesh */
+void mdl_async_full_load_std( mdl_context *mdl );
 
-VG_STATIC void *mdl_get_entdata( mdl_context *mdl, mdl_node *pnode )
-{
-   return mdl->entdata_buffer + pnode->offset;
-}
+/* rendering */
+void mdl_draw_submesh( mdl_submesh *sm );
+mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name );
 
-VG_STATIC mdl_keyframe *mdl_get_animdata( mdl_context *mdl, mdl_animation *anim )
-{
-   return mdl->keyframe_buffer + anim->offset;
-}
+/* pstrs */
+const char *mdl_pstr( mdl_context *mdl, u32 pstr );
+int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 );
+#define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
+   mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
 
-VG_STATIC void mdl_link_materials( mdl_context *root, mdl_context *child )
-{
-   u32 lookup[MDL_MATERIAL_MAX];
-   
-   for( int i=0; i<child->info.material_count; i++ )
-   {
-      mdl_material *mi = &child->material_buffer[i];
-      const char *si = mdl_pstr( child, mi->pstr_name );
-
-      lookup[i] = 0;
-
-      for( int j=0; j<root->info.material_count; j++ )
-      {
-         mdl_material *mj = &root->material_buffer[j];
-         const char *sj = mdl_pstr( root, mj->pstr_name );
-         
-         if( !strcmp( si, sj ) )
-         {
-            lookup[i] = j;
-            break;
-         }
-      }
-
-      if( lookup[i] == 0 && i != 0 )
-      {
-         vg_warn( "Could not link material '%s' (not present in root model)\n", 
-                   si );
-      }
-   }
-
-   for( int i=0; i<child->info.submesh_count; i++ )
-   {
-      mdl_submesh *sm = &child->submesh_buffer[i];
-      sm->material_id =  lookup[sm->material_id];
-   }
-}
-
-VG_STATIC void mdl_invert_uv_coordinates( mdl_context *mdl )
-{
-   for( int i=0; i<mdl->info.vertex_count; i++ )
-   {
-      mdl_vert *vert = &mdl->vertex_buffer[i];
-      vert->uv[1] = 1.0f-vert->uv[1];
-   }
-}
+void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx );
 
-#endif