oh yeah mr crabs
[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_header mdl_header;
17 typedef struct mdl_animation mdl_animation;
18 typedef struct mdl_keyframe mdl_keyframe;
19
20 #define MDL_SIZE_MAX 0x1000000
21 #define MDL_VERT_MAX 1000000
22 #define MDL_INDICE_MAX 1000000
23 #define MDL_MATERIAL_MAX 32
24 #define MDL_NODE_MAX 4000
25 #define MDL_SUBMESH_MAX 8000
26 #define MDL_STRING_LENGTH_MAX 64
27
28 enum classtype
29 {
30 k_classtype_none = 0,
31 k_classtype_gate = 1,
32 k_classtype_block = 2,
33 k_classtype_spawn = 3,
34 k_classtype_water = 4,
35 k_classtype_car_path = 5,
36 k_classtype_instance = 6,
37 k_classtype_capsule = 7,
38 k_classtype_route_node = 8,
39 k_classtype_route = 9,
40 k_classtype_bone = 10,
41 k_classtype_skeleton = 11,
42 k_classtype_skin = 12,
43 k_classtype_achievement_box = 13,
44 k_classtype_audio = 14
45 };
46
47
48 #pragma pack(push,1)
49
50 struct mdl_vert
51 {
52 v3f co,
53 norm;
54 v2f uv;
55 u8 colour[4];
56 u16 weights[4];
57 u8 groups[4];
58 };
59
60 struct mdl_submesh
61 {
62 u32 indice_start,
63 indice_count,
64 vertex_start,
65 vertex_count;
66
67 boxf bbx;
68 u32 material_id;
69 };
70
71 struct mdl_material
72 {
73 u32 pstr_name;
74 };
75
76 struct mdl_node
77 {
78 v3f co;
79 v4f q;
80 v3f s;
81
82 union{ u32 submesh_start, sub_uid; };
83
84 u32
85 submesh_count,
86 classtype,
87 offset,
88 parent,
89 pstr_name;
90 };
91
92 struct mdl_keyframe
93 {
94 v3f co;
95 v4f q;
96 v3f s;
97 };
98
99 struct mdl_animation
100 {
101 u32 pstr_name,
102 length;
103
104 float rate;
105
106 u32 offset;
107 };
108
109 struct mdl_header
110 {
111 u32 identifier, version, file_length;
112
113 u32 vertex_count, vertex_offset,
114 indice_count, indice_offset,
115 submesh_count, submesh_offset,
116 material_count, material_offset,
117 node_count, node_offset,
118 anim_count, anim_offset,
119 strings_offset, entdata_offset, animdata_offset;
120 };
121
122 /*
123 * Entity data structures
124 */
125
126 struct classtype_block
127 {
128 boxf bbx;
129 };
130
131 struct classtype_gate
132 {
133 u32 target;
134 v3f dims;
135 };
136
137 struct classtype_spawn
138 {
139 u32 target;
140 };
141
142 struct classtype_water
143 {
144 u32 temp;
145 };
146
147 struct classtype_car_path
148 {
149 u32 target, target1;
150 };
151
152 struct classtype_instance
153 {
154 u32 pstr_file;
155 };
156
157 struct classtype_capsule
158 {
159 float height, radius;
160 };
161
162 struct classtype_route_node
163 {
164 u32 target, target1;
165 };
166
167 struct classtype_route
168 {
169 u32 id_start;
170 v3f colour;
171 };
172
173 struct classtype_bone
174 {
175 u32 deform,
176 ik_target,
177 ik_pole,
178 collider,
179 use_limits;
180
181 v3f angle_limits[2];
182 boxf hitbox;
183 };
184
185 struct classtype_skeleton
186 {
187 u32 channels,
188 ik_count,
189 collider_count,
190 anim_start,
191 anim_count;
192 };
193
194 struct classtype_skin
195 {
196 u32 skeleton;
197 };
198
199 struct classtype_achievement_box
200 {
201 u32 pstr_name,
202 trigger;
203 };
204
205 struct classtype_audio
206 {
207 u32 pstr_file,
208 flags;
209
210 float volume;
211 };
212
213 #pragma pack(pop)
214
215 /*
216 * Simple mesh interface for OpenGL
217 */
218
219 struct glmesh
220 {
221 GLuint vao, vbo, ebo;
222 u32 indice_count;
223 u32 loaded;
224 };
225
226 static void mesh_upload( glmesh *mesh,
227 mdl_vert *verts, u32 vert_count,
228 u32 *indices, u32 indice_count )
229 {
230 //assert( mesh->loaded == 0 );
231
232 glGenVertexArrays( 1, &mesh->vao );
233 glGenBuffers( 1, &mesh->vbo );
234 glGenBuffers( 1, &mesh->ebo );
235 glBindVertexArray( mesh->vao );
236
237 size_t stride = sizeof(mdl_vert);
238
239 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
240 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
241
242 glBindVertexArray( mesh->vao );
243 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
244 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
245 indices, GL_STATIC_DRAW );
246
247 /* 0: coordinates */
248 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
249 glEnableVertexAttribArray( 0 );
250
251 /* 1: normal */
252 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
253 stride, (void *)offsetof(mdl_vert, norm) );
254 glEnableVertexAttribArray( 1 );
255
256 /* 2: uv */
257 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
258 stride, (void *)offsetof(mdl_vert, uv) );
259 glEnableVertexAttribArray( 2 );
260
261 /* 3: colour */
262 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
263 stride, (void *)offsetof(mdl_vert, colour) );
264 glEnableVertexAttribArray( 3 );
265
266 /* 4: weights */
267 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
268 stride, (void *)offsetof(mdl_vert, weights) );
269 glEnableVertexAttribArray( 4 );
270
271 /* 5: groups */
272 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
273 stride, (void *)offsetof(mdl_vert, groups) );
274 glEnableVertexAttribArray( 5 );
275
276 VG_CHECK_GL_ERR();
277
278 mesh->indice_count = indice_count;
279 mesh->loaded = 1;
280 }
281
282 static void mesh_bind( glmesh *mesh )
283 {
284 glBindVertexArray( mesh->vao );
285 }
286
287 static void mesh_drawn( u32 start, u32 count )
288 {
289 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
290 (void *)(start*sizeof(u32)) );
291 }
292
293 static void mesh_draw( glmesh *mesh )
294 {
295 mesh_drawn( 0, mesh->indice_count );
296 }
297
298 static void mesh_free( glmesh *mesh )
299 {
300 if( mesh->loaded )
301 {
302 glDeleteVertexArrays( 1, &mesh->vao );
303 glDeleteBuffers( 1, &mesh->ebo );
304 glDeleteBuffers( 1, &mesh->vbo );
305 }
306 }
307
308
309 /*
310 * Model implementation
311 */
312
313 static mdl_header *mdl_load( const char *path )
314 {
315 i64 size;
316 mdl_header *header = vg_asset_read_s( path, &size );
317
318 /*
319 * Check file is valid
320 */
321 if( !header )
322 {
323 vg_error( "Could not open '%s'\n", path );
324 return NULL;
325 }
326
327 if( size < sizeof(mdl_header) )
328 {
329 vg_free( header );
330 vg_error( "Invalid file '%s' (too small for header)\n", path );
331 return NULL;
332 }
333
334 if( header->file_length != size )
335 {
336 vg_error( "Invalid file '%s'"
337 "(wrong .file_length, %ub != real file size %ub)\n",
338 path, header->file_length, size );
339 vg_free( header );
340 return NULL;
341 }
342
343 /*
344 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
345 * and that they do not overlap.
346 */
347
348 struct memregion
349 {
350 const char *desc;
351 u32 count, max_count, size, offset;
352 }
353 regions[] = {
354 {
355 "Vertices",
356 header->vertex_count, MDL_VERT_MAX,
357 sizeof(mdl_vert), header->vertex_offset
358 },
359 {
360 "Indices",
361 header->indice_count, MDL_INDICE_MAX,
362 sizeof(u32), header->indice_offset
363 },
364 {
365 "Submesh",
366 header->submesh_count, MDL_SUBMESH_MAX,
367 sizeof(mdl_submesh), header->submesh_offset
368 },
369 {
370 "Materials",
371 header->material_count, MDL_MATERIAL_MAX,
372 sizeof(mdl_material), header->material_offset
373 },
374 {
375 "Nodes",
376 header->node_count, MDL_NODE_MAX,
377 sizeof(mdl_node), header->node_count
378 }
379 };
380
381 for( int i=0; i<vg_list_size(regions); i++ )
382 {
383 struct memregion *ri = &regions[i];
384
385 if( ri->count == 0 )
386 continue;
387
388 if( ri->count > ri->max_count )
389 {
390 vg_free( header );
391 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
392 path, ri->desc, ri->count, ri->max_count );
393 return NULL;
394 }
395
396 if( ri->offset >= header->file_length )
397 {
398 vg_free( header );
399 vg_error( "'%s': '%s' buffer offset is out of range\n",
400 path, ri->desc );
401 return NULL;
402 }
403
404 if( ri->offset + ri->size*ri->count > header->file_length )
405 {
406 vg_free( header );
407 vg_error( "'%s': '%s' buffer size is out of range\n",
408 path, ri->desc );
409 return NULL;
410 }
411
412 for( int j=0; j<vg_list_size(regions); j++ )
413 {
414 struct memregion *rj = &regions[j];
415 if( rj->count == 0 )
416 continue;
417
418 if( ri->offset >= rj->offset &&
419 (ri->offset+ri->size*ri->count < rj->offset+rj->size*rj->count))
420 {
421 vg_free( header );
422 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
423 path, ri->desc, rj->desc );
424 return NULL;
425 }
426 }
427 }
428
429 /*
430 * Pointer validation TODO(workshop)
431 */
432
433 /*
434 * strings TODO(workshop)
435 */
436
437 return header;
438 }
439
440 static void *mdl_baseptr( mdl_header *mdl, u32 offset )
441 {
442 return (void *)mdl + offset;
443 }
444
445 static const char *mdl_pstr( mdl_header *mdl, u32 pstr )
446 {
447 return (const char *)(mdl_baseptr( mdl, mdl->strings_offset )) + pstr;
448 }
449
450 static mdl_node *mdl_node_from_id( mdl_header *mdl, u32 id )
451 {
452 return ((mdl_node *)mdl_baseptr( mdl, mdl->node_offset )) + id;
453 }
454
455 static mdl_node *mdl_node_from_name( mdl_header *mdl, const char *name )
456 {
457 for( int i=0; i<mdl->node_count; i++ )
458 {
459 mdl_node *pnode = mdl_node_from_id( mdl, i );
460
461 if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) )
462 return pnode;
463 }
464
465 return NULL;
466 }
467
468 static mdl_submesh *mdl_submesh_from_id( mdl_header *mdl, u32 id )
469 {
470 if( id >= mdl->submesh_count )
471 return NULL;
472
473 return ((mdl_submesh *)mdl_baseptr( mdl, mdl->submesh_offset )) + id;
474 }
475
476 static mdl_submesh *mdl_node_submesh( mdl_header *mdl, mdl_node *node, u32 i )
477 {
478 if( i >= node->submesh_count )
479 return NULL;
480
481 return mdl_submesh_from_id( mdl, node->submesh_start+i );
482 }
483
484 static u32 *mdl_submesh_indices( mdl_header *mdl, mdl_submesh *sm )
485 {
486 return ((u32 *)mdl_baseptr( mdl, mdl->indice_offset )) + sm->indice_start;
487 }
488
489 static mdl_vert *mdl_submesh_vertices( mdl_header *mdl, mdl_submesh *sm )
490 {
491 return ((mdl_vert *)mdl_baseptr(mdl,mdl->vertex_offset)) + sm->vertex_start;
492 }
493
494 static mdl_material *mdl_material_from_id( mdl_header *mdl, u32 id )
495 {
496 return ((mdl_material *)mdl_baseptr(mdl,mdl->material_offset)) + id;
497 }
498
499 static mdl_animation *mdl_animation_from_id( mdl_header *mdl, u32 id )
500 {
501 return ((mdl_animation *)mdl_baseptr(mdl,mdl->anim_offset)) + id;
502 }
503
504 static void mdl_node_transform( mdl_node *pnode, m4x3f transform )
505 {
506 q_m3x3( pnode->q, transform );
507 v3_muls( transform[0], pnode->s[0], transform[0] );
508 v3_muls( transform[1], pnode->s[1], transform[1] );
509 v3_muls( transform[2], pnode->s[2], transform[2] );
510 v3_copy( pnode->co, transform[3] );
511 }
512
513 static void mdl_unpack_submesh( mdl_header *mdl, glmesh *mesh, mdl_submesh *sm )
514 {
515 mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count,
516 mdl_submesh_indices( mdl, sm ), sm->indice_count );
517 }
518
519 static void mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh )
520 {
521 u32 offset = mdl_submesh_from_id( mdl, 0 )->vertex_count;
522
523 for( int i=1; i< mdl->submesh_count; i++ )
524 {
525 mdl_submesh *sm = mdl_submesh_from_id( mdl, i );
526 u32 *indices = mdl_submesh_indices( mdl, sm );
527
528 for( u32 j=0; j<sm->indice_count; j++ )
529 indices[j] += offset;
530
531 offset += sm->vertex_count;
532 }
533
534 mdl_vert *vertex_base = mdl_baseptr( mdl, mdl->vertex_offset );
535 u32 *indice_base = mdl_baseptr( mdl, mdl->indice_offset );
536
537 mesh_upload( mesh, vertex_base, mdl->vertex_count,
538 indice_base, mdl->indice_count );
539 }
540
541 static void mdl_draw_submesh( mdl_submesh *sm )
542 {
543 mesh_drawn( sm->indice_start, sm->indice_count );
544 }
545
546 static void *mdl_get_entdata( mdl_header *mdl, mdl_node *pnode )
547 {
548 return mdl_baseptr( mdl, mdl->entdata_offset ) + pnode->offset;
549 }
550
551 static mdl_keyframe *mdl_get_animdata( mdl_header *mdl, mdl_animation *anim )
552 {
553 return mdl_baseptr( mdl, mdl->animdata_offset ) + anim->offset;
554 }
555
556 static void mdl_link_materials( mdl_header *root, mdl_header *child )
557 {
558 u32 lookup[MDL_MATERIAL_MAX];
559
560 for( int i=0; i<child->material_count; i++ )
561 {
562 mdl_material *mi = mdl_material_from_id( child, i );
563 const char *si = mdl_pstr( child, mi->pstr_name );
564
565 lookup[i] = 0;
566
567 for( int j=0; j<root->material_count; j++ )
568 {
569 mdl_material *mj = mdl_material_from_id( root, j );
570 const char *sj = mdl_pstr( root, mj->pstr_name );
571
572 if( !strcmp( si, sj ) )
573 {
574 lookup[i] = j;
575 break;
576 }
577 }
578
579 if( lookup[i] == 0 && i != 0 )
580 {
581 vg_warn( "Could not link material '%s' (not present in root model)\n",
582 si );
583 }
584 }
585
586 for( int i=0; i<child->submesh_count; i++ )
587 {
588 mdl_submesh *sm = mdl_submesh_from_id( child, i );
589 sm->material_id = lookup[sm->material_id];
590 }
591 }
592
593
594 #endif