X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=d72e93cc29a4ff90e3611c308394d4921d154925;hb=d13f2700b1773551307685cc7c34c804ccd6d664;hp=ad9dfeb53ce6e782125243df58b27a6f7ba49619;hpb=1030b1e134d422a3cbc1e06102053447da59ceba;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index ad9dfeb..d72e93c 100644 --- a/model.h +++ b/model.h @@ -10,6 +10,8 @@ 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_animation mdl_animation; +typedef struct mdl_keyframe mdl_keyframe; #define MDL_SIZE_MAX 0x1000000 #define MDL_VERT_MAX 1000000 @@ -25,8 +27,10 @@ struct mdl_vert { v3f co, norm; - v4f colour; v2f uv; + u8 colour[4]; + u16 weights[4]; + u8 groups[4]; }; struct mdl_submesh @@ -52,13 +56,32 @@ struct mdl_node v3f s; union{ u32 submesh_start, sub_uid; }; + u32 submesh_count, classtype, offset, + parent, pstr_name; }; +struct mdl_keyframe +{ + v3f co; + v4f q; + v3f s; +}; + +struct mdl_animation +{ + u32 pstr_name, + length; + + float rate; + + u32 offset; +}; + struct mdl_header { u32 identifier, version, file_length; @@ -68,7 +91,8 @@ struct mdl_header submesh_count, submesh_offset, material_count, material_offset, node_count, node_offset, - strings_offset, entdata_offset; + anim_count, anim_offset, + strings_offset, entdata_offset, animdata_offset; }; /* @@ -118,11 +142,36 @@ struct classtype_route_node struct classtype_route { - u32 pstr_name; u32 id_start; v3f colour; }; +struct classtype_bone +{ + u32 deform, + ik_target, + ik_pole, + collider, + use_limits; + + v3f angle_limits[2]; + boxf hitbox; +}; + +struct classtype_skeleton +{ + u32 channels, + ik_count, + collider_count, + anim_start, + anim_count; +}; + +struct classtype_skin +{ + u32 skeleton; +}; + #pragma pack(pop) /* @@ -133,12 +182,15 @@ 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 ) + 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 ); @@ -154,23 +206,39 @@ static void mesh_upload( glmesh *mesh, glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32), indices, GL_STATIC_DRAW ); + /* 0: coordinates */ glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 ); glEnableVertexAttribArray( 0 ); + /* 1: normal */ 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) ); + /* 2: uv */ + glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, + stride, (void *)offsetof(mdl_vert, uv) ); glEnableVertexAttribArray( 2 ); - glVertexAttribPointer( 3, 2, GL_FLOAT, GL_FALSE, - stride, (void *)offsetof(mdl_vert, uv) ); + /* 3: colour */ + glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE, + stride, (void *)offsetof(mdl_vert, colour) ); glEnableVertexAttribArray( 3 ); - - VG_CHECK_GL(); + + /* 4: weights */ + glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE, + stride, (void *)offsetof(mdl_vert, weights) ); + glEnableVertexAttribArray( 4 ); + + /* 5: groups */ + glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE, + stride, (void *)offsetof(mdl_vert, groups) ); + glEnableVertexAttribArray( 5 ); + + VG_CHECK_GL_ERR(); + mesh->indice_count = indice_count; + mesh->loaded = 1; } static void mesh_bind( glmesh *mesh ) @@ -191,9 +259,12 @@ static void mesh_draw( glmesh *mesh ) static void mesh_free( glmesh *mesh ) { - glDeleteVertexArrays( 1, &mesh->vao ); - glDeleteBuffers( 1, &mesh->ebo ); - glDeleteBuffers( 1, &mesh->vbo ); + if( mesh->loaded ) + { + glDeleteVertexArrays( 1, &mesh->vao ); + glDeleteBuffers( 1, &mesh->ebo ); + glDeleteBuffers( 1, &mesh->vbo ); + } } @@ -217,7 +288,7 @@ static mdl_header *mdl_load( const char *path ) if( size < sizeof(mdl_header) ) { - free( header ); + vg_free( header ); vg_error( "Invalid file '%s' (too small for header)\n", path ); return NULL; } @@ -227,7 +298,7 @@ static mdl_header *mdl_load( const char *path ) vg_error( "Invalid file '%s'" "(wrong .file_length, %ub != real file size %ub)\n", path, header->file_length, size ); - free( header ); + vg_free( header ); return NULL; } @@ -278,7 +349,7 @@ static mdl_header *mdl_load( const char *path ) if( ri->count > ri->max_count ) { - free( header ); + vg_free( header ); vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n", path, ri->desc, ri->count, ri->max_count ); return NULL; @@ -286,7 +357,7 @@ static mdl_header *mdl_load( const char *path ) if( ri->offset >= header->file_length ) { - free( header ); + vg_free( header ); vg_error( "'%s': '%s' buffer offset is out of range\n", path, ri->desc ); return NULL; @@ -294,7 +365,7 @@ static mdl_header *mdl_load( const char *path ) if( ri->offset + ri->size*ri->count > header->file_length ) { - free( header ); + vg_free( header ); vg_error( "'%s': '%s' buffer size is out of range\n", path, ri->desc ); return NULL; @@ -309,7 +380,7 @@ static mdl_header *mdl_load( const char *path ) if( ri->offset >= rj->offset && (ri->offset+ri->size*ri->count < rj->offset+rj->size*rj->count)) { - free( header ); + vg_free( header ); vg_error( "'%s': '%s' buffer overlaps '%s'\n", path, ri->desc, rj->desc ); return NULL; @@ -387,6 +458,11 @@ static mdl_material *mdl_material_from_id( mdl_header *mdl, u32 id ) return ((mdl_material *)mdl_baseptr(mdl,mdl->material_offset)) + id; } +static mdl_animation *mdl_animation_from_id( mdl_header *mdl, u32 id ) +{ + return ((mdl_animation *)mdl_baseptr(mdl,mdl->anim_offset)) + id; +} + static void mdl_node_transform( mdl_node *pnode, m4x3f transform ) { q_m3x3( pnode->q, transform ); @@ -434,6 +510,11 @@ 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];