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