X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=ba1c3f228c8cc827176bc9a8b4a92126c82cba8a;hb=888e62fcd8f9777cee774fbb8fab2e52660303a7;hp=da0122397601ecfa84e6b63858858acb13819a09;hpb=63fe317d7db724162561da52aa382c82ac3c29be;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index da01223..ba1c3f2 100644 --- a/model.h +++ b/model.h @@ -8,7 +8,7 @@ #include "skaterift.h" #define MDL_VERSION_MIN 101 -#define MDL_VERSION_NR 104 +#define MDL_VERSION_NR 105 enum mdl_shader{ k_shader_standard = 0, @@ -20,15 +20,19 @@ enum mdl_shader{ 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 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_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 material_flag{ @@ -63,6 +67,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 +287,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 +304,48 @@ 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 { + vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n", + buffer, arr->item_size, stride, arr->item_count ); + if( stride < arr->item_size ) + vg_fatal_error( "not safe\n" ); + + 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 ) { + if( stride < arr->item_size ){ + vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size ); + vg_fatal_error( "not safe\n" ); + } + 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 +371,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 +388,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 +444,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 +612,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 ) -{ +static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, + u32 *fixup_table ){ 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 +634,20 @@ 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) ); + + if( fixup_table ){ + for( u32 i=0; ivertex_count; i ++ ){ + mdl_vert *vert = &job->verts[i]; + + for( u32 j=0; j<4; j++ ){ + vert->groups[j] = fixup_table[vert->groups[j]]; + } + } + } /* * Unpack the indices (if there are meshes) @@ -655,7 +683,7 @@ static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh ) /* uploads the glmesh, and textures. everything is saved into the mdl_context */ static void mdl_async_full_load_std( mdl_context *mdl ){ - mdl_async_load_glmesh( mdl, &mdl->mesh ); + mdl_async_load_glmesh( mdl, &mdl->mesh, NULL ); for( u32 i=0; itextures ); i ++ ){ vg_linear_clear( vg_mem.scratch );