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