-VG_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 );
-
- 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 );
-}
-
-/*
- * 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 )
-{
- memset( mdl, 0, sizeof( mdl_context ) );
- mdl->file = fopen( path, "rb" );
-
- 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_NR ){
- vg_warn( "For model: %s\n", path );
- vg_warn( " version: %u (current: %u)\n", mdl->info.version,
- MDL_VERSION_NR );
-
- vg_fatal_error( "Legacy model version incompatable" );
- }
-
- mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc );
-
- 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
- */
-VG_STATIC void mdl_close( mdl_context *mdl )
-{
- fclose( mdl->file );
- mdl->file = NULL;
-}
-
-/* useful things you can do with the model */
-
-VG_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] );
-}
-
-VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr )
-{
- return mdl_arritm( &mdl->strings, pstr );
-}
-
-/*
- * 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,
- 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 );
- glBindVertexArray( mesh->vao );
-
- size_t stride = sizeof(mdl_vert);
-
- glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
- glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
-
- glBindVertexArray( mesh->vao );
- glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
- 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 );
-
- /* 2: uv */
- glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
- stride, (void *)offsetof(mdl_vert, uv) );
- glEnableVertexAttribArray( 2 );
-
- /* 3: colour */
- glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
- stride, (void *)offsetof(mdl_vert, colour) );
- glEnableVertexAttribArray( 3 );
-
- /* 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;
-}
-
-VG_STATIC void mesh_bind( glmesh *mesh )
-{
- glBindVertexArray( mesh->vao );
-}
-
-VG_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 )
-{
- mesh_drawn( 0, mesh->indice_count );
-}
-
-VG_STATIC void mesh_free( glmesh *mesh )
-{
- if( mesh->loaded ){
- glDeleteVertexArrays( 1, &mesh->vao );
- glDeleteBuffers( 1, &mesh->ebo );
- glDeleteBuffers( 1, &mesh->vbo );
- mesh->loaded = 0;
- }
-}
-
-VG_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 )
-{
- for( u32 i=0; i<mdl_arrcount( &mdl->meshs ); i++ ){
- mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
- if( !strcmp( name, mdl_pstr( mdl, mesh->pstr_name ))){
- return mesh;
- }
- }
- return NULL;
-}
-
-struct payload_glmesh_load{
- mdl_vert *verts;
- u32 *indices;
-
- u32 vertex_count,
- indice_count;
-
- glmesh *mesh;