#include "vg/vg_msg.h"
#include "steam.h"
#include "workshop.h"
+#include <string.h>
struct addon_system addon_system;
#include "shaders/scene_font.h"
#include "world_render.h"
#include "depth_compare.h"
+#include <string.h>
enum efont_SRglyph{
k_SRglyph_end = 0x00, /* control characters */
--- /dev/null
+/*
+ * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+
+#pragma once
+
+#include "model.h"
+#include "vg/vg_io.h"
+#include "vg/vg_async.h"
+#include "vg/vg_tex.h"
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+static void mdl_load_fatal_corrupt( mdl_context *mdl )
+{
+ fclose( mdl->file );
+ vg_file_print_invalid( mdl->file );
+ vg_fatal_error( "Corrupt model" );
+}
+
+/*
+ * Model implementation
+ */
+
+void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
+{
+ if( !info->pack_size )
+ {
+ vg_warn( "path: %s\n", mdl_pstr( mdl, info->pstr_path ) );
+ vg_fatal_error( "Packed file is only a header; it is not packed" );
+ }
+
+ fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET );
+ u64 l = fread( dst, info->pack_size, 1, mdl->file );
+
+ if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+}
+
+/* TODO: Rename these */
+static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
+ void *buffer, u32 stride )
+{
+ if( arr->item_count )
+ {
+ fseek( mdl->file, arr->file_offset, SEEK_SET );
+
+ if( stride == arr->item_size )
+ {
+ u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
+ if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+ }
+ else
+ {
+ vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n",
+ buffer, arr->item_size, stride, arr->item_count );
+ if( stride < arr->item_size )
+ vg_fatal_error( "not safe\n" );
+
+ for( u32 i=0; i<arr->item_count; i++ )
+ {
+ u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
+ if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+ }
+ }
+ }
+}
+
+static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
+ mdl_array *arr, void *lin_alloc, u32 stride )
+{
+ if( stride < arr->item_size )
+ {
+ vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size );
+ vg_fatal_error( "not safe\n" );
+ }
+
+ if( arr->item_count )
+ {
+ u32 size = stride*arr->item_count;
+ ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
+ mdl_load_array_file_buffer( mdl, arr, ptr->data, stride );
+ }
+ else
+ {
+ ptr->data = NULL;
+ }
+
+ ptr->stride = stride;
+ ptr->count = arr->item_count;
+}
+
+void *mdl_arritm( mdl_array_ptr *arr, u32 index )
+{
+ return ((u8 *)arr->data) + index*arr->stride;
+}
+
+u32 mdl_arrcount( mdl_array_ptr *arr )
+{
+ return arr->count;
+}
+
+static mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
+{
+ for( u32 i=0; i<mdl_arrcount(&mdl->index); i++ )
+ {
+ mdl_array *arr = mdl_arritm( &mdl->index, i );
+
+ if( !strncmp(arr->name,name,16) )
+ return arr;
+ }
+
+ return NULL;
+}
+
+int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
+ const char *name, void *lin_alloc, u32 stride )
+{
+ mdl_array *arr = mdl_find_array( mdl, name );
+
+ if( arr )
+ {
+ mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride );
+ return 1;
+ }
+ else
+ {
+ ptr->data = NULL;
+ ptr->count = 0;
+ ptr->stride = 0;
+ return 0;
+ }
+}
+
+int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc )
+{
+ int success = 1;
+
+ success &= MDL_LOAD_ARRAY( mdl, &mdl->verts, mdl_vert, lin_alloc );
+ success &= MDL_LOAD_ARRAY( mdl, &mdl->indices, mdl_indice, lin_alloc );
+
+ return success;
+}
+
+int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc )
+{
+ int success = 1;
+
+ success &= _mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc, 1 );
+ 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;
+}
+
+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
+ */
+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_MIN )
+ {
+ vg_warn( "For model: %s\n", path );
+ vg_warn( " version: %u (min: %u, current: %u)\n",
+ mdl->info.version, MDL_VERSION_MIN, MDL_VERSION_NR );
+
+ vg_fatal_error( "Legacy model version incompatable" );
+ }
+
+ mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc,
+ sizeof(mdl_array) );
+
+ 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
+ */
+void mdl_close( mdl_context *mdl )
+{
+ fclose( mdl->file );
+ mdl->file = NULL;
+}
+
+/* useful things you can do with the model */
+
+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] );
+}
+
+const char *mdl_pstr( mdl_context *mdl, u32 pstr )
+{
+ return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4;
+}
+
+
+int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 )
+{
+ u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr ));
+ if( hash == djb2 ){
+ if( !strcmp( str, mdl_pstr( mdl, pstr ))) return 1;
+ else return 0;
+ }
+ else return 0;
+}
+
+/*
+ * Simple mesh interface for OpenGL
+ * ----------------------------------------------------------------------------
+ */
+
+static void mesh_upload( glmesh *mesh,
+ mdl_vert *verts, u32 vert_count,
+ u32 *indices, u32 indice_count )
+{
+ 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;
+}
+
+void mesh_bind( glmesh *mesh )
+{
+ glBindVertexArray( mesh->vao );
+}
+
+void mesh_drawn( u32 start, u32 count )
+{
+ glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
+ (void *)(start*sizeof(u32)) );
+}
+
+void mesh_draw( glmesh *mesh )
+{
+ mesh_drawn( 0, mesh->indice_count );
+}
+
+void mesh_free( glmesh *mesh )
+{
+ if( mesh->loaded )
+ {
+ glDeleteVertexArrays( 1, &mesh->vao );
+ glDeleteBuffers( 1, &mesh->ebo );
+ glDeleteBuffers( 1, &mesh->vbo );
+ mesh->loaded = 0;
+ }
+}
+
+void mdl_draw_submesh( mdl_submesh *sm )
+{
+ mesh_drawn( sm->indice_start, sm->indice_count );
+}
+
+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 _sync_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 );
+}
+
+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, _sync_mdl_load_glmesh );
+ }
+ else
+ {
+ vg_fatal_error( "no vertex/indice data\n" );
+ }
+}
+
+/* uploads the glmesh, and textures. everything is saved into the mdl_context */
+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 );
+ }
+}
#pragma once
-#include "vg/vg_io.h"
-#include "vg/vg_async.h"
-#include "vg/vg_tex.h"
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-
#define MDL_VERSION_MIN 101
#define MDL_VERSION_NR 105
v4f colour,
colour1;
- u32 tex_diffuse,
+ u32 tex_diffuse, /* Indexes start from 1. 0 if missing. */
tex_none0,
tex_none1;
};
FILE *file;
mdl_header info;
- struct mdl_array_ptr{
+ struct mdl_array_ptr
+ {
void *data;
u32 count, stride;
}
glmesh mesh;
};
+/* mesh. TODO: move? */
+void mesh_bind( glmesh *mesh );
+void mesh_drawn( u32 start, u32 count );
+void mesh_draw( glmesh *mesh );
+void mesh_free( glmesh *mesh );
-static void mdl_load_fatal_corrupt( mdl_context *mdl )
-{
- fclose( mdl->file );
- vg_file_print_invalid( mdl->file );
- vg_fatal_error( "Corrupt model" );
-}
-
-/*
- * Model implementation
- */
-
-static const char *mdl_pstr( mdl_context *mdl, u32 pstr );
-static
-void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
-{
- if( !info->pack_size ){
- vg_warn( "path: %s\n", mdl_pstr( mdl, info->pstr_path ) );
- vg_fatal_error( "Packed file is only a header; it is not packed" );
- }
-
- fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET );
- u64 l = fread( dst, info->pack_size, 1, mdl->file );
-
- if( l != 1 ) mdl_load_fatal_corrupt( mdl );
-}
-
-/* TODO: Rename these */
-static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
- void *buffer, u32 stride )
-{
- if( arr->item_count ){
- fseek( mdl->file, arr->file_offset, SEEK_SET );
-
- if( stride == arr->item_size ){
- u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
- if( l != 1 ) mdl_load_fatal_corrupt( mdl );
- }
- else {
- vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n",
- buffer, arr->item_size, stride, arr->item_count );
- if( stride < arr->item_size )
- vg_fatal_error( "not safe\n" );
-
- for( u32 i=0; i<arr->item_count; i++ ){
- u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
- if( l != 1 ) mdl_load_fatal_corrupt( mdl );
- }
- }
- }
-}
-
-static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
- mdl_array *arr, void *lin_alloc, u32 stride )
-{
- if( stride < arr->item_size ){
- vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size );
- vg_fatal_error( "not safe\n" );
- }
-
- if( arr->item_count ){
- u32 size = stride*arr->item_count;
- ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
- mdl_load_array_file_buffer( mdl, arr, ptr->data, stride );
- }
- else{
- ptr->data = NULL;
- }
-
- ptr->stride = stride;
- ptr->count = arr->item_count;
-}
-
-static void *mdl_arritm( mdl_array_ptr *arr, u32 index )
-{
- return ((u8 *)arr->data) + index*arr->stride;
-}
-
-static u32 mdl_arrcount( mdl_array_ptr *arr )
-{
- return arr->count;
-}
-
-static mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
-{
- for( u32 i=0; i<mdl_arrcount(&mdl->index); i++ ){
- mdl_array *arr = mdl_arritm( &mdl->index, i );
-
- if( !strncmp(arr->name,name,16) ){
- return arr;
- }
- }
-
- return NULL;
-}
-
-static int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
- const char *name, void *lin_alloc, u32 stride )
-{
- mdl_array *arr = mdl_find_array( mdl, name );
-
- if( arr ){
- mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride );
- return 1;
- }
- else{
- ptr->data = NULL;
- ptr->count = 0;
- ptr->stride = 0;
- return 0;
- }
-}
+/* file context management */
+void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc );
+void mdl_close( mdl_context *mdl );
+/* array loading */
+int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
+ const char *name, void *lin_alloc, u32 stride );
#define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
_mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
-static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc ){
- int success = 1;
-
- success &= MDL_LOAD_ARRAY( mdl, &mdl->verts, mdl_vert, lin_alloc );
- success &= MDL_LOAD_ARRAY( mdl, &mdl->indices, mdl_indice, lin_alloc );
-
- return success;
-}
-
-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, 1 );
- 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;
-}
-
-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
- */
-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_MIN ){
- vg_warn( "For model: %s\n", path );
- vg_warn( " version: %u (min: %u, current: %u)\n",
- mdl->info.version, MDL_VERSION_MIN, MDL_VERSION_NR );
+/* array access */
+void *mdl_arritm( mdl_array_ptr *arr, u32 index );
+u32 mdl_arrcount( mdl_array_ptr *arr );
- vg_fatal_error( "Legacy model version incompatable" );
- }
+/* pack access */
+void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst );
- mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc,
- sizeof(mdl_array) );
+/* standard array groups */
+int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc );
+int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc );
+int mdl_load_mesh_block( mdl_context *mdl, void *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;
-}
+/* load mesh */
+void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table );
-/*
- * close file handle
- */
-static void mdl_close( mdl_context *mdl )
-{
- fclose( mdl->file );
- mdl->file = NULL;
-}
+/* load textures and mesh */
+void mdl_async_full_load_std( mdl_context *mdl );
-/* useful things you can do with the model */
-
-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] );
-}
-
-static const char *mdl_pstr( mdl_context *mdl, u32 pstr )
-{
- return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4;
-}
-
-
-static int
-mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 )
-{
- u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr ));
- if( hash == djb2 ){
- if( !strcmp( str, mdl_pstr( mdl, pstr ))) return 1;
- else return 0;
- }
- else return 0;
-}
+/* rendering */
+void mdl_draw_submesh( mdl_submesh *sm );
+mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name );
+/* pstrs */
+const char *mdl_pstr( mdl_context *mdl, u32 pstr );
+int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 );
#define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
-/*
- * Simple mesh interface for OpenGL
- * ----------------------------------------------------------------------------
- */
-
-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;
-}
-
-static void mesh_bind( glmesh *mesh )
-{
- glBindVertexArray( mesh->vao );
-}
+void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx );
-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 );
- }
-}
#include "player_skate.h"
#include "player_basic_info.h"
#include "player_glide.h"
+#include <string.h>
i32 k_invert_y = 0;
struct localplayer localplayer =
mdl_open( mdl, "models/glider.mdl", alloc );
mdl_load_metadata_block( mdl, alloc );
-
- vg_linear_clear( vg_mem.scratch );
-
- u32 count = mdl_arrcount( &mdl->textures );
- player_glide.glider_textures =
- vg_linear_alloc( alloc, vg_align8(sizeof(GLuint)*(count+1)));
- player_glide.glider_textures[0] = vg.tex_missing;
- mdl_async_load_glmesh( mdl, &player_glide.glider_mesh, NULL );
-
- for( u32 i=0; i<count; i ++ ){
- vg_linear_clear( vg_mem.scratch );
- player_glide.glider_textures[i+1] = vg.tex_missing;
-
- 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_LINEAR|VG_TEX2D_CLAMP,
- &player_glide.glider_textures[i+1] );
- }
+ mdl_async_full_load_std( mdl );
/* load trail positions */
mdl_array_ptr markers;
MDL_LOAD_ARRAY( mdl, &markers, ent_marker, vg_mem.scratch );
+ mdl_close( mdl );
- for( u32 i=0; i<mdl_arrcount( &markers ); i ++ ){
+ for( u32 i=0; i<mdl_arrcount( &markers ); i ++ )
+ {
ent_marker *marker = mdl_arritm( &markers, i );
v3_copy( marker->transform.co,
player_glide.trail_positions[ player_glide.trail_count ++ ] );
break;
}
- mdl_close( mdl );
-
/* allocate effects */
- for( u32 i=0; i<vg_list_size(trails_glider); i ++ ){
+ for( u32 i=0; i<vg_list_size(trails_glider); i ++ )
+ {
trail_alloc( &trails_glider[i], 200 );
}
}
glActiveTexture( GL_TEXTURE0 );
mdl_context *mdl = &player_glide.glider;
- mesh_bind( &player_glide.glider_mesh );
+ mesh_bind( &player_glide.glider.mesh );
- for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i ++ ){
+ for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i ++ )
+ {
mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
m4x3f mmmdl;
if( shader == k_board_shader_player )
shader_model_board_view_uMdl( mmmdl );
- else if( shader == k_board_shader_entity ){
+ else if( shader == k_board_shader_entity )
+ {
m4x4f m4mmmdl;
m4x3_expand( mmmdl, m4mmmdl );
m4x4_mul( cam->mtx_prev.pv, m4mmmdl, m4mmmdl );
shader_model_entity_uPvmPrev( m4mmmdl );
}
- for( u32 j=0; j<mesh->submesh_count; j ++ ){
+ for( u32 j=0; j<mesh->submesh_count; j ++ )
+ {
mdl_submesh *sm = mdl_arritm( &mdl->submeshs, mesh->submesh_start+j );
- if( !sm->material_id ) {
+ if( !sm->material_id )
+ {
vg_error( "Invalid material ID 0\n" );
continue;
}
mdl_material *mat = mdl_arritm( &mdl->materials, sm->material_id-1 );
- if( mat->tex_diffuse != current_tex ){
- glBindTexture( GL_TEXTURE_2D,
- player_glide.glider_textures[ mat->tex_diffuse ] );
+ if( mat->tex_diffuse != current_tex )
+ {
+ GLuint tex = vg.tex_missing;
+
+ if( mat->tex_diffuse )
+ {
+ u32 index = mat->tex_diffuse-1;
+ mdl_texture *ptex = mdl_arritm( &mdl->textures, index );
+ tex = ptex->glname;
+ }
+
+ glBindTexture( GL_TEXTURE_2D, tex );
current_tex = mat->tex_diffuse;
}
v3f trail_positions[2];
mdl_context glider;
- GLuint *glider_textures;
- glmesh glider_mesh;
}
extern player_glide;
extern struct player_subsystem_interface player_subsystem_glide;
#pragma once
#include "vg/vg_bvh.h"
+#include "vg/vg_async.h"
#include "common.h"
#include "model.h"
#include "world_volumes.c"
#include "world_water.c"
#include "ent_npc.c"
+#include "model.c"