50913017fab68d1925f2d838668df961303cca76
[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 pstr_name;
146 u32 id_start;
147 v3f colour;
148 };
149
150 struct classtype_bone
151 {
152 u32 deform;
153 };
154
155 struct classtype_skeleton
156 {
157 u32 channels,
158 anim_start,
159 anim_count;
160 };
161
162 struct classtype_skin
163 {
164 u32 skeleton;
165 };
166
167 #pragma pack(pop)
168
169 /*
170 * Simple mesh interface for OpenGL
171 */
172
173 struct glmesh
174 {
175 GLuint vao, vbo, ebo;
176 u32 indice_count;
177 };
178
179 static void mesh_upload( glmesh *mesh,
180 mdl_vert *verts, u32 vert_count,
181 u32 *indices, u32 indice_count )
182 {
183 glGenVertexArrays( 1, &mesh->vao );
184 glGenBuffers( 1, &mesh->vbo );
185 glGenBuffers( 1, &mesh->ebo );
186 glBindVertexArray( mesh->vao );
187
188 size_t stride = sizeof(mdl_vert);
189
190 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
191 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
192
193 glBindVertexArray( mesh->vao );
194 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
195 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
196 indices, GL_STATIC_DRAW );
197
198 /* 0: coordinates */
199 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
200 glEnableVertexAttribArray( 0 );
201
202 /* 1: normal */
203 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
204 stride, (void *)offsetof(mdl_vert, norm) );
205 glEnableVertexAttribArray( 1 );
206
207 /* 2: uv */
208 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
209 stride, (void *)offsetof(mdl_vert, uv) );
210 glEnableVertexAttribArray( 2 );
211
212 /* 3: colour */
213 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
214 stride, (void *)offsetof(mdl_vert, colour) );
215 glEnableVertexAttribArray( 3 );
216
217 /* 4: weights */
218 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
219 stride, (void *)offsetof(mdl_vert, weights) );
220 glEnableVertexAttribArray( 4 );
221
222 /* 5: groups */
223 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
224 stride, (void *)offsetof(mdl_vert, groups) );
225 glEnableVertexAttribArray( 5 );
226
227
228 VG_CHECK_GL();
229 mesh->indice_count = indice_count;
230 }
231
232 static void mesh_bind( glmesh *mesh )
233 {
234 glBindVertexArray( mesh->vao );
235 }
236
237 static void mesh_drawn( u32 start, u32 count )
238 {
239 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
240 (void *)(start*sizeof(u32)) );
241 }
242
243 static void mesh_draw( glmesh *mesh )
244 {
245 mesh_drawn( 0, mesh->indice_count );
246 }
247
248 static void mesh_free( glmesh *mesh )
249 {
250 glDeleteVertexArrays( 1, &mesh->vao );
251 glDeleteBuffers( 1, &mesh->ebo );
252 glDeleteBuffers( 1, &mesh->vbo );
253 }
254
255
256 /*
257 * Model implementation
258 */
259
260 static mdl_header *mdl_load( const char *path )
261 {
262 i64 size;
263 mdl_header *header = vg_asset_read_s( path, &size );
264
265 /*
266 * Check file is valid
267 */
268 if( !header )
269 {
270 vg_error( "Could not open '%s'\n", path );
271 return NULL;
272 }
273
274 if( size < sizeof(mdl_header) )
275 {
276 free( header );
277 vg_error( "Invalid file '%s' (too small for header)\n", path );
278 return NULL;
279 }
280
281 if( header->file_length != size )
282 {
283 vg_error( "Invalid file '%s'"
284 "(wrong .file_length, %ub != real file size %ub)\n",
285 path, header->file_length, size );
286 free( header );
287 return NULL;
288 }
289
290 /*
291 * Validate offsets and memory sections, to ensure all arrays are in-bounds,
292 * and that they do not overlap.
293 */
294
295 struct memregion
296 {
297 const char *desc;
298 u32 count, max_count, size, offset;
299 }
300 regions[] = {
301 {
302 "Vertices",
303 header->vertex_count, MDL_VERT_MAX,
304 sizeof(mdl_vert), header->vertex_offset
305 },
306 {
307 "Indices",
308 header->indice_count, MDL_INDICE_MAX,
309 sizeof(u32), header->indice_offset
310 },
311 {
312 "Submesh",
313 header->submesh_count, MDL_SUBMESH_MAX,
314 sizeof(mdl_submesh), header->submesh_offset
315 },
316 {
317 "Materials",
318 header->material_count, MDL_MATERIAL_MAX,
319 sizeof(mdl_material), header->material_offset
320 },
321 {
322 "Nodes",
323 header->node_count, MDL_NODE_MAX,
324 sizeof(mdl_node), header->node_count
325 }
326 };
327
328 for( int i=0; i<vg_list_size(regions); i++ )
329 {
330 struct memregion *ri = &regions[i];
331
332 if( ri->count == 0 )
333 continue;
334
335 if( ri->count > ri->max_count )
336 {
337 free( header );
338 vg_error( "'%s': '%s' buffer exceeds the maximum (%u/%u)\n",
339 path, ri->desc, ri->count, ri->max_count );
340 return NULL;
341 }
342
343 if( ri->offset >= header->file_length )
344 {
345 free( header );
346 vg_error( "'%s': '%s' buffer offset is out of range\n",
347 path, ri->desc );
348 return NULL;
349 }
350
351 if( ri->offset + ri->size*ri->count > header->file_length )
352 {
353 free( header );
354 vg_error( "'%s': '%s' buffer size is out of range\n",
355 path, ri->desc );
356 return NULL;
357 }
358
359 for( int j=0; j<vg_list_size(regions); j++ )
360 {
361 struct memregion *rj = &regions[j];
362 if( rj->count == 0 )
363 continue;
364
365 if( ri->offset >= rj->offset &&
366 (ri->offset+ri->size*ri->count < rj->offset+rj->size*rj->count))
367 {
368 free( header );
369 vg_error( "'%s': '%s' buffer overlaps '%s'\n",
370 path, ri->desc, rj->desc );
371 return NULL;
372 }
373 }
374 }
375
376 /*
377 * Pointer validation TODO(workshop)
378 */
379
380 /*
381 * strings TODO(workshop)
382 */
383
384 return header;
385 }
386
387 static void *mdl_baseptr( mdl_header *mdl, u32 offset )
388 {
389 return (void *)mdl + offset;
390 }
391
392 static const char *mdl_pstr( mdl_header *mdl, u32 pstr )
393 {
394 return (const char *)(mdl_baseptr( mdl, mdl->strings_offset )) + pstr;
395 }
396
397 static mdl_node *mdl_node_from_id( mdl_header *mdl, u32 id )
398 {
399 return ((mdl_node *)mdl_baseptr( mdl, mdl->node_offset )) + id;
400 }
401
402 static mdl_node *mdl_node_from_name( mdl_header *mdl, const char *name )
403 {
404 for( int i=0; i<mdl->node_count; i++ )
405 {
406 mdl_node *pnode = mdl_node_from_id( mdl, i );
407
408 if( !strcmp( name, mdl_pstr( mdl, pnode->pstr_name )) )
409 return pnode;
410 }
411
412 return NULL;
413 }
414
415 static mdl_submesh *mdl_submesh_from_id( mdl_header *mdl, u32 id )
416 {
417 if( id >= mdl->submesh_count )
418 return NULL;
419
420 return ((mdl_submesh *)mdl_baseptr( mdl, mdl->submesh_offset )) + id;
421 }
422
423 static mdl_submesh *mdl_node_submesh( mdl_header *mdl, mdl_node *node, u32 i )
424 {
425 if( i >= node->submesh_count )
426 return NULL;
427
428 return mdl_submesh_from_id( mdl, node->submesh_start+i );
429 }
430
431 static u32 *mdl_submesh_indices( mdl_header *mdl, mdl_submesh *sm )
432 {
433 return ((u32 *)mdl_baseptr( mdl, mdl->indice_offset )) + sm->indice_start;
434 }
435
436 static mdl_vert *mdl_submesh_vertices( mdl_header *mdl, mdl_submesh *sm )
437 {
438 return ((mdl_vert *)mdl_baseptr(mdl,mdl->vertex_offset)) + sm->vertex_start;
439 }
440
441 static mdl_material *mdl_material_from_id( mdl_header *mdl, u32 id )
442 {
443 return ((mdl_material *)mdl_baseptr(mdl,mdl->material_offset)) + id;
444 }
445
446 static mdl_animation *mdl_animation_from_id( mdl_header *mdl, u32 id )
447 {
448 return ((mdl_animation *)mdl_baseptr(mdl,mdl->anim_offset)) + id;
449 }
450
451 static void mdl_node_transform( mdl_node *pnode, m4x3f transform )
452 {
453 q_m3x3( pnode->q, transform );
454 v3_muls( transform[0], pnode->s[0], transform[0] );
455 v3_muls( transform[1], pnode->s[1], transform[1] );
456 v3_muls( transform[2], pnode->s[2], transform[2] );
457 v3_copy( pnode->co, transform[3] );
458 }
459
460 static void mdl_unpack_submesh( mdl_header *mdl, glmesh *mesh, mdl_submesh *sm )
461 {
462 mesh_upload( mesh, mdl_submesh_vertices( mdl, sm ), sm->vertex_count,
463 mdl_submesh_indices( mdl, sm ), sm->indice_count );
464 }
465
466 static void mdl_unpack_glmesh( mdl_header *mdl, glmesh *mesh )
467 {
468 u32 offset = mdl_submesh_from_id( mdl, 0 )->vertex_count;
469
470 for( int i=1; i< mdl->submesh_count; i++ )
471 {
472 mdl_submesh *sm = mdl_submesh_from_id( mdl, i );
473 u32 *indices = mdl_submesh_indices( mdl, sm );
474
475 for( u32 j=0; j<sm->indice_count; j++ )
476 indices[j] += offset;
477
478 offset += sm->vertex_count;
479 }
480
481 mdl_vert *vertex_base = mdl_baseptr( mdl, mdl->vertex_offset );
482 u32 *indice_base = mdl_baseptr( mdl, mdl->indice_offset );
483
484 mesh_upload( mesh, vertex_base, mdl->vertex_count,
485 indice_base, mdl->indice_count );
486 }
487
488 static void mdl_draw_submesh( mdl_submesh *sm )
489 {
490 mesh_drawn( sm->indice_start, sm->indice_count );
491 }
492
493 static void *mdl_get_entdata( mdl_header *mdl, mdl_node *pnode )
494 {
495 return mdl_baseptr( mdl, mdl->entdata_offset ) + pnode->offset;
496 }
497
498 static mdl_keyframe *mdl_get_animdata( mdl_header *mdl, mdl_animation *anim )
499 {
500 return mdl_baseptr( mdl, mdl->animdata_offset ) + anim->offset;
501 }
502
503 static void mdl_link_materials( mdl_header *root, mdl_header *child )
504 {
505 u32 lookup[MDL_MATERIAL_MAX];
506
507 for( int i=0; i<child->material_count; i++ )
508 {
509 mdl_material *mi = mdl_material_from_id( child, i );
510 const char *si = mdl_pstr( child, mi->pstr_name );
511
512 lookup[i] = 0;
513
514 for( int j=0; j<root->material_count; j++ )
515 {
516 mdl_material *mj = mdl_material_from_id( root, j );
517 const char *sj = mdl_pstr( root, mj->pstr_name );
518
519 if( !strcmp( si, sj ) )
520 {
521 lookup[i] = j;
522 break;
523 }
524 }
525
526 if( lookup[i] == 0 && i != 0 )
527 {
528 vg_warn( "Could not link material '%s' (not present in root model)\n",
529 si );
530 }
531 }
532
533 for( int i=0; i<child->submesh_count; i++ )
534 {
535 mdl_submesh *sm = mdl_submesh_from_id( child, i );
536 sm->material_id = lookup[sm->material_id];
537 }
538 }
539
540
541 #endif