model fmt & heisenbug
[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 typedef struct glmesh glmesh;
11
12 typedef struct mdl_vert mdl_vert;
13 typedef struct mdl_submesh mdl_submesh;
14 typedef struct mdl_material mdl_material;
15 typedef struct mdl_node mdl_node;
16 typedef struct mdl_file_header mdl_file_header;
17 typedef struct mdl_animation mdl_animation;
18 typedef struct mdl_keyframe mdl_keyframe;
19 typedef struct mdl_context mdl_context;
20
21 #define MDL_SIZE_MAX 0x1000000
22 #define MDL_VERT_MAX 1000000
23 #define MDL_INDICE_MAX 1000000
24 #define MDL_MATERIAL_MAX 32
25 #define MDL_NODE_MAX 4000
26 #define MDL_SUBMESH_MAX 8000
27 #define MDL_STRING_LENGTH_MAX 64
28
29 enum classtype
30 {
31 k_classtype_none = 0,
32 k_classtype_gate = 1,
33 k_classtype_spawn = 3,
34 k_classtype_water = 4,
35 k_classtype_route_node = 8,
36 k_classtype_route = 9,
37 k_classtype_bone = 10,
38 k_classtype_skeleton = 11,
39 k_classtype_skin = 12,
40 k_classtype_achievement_box = 13,
41 k_classtype_audio = 14,
42 k_classtype_trigger = 15
43 };
44
45
46 #pragma pack(push,1)
47
48 struct mdl_vert
49 {
50 v3f co,
51 norm;
52 v2f uv;
53 u8 colour[4];
54 u16 weights[4];
55 u8 groups[4];
56 };
57
58 struct mdl_submesh
59 {
60 u32 indice_start,
61 indice_count,
62 vertex_start,
63 vertex_count;
64
65 boxf bbx;
66 u32 material_id;
67 };
68
69 struct mdl_material
70 {
71 u32 pstr_name;
72 };
73
74 struct mdl_node
75 {
76 v3f co;
77 v4f q;
78 v3f s;
79
80 u32 sub_uid, /* allocated in-file... too bad. */
81 submesh_start,
82 submesh_count,
83 classtype,
84 offset,
85 parent,
86 pstr_name;
87 };
88
89 struct mdl_keyframe
90 {
91 v3f co;
92 v4f q;
93 v3f s;
94 };
95
96 struct mdl_animation
97 {
98 u32 pstr_name,
99 length;
100
101 float rate;
102
103 u32 offset;
104 };
105
106 struct mdl_file_header
107 {
108 u32 identifier, version, file_length, pad0;
109
110 u32
111 node_count, node_offset,
112 submesh_count, submesh_offset,
113 material_count, material_offset,
114 anim_count, anim_offset,
115 entdata_size, entdata_offset,
116 strings_size, strings_offset,
117
118 keyframe_count, keyframe_offset,
119
120 vertex_count, vertex_offset,
121 indice_count, indice_offset;
122 };
123
124 /*
125 * Entity data structures
126 */
127
128 struct classtype_gate
129 {
130 u32 target;
131 v3f dims;
132 };
133
134 struct classtype_spawn
135 {
136 u32 pstr_alias;
137 };
138
139 struct classtype_water
140 {
141 u32 temp;
142 };
143
144 struct classtype_route_node
145 {
146 u32 target, target1;
147 };
148
149 struct classtype_route
150 {
151 u32 id_start;
152 v3f colour;
153 };
154
155 struct classtype_bone
156 {
157 u32 deform,
158 ik_target,
159 ik_pole,
160 collider,
161 use_limits;
162
163 v3f angle_limits[2];
164 boxf hitbox;
165 };
166
167 struct classtype_skeleton
168 {
169 u32 channels,
170 ik_count,
171 collider_count,
172 anim_start,
173 anim_count;
174 };
175
176 struct classtype_skin
177 {
178 u32 skeleton;
179 };
180
181 struct classtype_achievement_box
182 {
183 u32 pstr_name,
184 trigger;
185 };
186
187 struct classtype_audio
188 {
189 u32 pstr_file,
190 flags;
191
192 float volume;
193 };
194
195 #pragma pack(pop)
196
197
198 struct mdl_context
199 {
200 FILE *file;
201 mdl_file_header info;
202
203 /* each buffer becomes availible after each _load function is called */
204 mdl_node *node_buffer; /* mdl_load_metadata() */
205 mdl_submesh *submesh_buffer;
206 mdl_material *material_buffer;
207 mdl_animation *anim_buffer;
208 void *entdata_buffer;
209 const char *string_buffer;
210
211 mdl_keyframe *keyframe_buffer; /* mdl_load_anim_data() */
212
213 mdl_vert *vertex_buffer; /* mdl_load_mesh_data() */
214 u32 *index_buffer;
215 };
216
217 /*
218 * Simple mesh interface for OpenGL
219 */
220
221 struct glmesh
222 {
223 GLuint vao, vbo, ebo;
224 u32 indice_count;
225 u32 loaded;
226 };
227
228 VG_STATIC void mesh_upload( glmesh *mesh,
229 mdl_vert *verts, u32 vert_count,
230 u32 *indices, u32 indice_count )
231 {
232 //assert( mesh->loaded == 0 );
233
234 glGenVertexArrays( 1, &mesh->vao );
235 glGenBuffers( 1, &mesh->vbo );
236 glGenBuffers( 1, &mesh->ebo );
237 glBindVertexArray( mesh->vao );
238
239 size_t stride = sizeof(mdl_vert);
240
241 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
242 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
243
244 glBindVertexArray( mesh->vao );
245 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
246 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
247 indices, GL_STATIC_DRAW );
248
249 /* 0: coordinates */
250 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
251 glEnableVertexAttribArray( 0 );
252
253 /* 1: normal */
254 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
255 stride, (void *)offsetof(mdl_vert, norm) );
256 glEnableVertexAttribArray( 1 );
257
258 /* 2: uv */
259 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
260 stride, (void *)offsetof(mdl_vert, uv) );
261 glEnableVertexAttribArray( 2 );
262
263 /* 3: colour */
264 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
265 stride, (void *)offsetof(mdl_vert, colour) );
266 glEnableVertexAttribArray( 3 );
267
268 /* 4: weights */
269 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
270 stride, (void *)offsetof(mdl_vert, weights) );
271 glEnableVertexAttribArray( 4 );
272
273 /* 5: groups */
274 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
275 stride, (void *)offsetof(mdl_vert, groups) );
276 glEnableVertexAttribArray( 5 );
277
278 VG_CHECK_GL_ERR();
279
280 mesh->indice_count = indice_count;
281 mesh->loaded = 1;
282 }
283
284 VG_STATIC void mesh_bind( glmesh *mesh )
285 {
286 glBindVertexArray( mesh->vao );
287 }
288
289 VG_STATIC void mesh_drawn( u32 start, u32 count )
290 {
291 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
292 (void *)(start*sizeof(u32)) );
293 }
294
295 VG_STATIC void mesh_draw( glmesh *mesh )
296 {
297 mesh_drawn( 0, mesh->indice_count );
298 }
299
300 VG_STATIC void mesh_free( glmesh *mesh )
301 {
302 if( mesh->loaded )
303 {
304 glDeleteVertexArrays( 1, &mesh->vao );
305 glDeleteBuffers( 1, &mesh->ebo );
306 glDeleteBuffers( 1, &mesh->vbo );
307 mesh->loaded = 0;
308 }
309 }
310
311 VG_STATIC void mdl_load_fatal_corrupt( mdl_context *mdl )
312 {
313 fclose( mdl->file );
314 vg_file_print_invalid( mdl->file );
315 vg_fatal_exit_loop( "Corrupt model" );
316 }
317
318 /*
319 * Model implementation
320 *
321 * TODO.
322 *
323 * you have two api options for loading a model, first, the easy way:
324 * mdl_load ...
325 * will put the entire model straight into the linear_alloc
326 *
327 * or, to target different allocators:
328 *
329 * mdl_open
330 * mdl_load_metadata
331 * mdl_load_vertex_data
332 * mdl_load_indice_data
333 * mdl_close
334 *
335 * these should ideally be called in quick succession to limit stalls.
336 */
337
338 /*
339 * if calling mdl_open, and the file does not exist, the game will fatal quit
340 */
341 VG_STATIC void mdl_open( mdl_context *mdl, const char *path )
342 {
343 memset( mdl, 0, sizeof( mdl_context ) );
344 mdl->file = fopen( path, "rb" );
345
346 if( !mdl->file )
347 {
348 vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
349 vg_fatal_exit_loop( "see above for details" );
350 }
351
352 u64 l = fread( &mdl->info, sizeof(mdl_file_header), 1, mdl->file );
353 if( l != 1 )
354 mdl_load_fatal_corrupt( mdl );
355 }
356
357 /*
358 * Load all metadata (everything up until the large buffers). Probs at most 50k
359 */
360 VG_STATIC void mdl_load_metadata( mdl_context *mdl, void *lin_alloc )
361 {
362 assert( mdl->file );
363
364 u64 lheader = sizeof(mdl_file_header),
365 ldata = mdl->info.keyframe_offset - lheader;
366
367 void *all_data = vg_linear_alloc( lin_alloc, ldata );
368
369 fseek( mdl->file, lheader, SEEK_SET );
370 u64 l = fread( all_data, ldata, 1, mdl->file );
371
372 if( l != 1 )
373 {
374 vg_file_print_invalid( mdl->file );
375 vg_fatal_exit_loop( "Corrupt model" );
376 }
377
378 mdl->node_buffer = all_data + (mdl->info.node_offset - lheader);
379 mdl->submesh_buffer = all_data + (mdl->info.submesh_offset - lheader);
380 mdl->material_buffer = all_data + (mdl->info.material_offset - lheader);
381 mdl->anim_buffer = all_data + (mdl->info.anim_offset - lheader);
382 mdl->entdata_buffer = all_data + (mdl->info.entdata_offset - lheader);
383 mdl->string_buffer = all_data + (mdl->info.strings_offset - lheader);
384 }
385
386 /*
387 * Load just the mesh data
388 */
389 VG_STATIC void mdl_load_mesh_data( mdl_context *mdl, void *lin_alloc )
390 {
391 assert( mdl->file );
392
393 u64 size_verts = mdl->info.vertex_count * sizeof(mdl_vert),
394 size_index = mdl->info.indice_count * sizeof(u32);
395
396 mdl->vertex_buffer = vg_linear_alloc( lin_alloc, size_verts );
397 mdl->index_buffer = vg_linear_alloc( lin_alloc, size_index );
398
399 {
400 fseek( mdl->file, mdl->info.vertex_offset, SEEK_SET );
401 u64 l = fread( mdl->vertex_buffer, size_verts, 1, mdl->file );
402 if( l != 1 )
403 mdl_load_fatal_corrupt( mdl );
404 }
405 {
406 fseek( mdl->file, mdl->info.indice_offset, SEEK_SET );
407 u64 l = fread( mdl->index_buffer, size_index, 1, mdl->file );
408 if( l != 1 )
409 mdl_load_fatal_corrupt( mdl );
410 }
411 }
412
413 /*
414 * Load animation data
415 */
416 VG_STATIC void mdl_load_anim_data( mdl_context *mdl, void *lin_alloc )
417 {
418 assert( mdl->file );
419
420 if( mdl->info.keyframe_count == 0 )
421 return;
422
423 u64 size_kf = mdl->info.keyframe_count * sizeof(mdl_keyframe);
424 mdl->keyframe_buffer = vg_linear_alloc( lin_alloc, size_kf );
425
426 fseek( mdl->file, mdl->info.keyframe_offset, SEEK_SET );
427 u64 l = fread( mdl->keyframe_buffer, size_kf, 1, mdl->file );
428 if( l != 1 )
429 mdl_load_fatal_corrupt( mdl );
430 }
431
432 /*
433 * close file handle
434 */
435 VG_STATIC void mdl_close( mdl_context *mdl )
436 {
437 fclose( mdl->file );
438 mdl->file = NULL;
439 }
440
441 /* open a model */
442 VG_STATIC mdl_context *mdl_load_full( void *lin_alloc, const char *path )
443 {
444 /* Inspect the header by opening it, give us the size needed */
445 mdl_context temp_ctx;
446 mdl_open( &temp_ctx, path );
447
448 /* create allocator */
449 u32 tot_size = temp_ctx.info.file_length + sizeof( mdl_context );
450 void *data = vg_create_linear_allocator( lin_alloc, tot_size,
451 VG_MEMORY_SYSTEM );
452
453 /* copy context and load all other data */
454 mdl_context *ctx = vg_linear_alloc( data, sizeof(mdl_context) );
455 memcpy( ctx, &temp_ctx, sizeof(mdl_context) );
456
457 mdl_load_metadata( ctx, data );
458 mdl_load_anim_data( ctx, data );
459 mdl_load_mesh_data( ctx, data );
460 mdl_close( ctx );
461
462 return ctx;
463 }
464
465 /*
466 * Item getters
467 * ----------------------------------------------------------------------------
468 * TODO: Clamp access and oob errors
469 */
470 VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr )
471 {
472 return mdl->string_buffer + pstr;
473 }
474
475 VG_STATIC mdl_node *mdl_node_from_id( mdl_context *mdl, u32 id )
476 {
477 return &mdl->node_buffer[id];
478 }
479
480 VG_STATIC mdl_node *mdl_node_from_name( mdl_context *mdl, const char *name )
481 {
482 for( int i=0; i < mdl->info.node_count; i++ )
483 {
484 mdl_node *pnode = mdl_node_from_id( mdl, i );
485
486 if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) )
487 return pnode;
488 }
489
490 return NULL;
491 }
492
493 VG_STATIC mdl_submesh *mdl_node_submesh( mdl_context *mdl,
494 mdl_node *node, u32 i )
495 {
496 return &mdl->submesh_buffer[ node->submesh_start+i ];
497 }
498
499 VG_STATIC u32 *mdl_submesh_indices( mdl_context *mdl, mdl_submesh *sm )
500 {
501 return &mdl->index_buffer[ sm->indice_start ];
502 }
503
504 VG_STATIC mdl_vert *mdl_submesh_vertices( mdl_context *mdl, mdl_submesh *sm )
505 {
506 return &mdl->vertex_buffer[ sm->vertex_start ];
507 }
508
509 VG_STATIC void mdl_node_transform( mdl_node *pnode, m4x3f transform )
510 {
511 q_m3x3( pnode->q, transform );
512 v3_muls( transform[0], pnode->s[0], transform[0] );
513 v3_muls( transform[1], pnode->s[1], transform[1] );
514 v3_muls( transform[2], pnode->s[2], transform[2] );
515 v3_copy( pnode->co, transform[3] );
516 }
517
518 /* upload a mesh based on file submesh */
519 VG_STATIC void mdl_unpack_submesh( mdl_context *mdl, glmesh *mesh,
520 mdl_submesh *sm )
521 {
522 mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count,
523 mdl_submesh_indices( mdl, sm ), sm->indice_count );
524 }
525
526 /* upload entire mesh from model */
527 VG_STATIC void mdl_unpack_glmesh( mdl_context *mdl, glmesh *mesh )
528 {
529 u32 offset = mdl->submesh_buffer[0].vertex_count;
530
531 for( int i=1; i< mdl->info.submesh_count; i++ )
532 {
533 mdl_submesh *sm = &mdl->submesh_buffer[i];
534 u32 *indices = mdl_submesh_indices( mdl, sm );
535
536 for( u32 j=0; j<sm->indice_count; j++ )
537 indices[j] += offset;
538
539 offset += sm->vertex_count;
540 }
541
542 mesh_upload( mesh, mdl->vertex_buffer, mdl->info.vertex_count,
543 mdl->index_buffer, mdl->info.indice_count );
544 }
545
546 VG_STATIC void mdl_draw_submesh( mdl_submesh *sm )
547 {
548 mesh_drawn( sm->indice_start, sm->indice_count );
549 }
550
551 VG_STATIC void *mdl_get_entdata( mdl_context *mdl, mdl_node *pnode )
552 {
553 return mdl->entdata_buffer + pnode->offset;
554 }
555
556 VG_STATIC mdl_keyframe *mdl_get_animdata( mdl_context *mdl, mdl_animation *anim )
557 {
558 return mdl->keyframe_buffer + anim->offset;
559 }
560
561 VG_STATIC void mdl_link_materials( mdl_context *root, mdl_context *child )
562 {
563 u32 lookup[MDL_MATERIAL_MAX];
564
565 for( int i=0; i<child->info.material_count; i++ )
566 {
567 mdl_material *mi = &child->material_buffer[i];
568 const char *si = mdl_pstr( child, mi->pstr_name );
569
570 lookup[i] = 0;
571
572 for( int j=0; j<root->info.material_count; j++ )
573 {
574 mdl_material *mj = &root->material_buffer[j];
575 const char *sj = mdl_pstr( root, mj->pstr_name );
576
577 if( !strcmp( si, sj ) )
578 {
579 lookup[i] = j;
580 break;
581 }
582 }
583
584 if( lookup[i] == 0 && i != 0 )
585 {
586 vg_warn( "Could not link material '%s' (not present in root model)\n",
587 si );
588 }
589 }
590
591 for( int i=0; i<child->info.submesh_count; i++ )
592 {
593 mdl_submesh *sm = &child->submesh_buffer[i];
594 sm->material_id = lookup[sm->material_id];
595 }
596 }
597
598
599 #endif