2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #define MDL_VERSION_MIN 101
11 #define MDL_VERSION_NR 104
14 k_shader_standard
= 0,
15 k_shader_standard_cutout
= 1,
16 k_shader_terrain_blend
= 2,
17 k_shader_standard_vertex_blend
= 3,
19 k_shader_invisible
= 5,
20 k_shader_boundary
= 6,
23 k_shader_override
= 30000
26 enum mdl_surface_prop
{
27 k_surface_prop_concrete
= 0,
28 k_surface_prop_wood
= 1,
29 k_surface_prop_grass
= 2,
30 k_surface_prop_tiles
= 3,
31 k_surface_prop_metal
= 4
35 k_material_flag_skate_target
= 0x0001,
36 k_material_flag_collision
= 0x0002,
37 k_material_flag_grow_grass
= 0x0004,
38 k_material_flag_grindable
= 0x0008,
39 k_material_flag_invisible
= 0x0010,
40 k_material_flag_boundary
= 0x0020,
41 k_material_flag_preview_visibile
= 0x0040,
42 k_material_flag_walking
= 0x0080,
44 k_material_flag_ghosts
=
45 k_material_flag_boundary
|
46 k_material_flag_invisible
|
47 k_material_flag_walking
59 u8 colour
[4]; /* 4*8 */
60 u16 weights
[4];/* 4*16 */
61 u8 groups
[4]; /* 4*8 */
66 typedef u32 mdl_indice
;
68 typedef struct mdl_context mdl_context
;
69 typedef struct mdl_array_ptr mdl_array_ptr
;
70 typedef struct mdl_vert mdl_vert
;
71 typedef struct mdl_transform mdl_transform
;
72 typedef struct mdl_submesh mdl_submesh
;
73 typedef struct mdl_material mdl_material
;
74 typedef struct mdl_bone mdl_bone
;
75 typedef struct mdl_armature mdl_armature
;
76 typedef struct mdl_animation mdl_animation
;
77 typedef struct mdl_transform mdl_keyframe
;
78 typedef struct mdl_mesh mdl_mesh
;
79 typedef struct mdl_file mdl_file
;
80 typedef struct mdl_texture mdl_texture
;
81 typedef struct mdl_array mdl_array
;
82 typedef struct mdl_header mdl_header
;
84 typedef struct glmesh glmesh
;
98 static void transform_identity( mdl_transform
*transform
)
100 v3_zero( transform
->co
);
101 q_identity( transform
->q
);
102 v3_fill( transform
->s
, 1.0f
);
105 static void mdl_transform_vector( mdl_transform
*transform
, v3f vec
, v3f dest
)
107 v3_mul( transform
->s
, vec
, dest
);
108 q_mulv( transform
->q
, dest
, dest
);
111 static void mdl_transform_point( mdl_transform
*transform
, v3f co
, v3f dest
)
113 mdl_transform_vector( transform
, co
, dest
);
114 v3_add( transform
->co
, dest
, dest
);
117 static void mdl_transform_mul( mdl_transform
*a
, mdl_transform
*b
,
120 mdl_transform_point( a
, b
->co
, d
->co
);
121 q_mul( a
->q
, b
->q
, d
->q
);
123 v3_mul( a
->s
, b
->s
, d
->s
);
152 v3f conevx
, conevy
, coneva
;
158 k_bone_flag_deform
= 0x00000001,
159 k_bone_flag_ik
= 0x00000002,
160 k_bone_flag_cone_constraint
= 0x00000004
165 k_bone_collider_none
= 0,
166 k_bone_collider_box
= 1,
167 k_bone_collider_capsule
= 2
172 mdl_transform transform
;
195 u16 material_id
, flags
;
200 k_submesh_flag_none
= 0x0000,
201 k_submesh_flag_consumed
= 0x0001
206 mdl_transform transform
;
210 entity_id
, /* upper 16 bits: type, lower 16 bits: index */
246 struct mdl_array_ptr
{
262 /* animation buffers */
268 u32 pack_base_offset
;
275 static void mdl_load_fatal_corrupt( mdl_context
*mdl
)
278 vg_file_print_invalid( mdl
->file
);
279 vg_fatal_error( "Corrupt model" );
283 * Model implementation
286 static const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
);
288 void mdl_fread_pack_file( mdl_context
*mdl
, mdl_file
*info
, void *dst
)
290 if( !info
->pack_size
){
291 vg_warn( "path: %s\n", mdl_pstr( mdl
, info
->pstr_path
) );
292 vg_fatal_error( "Packed file is only a header; it is not packed" );
295 fseek( mdl
->file
, mdl
->pack_base_offset
+info
->pack_offset
, SEEK_SET
);
296 u64 l
= fread( dst
, info
->pack_size
, 1, mdl
->file
);
298 if( l
!= 1 ) mdl_load_fatal_corrupt( mdl
);
301 /* TODO: Rename these */
302 static void mdl_load_array_file_buffer( mdl_context
*mdl
, mdl_array
*arr
,
303 void *buffer
, u32 stride
)
305 if( arr
->item_count
){
306 fseek( mdl
->file
, arr
->file_offset
, SEEK_SET
);
308 if( stride
== arr
->item_size
){
309 u64 l
= fread( buffer
, arr
->item_size
*arr
->item_count
, 1, mdl
->file
);
310 if( l
!= 1 ) mdl_load_fatal_corrupt( mdl
);
313 assert( stride
>= arr
->item_size
);
315 for( u32 i
=0; i
<arr
->item_count
; i
++ ){
316 u64 l
= fread( buffer
+i
*stride
, arr
->item_size
, 1, mdl
->file
);
317 if( l
!= 1 ) mdl_load_fatal_corrupt( mdl
);
323 static void mdl_load_array_file( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
324 mdl_array
*arr
, void *lin_alloc
, u32 stride
)
326 assert( stride
>= arr
->item_size
);
328 if( arr
->item_count
){
329 u32 size
= stride
*arr
->item_count
;
330 ptr
->data
= vg_linear_alloc( lin_alloc
, vg_align8(size
) );
331 mdl_load_array_file_buffer( mdl
, arr
, ptr
->data
, stride
);
337 ptr
->stride
= stride
;
338 ptr
->count
= arr
->item_count
;
341 static void *mdl_arritm( mdl_array_ptr
*arr
, u32 index
)
343 return ((u8
*)arr
->data
) + index
*arr
->stride
;
346 static u32
mdl_arrcount( mdl_array_ptr
*arr
)
351 static mdl_array
*mdl_find_array( mdl_context
*mdl
, const char *name
)
353 for( u32 i
=0; i
<mdl_arrcount(&mdl
->index
); i
++ ){
354 mdl_array
*arr
= mdl_arritm( &mdl
->index
, i
);
356 if( !strncmp(arr
->name
,name
,16) ){
364 static int _mdl_load_array( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
365 const char *name
, void *lin_alloc
, u32 stride
)
367 mdl_array
*arr
= mdl_find_array( mdl
, name
);
370 mdl_load_array_file( mdl
, ptr
, arr
, lin_alloc
, stride
);
381 #define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
382 _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
384 static int mdl_load_mesh_block( mdl_context
*mdl
, void *lin_alloc
){
387 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->verts
, mdl_vert
, lin_alloc
);
388 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->indices
, mdl_indice
, lin_alloc
);
393 static int mdl_load_metadata_block( mdl_context
*mdl
, void *lin_alloc
){
396 success
&= _mdl_load_array( mdl
, &mdl
->strings
, "strings", lin_alloc
, 1 );
397 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->meshs
, mdl_mesh
, lin_alloc
);
398 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->submeshs
, mdl_submesh
, lin_alloc
);
399 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->materials
, mdl_material
, lin_alloc
);
400 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->textures
, mdl_texture
, lin_alloc
);
401 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->armatures
, mdl_armature
, lin_alloc
);
402 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->bones
, mdl_bone
, lin_alloc
);
403 success
&= MDL_LOAD_ARRAY( mdl
, &mdl
->animations
,mdl_animation
,lin_alloc
);
408 static int mdl_load_animation_block( mdl_context
*mdl
, void *lin_alloc
){
409 return MDL_LOAD_ARRAY( mdl
, &mdl
->keyframes
, mdl_keyframe
, lin_alloc
);
413 * if calling mdl_open, and the file does not exist, the game will fatal quit
415 static void mdl_open( mdl_context
*mdl
, const char *path
, void *lin_alloc
)
417 memset( mdl
, 0, sizeof( mdl_context
) );
418 mdl
->file
= fopen( path
, "rb" );
421 vg_error( "mdl_open('%s'): %s\n", path
, strerror(errno
) );
422 vg_fatal_error( "see above for details" );
425 u64 l
= fread( &mdl
->info
, sizeof(mdl_header
), 1, mdl
->file
);
427 mdl_load_fatal_corrupt( mdl
);
429 if( mdl
->info
.version
< MDL_VERSION_MIN
){
430 vg_warn( "For model: %s\n", path
);
431 vg_warn( " version: %u (min: %u, current: %u)\n",
432 mdl
->info
.version
, MDL_VERSION_MIN
, MDL_VERSION_NR
);
434 vg_fatal_error( "Legacy model version incompatable" );
437 mdl_load_array_file( mdl
, &mdl
->index
, &mdl
->info
.index
, lin_alloc
,
440 mdl_array
*pack
= mdl_find_array( mdl
, "pack" );
441 if( pack
) mdl
->pack_base_offset
= pack
->file_offset
;
442 else mdl
->pack_base_offset
= 0;
448 static void mdl_close( mdl_context
*mdl
)
454 /* useful things you can do with the model */
456 static void mdl_transform_m4x3( mdl_transform
*transform
, m4x3f mtx
)
458 q_m3x3( transform
->q
, mtx
);
459 v3_muls( mtx
[0], transform
->s
[0], mtx
[0] );
460 v3_muls( mtx
[1], transform
->s
[1], mtx
[1] );
461 v3_muls( mtx
[2], transform
->s
[2], mtx
[2] );
462 v3_copy( transform
->co
, mtx
[3] );
465 static const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
)
467 return ((char *)mdl_arritm( &mdl
->strings
, pstr
)) + 4;
472 mdl_pstreq( mdl_context
*mdl
, u32 pstr
, const char *str
, u32 djb2
)
474 u32 hash
= *((u32
*)mdl_arritm( &mdl
->strings
, pstr
));
476 if( !strcmp( str
, mdl_pstr( mdl
, pstr
))) return 1;
482 #define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
483 mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
486 * Simple mesh interface for OpenGL
487 * ----------------------------------------------------------------------------
490 static void mesh_upload( glmesh
*mesh
,
491 mdl_vert
*verts
, u32 vert_count
,
492 u32
*indices
, u32 indice_count
)
494 //assert( mesh->loaded == 0 );
496 glGenVertexArrays( 1, &mesh
->vao
);
497 glGenBuffers( 1, &mesh
->vbo
);
498 glGenBuffers( 1, &mesh
->ebo
);
499 glBindVertexArray( mesh
->vao
);
501 size_t stride
= sizeof(mdl_vert
);
503 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
504 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
506 glBindVertexArray( mesh
->vao
);
507 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
508 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
509 indices
, GL_STATIC_DRAW
);
512 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
513 glEnableVertexAttribArray( 0 );
516 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
517 stride
, (void *)offsetof(mdl_vert
, norm
) );
518 glEnableVertexAttribArray( 1 );
521 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
522 stride
, (void *)offsetof(mdl_vert
, uv
) );
523 glEnableVertexAttribArray( 2 );
526 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
527 stride
, (void *)offsetof(mdl_vert
, colour
) );
528 glEnableVertexAttribArray( 3 );
531 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
532 stride
, (void *)offsetof(mdl_vert
, weights
) );
533 glEnableVertexAttribArray( 4 );
536 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
537 stride
, (void *)offsetof(mdl_vert
, groups
) );
538 glEnableVertexAttribArray( 5 );
542 mesh
->indice_count
= indice_count
;
546 static void mesh_bind( glmesh
*mesh
)
548 glBindVertexArray( mesh
->vao
);
551 static void mesh_drawn( u32 start
, u32 count
)
553 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
554 (void *)(start
*sizeof(u32
)) );
557 static void mesh_draw( glmesh
*mesh
)
559 mesh_drawn( 0, mesh
->indice_count
);
562 static void mesh_free( glmesh
*mesh
)
565 glDeleteVertexArrays( 1, &mesh
->vao
);
566 glDeleteBuffers( 1, &mesh
->ebo
);
567 glDeleteBuffers( 1, &mesh
->vbo
);
572 static void mdl_draw_submesh( mdl_submesh
*sm
)
574 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
577 static mdl_mesh
*mdl_find_mesh( mdl_context
*mdl
, const char *name
)
579 for( u32 i
=0; i
<mdl_arrcount( &mdl
->meshs
); i
++ ){
580 mdl_mesh
*mesh
= mdl_arritm( &mdl
->meshs
, i
);
581 if( !strcmp( name
, mdl_pstr( mdl
, mesh
->pstr_name
))){
588 struct payload_glmesh_load
{
598 static void async_mdl_load_glmesh( void *payload
, u32 size
)
600 struct payload_glmesh_load
*job
= payload
;
601 mesh_upload( job
->mesh
, job
->verts
, job
->vertex_count
,
602 job
->indices
, job
->indice_count
);
605 static void mdl_async_load_glmesh( mdl_context
*mdl
, glmesh
*mesh
)
607 mdl_array
*arr_vertices
= mdl_find_array( mdl
, "mdl_vert" );
608 mdl_array
*arr_indices
= mdl_find_array( mdl
, "mdl_indice" );
610 if( arr_vertices
&& arr_indices
){
611 u32 size_verts
= vg_align8(sizeof(mdl_vert
)*arr_vertices
->item_count
),
612 size_indices
= vg_align8(sizeof(mdl_indice
)*arr_indices
->item_count
),
613 size_hdr
= vg_align8(sizeof(struct payload_glmesh_load
)),
614 total
= size_hdr
+ size_verts
+ size_indices
;
616 vg_async_item
*call
= vg_async_alloc( total
);
617 struct payload_glmesh_load
*job
= call
->payload
;
619 u8
*payload
= call
->payload
;
622 job
->verts
= (void*)(payload
+ size_hdr
);
623 job
->indices
= (void*)(payload
+ size_hdr
+ size_verts
);
624 job
->vertex_count
= arr_vertices
->item_count
;
625 job
->indice_count
= arr_indices
->item_count
;
627 mdl_load_array_file_buffer( mdl
, arr_vertices
,
628 job
->verts
, sizeof(mdl_vert
) );
629 mdl_load_array_file_buffer( mdl
, arr_indices
, job
->indices
,
630 sizeof(mdl_indice
) );
633 * Unpack the indices (if there are meshes)
634 * ---------------------------------------------------------
637 if( mdl_arrcount( &mdl
->submeshs
) ){
638 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, 0 );
639 u32 offset
= sm
->vertex_count
;
641 for( u32 i
=1; i
<mdl_arrcount( &mdl
->submeshs
); i
++ ){
642 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, i
);
643 u32
*indices
= job
->indices
+ sm
->indice_start
;
645 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
646 indices
[j
] += offset
;
648 offset
+= sm
->vertex_count
;
654 * -------------------------
657 vg_async_dispatch( call
, async_mdl_load_glmesh
);
660 vg_fatal_error( "no vertex/indice data\n" );
664 /* uploads the glmesh, and textures. everything is saved into the mdl_context */
665 static void mdl_async_full_load_std( mdl_context
*mdl
){
666 mdl_async_load_glmesh( mdl
, &mdl
->mesh
);
668 for( u32 i
=0; i
<mdl_arrcount( &mdl
->textures
); i
++ ){
669 vg_linear_clear( vg_mem
.scratch
);
670 mdl_texture
*tex
= mdl_arritm( &mdl
->textures
, i
);
672 void *data
= vg_linear_alloc( vg_mem
.scratch
, tex
->file
.pack_size
);
673 mdl_fread_pack_file( mdl
, &tex
->file
, data
);
675 vg_tex2d_load_qoi_async( data
, tex
->file
.pack_size
,
676 VG_TEX2D_CLAMP
|VG_TEX2D_NEAREST
, &tex
->glname
);