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