X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=model.h;h=59b75beeb797e89aab8356eeedc5002cc77fa079;hb=c2d67378dd5c82de50b8fbbbe222ec6be2da4eee;hp=4ed39d4780500a4d876c19955ed4aea24dbb8f46;hpb=9c85e110fa8b965195438d96625ff9753af362a6;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/model.h b/model.h index 4ed39d4..59b75be 100644 --- a/model.h +++ b/model.h @@ -10,11 +10,13 @@ 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 #define MDL_INDICE_MAX 1000000 -#define MDL_MATERIAL_MAX 500 +#define MDL_MATERIAL_MAX 32 #define MDL_NODE_MAX 4000 #define MDL_SUBMESH_MAX 8000 #define MDL_STRING_LENGTH_MAX 64 @@ -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 @@ -50,14 +54,34 @@ struct mdl_node v3f co; v4f q; v3f s; + + union{ u32 submesh_start, sub_uid; }; - u32 submesh_start, + 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; @@ -67,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; }; /* @@ -82,6 +107,7 @@ struct classtype_block struct classtype_gate { u32 target; + v3f dims; }; struct classtype_spawn @@ -94,6 +120,58 @@ struct classtype_water u32 temp; }; +struct classtype_car_path +{ + u32 target, target1; +}; + +struct classtype_instance +{ + u32 pstr_file; +}; + +struct classtype_capsule +{ + float height, radius; +}; + +struct classtype_route_node +{ + u32 target, target1; +}; + +struct classtype_route +{ + 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) /* @@ -106,7 +184,8 @@ struct glmesh u32 indice_count; }; -static void mesh_upload( glmesh *mesh, +__attribute__((warn_unused_result)) +static int mesh_upload( glmesh *mesh, mdl_vert *verts, u32 vert_count, u32 *indices, u32 indice_count ) { @@ -125,23 +204,42 @@ 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 ); + + if( VG_CHECK_GL_ERR() ) + { + return 0; + } + mesh->indice_count = indice_count; + return 1; } static void mesh_bind( glmesh *mesh ) @@ -358,22 +456,29 @@ 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 ); - transform[0][0] *= pnode->s[0]; - transform[1][1] *= pnode->s[1]; - transform[2][2] *= pnode->s[2]; + 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] ); } -static void mdl_unpack_submesh( mdl_header *mdl, glmesh *mesh, mdl_submesh *sm ) +__attribute__((warn_unused_result)) +static int mdl_unpack_submesh( mdl_header *mdl, glmesh *mesh, mdl_submesh *sm ) { - mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count, - mdl_submesh_indices( mdl, sm ), sm->indice_count ); + return mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count, + mdl_submesh_indices( mdl, sm ), sm->indice_count ); } -static void mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh ) +__attribute__((warn_unused_result)) +static int mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh ) { u32 offset = mdl_submesh_from_id( mdl, 0 )->vertex_count; @@ -391,8 +496,8 @@ static void mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh ) mdl_vert *vertex_base = mdl_baseptr( mdl, mdl->vertex_offset ); u32 *indice_base = mdl_baseptr( mdl, mdl->indice_offset ); - mesh_upload( mesh, vertex_base, mdl->vertex_count, - indice_base, mdl->indice_count ); + return mesh_upload( mesh, vertex_base, mdl->vertex_count, + indice_base, mdl->indice_count ); } static void mdl_draw_submesh( mdl_submesh *sm ) @@ -405,5 +510,47 @@ 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]; + + 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 ); + + lookup[i] = 0; + + 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]; + } +} + #endif