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