-static void mesh_drawn( u32 start, u32 count )
-{
- glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
- (void *)(start*sizeof(u32)) );
-}
-
-static void mesh_draw( glmesh *mesh )
-{
- mesh_drawn( 0, mesh->indice_count );
-}
-
-static void mesh_free( glmesh *mesh )
-{
- if( mesh->loaded ){
- glDeleteVertexArrays( 1, &mesh->vao );
- glDeleteBuffers( 1, &mesh->ebo );
- glDeleteBuffers( 1, &mesh->vbo );
- mesh->loaded = 0;
- }
-}
-
-static void mdl_draw_submesh( mdl_submesh *sm )
-{
- mesh_drawn( sm->indice_start, sm->indice_count );
-}
-
-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;
-};
-
-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 );
-}
-
-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; i<job->vertex_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)
- * ---------------------------------------------------------
- */
-
- if( mdl_arrcount( &mdl->submeshs ) ){
- mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 );
- u32 offset = sm->vertex_count;
-
- for( u32 i=1; i<mdl_arrcount( &mdl->submeshs ); i++ ){
- mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i );
- u32 *indices = job->indices + sm->indice_start;
-
- for( u32 j=0; j<sm->indice_count; j++ )
- indices[j] += offset;
-
- offset += sm->vertex_count;
- }
- }
-
- /*
- * Dispatch
- * -------------------------
- */
-
- vg_async_dispatch( call, async_mdl_load_glmesh );
- }
- else{
- 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; i<mdl_arrcount( &mdl->textures ); 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