2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #define MDL_VERSION_NR 100
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,
25 k_surface_prop_concrete
= 0,
26 k_surface_prop_wood
= 1,
27 k_surface_prop_grass
= 2,
28 k_surface_prop_tiles
= 3,
29 k_surface_prop_metal
= 4
34 k_material_flag_skate_target
= 0x00000001,
35 k_material_flag_collision
= 0x00000002,
36 k_material_flag_grow_grass
= 0x00000004,
37 k_material_flag_grindable
= 0x00000008,
38 k_material_flag_invisible
= 0x00000010,
39 k_material_flag_boundary
= 0x00000020
51 u8 colour
[4]; /* 4*8 */
52 u16 weights
[4];/* 4*16 */
53 u8 groups
[4]; /* 4*8 */
58 typedef struct mdl_context mdl_context
;
59 typedef struct mdl_array_ptr mdl_array_ptr
;
60 typedef struct mdl_vert mdl_vert
;
61 typedef struct mdl_transform mdl_transform
;
62 typedef struct mdl_submesh mdl_submesh
;
63 typedef struct mdl_material mdl_material
;
64 typedef struct mdl_bone mdl_bone
;
65 typedef struct mdl_armature mdl_armature
;
66 typedef struct mdl_animation mdl_animation
;
67 typedef struct mdl_transform mdl_keyframe
;
68 typedef struct mdl_mesh mdl_mesh
;
69 typedef struct mdl_file mdl_file
;
70 typedef struct mdl_texture mdl_texture
;
71 typedef struct mdl_array mdl_array
;
72 typedef struct mdl_header mdl_header
;
106 v3f conevx
, conevy
, coneva
;
112 k_bone_flag_deform
= 0x00000001,
113 k_bone_flag_ik
= 0x00000002,
114 k_bone_flag_cone_constraint
= 0x00000004
119 k_bone_collider_none
= 0,
120 k_bone_collider_box
= 1,
121 k_bone_collider_capsule
= 2
126 mdl_transform transform
;
149 u16 material_id
, flags
;
154 k_submesh_flag_none
= 0x0000,
155 k_submesh_flag_consumed
= 0x0001
160 mdl_transform transform
;
164 entity_id
, /* upper 16 bits: type, lower 16 bits: index */
218 /* animation buffers */
225 u32 pack_base_offset
;
232 VG_STATIC
void mdl_load_fatal_corrupt( mdl_context
*mdl
)
235 vg_file_print_invalid( mdl
->file
);
236 vg_fatal_exit_loop( "Corrupt model" );
240 * Model implementation
243 VG_STATIC u32
mdl_query_array_size( mdl_array
*arr
)
245 if( arr
->item_count
){
246 u32 size
= arr
->item_size
*arr
->item_count
;
247 return vg_align8(size
);
253 VG_STATIC
const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
);
255 void mdl_fread_pack_file( mdl_context
*mdl
, mdl_file
*info
, void *dst
)
257 if( !info
->pack_size
){
258 vg_warn( "path: %s\n", mdl_pstr( mdl
, info
->pstr_path
) );
259 vg_fatal_exit_loop( "Packed file is only a header; it is not packed" );
262 fseek( mdl
->file
, mdl
->pack_base_offset
+info
->pack_offset
, SEEK_SET
);
263 u64 l
= fread( dst
, info
->pack_size
, 1, mdl
->file
);
266 mdl_load_fatal_corrupt( mdl
);
269 VG_STATIC
void mdl_load_array_file( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
270 mdl_array
*arr
, void *lin_alloc
)
272 if( arr
->item_count
){
273 u32 size
= arr
->item_size
*arr
->item_count
;
274 ptr
->data
= vg_linear_alloc( lin_alloc
, vg_align8(size
) );
276 fseek( mdl
->file
, arr
->file_offset
, SEEK_SET
);
277 u64 l
= fread( ptr
->data
, arr
->item_size
*arr
->item_count
, 1, mdl
->file
);
280 mdl_load_fatal_corrupt( mdl
);
285 ptr
->count
= arr
->item_count
;
286 ptr
->stride
= arr
->item_size
;
289 VG_STATIC
void *mdl_arritm( mdl_array_ptr
*arr
, u32 index
)
291 return ((u8
*)arr
->data
) + index
*arr
->stride
;
294 VG_STATIC u32
mdl_arrcount( mdl_array_ptr
*arr
)
299 VG_STATIC mdl_array
*mdl_find_array( mdl_context
*mdl
, const char *name
)
301 for( u32 i
=0; i
<mdl_arrcount(&mdl
->index
); i
++ ){
302 mdl_array
*arr
= mdl_arritm( &mdl
->index
, i
);
304 if( !strncmp(arr
->name
,name
,16) ){
312 VG_STATIC
int mdl_load_array( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
313 const char *name
, void *lin_alloc
)
315 mdl_array
*arr
= mdl_find_array( mdl
, name
);
318 mdl_load_array_file( mdl
, ptr
, arr
, lin_alloc
);
329 VG_STATIC
int mdl_load_mesh_block( mdl_context
*mdl
, void *lin_alloc
)
333 success
&= mdl_load_array( mdl
, &mdl
->verts
, "mdl_vert", lin_alloc
);
334 success
&= mdl_load_array( mdl
, &mdl
->indices
, "mdl_indice", lin_alloc
);
339 VG_STATIC
int mdl_load_metadata_block( mdl_context
*mdl
, void *lin_alloc
)
343 success
&= mdl_load_array( mdl
, &mdl
->strings
, "strings", lin_alloc
);
344 success
&= mdl_load_array( mdl
, &mdl
->meshs
, "mdl_mesh", lin_alloc
);
345 success
&= mdl_load_array( mdl
, &mdl
->submeshs
, "mdl_submesh", lin_alloc
);
346 success
&= mdl_load_array( mdl
, &mdl
->materials
, "mdl_material", lin_alloc
);
347 success
&= mdl_load_array( mdl
, &mdl
->textures
, "mdl_texture", lin_alloc
);
348 success
&= mdl_load_array( mdl
, &mdl
->armatures
, "mdl_armature", lin_alloc
);
349 success
&= mdl_load_array( mdl
, &mdl
->bones
, "mdl_bone", lin_alloc
);
350 success
&= mdl_load_array( mdl
, &mdl
->animations
,"mdl_animation",lin_alloc
);
355 VG_STATIC
int mdl_load_animation_block( mdl_context
*mdl
, void *lin_alloc
)
357 return mdl_load_array( mdl
, &mdl
->keyframes
, "mdl_keyframe", lin_alloc
);
361 * if calling mdl_open, and the file does not exist, the game will fatal quit
363 VG_STATIC
void mdl_open( mdl_context
*mdl
, const char *path
, void *lin_alloc
)
365 memset( mdl
, 0, sizeof( mdl_context
) );
366 mdl
->file
= fopen( path
, "rb" );
369 vg_error( "mdl_open('%s'): %s\n", path
, strerror(errno
) );
370 vg_fatal_exit_loop( "see above for details" );
373 u64 l
= fread( &mdl
->info
, sizeof(mdl_header
), 1, mdl
->file
);
375 mdl_load_fatal_corrupt( mdl
);
377 if( mdl
->info
.version
!= MDL_VERSION_NR
){
378 vg_warn( "For model: %s\n", path
);
379 vg_warn( " version: %u (current: %u)\n", mdl
->info
.version
,
382 vg_fatal_exit_loop( "Legacy model version incompatable" );
385 mdl_load_array_file( mdl
, &mdl
->index
, &mdl
->info
.index
, lin_alloc
);
387 mdl_array
*pack
= mdl_find_array( mdl
, "pack" );
388 if( pack
) mdl
->pack_base_offset
= pack
->file_offset
;
389 else mdl
->pack_base_offset
= 0;
395 VG_STATIC
void mdl_close( mdl_context
*mdl
)
401 /* useful things you can do with the model */
403 VG_STATIC
void mdl_transform_m4x3( mdl_transform
*transform
, m4x3f mtx
)
405 q_m3x3( transform
->q
, mtx
);
406 v3_muls( mtx
[0], transform
->s
[0], mtx
[0] );
407 v3_muls( mtx
[1], transform
->s
[1], mtx
[1] );
408 v3_muls( mtx
[2], transform
->s
[2], mtx
[2] );
409 v3_copy( transform
->co
, mtx
[3] );
412 VG_STATIC
const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
)
414 return mdl_arritm( &mdl
->strings
, pstr
);
418 * Simple mesh interface for OpenGL
419 * ----------------------------------------------------------------------------
422 typedef struct glmesh glmesh
;
425 GLuint vao
, vbo
, ebo
;
430 VG_STATIC
void mesh_upload( glmesh
*mesh
,
431 mdl_vert
*verts
, u32 vert_count
,
432 u32
*indices
, u32 indice_count
)
434 //assert( mesh->loaded == 0 );
436 glGenVertexArrays( 1, &mesh
->vao
);
437 glGenBuffers( 1, &mesh
->vbo
);
438 glGenBuffers( 1, &mesh
->ebo
);
439 glBindVertexArray( mesh
->vao
);
441 size_t stride
= sizeof(mdl_vert
);
443 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
444 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
446 glBindVertexArray( mesh
->vao
);
447 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
448 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
449 indices
, GL_STATIC_DRAW
);
452 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
453 glEnableVertexAttribArray( 0 );
456 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
457 stride
, (void *)offsetof(mdl_vert
, norm
) );
458 glEnableVertexAttribArray( 1 );
461 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
462 stride
, (void *)offsetof(mdl_vert
, uv
) );
463 glEnableVertexAttribArray( 2 );
466 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
467 stride
, (void *)offsetof(mdl_vert
, colour
) );
468 glEnableVertexAttribArray( 3 );
471 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
472 stride
, (void *)offsetof(mdl_vert
, weights
) );
473 glEnableVertexAttribArray( 4 );
476 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
477 stride
, (void *)offsetof(mdl_vert
, groups
) );
478 glEnableVertexAttribArray( 5 );
482 mesh
->indice_count
= indice_count
;
486 VG_STATIC
void mesh_bind( glmesh
*mesh
)
488 glBindVertexArray( mesh
->vao
);
491 VG_STATIC
void mesh_drawn( u32 start
, u32 count
)
493 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
494 (void *)(start
*sizeof(u32
)) );
497 VG_STATIC
void mesh_draw( glmesh
*mesh
)
499 mesh_drawn( 0, mesh
->indice_count
);
502 VG_STATIC
void mesh_free( glmesh
*mesh
)
505 glDeleteVertexArrays( 1, &mesh
->vao
);
506 glDeleteBuffers( 1, &mesh
->ebo
);
507 glDeleteBuffers( 1, &mesh
->vbo
);
512 VG_STATIC
void mdl_draw_submesh( mdl_submesh
*sm
)
514 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
517 /* WARNING: Destructive! Only use this once and then discard the context. */
518 VG_STATIC
void mdl_unpack_glmesh( mdl_context
*mdl
, glmesh
*mesh
)
520 if( !mdl
->submeshs
.count
)
521 vg_fatal_exit_loop( "Tried to unpack empty model file" );
523 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, 0 );
524 u32 offset
= sm
->vertex_count
;
526 for( u32 i
=1; i
<mdl_arrcount( &mdl
->submeshs
); i
++ ){
527 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, i
);
528 u32
*indices
= mdl_arritm( &mdl
->indices
, sm
->indice_start
);
530 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
531 indices
[j
] += offset
;
533 offset
+= sm
->vertex_count
;
536 mesh_upload( mesh
, mdl
->verts
.data
, mdl
->verts
.count
,
537 mdl
->indices
.data
, mdl
->indices
.count
);
540 VG_STATIC mdl_mesh
*mdl_find_mesh( mdl_context
*mdl
, const char *name
)
542 for( u32 i
=0; i
<mdl_arrcount( &mdl
->meshs
); i
++ ){
543 mdl_mesh
*mesh
= mdl_arritm( &mdl
->meshs
, i
);
544 if( !strcmp( name
, mdl_pstr( mdl
, mesh
->pstr_name
))){