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