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