remove some todos
[carveJwlIkooP6JGAAIwe30JlM.git] / model.h
diff --git a/model.h b/model.h
index a65f11fd2ae20dcdde75ab1e72f6a50545af56f6..ae19f6f090d57848ff97fdb9d8f9df2533377a6e 100644 (file)
--- a/model.h
+++ b/model.h
-#ifndef MODEL_H
-#define MODEL_H
-
-#include "common.h"
-
-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_header mdl_header;
-
-#define MDL_SIZE_MAX          0x1000000
-#define MDL_VERT_MAX          1000000
-#define MDL_INDICE_MAX        1000000
-#define MDL_MATERIAL_MAX      500
-#define MDL_NODE_MAX          4000
-#define MDL_SUBMESH_MAX       8000
-#define MDL_STRING_LENGTH_MAX 64
-
-#pragma pack(push,1)
-
-struct mdl_vert
-{
-   v3f co,
-       norm;
-   v4f colour;
-   v2f uv;
-};
-
-struct mdl_submesh
-{
-   u32 indice_start,
-       indice_count,
-       vertex_start,
-       vertex_count;
-   
-   boxf bbx;
-   u32 material_id;
-};
-
-struct mdl_material
-{
-   u32 pstr_name;
-};
-
-struct mdl_node
-{
-   v3f co;
-   v4f q;
-   v3f s;
-   
-   union{ u32 submesh_start, sub_uid; };
-   u32 
-       submesh_count,
-       classtype,
-       offset,
-       pstr_name;
-};
-
-struct mdl_header
-{
-   u32 identifier, version, file_length;
-
-   u32 vertex_count, vertex_offset,
-       indice_count, indice_offset,
-       submesh_count, submesh_offset,
-       material_count, material_offset,
-       node_count, node_offset,
-       strings_offset, entdata_offset;
-};
-
-/* 
- * Entity data structures
+/*
+ * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
  */
 
-struct classtype_block
-{
-   boxf bbx;
+#pragma once
+
+#define MDL_VERSION_MIN 101
+#define MDL_VERSION_NR 105
+
+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,
+   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
 };
 
-struct classtype_gate
-{
-   u32 target;
+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
 };
 
-struct classtype_spawn
-{
-   u32 target;
+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
 };
 
-struct classtype_water
-{
-   u32 temp;
-};
+#pragma pack(push,1)
 
-struct classtype_car_path
+/* 48 byte */
+struct mdl_vert
 {
-   u32 target, target1;
+   v3f co,        /* 3*32 */
+       norm;      /* 3*32 */
+   v2f uv;        /* 2*32 */
+
+   u8  colour[4]; /* 4*8 */
+   u16 weights[4];/* 4*16 */
+   u8  groups[4]; /* 4*8 */
 };
 
 #pragma pack(pop)
 
-/*
- * Simple mesh interface for OpenGL
- */
+typedef u32 mdl_indice;
+
+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;
 
+typedef struct glmesh glmesh;
 struct glmesh
 {
    GLuint vao, vbo, ebo;
    u32 indice_count;
+   u32 loaded;
 };
 
-static void mesh_upload( glmesh *mesh,
-      mdl_vert *verts, u32 vert_count,
-      u32 *indices, u32 indice_count )
+struct mdl_transform
 {
-   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 );
-   
-   glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
-   glEnableVertexAttribArray( 0 );
-
-   glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 
-         stride, (void *)offsetof(mdl_vert, norm) );
-   glEnableVertexAttribArray( 1 );
-
-   glVertexAttribPointer( 2, 4, GL_FLOAT, GL_FALSE, 
-         stride, (void *)offsetof(mdl_vert, colour) );
-   glEnableVertexAttribArray( 2 );
-
-   glVertexAttribPointer( 3, 2, GL_FLOAT, GL_FALSE, 
-         stride, (void *)offsetof(mdl_vert, uv) );
-   glEnableVertexAttribArray( 3 );
-   
-   VG_CHECK_GL();
-   mesh->indice_count = indice_count;
-}
+   v3f co, s;
+   v4f q;
+};
 
-static void mesh_bind( glmesh *mesh )
+static void transform_identity( mdl_transform *transform )
 {
-   glBindVertexArray( mesh->vao );
+   v3_zero( transform->co );
+   q_identity( transform->q );
+   v3_fill( transform->s, 1.0f );
 }
 
-static void mesh_drawn( u32 start, u32 count )
+static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest )
 {
-   glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT, 
-         (void *)(start*sizeof(u32)) );
+   v3_mul( transform->s, vec, dest );
+   q_mulv( transform->q, dest, dest );
 }
 
-static void mesh_draw( glmesh *mesh )
+static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest )
 {
-   mesh_drawn( 0, mesh->indice_count );
+   mdl_transform_vector( transform, co, dest );
+   v3_add( transform->co, dest, dest );
 }
 
-static void mesh_free( glmesh *mesh )
+static void mdl_transform_mul( mdl_transform *a, mdl_transform *b, 
+                               mdl_transform *d )
 {
-   glDeleteVertexArrays( 1, &mesh->vao );
-   glDeleteBuffers( 1, &mesh->ebo );
-   glDeleteBuffers( 1, &mesh->vbo );
+   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 );
 }
 
-
-/*
- * Model implementation
- */
-
-static mdl_header *mdl_load( const char *path )
+struct mdl_material
 {
-   i64 size;
-   mdl_header *header = vg_asset_read_s( path, &size );
-
-   /* 
-    * Check file is valid
-    */
-   if( !header )
-   {
-      vg_error( "Could not open '%s'\n", path );
-      return NULL;
-   }
+   u32 pstr_name,
+       shader,
+       flags,
+       surface_prop;
 
-   if( size < sizeof(mdl_header) )
-   {
-      free( header );
-      vg_error( "Invalid file '%s' (too small for header)\n", path );
-      return NULL;
-   }
+   v4f colour,
+       colour1;
 
-   if( header->file_length != size )
-   {
-      vg_error( "Invalid file '%s'"
-                "(wrong .file_length, %ub != real file size %ub)\n", 
-                path, header->file_length, size );
-      free( header );
-      return NULL;
-   }
-
-   /*
-    * Validate offsets and memory sections, to ensure all arrays are in-bounds,
-    * and that they do not overlap.
-    */
-
-   struct memregion
-   {
-      const char *desc;
-      u32 count, max_count, size, offset;
-   }
-   regions[] = {
-      { 
-         "Vertices",
-         header->vertex_count, MDL_VERT_MAX, 
-         sizeof(mdl_vert), header->vertex_offset 
-      },
-      {
-         "Indices",
-         header->indice_count, MDL_INDICE_MAX,
-         sizeof(u32), header->indice_offset 
-      },
-      { 
-         "Submesh",
-         header->submesh_count, MDL_SUBMESH_MAX,
-         sizeof(mdl_submesh), header->submesh_offset 
-      },
-      { 
-         "Materials",
-         header->material_count, MDL_MATERIAL_MAX,
-         sizeof(mdl_material), header->material_offset
-      },
-      { 
-         "Nodes",
-         header->node_count, MDL_NODE_MAX,
-         sizeof(mdl_node), header->node_count
-      }
-   };
-
-   for( int i=0; i<vg_list_size(regions); i++ )
-   {
-      struct memregion *ri = &regions[i];
-
-      if( ri->count == 0 )
-         continue;
-
-      if( ri->count > ri->max_count )
-      {
-         free( header );
-         vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
-               path, ri->desc, ri->count, ri->max_count );
-         return NULL;
-      }
-
-      if( ri->offset >= header->file_length )
-      {
-         free( header );
-         vg_error( "'%s': '%s' buffer offset is out of range\n",
-               path, ri->desc );
-         return NULL;
-      }
-
-      if( ri->offset + ri->size*ri->count > header->file_length )
-      {
-         free( header );
-         vg_error( "'%s': '%s' buffer size is out of range\n",
-               path, ri->desc );
-         return NULL;
-      }
-
-      for( int j=0; j<vg_list_size(regions); j++ )
-      {
-         struct memregion *rj = &regions[j];
-         if( rj->count == 0 )
-            continue;
-
-         if( ri->offset >= rj->offset && 
-               (ri->offset+ri->size*ri->count < rj->offset+rj->size*rj->count))
-         {
-            free( header );
-            vg_error( "'%s': '%s' buffer overlaps '%s'\n",
-                  path, ri->desc, rj->desc );
-            return NULL;
-         }
-      }
-   }
-
-   /*
-    * Pointer validation TODO(workshop)
-    */
+   u32 tex_diffuse, /* Indexes start from 1. 0 if missing. */
+       tex_none0,
+       tex_none1;
+};
 
-   /*
-    * strings TODO(workshop)
-    */
+struct mdl_bone
+{
+   v3f co, end;
+   u32 parent,
+       collider,
+       ik_target,
+       ik_pole,
+       flags,
+       pstr_name;
 
-   return header;
-}
+   boxf hitbox;
+   v3f conevx, conevy, coneva;
+   float conet;
+};
 
-static void *mdl_baseptr( mdl_header *mdl, u32 offset )
+enum bone_flag
 {
-   return (void *)mdl + offset;
-}
+   k_bone_flag_deform               = 0x00000001,
+   k_bone_flag_ik                   = 0x00000002,
+   k_bone_flag_cone_constraint      = 0x00000004
+};
 
-static const char *mdl_pstr( mdl_header *mdl, u32 pstr )
+enum bone_collider
 {
-   return (const char *)(mdl_baseptr( mdl, mdl->strings_offset )) + pstr;
-}
-
-static mdl_node *mdl_node_from_id( mdl_header *mdl, u32 id )
+   k_bone_collider_none = 0,
+   k_bone_collider_box = 1,
+   k_bone_collider_capsule = 2
+};
+         
+struct mdl_armature
 {
-   return ((mdl_node *)mdl_baseptr( mdl, mdl->node_offset )) + id;
-}
+   mdl_transform transform;
+   u32 bone_start,
+       bone_count,
+       anim_start,
+       anim_count;
+};
 
-static mdl_node *mdl_node_from_name( mdl_header *mdl, const char *name )
+struct mdl_animation
 {
-   for( int i=0; i<mdl->node_count; i++ )
-   {
-      mdl_node *pnode = mdl_node_from_id( mdl, i );
-      
-      if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) )
-         return pnode;
-   }
-   
-   return NULL;
-}
+   u32 pstr_name,
+       length;
+   float rate;
+   u32 offset;
+};
 
-static mdl_submesh *mdl_submesh_from_id( mdl_header *mdl, u32 id )
+struct mdl_submesh
 {
-   if( id >= mdl->submesh_count )
-      return NULL;
+   u32 indice_start,
+       indice_count,
+       vertex_start,
+       vertex_count;
 
-   return ((mdl_submesh *)mdl_baseptr( mdl, mdl->submesh_offset )) + id;
-}
+   boxf bbx;
+   u16 material_id, flags;
+};
 
-static mdl_submesh *mdl_node_submesh( mdl_header *mdl, mdl_node *node, u32 i )
+enum esubmesh_flags
 {
-   if( i >= node->submesh_count )
-      return NULL;
-
-   return mdl_submesh_from_id( mdl, node->submesh_start+i );
-}
+   k_submesh_flag_none     = 0x0000,
+   k_submesh_flag_consumed = 0x0001
+};
 
-static u32 *mdl_submesh_indices( mdl_header *mdl, mdl_submesh *sm )
+struct mdl_mesh
 {
-   return ((u32 *)mdl_baseptr( mdl, mdl->indice_offset )) + sm->indice_start;
-}
+   mdl_transform transform;
+   u32 submesh_start,
+       submesh_count,
+       pstr_name,
+       entity_id,    /* upper 16 bits: type, lower 16 bits: index */
+       armature_id;
+};
 
-static mdl_vert *mdl_submesh_vertices( mdl_header *mdl, mdl_submesh *sm )
+struct mdl_file
 {
-   return ((mdl_vert *)mdl_baseptr(mdl,mdl->vertex_offset)) + sm->vertex_start;
-}
+   u32 pstr_path,
+       pack_offset,
+       pack_size;
+};
 
-static mdl_material *mdl_material_from_id( mdl_header *mdl, u32 id )
+struct mdl_texture
 {
-   return ((mdl_material *)mdl_baseptr(mdl,mdl->material_offset)) + id;
-}
+   mdl_file file;
+   u32 glname;
+};
 
-static void mdl_node_transform( mdl_node *pnode, m4x3f transform )
+struct mdl_array
 {
-   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] );
-}
+   u32 file_offset,
+       item_count,
+       item_size;
 
-static void mdl_unpack_submesh( mdl_header *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 );
-}
+   char name[16];
+};
 
-static void mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh )
+struct mdl_header
 {
-   u32 offset = mdl_submesh_from_id( mdl, 0 )->vertex_count;
+   u32 version;
+   mdl_array index;
+};
+
+struct mdl_context{
+   FILE *file;
+   mdl_header info;
 
-   for( int i=1; i< mdl->submesh_count; i++ )
+   struct mdl_array_ptr
    {
-      mdl_submesh *sm = mdl_submesh_from_id( mdl, i );
-      u32 *indices =    mdl_submesh_indices( mdl, sm );
+      void *data;
+      u32 count, stride;
+   }
+   index,
+
+   /* metadata */
+   strings,
+   meshs,
+   submeshs,
+   materials,
+   textures,
+   armatures,
+   bones,
+   animations,
+
+   /* animation buffers */
+   keyframes,
+
+   /* mesh buffers */
+   verts,
+   indices;
+   u32 pack_base_offset;
+
+   /* runtime */
+   glmesh mesh;
+};
 
-      for( u32 j=0; j<sm->indice_count; j++ )
-         indices[j] += offset;
+void mesh_bind( glmesh *mesh );
+void mesh_drawn( u32 start, u32 count );
+void mesh_draw( glmesh *mesh );
+void mesh_free( glmesh *mesh );
 
-      offset += sm->vertex_count;
-   }
+/* file context management */
+void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc );
+void mdl_close( mdl_context *mdl );
 
-   mdl_vert *vertex_base = mdl_baseptr( mdl, mdl->vertex_offset );
-   u32 *indice_base = mdl_baseptr( mdl, mdl->indice_offset );
+/* 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) )
 
-   mesh_upload( mesh, vertex_base, mdl->vertex_count,
-                      indice_base, mdl->indice_count ); 
-}
+/* array access */
+void *mdl_arritm( mdl_array_ptr *arr, u32 index );
+u32 mdl_arrcount( mdl_array_ptr *arr );
 
-static void mdl_draw_submesh( mdl_submesh *sm )
-{
-   mesh_drawn( sm->indice_start, sm->indice_count );
-}
+/* pack access */
+void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst );
 
-static void *mdl_get_entdata( mdl_header *mdl, mdl_node *pnode )
-{
-   return mdl_baseptr( mdl, mdl->entdata_offset ) + pnode->offset;
-}
+/* 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 );
+
+/* load mesh */
+void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table );
+
+/* load textures and mesh */
+void mdl_async_full_load_std( mdl_context *mdl );
+
+/* rendering */
+void mdl_draw_submesh( mdl_submesh *sm );
+mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name );
+
+/* 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 ) )
 
+void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx );
 
-#endif