X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=a182e51293d9b1d18901e1671d2d39d92a251f1f;hb=e311bbe2fa903a7e2a922f202f389b799193195d;hp=e37e3239bd8ab173f94870dda2a8bdf8e23911ac;hpb=9d0ff08fef8507613586856de7ce25d43704db92;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index e37e323..a182e51 100644 --- a/model.h +++ b/model.h @@ -1,27 +1,31 @@ /* - * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved + * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved */ #ifndef MODEL_H #define MODEL_H -#include "common.h" +#include "skaterift.h" -#define MDL_VERSION_NR 101 +#define MDL_VERSION_MIN 101 +#define MDL_VERSION_NR 104 -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, k_shader_invisible = 5, - k_shader_boundary = 6 + 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 -{ +enum mdl_surface_prop{ k_surface_prop_concrete = 0, k_surface_prop_wood = 1, k_surface_prop_grass = 2, @@ -29,14 +33,20 @@ enum mdl_surface_prop k_surface_prop_metal = 4 }; -enum material_flag -{ - k_material_flag_skate_target = 0x00000001, - k_material_flag_collision = 0x00000002, - k_material_flag_grow_grass = 0x00000004, - k_material_flag_grindable = 0x00000008, - k_material_flag_invisible = 0x00000010, - k_material_flag_boundary = 0x00000020 +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) @@ -55,6 +65,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; @@ -71,6 +83,14 @@ 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; +}; + struct mdl_transform { v3f co, s; @@ -221,13 +241,11 @@ struct mdl_header mdl_array index; }; -struct mdl_context -{ +struct mdl_context{ FILE *file; mdl_header info; - struct mdl_array_ptr - { + struct mdl_array_ptr{ void *data; u32 count, stride; } @@ -249,15 +267,14 @@ struct mdl_context /* mesh buffers */ verts, indices; - u32 pack_base_offset; - - /* pack data */ - //pack; + + /* runtime */ + glmesh mesh; }; -VG_STATIC void mdl_load_fatal_corrupt( mdl_context *mdl ) +static void mdl_load_fatal_corrupt( mdl_context *mdl ) { fclose( mdl->file ); vg_file_print_invalid( mdl->file ); @@ -268,17 +285,8 @@ VG_STATIC void mdl_load_fatal_corrupt( mdl_context *mdl ) * Model implementation */ -VG_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; -} - -VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr ); -VG_STATIC +static const char *mdl_pstr( mdl_context *mdl, u32 pstr ); +static void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst ) { if( !info->pack_size ){ @@ -293,43 +301,62 @@ void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst ) } /* TODO: Rename these */ -VG_STATIC void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr, - void *buffer ) +static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr, + 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 ); + } + } } } -VG_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; } -VG_STATIC void *mdl_arritm( mdl_array_ptr *arr, u32 index ) +static void *mdl_arritm( mdl_array_ptr *arr, u32 index ) { return ((u8 *)arr->data) + index*arr->stride; } -VG_STATIC u32 mdl_arrcount( mdl_array_ptr *arr ) +static u32 mdl_arrcount( mdl_array_ptr *arr ) { return arr->count; } -VG_STATIC mdl_array *mdl_find_array( mdl_context *mdl, const char *name ) +static mdl_array *mdl_find_array( mdl_context *mdl, const char *name ) { for( u32 i=0; iindex); i++ ){ mdl_array *arr = mdl_arritm( &mdl->index, i ); @@ -342,13 +369,13 @@ VG_STATIC mdl_array *mdl_find_array( mdl_context *mdl, const char *name ) return NULL; } -VG_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{ @@ -359,41 +386,41 @@ VG_STATIC int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr, } } -VG_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; } -VG_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; } -VG_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 ); } /* * 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, void *lin_alloc ) +static void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc ) { memset( mdl, 0, sizeof( mdl_context ) ); mdl->file = fopen( path, "rb" ); @@ -407,15 +434,16 @@ VG_STATIC void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc ) if( l != 1 ) mdl_load_fatal_corrupt( mdl ); - if( mdl->info.version < MDL_VERSION_NR ){ + if( mdl->info.version < MDL_VERSION_MIN ){ vg_warn( "For model: %s\n", path ); - vg_warn( " version: %u (current: %u)\n", mdl->info.version, - MDL_VERSION_NR ); + vg_warn( " version: %u (min: %u, current: %u)\n", + mdl->info.version, MDL_VERSION_MIN, MDL_VERSION_NR ); 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; @@ -425,7 +453,7 @@ VG_STATIC void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc ) /* * close file handle */ -VG_STATIC void mdl_close( mdl_context *mdl ) +static void mdl_close( mdl_context *mdl ) { fclose( mdl->file ); mdl->file = NULL; @@ -433,7 +461,7 @@ VG_STATIC void mdl_close( mdl_context *mdl ) /* useful things you can do with the model */ -VG_STATIC void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx ) +static void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx ) { q_m3x3( transform->q, mtx ); v3_muls( mtx[0], transform->s[0], mtx[0] ); @@ -442,12 +470,13 @@ VG_STATIC void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx ) v3_copy( transform->co, mtx[3] ); } -VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr ) +static const char *mdl_pstr( mdl_context *mdl, u32 pstr ) { return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4; } -VG_STATIC int + +static int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 ) { u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr )); @@ -458,20 +487,15 @@ mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 ) else return 0; } +#define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\ + mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) ) + /* * Simple mesh interface for OpenGL * ---------------------------------------------------------------------------- */ -typedef struct glmesh glmesh; -struct glmesh -{ - GLuint vao, vbo, ebo; - u32 indice_count; - u32 loaded; -}; - -VG_STATIC void mesh_upload( glmesh *mesh, +static void mesh_upload( glmesh *mesh, mdl_vert *verts, u32 vert_count, u32 *indices, u32 indice_count ) { @@ -527,23 +551,23 @@ VG_STATIC void mesh_upload( glmesh *mesh, mesh->loaded = 1; } -VG_STATIC void mesh_bind( glmesh *mesh ) +static void mesh_bind( glmesh *mesh ) { glBindVertexArray( mesh->vao ); } -VG_STATIC void mesh_drawn( u32 start, u32 count ) +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 ) +static void mesh_draw( glmesh *mesh ) { mesh_drawn( 0, mesh->indice_count ); } -VG_STATIC void mesh_free( glmesh *mesh ) +static void mesh_free( glmesh *mesh ) { if( mesh->loaded ){ glDeleteVertexArrays( 1, &mesh->vao ); @@ -553,12 +577,12 @@ VG_STATIC void mesh_free( glmesh *mesh ) } } -VG_STATIC void mdl_draw_submesh( mdl_submesh *sm ) +static void mdl_draw_submesh( mdl_submesh *sm ) { mesh_drawn( sm->indice_start, sm->indice_count ); } -VG_STATIC mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name ) +static mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name ) { for( u32 i=0; imeshs ); i++ ){ mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i ); @@ -579,22 +603,21 @@ struct payload_glmesh_load{ glmesh *mesh; }; -VG_STATIC void async_mdl_load_glmesh( void *payload, u32 size ) +static void async_mdl_load_glmesh( void *payload, u32 size ) { struct payload_glmesh_load *job = payload; - mesh_upload( job->mesh, job->verts, job->vertex_count, job->indices, job->indice_count ); } -VG_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; @@ -609,8 +632,20 @@ VG_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) @@ -643,5 +678,21 @@ VG_STATIC void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh ) vg_fatal_error( "no vertex/indice data\n" ); } } + +/* 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, NULL ); + + for( u32 i=0; itextures ); i ++ ){ + vg_linear_clear( vg_mem.scratch ); + mdl_texture *tex = mdl_arritm( &mdl->textures, i ); + + void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size ); + mdl_fread_pack_file( mdl, &tex->file, data ); + + vg_tex2d_load_qoi_async( data, tex->file.pack_size, + VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, &tex->glname ); + } +} #endif