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
;
80 static void transform_identity( mdl_transform
*transform
)
82 v3_zero( transform
->co
);
83 q_identity( transform
->q
);
84 v3_fill( transform
->s
, 1.0f
);
87 static void mdl_transform_vector( mdl_transform
*transform
, v3f vec
, v3f dest
)
89 v3_mul( transform
->s
, vec
, dest
);
90 q_mulv( transform
->q
, dest
, dest
);
93 static void mdl_transform_point( mdl_transform
*transform
, v3f co
, v3f dest
)
95 mdl_transform_vector( transform
, co
, dest
);
96 v3_add( transform
->co
, dest
, dest
);
99 static void mdl_transform_mul( mdl_transform
*a
, mdl_transform
*b
,
102 mdl_transform_point( a
, b
->co
, d
->co
);
103 q_mul( a
->q
, b
->q
, d
->q
);
105 v3_mul( a
->s
, b
->s
, d
->s
);
134 v3f conevx
, conevy
, coneva
;
140 k_bone_flag_deform
= 0x00000001,
141 k_bone_flag_ik
= 0x00000002,
142 k_bone_flag_cone_constraint
= 0x00000004
147 k_bone_collider_none
= 0,
148 k_bone_collider_box
= 1,
149 k_bone_collider_capsule
= 2
154 mdl_transform transform
;
177 u16 material_id
, flags
;
182 k_submesh_flag_none
= 0x0000,
183 k_submesh_flag_consumed
= 0x0001
188 mdl_transform transform
;
192 entity_id
, /* upper 16 bits: type, lower 16 bits: index */
246 /* animation buffers */
253 u32 pack_base_offset
;
260 VG_STATIC
void mdl_load_fatal_corrupt( mdl_context
*mdl
)
263 vg_file_print_invalid( mdl
->file
);
264 vg_fatal_error( "Corrupt model" );
268 * Model implementation
271 VG_STATIC u32
mdl_query_array_size( mdl_array
*arr
)
273 if( arr
->item_count
){
274 u32 size
= arr
->item_size
*arr
->item_count
;
275 return vg_align8(size
);
281 VG_STATIC
const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
);
283 void mdl_fread_pack_file( mdl_context
*mdl
, mdl_file
*info
, void *dst
)
285 if( !info
->pack_size
){
286 vg_warn( "path: %s\n", mdl_pstr( mdl
, info
->pstr_path
) );
287 vg_fatal_error( "Packed file is only a header; it is not packed" );
290 fseek( mdl
->file
, mdl
->pack_base_offset
+info
->pack_offset
, SEEK_SET
);
291 u64 l
= fread( dst
, info
->pack_size
, 1, mdl
->file
);
294 mdl_load_fatal_corrupt( mdl
);
297 /* TODO: Rename these */
299 void mdl_load_array_file_buffer( mdl_context
*mdl
, mdl_array
*arr
,
302 if( arr
->item_count
){
303 fseek( mdl
->file
, arr
->file_offset
, SEEK_SET
);
304 u64 l
= fread( buffer
, arr
->item_size
*arr
->item_count
, 1, mdl
->file
);
307 mdl_load_fatal_corrupt( mdl
);
311 VG_STATIC
void mdl_load_array_file( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
312 mdl_array
*arr
, void *lin_alloc
)
314 if( arr
->item_count
){
315 u32 size
= arr
->item_size
*arr
->item_count
;
316 ptr
->data
= vg_linear_alloc( lin_alloc
, vg_align8(size
) );
317 mdl_load_array_file_buffer( mdl
, arr
, ptr
->data
);
322 ptr
->count
= arr
->item_count
;
323 ptr
->stride
= arr
->item_size
;
326 VG_STATIC
void *mdl_arritm( mdl_array_ptr
*arr
, u32 index
)
328 return ((u8
*)arr
->data
) + index
*arr
->stride
;
331 VG_STATIC u32
mdl_arrcount( mdl_array_ptr
*arr
)
336 VG_STATIC mdl_array
*mdl_find_array( mdl_context
*mdl
, const char *name
)
338 for( u32 i
=0; i
<mdl_arrcount(&mdl
->index
); i
++ ){
339 mdl_array
*arr
= mdl_arritm( &mdl
->index
, i
);
341 if( !strncmp(arr
->name
,name
,16) ){
349 VG_STATIC
int mdl_load_array( mdl_context
*mdl
, mdl_array_ptr
*ptr
,
350 const char *name
, void *lin_alloc
)
352 mdl_array
*arr
= mdl_find_array( mdl
, name
);
355 mdl_load_array_file( mdl
, ptr
, arr
, lin_alloc
);
366 VG_STATIC
int mdl_load_mesh_block( mdl_context
*mdl
, void *lin_alloc
)
370 success
&= mdl_load_array( mdl
, &mdl
->verts
, "mdl_vert", lin_alloc
);
371 success
&= mdl_load_array( mdl
, &mdl
->indices
, "mdl_indice", lin_alloc
);
376 VG_STATIC
int mdl_load_metadata_block( mdl_context
*mdl
, void *lin_alloc
)
380 success
&= mdl_load_array( mdl
, &mdl
->strings
, "strings", lin_alloc
);
381 success
&= mdl_load_array( mdl
, &mdl
->meshs
, "mdl_mesh", lin_alloc
);
382 success
&= mdl_load_array( mdl
, &mdl
->submeshs
, "mdl_submesh", lin_alloc
);
383 success
&= mdl_load_array( mdl
, &mdl
->materials
, "mdl_material", lin_alloc
);
384 success
&= mdl_load_array( mdl
, &mdl
->textures
, "mdl_texture", lin_alloc
);
385 success
&= mdl_load_array( mdl
, &mdl
->armatures
, "mdl_armature", lin_alloc
);
386 success
&= mdl_load_array( mdl
, &mdl
->bones
, "mdl_bone", lin_alloc
);
387 success
&= mdl_load_array( mdl
, &mdl
->animations
,"mdl_animation",lin_alloc
);
392 VG_STATIC
int mdl_load_animation_block( mdl_context
*mdl
, void *lin_alloc
)
394 return mdl_load_array( mdl
, &mdl
->keyframes
, "mdl_keyframe", lin_alloc
);
398 * if calling mdl_open, and the file does not exist, the game will fatal quit
400 VG_STATIC
void mdl_open( mdl_context
*mdl
, const char *path
, void *lin_alloc
)
402 memset( mdl
, 0, sizeof( mdl_context
) );
403 mdl
->file
= fopen( path
, "rb" );
406 vg_error( "mdl_open('%s'): %s\n", path
, strerror(errno
) );
407 vg_fatal_error( "see above for details" );
410 u64 l
= fread( &mdl
->info
, sizeof(mdl_header
), 1, mdl
->file
);
412 mdl_load_fatal_corrupt( mdl
);
414 if( mdl
->info
.version
!= MDL_VERSION_NR
){
415 vg_warn( "For model: %s\n", path
);
416 vg_warn( " version: %u (current: %u)\n", mdl
->info
.version
,
419 vg_fatal_error( "Legacy model version incompatable" );
422 mdl_load_array_file( mdl
, &mdl
->index
, &mdl
->info
.index
, lin_alloc
);
424 mdl_array
*pack
= mdl_find_array( mdl
, "pack" );
425 if( pack
) mdl
->pack_base_offset
= pack
->file_offset
;
426 else mdl
->pack_base_offset
= 0;
432 VG_STATIC
void mdl_close( mdl_context
*mdl
)
438 /* useful things you can do with the model */
440 VG_STATIC
void mdl_transform_m4x3( mdl_transform
*transform
, m4x3f mtx
)
442 q_m3x3( transform
->q
, mtx
);
443 v3_muls( mtx
[0], transform
->s
[0], mtx
[0] );
444 v3_muls( mtx
[1], transform
->s
[1], mtx
[1] );
445 v3_muls( mtx
[2], transform
->s
[2], mtx
[2] );
446 v3_copy( transform
->co
, mtx
[3] );
449 VG_STATIC
const char *mdl_pstr( mdl_context
*mdl
, u32 pstr
)
451 return mdl_arritm( &mdl
->strings
, pstr
);
455 * Simple mesh interface for OpenGL
456 * ----------------------------------------------------------------------------
459 typedef struct glmesh glmesh
;
462 GLuint vao
, vbo
, ebo
;
467 VG_STATIC
void mesh_upload( glmesh
*mesh
,
468 mdl_vert
*verts
, u32 vert_count
,
469 u32
*indices
, u32 indice_count
)
471 //assert( mesh->loaded == 0 );
473 glGenVertexArrays( 1, &mesh
->vao
);
474 glGenBuffers( 1, &mesh
->vbo
);
475 glGenBuffers( 1, &mesh
->ebo
);
476 glBindVertexArray( mesh
->vao
);
478 size_t stride
= sizeof(mdl_vert
);
480 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
481 glBufferData( GL_ARRAY_BUFFER
, vert_count
*stride
, verts
, GL_STATIC_DRAW
);
483 glBindVertexArray( mesh
->vao
);
484 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
485 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, indice_count
*sizeof(u32
),
486 indices
, GL_STATIC_DRAW
);
489 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
490 glEnableVertexAttribArray( 0 );
493 glVertexAttribPointer( 1, 3, GL_FLOAT
, GL_FALSE
,
494 stride
, (void *)offsetof(mdl_vert
, norm
) );
495 glEnableVertexAttribArray( 1 );
498 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
499 stride
, (void *)offsetof(mdl_vert
, uv
) );
500 glEnableVertexAttribArray( 2 );
503 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
504 stride
, (void *)offsetof(mdl_vert
, colour
) );
505 glEnableVertexAttribArray( 3 );
508 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT
, GL_TRUE
,
509 stride
, (void *)offsetof(mdl_vert
, weights
) );
510 glEnableVertexAttribArray( 4 );
513 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE
,
514 stride
, (void *)offsetof(mdl_vert
, groups
) );
515 glEnableVertexAttribArray( 5 );
519 mesh
->indice_count
= indice_count
;
523 VG_STATIC
void mesh_bind( glmesh
*mesh
)
525 glBindVertexArray( mesh
->vao
);
528 VG_STATIC
void mesh_drawn( u32 start
, u32 count
)
530 glDrawElements( GL_TRIANGLES
, count
, GL_UNSIGNED_INT
,
531 (void *)(start
*sizeof(u32
)) );
534 VG_STATIC
void mesh_draw( glmesh
*mesh
)
536 mesh_drawn( 0, mesh
->indice_count
);
539 VG_STATIC
void mesh_free( glmesh
*mesh
)
542 glDeleteVertexArrays( 1, &mesh
->vao
);
543 glDeleteBuffers( 1, &mesh
->ebo
);
544 glDeleteBuffers( 1, &mesh
->vbo
);
549 VG_STATIC
void mdl_draw_submesh( mdl_submesh
*sm
)
551 mesh_drawn( sm
->indice_start
, sm
->indice_count
);
554 VG_STATIC mdl_mesh
*mdl_find_mesh( mdl_context
*mdl
, const char *name
)
556 for( u32 i
=0; i
<mdl_arrcount( &mdl
->meshs
); i
++ ){
557 mdl_mesh
*mesh
= mdl_arritm( &mdl
->meshs
, i
);
558 if( !strcmp( name
, mdl_pstr( mdl
, mesh
->pstr_name
))){
565 struct payload_glmesh_load
{
575 VG_STATIC
void async_mdl_load_glmesh( void *payload
, u32 size
)
577 struct payload_glmesh_load
*job
= payload
;
579 mesh_upload( job
->mesh
, job
->verts
, job
->vertex_count
,
580 job
->indices
, job
->indice_count
);
583 VG_STATIC
void mdl_async_load_glmesh( mdl_context
*mdl
, glmesh
*mesh
)
585 mdl_array
*arr_vertices
= mdl_find_array( mdl
, "mdl_vert" );
586 mdl_array
*arr_indices
= mdl_find_array( mdl
, "mdl_indice" );
588 if( arr_vertices
&& arr_indices
){
589 u32 size_verts
= vg_align8(mdl_query_array_size( arr_vertices
)),
590 size_indices
= vg_align8(mdl_query_array_size( arr_indices
)),
591 size_hdr
= vg_align8(sizeof(struct payload_glmesh_load
)),
592 total
= size_hdr
+ size_verts
+ size_indices
;
594 vg_async_item
*call
= vg_async_alloc( total
);
595 struct payload_glmesh_load
*job
= call
->payload
;
597 u8
*payload
= call
->payload
;
600 job
->verts
= (void*)(payload
+ size_hdr
);
601 job
->indices
= (void*)(payload
+ size_hdr
+ size_verts
);
602 job
->vertex_count
= arr_vertices
->item_count
;
603 job
->indice_count
= arr_indices
->item_count
;
605 mdl_load_array_file_buffer( mdl
, arr_vertices
, job
->verts
);
606 mdl_load_array_file_buffer( mdl
, arr_indices
, job
->indices
);
609 * Unpack the indices (if there are meshes)
610 * ---------------------------------------------------------
613 if( mdl_arrcount( &mdl
->submeshs
) ){
614 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, 0 );
615 u32 offset
= sm
->vertex_count
;
617 for( u32 i
=1; i
<mdl_arrcount( &mdl
->submeshs
); i
++ ){
618 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, i
);
619 u32
*indices
= job
->indices
+ sm
->indice_start
;
621 for( u32 j
=0; j
<sm
->indice_count
; j
++ )
622 indices
[j
] += offset
;
624 offset
+= sm
->vertex_count
;
630 * -------------------------
633 vg_async_dispatch( call
, async_mdl_load_glmesh
);
636 vg_fatal_error( "no vertex/indice data\n" );