nonlocal stuff again
[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
11 enum mdl_shader
12 {
13 k_shader_standard = 0,
14 k_shader_standard_cutout = 1,
15 k_shader_terrain_blend = 2,
16 k_shader_standard_vertex_blend = 3,
17 k_shader_water = 4
18 };
19
20 enum mdl_surface_prop
21 {
22 k_surface_prop_concrete = 0,
23 k_surface_prop_wood = 1,
24 k_surface_prop_grass = 2,
25 k_surface_prop_tiles = 3,
26 k_surface_prop_metal = 4
27 };
28
29 enum material_flag
30 {
31 k_material_flag_skate_surface = 0x1,
32 k_material_flag_collision = 0x2,
33 k_material_flag_grow_grass = 0x4,
34 k_material_flag_grind_surface = 0x8
35 };
36
37 #pragma pack(push,1)
38
39 /* 48 byte */
40 struct mdl_vert
41 {
42 v3f co, /* 3*32 */
43 norm; /* 3*32 */
44 v2f uv; /* 2*32 */
45
46 u8 colour[4]; /* 4*8 */
47 u16 weights[4];/* 4*16 */
48 u8 groups[4]; /* 4*8 */
49 };
50
51 #pragma pack(pop)
52
53 typedef struct mdl_context mdl_context;
54 typedef struct mdl_array_ptr mdl_array_ptr;
55 typedef struct mdl_vert mdl_vert;
56 typedef struct mdl_transform mdl_transform;
57 typedef struct mdl_submesh mdl_submesh;
58 typedef struct mdl_material mdl_material;
59 typedef struct mdl_bone mdl_bone;
60 typedef struct mdl_armature mdl_armature;
61 typedef struct mdl_animation mdl_animation;
62 typedef struct mdl_transform mdl_keyframe;
63 typedef struct mdl_mesh mdl_mesh;
64 typedef struct mdl_file mdl_file;
65 typedef struct mdl_texture mdl_texture;
66 typedef struct mdl_array mdl_array;
67 typedef struct mdl_header mdl_header;
68
69 struct mdl_transform
70 {
71 v3f co, s;
72 v4f q;
73 };
74
75 struct mdl_submesh
76 {
77 u32 indice_start,
78 indice_count,
79 vertex_start,
80 vertex_count;
81
82 boxf bbx;
83 u32 material_id;
84 };
85
86 struct mdl_material
87 {
88 u32 pstr_name,
89 shader,
90 flags,
91 surface_prop;
92
93 v4f colour,
94 colour1;
95
96 u32 tex_diffuse,
97 tex_none0,
98 tex_none1;
99 };
100
101 struct mdl_bone
102 {
103 v3f co, end;
104 u32 parent,
105 collider,
106 ik_target,
107 ik_pole,
108 flags,
109 pstr_name;
110
111 boxf hitbox;
112 v3f conevx, conevy, coneva;
113 float conet;
114 };
115
116 enum bone_flag
117 {
118 k_bone_flag_deform = 0x1,
119 k_bone_flag_ik = 0x2,
120 k_bone_flag_cone_constraint = 0x4
121 };
122
123 enum bone_collider
124 {
125 k_bone_collider_none = 0,
126 k_bone_collider_box = 1,
127 k_bone_collider_capsule = 2
128 };
129
130 struct mdl_armature
131 {
132 mdl_transform transform;
133 u32 bone_start,
134 bone_count,
135 anim_start,
136 anim_count;
137 };
138
139 struct mdl_animation
140 {
141 u32 pstr_name,
142 length;
143 float rate;
144 u32 offset;
145 };
146
147 struct mdl_mesh
148 {
149 mdl_transform transform;
150 u32 submesh_start,
151 submesh_count,
152 pstr_name,
153 flags,
154 armature_id;
155 };
156
157 struct mdl_file
158 {
159 u32 pstr_path,
160 pack_offset,
161 pack_size;
162 };
163
164 struct mdl_texture
165 {
166 mdl_file file;
167 u32 type;
168 };
169
170 struct mdl_array
171 {
172 u32 file_offset,
173 item_count,
174 item_size;
175
176 char name[16];
177 };
178
179 struct mdl_header
180 {
181 u32 version;
182 mdl_array index;
183 };
184
185 struct mdl_context
186 {
187 FILE *file;
188 mdl_header info;
189
190 struct mdl_array_ptr
191 {
192 void *data;
193 u32 count, stride;
194 }
195 index,
196
197 /* metadata */
198 strings,
199 meshs,
200 submeshs,
201 materials,
202 textures,
203 armatures,
204 bones,
205 animations,
206
207 /* animation buffers */
208 keyframes,
209
210 /* mesh buffers */
211 verts,
212 indices;
213
214 u32 pack_base_offset;
215
216 /* pack data */
217 //pack;
218 };
219
220
221 VG_STATIC void mdl_load_fatal_corrupt( mdl_context *mdl )
222 {
223 fclose( mdl->file );
224 vg_file_print_invalid( mdl->file );
225 vg_fatal_exit_loop( "Corrupt model" );
226 }
227
228 /*
229 * Model implementation
230 */
231
232 VG_STATIC u32 mdl_query_array_size( mdl_array *arr )
233 {
234 if( arr->item_count ){
235 u32 size = arr->item_size*arr->item_count;
236 return vg_align8(size);
237 }
238 else
239 return 0;
240 }
241
242 VG_STATIC
243 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
244 {
245 fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET );
246 u64 l = fread( dst, info->pack_size, 1, mdl->file );
247
248 if( l != 1 )
249 mdl_load_fatal_corrupt( mdl );
250 }
251
252 VG_STATIC void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
253 mdl_array *arr, void *lin_alloc )
254 {
255 if( arr->item_count ){
256 u32 size = arr->item_size*arr->item_count;
257 ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
258
259 fseek( mdl->file, arr->file_offset, SEEK_SET );
260 u64 l = fread( ptr->data, arr->item_size*arr->item_count, 1, mdl->file );
261
262 if( l != 1 )
263 mdl_load_fatal_corrupt( mdl );
264 }
265 else
266 ptr->data = NULL;
267
268 ptr->count = arr->item_count;
269 ptr->stride = arr->item_size;
270 }
271
272 VG_STATIC void *mdl_arritm( mdl_array_ptr *arr, u32 index )
273 {
274 return ((u8 *)arr->data) + index*arr->stride;
275 }
276
277 VG_STATIC u32 mdl_arrcount( mdl_array_ptr *arr )
278 {
279 return arr->count;
280 }
281
282 VG_STATIC mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
283 {
284 for( u32 i=0; i<mdl_arrcount(&mdl->index); i++ ){
285 mdl_array *arr = mdl_arritm( &mdl->index, i );
286
287 if( !strncmp(arr->name,name,16) ){
288 return arr;
289 }
290 }
291
292 return NULL;
293 }
294
295 VG_STATIC int mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
296 const char *name, void *lin_alloc )
297 {
298 mdl_array *arr = mdl_find_array( mdl, name );
299
300 if( arr ){
301 mdl_load_array_file( mdl, ptr, arr, lin_alloc );
302 return 1;
303 }
304 else{
305 ptr->data = NULL;
306 ptr->count = 0;
307 ptr->stride = 0;
308 return 0;
309 }
310 }
311
312 VG_STATIC int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc )
313 {
314 int success = 1;
315
316 success &= mdl_load_array( mdl, &mdl->verts, "mdl_vert", lin_alloc );
317 success &= mdl_load_array( mdl, &mdl->indices, "mdl_indice", lin_alloc );
318
319 return success;
320 }
321
322 VG_STATIC int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc )
323 {
324 int success = 1;
325
326 success &= mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc );
327 success &= mdl_load_array( mdl, &mdl->meshs, "mdl_mesh", lin_alloc );
328 success &= mdl_load_array( mdl, &mdl->submeshs, "mdl_submesh", lin_alloc );
329 success &= mdl_load_array( mdl, &mdl->materials, "mdl_material", lin_alloc );
330 success &= mdl_load_array( mdl, &mdl->textures, "mdl_texture", lin_alloc );
331 success &= mdl_load_array( mdl, &mdl->armatures, "mdl_armature", lin_alloc );
332 success &= mdl_load_array( mdl, &mdl->bones, "mdl_bone", lin_alloc );
333 success &= mdl_load_array( mdl, &mdl->animations,"mdl_animation",lin_alloc );
334
335 return success;
336 }
337
338 VG_STATIC int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc )
339 {
340 return mdl_load_array( mdl, &mdl->keyframes, "mdl_keyframe", lin_alloc );
341 }
342
343 #if 0
344 VG_STATIC int mdl_load_pack_block( mdl_context *mdl, void *lin_alloc )
345 {
346 return mdl_load_array( mdl, &mdl->pack, "pack", lin_alloc );
347 }
348 #endif
349
350 /*
351 * if calling mdl_open, and the file does not exist, the game will fatal quit
352 */
353 VG_STATIC void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc )
354 {
355 memset( mdl, 0, sizeof( mdl_context ) );
356 mdl->file = fopen( path, "rb" );
357
358 if( !mdl->file ){
359 vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
360 vg_fatal_exit_loop( "see above for details" );
361 }
362
363 u64 l = fread( &mdl->info, sizeof(mdl_header), 1, mdl->file );
364 if( l != 1 )
365 mdl_load_fatal_corrupt( mdl );
366
367 mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc );
368
369 mdl_array *pack = mdl_find_array( mdl, "pack" );
370 if( pack ) mdl->pack_base_offset = pack->file_offset;
371 else mdl->pack_base_offset = 0;
372 }
373
374 /*
375 * close file handle
376 */
377 VG_STATIC void mdl_close( mdl_context *mdl )
378 {
379 fclose( mdl->file );
380 mdl->file = NULL;
381 }
382
383 /* useful things you can do with the model */
384
385 VG_STATIC void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx )
386 {
387 q_m3x3( transform->q, mtx );
388 v3_muls( mtx[0], transform->s[0], mtx[0] );
389 v3_muls( mtx[1], transform->s[1], mtx[1] );
390 v3_muls( mtx[2], transform->s[2], mtx[2] );
391 v3_copy( transform->co, mtx[3] );
392 }
393
394 VG_STATIC const char *mdl_pstr( mdl_context *mdl, u32 pstr )
395 {
396 return mdl_arritm( &mdl->strings, pstr );
397 }
398
399 /*
400 * Simple mesh interface for OpenGL
401 * ----------------------------------------------------------------------------
402 */
403
404 typedef struct glmesh glmesh;
405 struct glmesh
406 {
407 GLuint vao, vbo, ebo;
408 u32 indice_count;
409 u32 loaded;
410 };
411
412 VG_STATIC void mesh_upload( glmesh *mesh,
413 mdl_vert *verts, u32 vert_count,
414 u32 *indices, u32 indice_count )
415 {
416 //assert( mesh->loaded == 0 );
417
418 glGenVertexArrays( 1, &mesh->vao );
419 glGenBuffers( 1, &mesh->vbo );
420 glGenBuffers( 1, &mesh->ebo );
421 glBindVertexArray( mesh->vao );
422
423 size_t stride = sizeof(mdl_vert);
424
425 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
426 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
427
428 glBindVertexArray( mesh->vao );
429 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
430 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
431 indices, GL_STATIC_DRAW );
432
433 /* 0: coordinates */
434 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
435 glEnableVertexAttribArray( 0 );
436
437 /* 1: normal */
438 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
439 stride, (void *)offsetof(mdl_vert, norm) );
440 glEnableVertexAttribArray( 1 );
441
442 /* 2: uv */
443 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
444 stride, (void *)offsetof(mdl_vert, uv) );
445 glEnableVertexAttribArray( 2 );
446
447 /* 3: colour */
448 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
449 stride, (void *)offsetof(mdl_vert, colour) );
450 glEnableVertexAttribArray( 3 );
451
452 /* 4: weights */
453 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
454 stride, (void *)offsetof(mdl_vert, weights) );
455 glEnableVertexAttribArray( 4 );
456
457 /* 5: groups */
458 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
459 stride, (void *)offsetof(mdl_vert, groups) );
460 glEnableVertexAttribArray( 5 );
461
462 VG_CHECK_GL_ERR();
463
464 mesh->indice_count = indice_count;
465 mesh->loaded = 1;
466 }
467
468 VG_STATIC void mesh_bind( glmesh *mesh )
469 {
470 glBindVertexArray( mesh->vao );
471 }
472
473 VG_STATIC void mesh_drawn( u32 start, u32 count )
474 {
475 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
476 (void *)(start*sizeof(u32)) );
477 }
478
479 VG_STATIC void mesh_draw( glmesh *mesh )
480 {
481 mesh_drawn( 0, mesh->indice_count );
482 }
483
484 VG_STATIC void mesh_free( glmesh *mesh )
485 {
486 if( mesh->loaded ){
487 glDeleteVertexArrays( 1, &mesh->vao );
488 glDeleteBuffers( 1, &mesh->ebo );
489 glDeleteBuffers( 1, &mesh->vbo );
490 mesh->loaded = 0;
491 }
492 }
493
494 VG_STATIC void mdl_draw_submesh( mdl_submesh *sm )
495 {
496 mesh_drawn( sm->indice_start, sm->indice_count );
497 }
498
499 /* WARNING: Destructive! Only use this once and then discard the context. */
500 VG_STATIC void mdl_unpack_glmesh( mdl_context *mdl, glmesh *mesh )
501 {
502 if( !mdl->submeshs.count )
503 vg_fatal_exit_loop( "Tried to unpack empty model file" );
504
505 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 );
506 u32 offset = sm->vertex_count;
507
508 for( u32 i=1; i<mdl_arrcount( &mdl->submeshs ); i++ ){
509 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i );
510 u32 *indices = mdl_arritm( &mdl->indices, sm->indice_start );
511
512 for( u32 j=0; j<sm->indice_count; j++ )
513 indices[j] += offset;
514
515 offset += sm->vertex_count;
516 }
517
518 mesh_upload( mesh, mdl->verts.data, mdl->verts.count,
519 mdl->indices.data, mdl->indices.count );
520 }
521
522 VG_STATIC mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name )
523 {
524 for( u32 i=0; i<mdl_arrcount( &mdl->meshs ); i++ ){
525 mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
526 if( !strcmp( name, mdl_pstr( mdl, mesh->pstr_name ))){
527 return mesh;
528 }
529 }
530 return NULL;
531 }
532
533 #endif