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