X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;fp=model.h;h=5608becf70fb90c4621c8d0556cd23a477bffbf0;hb=0ff2713a286a77fa3e3538f5a76b0bf60525eb5b;hp=da0122397601ecfa84e6b63858858acb13819a09;hpb=f7c3600e850cc57df80d70c1862cf554c0cf4502;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index da01223..5608bec 100644 --- a/model.h +++ b/model.h @@ -63,6 +63,8 @@ struct mdl_vert #pragma pack(pop) +typedef u32 mdl_indice; + typedef struct mdl_context mdl_context; typedef struct mdl_array_ptr mdl_array_ptr; typedef struct mdl_vert mdl_vert; @@ -281,15 +283,6 @@ static void mdl_load_fatal_corrupt( mdl_context *mdl ) * Model implementation */ -static u32 mdl_query_array_size( mdl_array *arr ) -{ - if( arr->item_count ){ - u32 size = arr->item_size*arr->item_count; - return vg_align8(size); - } - else return 0; -} - static const char *mdl_pstr( mdl_context *mdl, u32 pstr ); static void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst ) @@ -307,29 +300,42 @@ void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst ) /* TODO: Rename these */ static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr, - void *buffer ) + void *buffer, u32 stride ) { if( arr->item_count ){ fseek( mdl->file, arr->file_offset, SEEK_SET ); - u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file ); - if( l != 1 ) mdl_load_fatal_corrupt( mdl ); + if( stride == arr->item_size ){ + u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file ); + if( l != 1 ) mdl_load_fatal_corrupt( mdl ); + } + else { + assert( stride >= arr->item_size ); + + for( u32 i=0; iitem_count; i++ ){ + u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file ); + if( l != 1 ) mdl_load_fatal_corrupt( mdl ); + } + } } } -static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr, - mdl_array *arr, void *lin_alloc ) +static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr, + mdl_array *arr, void *lin_alloc, u32 stride ) { + assert( stride >= arr->item_size ); + if( arr->item_count ){ - u32 size = arr->item_size*arr->item_count; + u32 size = stride*arr->item_count; ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) ); - mdl_load_array_file_buffer( mdl, arr, ptr->data ); + mdl_load_array_file_buffer( mdl, arr, ptr->data, stride ); } - else + else{ ptr->data = NULL; - + } + + ptr->stride = stride; ptr->count = arr->item_count; - ptr->stride = arr->item_size; } static void *mdl_arritm( mdl_array_ptr *arr, u32 index ) @@ -355,13 +361,13 @@ static mdl_array *mdl_find_array( mdl_context *mdl, const char *name ) return NULL; } -static int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr, - const char *name, void *lin_alloc ) +static int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr, + const char *name, void *lin_alloc, u32 stride ) { mdl_array *arr = mdl_find_array( mdl, name ); if( arr ){ - mdl_load_array_file( mdl, ptr, arr, lin_alloc ); + mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride ); return 1; } else{ @@ -372,35 +378,35 @@ static int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr, } } -static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc ) -{ +#define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \ + _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) ) + +static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc ){ int success = 1; - success &= mdl_load_array( mdl, &mdl->verts, "mdl_vert", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->indices, "mdl_indice", lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->verts, mdl_vert, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->indices, mdl_indice, lin_alloc ); return success; } -static int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc ) -{ +static int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc ){ int success = 1; - success &= mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->meshs, "mdl_mesh", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->submeshs, "mdl_submesh", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->materials, "mdl_material", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->textures, "mdl_texture", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->armatures, "mdl_armature", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->bones, "mdl_bone", lin_alloc ); - success &= mdl_load_array( mdl, &mdl->animations,"mdl_animation",lin_alloc ); + success &= _mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc, 1 ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->meshs, mdl_mesh, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->submeshs, mdl_submesh, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->materials, mdl_material, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->textures, mdl_texture, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->armatures, mdl_armature, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->bones, mdl_bone, lin_alloc ); + success &= MDL_LOAD_ARRAY( mdl, &mdl->animations,mdl_animation,lin_alloc ); return success; } -static int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc ) -{ - return mdl_load_array( mdl, &mdl->keyframes, "mdl_keyframe", lin_alloc ); +static int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc ){ + return MDL_LOAD_ARRAY( mdl, &mdl->keyframes, mdl_keyframe, lin_alloc ); } /* @@ -428,7 +434,8 @@ static void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc ) vg_fatal_error( "Legacy model version incompatable" ); } - mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc ); + mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc, + sizeof(mdl_array) ); mdl_array *pack = mdl_find_array( mdl, "pack" ); if( pack ) mdl->pack_base_offset = pack->file_offset; @@ -595,15 +602,14 @@ static void async_mdl_load_glmesh( void *payload, u32 size ) job->indices, job->indice_count ); } -/* TODO: Find out if this needs deprecating in favour of the new full loader */ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh ) { mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" ); mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" ); if( arr_vertices && arr_indices ){ - u32 size_verts = vg_align8(mdl_query_array_size( arr_vertices )), - size_indices = vg_align8(mdl_query_array_size( arr_indices )), + u32 size_verts = vg_align8(sizeof(mdl_vert)*arr_vertices->item_count), + size_indices = vg_align8(sizeof(mdl_indice)*arr_indices->item_count), size_hdr = vg_align8(sizeof(struct payload_glmesh_load)), total = size_hdr + size_verts + size_indices; @@ -618,8 +624,10 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh ) job->vertex_count = arr_vertices->item_count; job->indice_count = arr_indices->item_count; - mdl_load_array_file_buffer( mdl, arr_vertices, job->verts ); - mdl_load_array_file_buffer( mdl, arr_indices, job->indices ); + mdl_load_array_file_buffer( mdl, arr_vertices, + job->verts, sizeof(mdl_vert) ); + mdl_load_array_file_buffer( mdl, arr_indices, job->indices, + sizeof(mdl_indice) ); /* * Unpack the indices (if there are meshes)