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