- //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_load_fatal_corrupt( mdl_context *mdl )
-{
- fclose( mdl->file );
- vg_file_print_invalid( mdl->file );
- vg_fatal_exit_loop( "Corrupt model" );
-}
-
-/*
- * Model implementation
- *
- * TODO.
- *
- * you have two api options for loading a model, first, the easy way:
- * mdl_load ...
- * will put the entire model straight into the linear_alloc
- *
- * or, to target different allocators:
- *
- * mdl_open
- * mdl_load_metadata
- * mdl_load_vertex_data
- * mdl_load_indice_data
- * mdl_close
- *
- * these should ideally be called in quick succession to limit stalls.
- */
-
-/*
- * 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 )
-{
- 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_exit_loop( "see above for details" );
- }
-
- u64 l = fread( &mdl->info, sizeof(mdl_file_header), 1, mdl->file );
- if( l != 1 )
- mdl_load_fatal_corrupt( mdl );
-}
-
-/*
- * Load all metadata (everything up until the large buffers). Probs at most 50k
- */
-VG_STATIC void mdl_load_metadata( mdl_context *mdl, void *lin_alloc )
-{
- assert( mdl->file );
-
- u64 lheader = sizeof(mdl_file_header),
- ldata = mdl->info.keyframe_offset - lheader;
-
- void *all_data = vg_linear_alloc( lin_alloc, ldata );
-
- fseek( mdl->file, lheader, SEEK_SET );
- u64 l = fread( all_data, ldata, 1, mdl->file );
-
- if( l != 1 )
- {
- vg_file_print_invalid( mdl->file );
- vg_fatal_exit_loop( "Corrupt model" );
- }
-
- mdl->node_buffer = all_data + (mdl->info.node_offset - lheader);
- mdl->submesh_buffer = all_data + (mdl->info.submesh_offset - lheader);
- mdl->material_buffer = all_data + (mdl->info.material_offset - lheader);
- mdl->anim_buffer = all_data + (mdl->info.anim_offset - lheader);
- mdl->entdata_buffer = all_data + (mdl->info.entdata_offset - lheader);
- mdl->string_buffer = all_data + (mdl->info.strings_offset - lheader);
-}
-
-/*
- * Load just the mesh data
- */
-VG_STATIC void mdl_load_mesh_data( mdl_context *mdl, void *lin_alloc )
-{
- assert( mdl->file );
-
- u64 size_verts = mdl->info.vertex_count * sizeof(mdl_vert),
- size_index = mdl->info.indice_count * sizeof(u32);
-
- mdl->vertex_buffer = vg_linear_alloc( lin_alloc, size_verts );
- mdl->index_buffer = vg_linear_alloc( lin_alloc, size_index );
-
- {
- fseek( mdl->file, mdl->info.vertex_offset, SEEK_SET );
- u64 l = fread( mdl->vertex_buffer, size_verts, 1, mdl->file );
- if( l != 1 )
- mdl_load_fatal_corrupt( mdl );
- }
- {
- fseek( mdl->file, mdl->info.indice_offset, SEEK_SET );
- u64 l = fread( mdl->index_buffer, size_index, 1, mdl->file );
- if( l != 1 )
- mdl_load_fatal_corrupt( mdl );
- }
-}