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