/*
- * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
*/
#ifndef MODEL_H
#define MODEL_H
-#include "common.h"
+#include "skaterift.h"
+#define MDL_VERSION_MIN 101
+#define MDL_VERSION_NR 102
-enum mdl_shader
-{
+enum mdl_shader{
k_shader_standard = 0,
k_shader_standard_cutout = 1,
k_shader_terrain_blend = 2,
k_shader_standard_vertex_blend = 3,
- k_shader_water = 4
+ k_shader_water = 4,
+ k_shader_invisible = 5,
+ k_shader_boundary = 6,
+ k_shader_fxglow = 7,
+ k_shader_cubemap = 8
};
-enum mdl_surface_prop
-{
+enum mdl_surface_prop{
k_surface_prop_concrete = 0,
k_surface_prop_wood = 1,
k_surface_prop_grass = 2,
k_surface_prop_metal = 4
};
-enum material_flag
-{
- k_material_flag_skate_surface = 0x1,
- k_material_flag_collision = 0x2,
- k_material_flag_grow_grass = 0x4,
- k_material_flag_grind_surface = 0x8
+enum material_flag{
+ k_material_flag_skate_target = 0x00000001,
+ k_material_flag_collision = 0x00000002,
+ k_material_flag_grow_grass = 0x00000004,
+ k_material_flag_grindable = 0x00000008,
+ k_material_flag_invisible = 0x00000010,
+ k_material_flag_boundary = 0x00000020,
+ k_material_flag_preview_visibile = 0x00000040,
+ k_material_flag_walking = 0x00000080
};
#pragma pack(push,1)
typedef struct mdl_array mdl_array;
typedef struct mdl_header mdl_header;
+typedef struct glmesh glmesh;
+struct glmesh
+{
+ GLuint vao, vbo, ebo;
+ u32 indice_count;
+ u32 loaded;
+};
+
struct mdl_transform
{
v3f co, s;
v4f q;
};
-struct mdl_submesh
+static void transform_identity( mdl_transform *transform )
{
- u32 indice_start,
- indice_count,
- vertex_start,
- vertex_count;
+ v3_zero( transform->co );
+ q_identity( transform->q );
+ v3_fill( transform->s, 1.0f );
+}
- boxf bbx;
- u32 material_id;
-};
+static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest )
+{
+ v3_mul( transform->s, vec, dest );
+ q_mulv( transform->q, dest, dest );
+}
+
+static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest )
+{
+ mdl_transform_vector( transform, co, dest );
+ v3_add( transform->co, dest, dest );
+}
+
+static void mdl_transform_mul( mdl_transform *a, mdl_transform *b,
+ mdl_transform *d )
+{
+ mdl_transform_point( a, b->co, d->co );
+ q_mul( a->q, b->q, d->q );
+ q_normalize( d->q );
+ v3_mul( a->s, b->s, d->s );
+}
struct mdl_material
{
enum bone_flag
{
- k_bone_flag_deform = 0x1,
- k_bone_flag_ik = 0x2,
- k_bone_flag_cone_constraint = 0x4
+ k_bone_flag_deform = 0x00000001,
+ k_bone_flag_ik = 0x00000002,
+ k_bone_flag_cone_constraint = 0x00000004
};
enum bone_collider
u32 offset;
};
+struct mdl_submesh
+{
+ u32 indice_start,
+ indice_count,
+ vertex_start,
+ vertex_count;
+
+ boxf bbx;
+ u16 material_id, flags;
+};
+
+enum esubmesh_flags
+{
+ k_submesh_flag_none = 0x0000,
+ k_submesh_flag_consumed = 0x0001
+};
+
struct mdl_mesh
{
mdl_transform transform;
u32 submesh_start,
submesh_count,
pstr_name,
- flags,
+ entity_id, /* upper 16 bits: type, lower 16 bits: index */
armature_id;
};
struct mdl_texture
{
mdl_file file;
- u32 type;
+ u32 glname;
};
struct mdl_array
mdl_array index;
};
-struct mdl_context
-{
+struct mdl_context{
FILE *file;
mdl_header info;
- struct mdl_array_ptr
- {
+ struct mdl_array_ptr{
void *data;
u32 count, stride;
}
/* mesh buffers */
verts,
indices;
-
u32 pack_base_offset;
-
- /* pack data */
- //pack;
+
+ /* runtime */
+ glmesh mesh;
};
{
fclose( mdl->file );
vg_file_print_invalid( mdl->file );
- vg_fatal_exit_loop( "Corrupt model" );
+ vg_fatal_error( "Corrupt model" );
}
/*
u32 size = arr->item_size*arr->item_count;
return vg_align8(size);
}
- else
- return 0;
+ else return 0;
}
+VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr );
VG_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 );
+ if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+}
+
+/* TODO: Rename these */
+VG_STATIC void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
+ void *buffer )
+{
+ if( arr->item_count ){
+ fseek( mdl->file, arr->file_offset, SEEK_SET );
+ u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
+
+ if( l != 1 ) mdl_load_fatal_corrupt( mdl );
+ }
}
VG_STATIC void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
if( arr->item_count ){
u32 size = arr->item_size*arr->item_count;
ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
-
- fseek( mdl->file, arr->file_offset, SEEK_SET );
- u64 l = fread( ptr->data, arr->item_size*arr->item_count, 1, mdl->file );
-
- if( l != 1 )
- mdl_load_fatal_corrupt( mdl );
+ mdl_load_array_file_buffer( mdl, arr, ptr->data );
}
else
ptr->data = NULL;
return mdl_load_array( mdl, &mdl->keyframes, "mdl_keyframe", lin_alloc );
}
-#if 0
-VG_STATIC int mdl_load_pack_block( mdl_context *mdl, void *lin_alloc )
-{
- return mdl_load_array( mdl, &mdl->pack, "pack", lin_alloc );
-}
-#endif
-
/*
* if calling mdl_open, and the file does not exist, the game will fatal quit
*/
if( !mdl->file ){
vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
- vg_fatal_exit_loop( "see above for details" );
+ 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 );
mdl_array *pack = mdl_find_array( mdl, "pack" );
VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr )
{
- return mdl_arritm( &mdl->strings, pstr );
+ return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4;
}
+
+VG_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;
+}
+
+#define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
+ mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
+
/*
* 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 )
mesh_drawn( sm->indice_start, sm->indice_count );
}
-/* WARNING: Destructive! Only use this once and then discard the context. */
-VG_STATIC void mdl_unpack_glmesh( mdl_context *mdl, glmesh *mesh )
+VG_STATIC mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name )
{
- if( !mdl->submeshs.count )
- vg_fatal_exit_loop( "Tried to unpack empty model file" );
-
- mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 );
- u32 offset = sm->vertex_count;
+ 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;
+}
- for( u32 i=1; i<mdl_arrcount( &mdl->submeshs ); i++ ){
- mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i );
- u32 *indices = mdl_arritm( &mdl->indices, sm->indice_start );
+struct payload_glmesh_load{
+ mdl_vert *verts;
+ u32 *indices;
- for( u32 j=0; j<sm->indice_count; j++ )
- indices[j] += offset;
+ u32 vertex_count,
+ indice_count;
- offset += sm->vertex_count;
- }
+ glmesh *mesh;
+};
- mesh_upload( mesh, mdl->verts.data, mdl->verts.count,
- mdl->indices.data, mdl->indices.count );
+VG_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 );
}
-VG_STATIC mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name )
+/* TODO: Find out if this needs deprecating in favour of the new full loader */
+VG_STATIC void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh )
{
- 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;
+ 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(mdl_query_array_size( arr_vertices )),
+ size_indices = vg_align8(mdl_query_array_size( arr_indices )),
+ 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 );
+ mdl_load_array_file_buffer( mdl, arr_indices, job->indices );
+
+ /*
+ * 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 */
+VG_STATIC void mdl_async_full_load_std( mdl_context *mdl ){
+ mdl_async_load_glmesh( mdl, &mdl->mesh );
+
+ 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 );
}
- return NULL;
}
#endif