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