stop air sound while paused
[carveJwlIkooP6JGAAIwe30JlM.git] / model.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef MODEL_H
6 #define MODEL_H
7
8 #include "skaterift.h"
9
10 #define MDL_VERSION_MIN 101
11 #define MDL_VERSION_NR 104
12
13 enum mdl_shader{
14 k_shader_standard = 0,
15 k_shader_standard_cutout = 1,
16 k_shader_terrain_blend = 2,
17 k_shader_standard_vertex_blend = 3,
18 k_shader_water = 4,
19 k_shader_invisible = 5,
20 k_shader_boundary = 6,
21 k_shader_fxglow = 7,
22 k_shader_cubemap = 8,
23 k_shader_walking = 9,
24 k_shader_foliage = 10,
25 k_shader_override = 30000
26 };
27
28 enum mdl_surface_prop{
29 k_surface_prop_concrete = 0,
30 k_surface_prop_wood = 1,
31 k_surface_prop_grass = 2,
32 k_surface_prop_tiles = 3,
33 k_surface_prop_metal = 4
34 };
35
36 enum material_flag{
37 k_material_flag_skate_target = 0x0001,
38 k_material_flag_collision = 0x0002,
39 k_material_flag_grow_grass = 0x0004,
40 k_material_flag_grindable = 0x0008,
41 k_material_flag_invisible = 0x0010,
42 k_material_flag_boundary = 0x0020,
43 k_material_flag_preview_visibile = 0x0040,
44 k_material_flag_walking = 0x0080,
45
46 k_material_flag_ghosts =
47 k_material_flag_boundary|
48 k_material_flag_invisible|
49 k_material_flag_walking
50 };
51
52 #pragma pack(push,1)
53
54 /* 48 byte */
55 struct mdl_vert
56 {
57 v3f co, /* 3*32 */
58 norm; /* 3*32 */
59 v2f uv; /* 2*32 */
60
61 u8 colour[4]; /* 4*8 */
62 u16 weights[4];/* 4*16 */
63 u8 groups[4]; /* 4*8 */
64 };
65
66 #pragma pack(pop)
67
68 typedef u32 mdl_indice;
69
70 typedef struct mdl_context mdl_context;
71 typedef struct mdl_array_ptr mdl_array_ptr;
72 typedef struct mdl_vert mdl_vert;
73 typedef struct mdl_transform mdl_transform;
74 typedef struct mdl_submesh mdl_submesh;
75 typedef struct mdl_material mdl_material;
76 typedef struct mdl_bone mdl_bone;
77 typedef struct mdl_armature mdl_armature;
78 typedef struct mdl_animation mdl_animation;
79 typedef struct mdl_transform mdl_keyframe;
80 typedef struct mdl_mesh mdl_mesh;
81 typedef struct mdl_file mdl_file;
82 typedef struct mdl_texture mdl_texture;
83 typedef struct mdl_array mdl_array;
84 typedef struct mdl_header mdl_header;
85
86 typedef struct glmesh glmesh;
87 struct glmesh
88 {
89 GLuint vao, vbo, ebo;
90 u32 indice_count;
91 u32 loaded;
92 };
93
94 struct mdl_transform
95 {
96 v3f co, s;
97 v4f q;
98 };
99
100 static void transform_identity( mdl_transform *transform )
101 {
102 v3_zero( transform->co );
103 q_identity( transform->q );
104 v3_fill( transform->s, 1.0f );
105 }
106
107 static void mdl_transform_vector( mdl_transform *transform, v3f vec, v3f dest )
108 {
109 v3_mul( transform->s, vec, dest );
110 q_mulv( transform->q, dest, dest );
111 }
112
113 static void mdl_transform_point( mdl_transform *transform, v3f co, v3f dest )
114 {
115 mdl_transform_vector( transform, co, dest );
116 v3_add( transform->co, dest, dest );
117 }
118
119 static void mdl_transform_mul( mdl_transform *a, mdl_transform *b,
120 mdl_transform *d )
121 {
122 mdl_transform_point( a, b->co, d->co );
123 q_mul( a->q, b->q, d->q );
124 q_normalize( d->q );
125 v3_mul( a->s, b->s, d->s );
126 }
127
128 struct mdl_material
129 {
130 u32 pstr_name,
131 shader,
132 flags,
133 surface_prop;
134
135 v4f colour,
136 colour1;
137
138 u32 tex_diffuse,
139 tex_none0,
140 tex_none1;
141 };
142
143 struct mdl_bone
144 {
145 v3f co, end;
146 u32 parent,
147 collider,
148 ik_target,
149 ik_pole,
150 flags,
151 pstr_name;
152
153 boxf hitbox;
154 v3f conevx, conevy, coneva;
155 float conet;
156 };
157
158 enum bone_flag
159 {
160 k_bone_flag_deform = 0x00000001,
161 k_bone_flag_ik = 0x00000002,
162 k_bone_flag_cone_constraint = 0x00000004
163 };
164
165 enum bone_collider
166 {
167 k_bone_collider_none = 0,
168 k_bone_collider_box = 1,
169 k_bone_collider_capsule = 2
170 };
171
172 struct mdl_armature
173 {
174 mdl_transform transform;
175 u32 bone_start,
176 bone_count,
177 anim_start,
178 anim_count;
179 };
180
181 struct mdl_animation
182 {
183 u32 pstr_name,
184 length;
185 float rate;
186 u32 offset;
187 };
188
189 struct mdl_submesh
190 {
191 u32 indice_start,
192 indice_count,
193 vertex_start,
194 vertex_count;
195
196 boxf bbx;
197 u16 material_id, flags;
198 };
199
200 enum esubmesh_flags
201 {
202 k_submesh_flag_none = 0x0000,
203 k_submesh_flag_consumed = 0x0001
204 };
205
206 struct mdl_mesh
207 {
208 mdl_transform transform;
209 u32 submesh_start,
210 submesh_count,
211 pstr_name,
212 entity_id, /* upper 16 bits: type, lower 16 bits: index */
213 armature_id;
214 };
215
216 struct mdl_file
217 {
218 u32 pstr_path,
219 pack_offset,
220 pack_size;
221 };
222
223 struct mdl_texture
224 {
225 mdl_file file;
226 u32 glname;
227 };
228
229 struct mdl_array
230 {
231 u32 file_offset,
232 item_count,
233 item_size;
234
235 char name[16];
236 };
237
238 struct mdl_header
239 {
240 u32 version;
241 mdl_array index;
242 };
243
244 struct mdl_context{
245 FILE *file;
246 mdl_header info;
247
248 struct mdl_array_ptr{
249 void *data;
250 u32 count, stride;
251 }
252 index,
253
254 /* metadata */
255 strings,
256 meshs,
257 submeshs,
258 materials,
259 textures,
260 armatures,
261 bones,
262 animations,
263
264 /* animation buffers */
265 keyframes,
266
267 /* mesh buffers */
268 verts,
269 indices;
270 u32 pack_base_offset;
271
272 /* runtime */
273 glmesh mesh;
274 };
275
276
277 static void mdl_load_fatal_corrupt( mdl_context *mdl )
278 {
279 fclose( mdl->file );
280 vg_file_print_invalid( mdl->file );
281 vg_fatal_error( "Corrupt model" );
282 }
283
284 /*
285 * Model implementation
286 */
287
288 static const char *mdl_pstr( mdl_context *mdl, u32 pstr );
289 static
290 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
291 {
292 if( !info->pack_size ){
293 vg_warn( "path: %s\n", mdl_pstr( mdl, info->pstr_path ) );
294 vg_fatal_error( "Packed file is only a header; it is not packed" );
295 }
296
297 fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET );
298 u64 l = fread( dst, info->pack_size, 1, mdl->file );
299
300 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
301 }
302
303 /* TODO: Rename these */
304 static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
305 void *buffer, u32 stride )
306 {
307 if( arr->item_count ){
308 fseek( mdl->file, arr->file_offset, SEEK_SET );
309
310 if( stride == arr->item_size ){
311 u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
312 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
313 }
314 else {
315 vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n",
316 buffer, arr->item_size, stride, arr->item_count );
317 if( stride < arr->item_size )
318 vg_fatal_error( "not safe\n" );
319
320 for( u32 i=0; i<arr->item_count; i++ ){
321 u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
322 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
323 }
324 }
325 }
326 }
327
328 static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
329 mdl_array *arr, void *lin_alloc, u32 stride )
330 {
331 if( stride < arr->item_size ){
332 vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size );
333 vg_fatal_error( "not safe\n" );
334 }
335
336 if( arr->item_count ){
337 u32 size = stride*arr->item_count;
338 ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
339 mdl_load_array_file_buffer( mdl, arr, ptr->data, stride );
340 }
341 else{
342 ptr->data = NULL;
343 }
344
345 ptr->stride = stride;
346 ptr->count = arr->item_count;
347 }
348
349 static void *mdl_arritm( mdl_array_ptr *arr, u32 index )
350 {
351 return ((u8 *)arr->data) + index*arr->stride;
352 }
353
354 static u32 mdl_arrcount( mdl_array_ptr *arr )
355 {
356 return arr->count;
357 }
358
359 static mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
360 {
361 for( u32 i=0; i<mdl_arrcount(&mdl->index); i++ ){
362 mdl_array *arr = mdl_arritm( &mdl->index, i );
363
364 if( !strncmp(arr->name,name,16) ){
365 return arr;
366 }
367 }
368
369 return NULL;
370 }
371
372 static int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
373 const char *name, void *lin_alloc, u32 stride )
374 {
375 mdl_array *arr = mdl_find_array( mdl, name );
376
377 if( arr ){
378 mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride );
379 return 1;
380 }
381 else{
382 ptr->data = NULL;
383 ptr->count = 0;
384 ptr->stride = 0;
385 return 0;
386 }
387 }
388
389 #define MDL_LOAD_ARRAY( MDL, PTR, STRUCT, ALLOCATOR ) \
390 _mdl_load_array( MDL, PTR, #STRUCT, ALLOCATOR, sizeof(STRUCT) )
391
392 static int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc ){
393 int success = 1;
394
395 success &= MDL_LOAD_ARRAY( mdl, &mdl->verts, mdl_vert, lin_alloc );
396 success &= MDL_LOAD_ARRAY( mdl, &mdl->indices, mdl_indice, lin_alloc );
397
398 return success;
399 }
400
401 static int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc ){
402 int success = 1;
403
404 success &= _mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc, 1 );
405 success &= MDL_LOAD_ARRAY( mdl, &mdl->meshs, mdl_mesh, lin_alloc );
406 success &= MDL_LOAD_ARRAY( mdl, &mdl->submeshs, mdl_submesh, lin_alloc );
407 success &= MDL_LOAD_ARRAY( mdl, &mdl->materials, mdl_material, lin_alloc );
408 success &= MDL_LOAD_ARRAY( mdl, &mdl->textures, mdl_texture, lin_alloc );
409 success &= MDL_LOAD_ARRAY( mdl, &mdl->armatures, mdl_armature, lin_alloc );
410 success &= MDL_LOAD_ARRAY( mdl, &mdl->bones, mdl_bone, lin_alloc );
411 success &= MDL_LOAD_ARRAY( mdl, &mdl->animations,mdl_animation,lin_alloc );
412
413 return success;
414 }
415
416 static int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc ){
417 return MDL_LOAD_ARRAY( mdl, &mdl->keyframes, mdl_keyframe, lin_alloc );
418 }
419
420 /*
421 * if calling mdl_open, and the file does not exist, the game will fatal quit
422 */
423 static void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc )
424 {
425 memset( mdl, 0, sizeof( mdl_context ) );
426 mdl->file = fopen( path, "rb" );
427
428 if( !mdl->file ){
429 vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
430 vg_fatal_error( "see above for details" );
431 }
432
433 u64 l = fread( &mdl->info, sizeof(mdl_header), 1, mdl->file );
434 if( l != 1 )
435 mdl_load_fatal_corrupt( mdl );
436
437 if( mdl->info.version < MDL_VERSION_MIN ){
438 vg_warn( "For model: %s\n", path );
439 vg_warn( " version: %u (min: %u, current: %u)\n",
440 mdl->info.version, MDL_VERSION_MIN, MDL_VERSION_NR );
441
442 vg_fatal_error( "Legacy model version incompatable" );
443 }
444
445 mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc,
446 sizeof(mdl_array) );
447
448 mdl_array *pack = mdl_find_array( mdl, "pack" );
449 if( pack ) mdl->pack_base_offset = pack->file_offset;
450 else mdl->pack_base_offset = 0;
451 }
452
453 /*
454 * close file handle
455 */
456 static void mdl_close( mdl_context *mdl )
457 {
458 fclose( mdl->file );
459 mdl->file = NULL;
460 }
461
462 /* useful things you can do with the model */
463
464 static void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx )
465 {
466 q_m3x3( transform->q, mtx );
467 v3_muls( mtx[0], transform->s[0], mtx[0] );
468 v3_muls( mtx[1], transform->s[1], mtx[1] );
469 v3_muls( mtx[2], transform->s[2], mtx[2] );
470 v3_copy( transform->co, mtx[3] );
471 }
472
473 static const char *mdl_pstr( mdl_context *mdl, u32 pstr )
474 {
475 return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4;
476 }
477
478
479 static int
480 mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 )
481 {
482 u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr ));
483 if( hash == djb2 ){
484 if( !strcmp( str, mdl_pstr( mdl, pstr ))) return 1;
485 else return 0;
486 }
487 else return 0;
488 }
489
490 #define MDL_CONST_PSTREQ( MDL, Q, CONSTSTR )\
491 mdl_pstreq( MDL, Q, CONSTSTR, vg_strdjb2( CONSTSTR ) )
492
493 /*
494 * Simple mesh interface for OpenGL
495 * ----------------------------------------------------------------------------
496 */
497
498 static void mesh_upload( glmesh *mesh,
499 mdl_vert *verts, u32 vert_count,
500 u32 *indices, u32 indice_count )
501 {
502 //assert( mesh->loaded == 0 );
503
504 glGenVertexArrays( 1, &mesh->vao );
505 glGenBuffers( 1, &mesh->vbo );
506 glGenBuffers( 1, &mesh->ebo );
507 glBindVertexArray( mesh->vao );
508
509 size_t stride = sizeof(mdl_vert);
510
511 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
512 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
513
514 glBindVertexArray( mesh->vao );
515 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
516 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
517 indices, GL_STATIC_DRAW );
518
519 /* 0: coordinates */
520 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
521 glEnableVertexAttribArray( 0 );
522
523 /* 1: normal */
524 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
525 stride, (void *)offsetof(mdl_vert, norm) );
526 glEnableVertexAttribArray( 1 );
527
528 /* 2: uv */
529 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
530 stride, (void *)offsetof(mdl_vert, uv) );
531 glEnableVertexAttribArray( 2 );
532
533 /* 3: colour */
534 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
535 stride, (void *)offsetof(mdl_vert, colour) );
536 glEnableVertexAttribArray( 3 );
537
538 /* 4: weights */
539 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
540 stride, (void *)offsetof(mdl_vert, weights) );
541 glEnableVertexAttribArray( 4 );
542
543 /* 5: groups */
544 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
545 stride, (void *)offsetof(mdl_vert, groups) );
546 glEnableVertexAttribArray( 5 );
547
548 VG_CHECK_GL_ERR();
549
550 mesh->indice_count = indice_count;
551 mesh->loaded = 1;
552 }
553
554 static void mesh_bind( glmesh *mesh )
555 {
556 glBindVertexArray( mesh->vao );
557 }
558
559 static void mesh_drawn( u32 start, u32 count )
560 {
561 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
562 (void *)(start*sizeof(u32)) );
563 }
564
565 static void mesh_draw( glmesh *mesh )
566 {
567 mesh_drawn( 0, mesh->indice_count );
568 }
569
570 static void mesh_free( glmesh *mesh )
571 {
572 if( mesh->loaded ){
573 glDeleteVertexArrays( 1, &mesh->vao );
574 glDeleteBuffers( 1, &mesh->ebo );
575 glDeleteBuffers( 1, &mesh->vbo );
576 mesh->loaded = 0;
577 }
578 }
579
580 static void mdl_draw_submesh( mdl_submesh *sm )
581 {
582 mesh_drawn( sm->indice_start, sm->indice_count );
583 }
584
585 static mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name )
586 {
587 for( u32 i=0; i<mdl_arrcount( &mdl->meshs ); i++ ){
588 mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
589 if( !strcmp( name, mdl_pstr( mdl, mesh->pstr_name ))){
590 return mesh;
591 }
592 }
593 return NULL;
594 }
595
596 struct payload_glmesh_load{
597 mdl_vert *verts;
598 u32 *indices;
599
600 u32 vertex_count,
601 indice_count;
602
603 glmesh *mesh;
604 };
605
606 static void async_mdl_load_glmesh( void *payload, u32 size )
607 {
608 struct payload_glmesh_load *job = payload;
609 mesh_upload( job->mesh, job->verts, job->vertex_count,
610 job->indices, job->indice_count );
611 }
612
613 static void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh,
614 u32 *fixup_table ){
615 mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" );
616 mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" );
617
618 if( arr_vertices && arr_indices ){
619 u32 size_verts = vg_align8(sizeof(mdl_vert)*arr_vertices->item_count),
620 size_indices = vg_align8(sizeof(mdl_indice)*arr_indices->item_count),
621 size_hdr = vg_align8(sizeof(struct payload_glmesh_load)),
622 total = size_hdr + size_verts + size_indices;
623
624 vg_async_item *call = vg_async_alloc( total );
625 struct payload_glmesh_load *job = call->payload;
626
627 u8 *payload = call->payload;
628
629 job->mesh = mesh;
630 job->verts = (void*)(payload + size_hdr);
631 job->indices = (void*)(payload + size_hdr + size_verts);
632 job->vertex_count = arr_vertices->item_count;
633 job->indice_count = arr_indices->item_count;
634
635 mdl_load_array_file_buffer( mdl, arr_vertices,
636 job->verts, sizeof(mdl_vert) );
637 mdl_load_array_file_buffer( mdl, arr_indices, job->indices,
638 sizeof(mdl_indice) );
639
640 if( fixup_table ){
641 for( u32 i=0; i<job->vertex_count; i ++ ){
642 mdl_vert *vert = &job->verts[i];
643
644 for( u32 j=0; j<4; j++ ){
645 vert->groups[j] = fixup_table[vert->groups[j]];
646 }
647 }
648 }
649
650 /*
651 * Unpack the indices (if there are meshes)
652 * ---------------------------------------------------------
653 */
654
655 if( mdl_arrcount( &mdl->submeshs ) ){
656 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 );
657 u32 offset = sm->vertex_count;
658
659 for( u32 i=1; i<mdl_arrcount( &mdl->submeshs ); i++ ){
660 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i );
661 u32 *indices = job->indices + sm->indice_start;
662
663 for( u32 j=0; j<sm->indice_count; j++ )
664 indices[j] += offset;
665
666 offset += sm->vertex_count;
667 }
668 }
669
670 /*
671 * Dispatch
672 * -------------------------
673 */
674
675 vg_async_dispatch( call, async_mdl_load_glmesh );
676 }
677 else{
678 vg_fatal_error( "no vertex/indice data\n" );
679 }
680 }
681
682 /* uploads the glmesh, and textures. everything is saved into the mdl_context */
683 static void mdl_async_full_load_std( mdl_context *mdl ){
684 mdl_async_load_glmesh( mdl, &mdl->mesh, NULL );
685
686 for( u32 i=0; i<mdl_arrcount( &mdl->textures ); i ++ ){
687 vg_linear_clear( vg_mem.scratch );
688 mdl_texture *tex = mdl_arritm( &mdl->textures, i );
689
690 void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size );
691 mdl_fread_pack_file( mdl, &tex->file, data );
692
693 vg_tex2d_load_qoi_async( data, tex->file.pack_size,
694 VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, &tex->glname );
695 }
696 }
697
698 #endif