X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=a182e51293d9b1d18901e1671d2d39d92a251f1f;hb=e311bbe2fa903a7e2a922f202f389b799193195d;hp=59b75beeb797e89aab8356eeedc5002cc77fa079;hpb=c2d67378dd5c82de50b8fbbbe222ec6be2da4eee;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index 59b75be..a182e51 100644 --- a/model.h +++ b/model.h @@ -1,194 +1,506 @@ +/* + * 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_MIN 101 +#define MDL_VERSION_NR 104 + +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 +}; -typedef struct glmesh glmesh; +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 +}; + +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) + +/* 48 byte */ +struct mdl_vert +{ + 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) + +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_node mdl_node; -typedef struct mdl_header mdl_header; +typedef struct mdl_bone mdl_bone; +typedef struct mdl_armature mdl_armature; typedef struct mdl_animation mdl_animation; -typedef struct mdl_keyframe mdl_keyframe; - -#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 - -#pragma pack(push,1) +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_vert +typedef struct glmesh glmesh; +struct glmesh { - v3f co, - norm; - v2f uv; - u8 colour[4]; - u16 weights[4]; - u8 groups[4]; + GLuint vao, vbo, ebo; + u32 indice_count; + u32 loaded; }; -struct mdl_submesh +struct mdl_transform { - u32 indice_start, - indice_count, - vertex_start, - vertex_count; - - boxf bbx; - u32 material_id; + v3f co, s; + v4f q; }; +static void transform_identity( mdl_transform *transform ) +{ + v3_zero( transform->co ); + q_identity( transform->q ); + v3_fill( transform->s, 1.0f ); +} + +static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest ) +{ + v3_mul( transform->s, vec, dest ); + q_mulv( transform->q, dest, dest ); +} + +static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest ) +{ + mdl_transform_vector( transform, co, dest ); + v3_add( transform->co, dest, dest ); +} + +static void mdl_transform_mul( mdl_transform *a, mdl_transform *b, + mdl_transform *d ) +{ + 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 mdl_material { - u32 pstr_name; + u32 pstr_name, + shader, + flags, + surface_prop; + + v4f colour, + colour1; + + u32 tex_diffuse, + tex_none0, + tex_none1; }; -struct mdl_node +struct mdl_bone { - v3f co; - v4f q; - v3f s; - - union{ u32 submesh_start, sub_uid; }; - - u32 - submesh_count, - classtype, - offset, - parent, + v3f co, end; + u32 parent, + collider, + ik_target, + ik_pole, + flags, pstr_name; + + boxf hitbox; + v3f conevx, conevy, coneva; + float conet; }; -struct mdl_keyframe +enum bone_flag { - v3f co; - v4f q; - v3f s; + k_bone_flag_deform = 0x00000001, + k_bone_flag_ik = 0x00000002, + k_bone_flag_cone_constraint = 0x00000004 +}; + +enum bone_collider +{ + k_bone_collider_none = 0, + k_bone_collider_box = 1, + k_bone_collider_capsule = 2 +}; + +struct mdl_armature +{ + mdl_transform transform; + u32 bone_start, + bone_count, + anim_start, + anim_count; }; struct mdl_animation { u32 pstr_name, length; - float rate; - u32 offset; }; -struct mdl_header +struct mdl_submesh { - u32 identifier, version, file_length; + u32 indice_start, + indice_count, + vertex_start, + vertex_count; - u32 vertex_count, vertex_offset, - indice_count, indice_offset, - submesh_count, submesh_offset, - material_count, material_offset, - node_count, node_offset, - anim_count, anim_offset, - strings_offset, entdata_offset, animdata_offset; + boxf bbx; + u16 material_id, flags; }; -/* - * Entity data structures - */ - -struct classtype_block +enum esubmesh_flags { - boxf bbx; + k_submesh_flag_none = 0x0000, + k_submesh_flag_consumed = 0x0001 }; -struct classtype_gate +struct mdl_mesh { - u32 target; - v3f dims; + mdl_transform transform; + u32 submesh_start, + submesh_count, + pstr_name, + entity_id, /* upper 16 bits: type, lower 16 bits: index */ + armature_id; }; -struct classtype_spawn +struct mdl_file { - u32 target; + u32 pstr_path, + pack_offset, + pack_size; }; -struct classtype_water +struct mdl_texture { - u32 temp; + mdl_file file; + u32 glname; }; -struct classtype_car_path +struct mdl_array { - u32 target, target1; + u32 file_offset, + item_count, + item_size; + + char name[16]; }; -struct classtype_instance +struct mdl_header { - u32 pstr_file; + u32 version; + mdl_array index; }; -struct classtype_capsule -{ - float height, radius; +struct mdl_context{ + FILE *file; + mdl_header info; + + struct mdl_array_ptr{ + 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; }; -struct classtype_route_node + +static void mdl_load_fatal_corrupt( mdl_context *mdl ) { - u32 target, target1; -}; + fclose( mdl->file ); + vg_file_print_invalid( mdl->file ); + vg_fatal_error( "Corrupt model" ); +} -struct classtype_route +/* + * Model implementation + */ + +static const char *mdl_pstr( mdl_context *mdl, u32 pstr ); +static +void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst ) { - u32 id_start; - v3f colour; -}; + if( !info->pack_size ){ + vg_warn( "path: %s\n", mdl_pstr( mdl, info->pstr_path ) ); + vg_fatal_error( "Packed file is only a header; it is not packed" ); + } -struct classtype_bone + fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET ); + u64 l = fread( dst, info->pack_size, 1, mdl->file ); + + if( l != 1 ) mdl_load_fatal_corrupt( mdl ); +} + +/* TODO: Rename these */ +static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr, + void *buffer, u32 stride ) { - u32 deform, - ik_target, - ik_pole, - collider, - use_limits; + if( arr->item_count ){ + fseek( mdl->file, arr->file_offset, SEEK_SET ); - v3f angle_limits[2]; - boxf hitbox; -}; + 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 ); + } + } + } +} -struct classtype_skeleton +static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr, + mdl_array *arr, void *lin_alloc, u32 stride ) { - u32 channels, - ik_count, - collider_count, - anim_start, - anim_count; -}; + if( stride < arr->item_size ){ + vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size ); + vg_fatal_error( "not safe\n" ); + } -struct classtype_skin + if( 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, stride ); + } + else{ + ptr->data = NULL; + } + + ptr->stride = stride; + ptr->count = arr->item_count; +} + +static void *mdl_arritm( mdl_array_ptr *arr, u32 index ) { - u32 skeleton; -}; + return ((u8 *)arr->data) + index*arr->stride; +} -#pragma pack(pop) +static u32 mdl_arrcount( mdl_array_ptr *arr ) +{ + return arr->count; +} + +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 ); + + if( !strncmp(arr->name,name,16) ){ + return arr; + } + } + + return NULL; +} + +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, stride ); + return 1; + } + else{ + ptr->data = NULL; + ptr->count = 0; + ptr->stride = 0; + return 0; + } +} + +#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 ); + + return success; +} + +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, 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 ); +} /* - * Simple mesh interface for OpenGL + * if calling mdl_open, and the file does not exist, the game will fatal quit */ +static void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc ) +{ + memset( mdl, 0, sizeof( mdl_context ) ); + mdl->file = fopen( path, "rb" ); -struct glmesh + if( !mdl->file ){ + vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) ); + vg_fatal_error( "see above for details" ); + } + + u64 l = fread( &mdl->info, sizeof(mdl_header), 1, mdl->file ); + if( l != 1 ) + mdl_load_fatal_corrupt( mdl ); + + if( mdl->info.version < MDL_VERSION_MIN ){ + vg_warn( "For model: %s\n", path ); + 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, + sizeof(mdl_array) ); + + mdl_array *pack = mdl_find_array( mdl, "pack" ); + if( pack ) mdl->pack_base_offset = pack->file_offset; + else mdl->pack_base_offset = 0; +} + +/* + * close file handle + */ +static void mdl_close( mdl_context *mdl ) { - GLuint vao, vbo, ebo; - u32 indice_count; -}; + fclose( mdl->file ); + mdl->file = NULL; +} + +/* useful things you can do with the model */ + +static void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx ) +{ + q_m3x3( transform->q, mtx ); + v3_muls( mtx[0], transform->s[0], mtx[0] ); + v3_muls( mtx[1], transform->s[1], mtx[1] ); + v3_muls( mtx[2], transform->s[2], mtx[2] ); + v3_copy( transform->co, mtx[3] ); +} -__attribute__((warn_unused_result)) -static int mesh_upload( glmesh *mesh, - mdl_vert *verts, u32 vert_count, - u32 *indices, u32 indice_count ) +static const char *mdl_pstr( mdl_context *mdl, u32 pstr ) { + return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4; +} + + +static int +mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 ) +{ + u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr )); + if( hash == djb2 ){ + if( !strcmp( str, mdl_pstr( mdl, pstr ))) return 1; + else return 0; + } + else return 0; +} + +#define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\ + mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) ) + +/* + * Simple mesh interface for OpenGL + * ---------------------------------------------------------------------------- + */ + +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 ); @@ -233,13 +545,10 @@ static int mesh_upload( glmesh *mesh, stride, (void *)offsetof(mdl_vert, groups) ); glEnableVertexAttribArray( 5 ); - if( VG_CHECK_GL_ERR() ) - { - return 0; - } + VG_CHECK_GL_ERR(); mesh->indice_count = indice_count; - return 1; + mesh->loaded = 1; } static void mesh_bind( glmesh *mesh ) @@ -260,297 +569,130 @@ static void mesh_draw( glmesh *mesh ) static void mesh_free( glmesh *mesh ) { - glDeleteVertexArrays( 1, &mesh->vao ); - glDeleteBuffers( 1, &mesh->ebo ); - glDeleteBuffers( 1, &mesh->vbo ); -} - - -/* - * Model implementation - */ - -static mdl_header *mdl_load( const char *path ) -{ - 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; - } - - if( size < sizeof(mdl_header) ) - { - free( header ); - vg_error( "Invalid file '%s' (too small for header)\n", path ); - return NULL; - } - - 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; icount == 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; jcount == 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; - } - } + if( mesh->loaded ){ + glDeleteVertexArrays( 1, &mesh->vao ); + glDeleteBuffers( 1, &mesh->ebo ); + glDeleteBuffers( 1, &mesh->vbo ); + mesh->loaded = 0; } - - /* - * Pointer validation TODO(workshop) - */ - - /* - * strings TODO(workshop) - */ - - return header; -} - -static void *mdl_baseptr( mdl_header *mdl, u32 offset ) -{ - return (void *)mdl + offset; -} - -static const char *mdl_pstr( mdl_header *mdl, u32 pstr ) -{ - return (const char *)(mdl_baseptr( mdl, mdl->strings_offset )) + pstr; } -static mdl_node *mdl_node_from_id( mdl_header *mdl, u32 id ) +static void mdl_draw_submesh( mdl_submesh *sm ) { - return ((mdl_node *)mdl_baseptr( mdl, mdl->node_offset )) + id; + mesh_drawn( sm->indice_start, sm->indice_count ); } -static mdl_node *mdl_node_from_name( mdl_header *mdl, const char *name ) +static mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name ) { - for( int i=0; inode_count; i++ ) - { - mdl_node *pnode = mdl_node_from_id( mdl, i ); - - if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) ) - return pnode; + for( u32 i=0; imeshs ); i++ ){ + mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i ); + if( !strcmp( name, mdl_pstr( mdl, mesh->pstr_name ))){ + return mesh; + } } - return NULL; } -static mdl_submesh *mdl_submesh_from_id( mdl_header *mdl, u32 id ) -{ - if( id >= mdl->submesh_count ) - return NULL; - - return ((mdl_submesh *)mdl_baseptr( mdl, mdl->submesh_offset )) + id; -} +struct payload_glmesh_load{ + mdl_vert *verts; + u32 *indices; -static mdl_submesh *mdl_node_submesh( mdl_header *mdl, mdl_node *node, u32 i ) -{ - if( i >= node->submesh_count ) - return NULL; + u32 vertex_count, + indice_count; - return mdl_submesh_from_id( mdl, node->submesh_start+i ); -} - -static u32 *mdl_submesh_indices( mdl_header *mdl, mdl_submesh *sm ) -{ - return ((u32 *)mdl_baseptr( mdl, mdl->indice_offset )) + sm->indice_start; -} + glmesh *mesh; +}; -static mdl_vert *mdl_submesh_vertices( mdl_header *mdl, mdl_submesh *sm ) +static void async_mdl_load_glmesh( void *payload, u32 size ) { - return ((mdl_vert *)mdl_baseptr(mdl,mdl->vertex_offset)) + sm->vertex_start; + struct payload_glmesh_load *job = payload; + mesh_upload( job->mesh, job->verts, job->vertex_count, + job->indices, job->indice_count ); } -static mdl_material *mdl_material_from_id( mdl_header *mdl, u32 id ) -{ - return ((mdl_material *)mdl_baseptr(mdl,mdl->material_offset)) + id; -} +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(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; + + vg_async_item *call = vg_async_alloc( total ); + struct payload_glmesh_load *job = call->payload; + + u8 *payload = call->payload; + + job->mesh = mesh; + job->verts = (void*)(payload + size_hdr); + job->indices = (void*)(payload + size_hdr + size_verts); + job->vertex_count = arr_vertices->item_count; + job->indice_count = arr_indices->item_count; + + 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]]; + } + } + } -static mdl_animation *mdl_animation_from_id( mdl_header *mdl, u32 id ) -{ - return ((mdl_animation *)mdl_baseptr(mdl,mdl->anim_offset)) + id; -} + /* + * Unpack the indices (if there are meshes) + * --------------------------------------------------------- + */ -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] ); -} + if( mdl_arrcount( &mdl->submeshs ) ){ + mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 ); + u32 offset = sm->vertex_count; -__attribute__((warn_unused_result)) -static int mdl_unpack_submesh( mdl_header *mdl, glmesh *mesh, mdl_submesh *sm ) -{ - return mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count, - mdl_submesh_indices( mdl, sm ), sm->indice_count ); -} + for( u32 i=1; isubmeshs ); i++ ){ + mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i ); + u32 *indices = job->indices + sm->indice_start; -__attribute__((warn_unused_result)) -static int mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh ) -{ - u32 offset = mdl_submesh_from_id( mdl, 0 )->vertex_count; + for( u32 j=0; jindice_count; j++ ) + indices[j] += offset; - for( int i=1; i< mdl->submesh_count; i++ ) - { - mdl_submesh *sm = mdl_submesh_from_id( mdl, i ); - u32 *indices = mdl_submesh_indices( mdl, sm ); + offset += sm->vertex_count; + } + } - for( u32 j=0; jindice_count; j++ ) - indices[j] += offset; + /* + * Dispatch + * ------------------------- + */ - offset += sm->vertex_count; + vg_async_dispatch( call, async_mdl_load_glmesh ); + } + else{ + vg_fatal_error( "no vertex/indice data\n" ); } - - mdl_vert *vertex_base = mdl_baseptr( mdl, mdl->vertex_offset ); - u32 *indice_base = mdl_baseptr( mdl, mdl->indice_offset ); - - return mesh_upload( mesh, vertex_base, mdl->vertex_count, - indice_base, mdl->indice_count ); -} - -static void mdl_draw_submesh( mdl_submesh *sm ) -{ - mesh_drawn( sm->indice_start, sm->indice_count ); -} - -static void *mdl_get_entdata( mdl_header *mdl, mdl_node *pnode ) -{ - return mdl_baseptr( mdl, mdl->entdata_offset ) + pnode->offset; -} - -static mdl_keyframe *mdl_get_animdata( mdl_header *mdl, mdl_animation *anim ) -{ - return mdl_baseptr( mdl, mdl->animdata_offset ) + anim->offset; } -static void mdl_link_materials( mdl_header *root, mdl_header *child ) -{ - u32 lookup[MDL_MATERIAL_MAX]; +/* 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( int i=0; imaterial_count; i++ ) - { - mdl_material *mi = mdl_material_from_id( child, i ); - const char *si = mdl_pstr( child, mi->pstr_name ); + for( u32 i=0; itextures ); i ++ ){ + vg_linear_clear( vg_mem.scratch ); + mdl_texture *tex = mdl_arritm( &mdl->textures, i ); - lookup[i] = 0; + void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size ); + mdl_fread_pack_file( mdl, &tex->file, data ); - for( int j=0; jmaterial_count; j++ ) - { - mdl_material *mj = mdl_material_from_id( root, 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; isubmesh_count; i++ ) - { - mdl_submesh *sm = mdl_submesh_from_id( child, i ); - sm->material_id = lookup[sm->material_id]; + vg_tex2d_load_qoi_async( data, tex->file.pack_size, + VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, &tex->glname ); } } - - + #endif