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