small refactor of model loading
[carveJwlIkooP6JGAAIwe30JlM.git] / model.c
1 /*
2 * Copyright (C) 2021-2024 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #pragma once
6
7 #include "model.h"
8 #include "vg/vg_io.h"
9 #include "vg/vg_async.h"
10 #include "vg/vg_tex.h"
11 #include <string.h>
12 #include <stdlib.h>
13 #include <errno.h>
14
15 static void mdl_load_fatal_corrupt( mdl_context *mdl )
16 {
17 fclose( mdl->file );
18 vg_file_print_invalid( mdl->file );
19 vg_fatal_error( "Corrupt model" );
20 }
21
22 /*
23 * Model implementation
24 */
25
26 void mdl_fread_pack_file( mdl_context *mdl, mdl_file *info, void *dst )
27 {
28 if( !info->pack_size )
29 {
30 vg_warn( "path: %s\n", mdl_pstr( mdl, info->pstr_path ) );
31 vg_fatal_error( "Packed file is only a header; it is not packed" );
32 }
33
34 fseek( mdl->file, mdl->pack_base_offset+info->pack_offset, SEEK_SET );
35 u64 l = fread( dst, info->pack_size, 1, mdl->file );
36
37 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
38 }
39
40 /* TODO: Rename these */
41 static void mdl_load_array_file_buffer( mdl_context *mdl, mdl_array *arr,
42 void *buffer, u32 stride )
43 {
44 if( arr->item_count )
45 {
46 fseek( mdl->file, arr->file_offset, SEEK_SET );
47
48 if( stride == arr->item_size )
49 {
50 u64 l = fread( buffer, arr->item_size*arr->item_count, 1, mdl->file );
51 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
52 }
53 else
54 {
55 vg_warn( "Applying alignment fixup to array @%p [%u -> %u] x %u\n",
56 buffer, arr->item_size, stride, arr->item_count );
57 if( stride < arr->item_size )
58 vg_fatal_error( "not safe\n" );
59
60 for( u32 i=0; i<arr->item_count; i++ )
61 {
62 u64 l = fread( buffer+i*stride, arr->item_size, 1, mdl->file );
63 if( l != 1 ) mdl_load_fatal_corrupt( mdl );
64 }
65 }
66 }
67 }
68
69 static void mdl_load_array_file( mdl_context *mdl, mdl_array_ptr *ptr,
70 mdl_array *arr, void *lin_alloc, u32 stride )
71 {
72 if( stride < arr->item_size )
73 {
74 vg_error( "Structure max: %u. Got: %u\n", stride, arr->item_size );
75 vg_fatal_error( "not safe\n" );
76 }
77
78 if( arr->item_count )
79 {
80 u32 size = stride*arr->item_count;
81 ptr->data = vg_linear_alloc( lin_alloc, vg_align8(size) );
82 mdl_load_array_file_buffer( mdl, arr, ptr->data, stride );
83 }
84 else
85 {
86 ptr->data = NULL;
87 }
88
89 ptr->stride = stride;
90 ptr->count = arr->item_count;
91 }
92
93 void *mdl_arritm( mdl_array_ptr *arr, u32 index )
94 {
95 return ((u8 *)arr->data) + index*arr->stride;
96 }
97
98 u32 mdl_arrcount( mdl_array_ptr *arr )
99 {
100 return arr->count;
101 }
102
103 static mdl_array *mdl_find_array( mdl_context *mdl, const char *name )
104 {
105 for( u32 i=0; i<mdl_arrcount(&mdl->index); i++ )
106 {
107 mdl_array *arr = mdl_arritm( &mdl->index, i );
108
109 if( !strncmp(arr->name,name,16) )
110 return arr;
111 }
112
113 return NULL;
114 }
115
116 int _mdl_load_array( mdl_context *mdl, mdl_array_ptr *ptr,
117 const char *name, void *lin_alloc, u32 stride )
118 {
119 mdl_array *arr = mdl_find_array( mdl, name );
120
121 if( arr )
122 {
123 mdl_load_array_file( mdl, ptr, arr, lin_alloc, stride );
124 return 1;
125 }
126 else
127 {
128 ptr->data = NULL;
129 ptr->count = 0;
130 ptr->stride = 0;
131 return 0;
132 }
133 }
134
135 int mdl_load_mesh_block( mdl_context *mdl, void *lin_alloc )
136 {
137 int success = 1;
138
139 success &= MDL_LOAD_ARRAY( mdl, &mdl->verts, mdl_vert, lin_alloc );
140 success &= MDL_LOAD_ARRAY( mdl, &mdl->indices, mdl_indice, lin_alloc );
141
142 return success;
143 }
144
145 int mdl_load_metadata_block( mdl_context *mdl, void *lin_alloc )
146 {
147 int success = 1;
148
149 success &= _mdl_load_array( mdl, &mdl->strings, "strings", lin_alloc, 1 );
150 success &= MDL_LOAD_ARRAY( mdl, &mdl->meshs, mdl_mesh, lin_alloc );
151 success &= MDL_LOAD_ARRAY( mdl, &mdl->submeshs, mdl_submesh, lin_alloc );
152 success &= MDL_LOAD_ARRAY( mdl, &mdl->materials, mdl_material, lin_alloc );
153 success &= MDL_LOAD_ARRAY( mdl, &mdl->textures, mdl_texture, lin_alloc );
154 success &= MDL_LOAD_ARRAY( mdl, &mdl->armatures, mdl_armature, lin_alloc );
155 success &= MDL_LOAD_ARRAY( mdl, &mdl->bones, mdl_bone, lin_alloc );
156 success &= MDL_LOAD_ARRAY( mdl, &mdl->animations,mdl_animation,lin_alloc );
157
158 return success;
159 }
160
161 int mdl_load_animation_block( mdl_context *mdl, void *lin_alloc )
162 {
163 return MDL_LOAD_ARRAY( mdl, &mdl->keyframes, mdl_keyframe, lin_alloc );
164 }
165
166 /*
167 * if calling mdl_open, and the file does not exist, the game will fatal quit
168 */
169 void mdl_open( mdl_context *mdl, const char *path, void *lin_alloc )
170 {
171 memset( mdl, 0, sizeof( mdl_context ) );
172 mdl->file = fopen( path, "rb" );
173
174 if( !mdl->file )
175 {
176 vg_error( "mdl_open('%s'): %s\n", path, strerror(errno) );
177 vg_fatal_error( "see above for details" );
178 }
179
180 u64 l = fread( &mdl->info, sizeof(mdl_header), 1, mdl->file );
181 if( l != 1 )
182 mdl_load_fatal_corrupt( mdl );
183
184 if( mdl->info.version < MDL_VERSION_MIN )
185 {
186 vg_warn( "For model: %s\n", path );
187 vg_warn( " version: %u (min: %u, current: %u)\n",
188 mdl->info.version, MDL_VERSION_MIN, MDL_VERSION_NR );
189
190 vg_fatal_error( "Legacy model version incompatable" );
191 }
192
193 mdl_load_array_file( mdl, &mdl->index, &mdl->info.index, lin_alloc,
194 sizeof(mdl_array) );
195
196 mdl_array *pack = mdl_find_array( mdl, "pack" );
197 if( pack ) mdl->pack_base_offset = pack->file_offset;
198 else mdl->pack_base_offset = 0;
199 }
200
201 /*
202 * close file handle
203 */
204 void mdl_close( mdl_context *mdl )
205 {
206 fclose( mdl->file );
207 mdl->file = NULL;
208 }
209
210 /* useful things you can do with the model */
211
212 void mdl_transform_m4x3( mdl_transform *transform, m4x3f mtx )
213 {
214 q_m3x3( transform->q, mtx );
215 v3_muls( mtx[0], transform->s[0], mtx[0] );
216 v3_muls( mtx[1], transform->s[1], mtx[1] );
217 v3_muls( mtx[2], transform->s[2], mtx[2] );
218 v3_copy( transform->co, mtx[3] );
219 }
220
221 const char *mdl_pstr( mdl_context *mdl, u32 pstr )
222 {
223 return ((char *)mdl_arritm( &mdl->strings, pstr )) + 4;
224 }
225
226
227 int mdl_pstreq( mdl_context *mdl, u32 pstr, const char *str, u32 djb2 )
228 {
229 u32 hash = *((u32 *)mdl_arritm( &mdl->strings, pstr ));
230 if( hash == djb2 ){
231 if( !strcmp( str, mdl_pstr( mdl, pstr ))) return 1;
232 else return 0;
233 }
234 else return 0;
235 }
236
237 /*
238 * Simple mesh interface for OpenGL
239 * ----------------------------------------------------------------------------
240 */
241
242 static void mesh_upload( glmesh *mesh,
243 mdl_vert *verts, u32 vert_count,
244 u32 *indices, u32 indice_count )
245 {
246 glGenVertexArrays( 1, &mesh->vao );
247 glGenBuffers( 1, &mesh->vbo );
248 glGenBuffers( 1, &mesh->ebo );
249 glBindVertexArray( mesh->vao );
250
251 size_t stride = sizeof(mdl_vert);
252
253 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
254 glBufferData( GL_ARRAY_BUFFER, vert_count*stride, verts, GL_STATIC_DRAW );
255
256 glBindVertexArray( mesh->vao );
257 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
258 glBufferData( GL_ELEMENT_ARRAY_BUFFER, indice_count*sizeof(u32),
259 indices, GL_STATIC_DRAW );
260
261 /* 0: coordinates */
262 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
263 glEnableVertexAttribArray( 0 );
264
265 /* 1: normal */
266 glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE,
267 stride, (void *)offsetof(mdl_vert, norm) );
268 glEnableVertexAttribArray( 1 );
269
270 /* 2: uv */
271 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
272 stride, (void *)offsetof(mdl_vert, uv) );
273 glEnableVertexAttribArray( 2 );
274
275 /* 3: colour */
276 glVertexAttribPointer( 3, 4, GL_UNSIGNED_BYTE, GL_TRUE,
277 stride, (void *)offsetof(mdl_vert, colour) );
278 glEnableVertexAttribArray( 3 );
279
280 /* 4: weights */
281 glVertexAttribPointer( 4, 4, GL_UNSIGNED_SHORT, GL_TRUE,
282 stride, (void *)offsetof(mdl_vert, weights) );
283 glEnableVertexAttribArray( 4 );
284
285 /* 5: groups */
286 glVertexAttribIPointer( 5, 4, GL_UNSIGNED_BYTE,
287 stride, (void *)offsetof(mdl_vert, groups) );
288 glEnableVertexAttribArray( 5 );
289
290 VG_CHECK_GL_ERR();
291
292 mesh->indice_count = indice_count;
293 mesh->loaded = 1;
294 }
295
296 void mesh_bind( glmesh *mesh )
297 {
298 glBindVertexArray( mesh->vao );
299 }
300
301 void mesh_drawn( u32 start, u32 count )
302 {
303 glDrawElements( GL_TRIANGLES, count, GL_UNSIGNED_INT,
304 (void *)(start*sizeof(u32)) );
305 }
306
307 void mesh_draw( glmesh *mesh )
308 {
309 mesh_drawn( 0, mesh->indice_count );
310 }
311
312 void mesh_free( glmesh *mesh )
313 {
314 if( mesh->loaded )
315 {
316 glDeleteVertexArrays( 1, &mesh->vao );
317 glDeleteBuffers( 1, &mesh->ebo );
318 glDeleteBuffers( 1, &mesh->vbo );
319 mesh->loaded = 0;
320 }
321 }
322
323 void mdl_draw_submesh( mdl_submesh *sm )
324 {
325 mesh_drawn( sm->indice_start, sm->indice_count );
326 }
327
328 mdl_mesh *mdl_find_mesh( mdl_context *mdl, const char *name )
329 {
330 for( u32 i=0; i<mdl_arrcount( &mdl->meshs ); i++ )
331 {
332 mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
333 if( !strcmp( name, mdl_pstr( mdl, mesh->pstr_name )))
334 {
335 return mesh;
336 }
337 }
338 return NULL;
339 }
340
341 struct payload_glmesh_load
342 {
343 mdl_vert *verts;
344 u32 *indices;
345
346 u32 vertex_count,
347 indice_count;
348
349 glmesh *mesh;
350 };
351
352 static void _sync_mdl_load_glmesh( void *payload, u32 size )
353 {
354 struct payload_glmesh_load *job = payload;
355 mesh_upload( job->mesh, job->verts, job->vertex_count,
356 job->indices, job->indice_count );
357 }
358
359 void mdl_async_load_glmesh( mdl_context *mdl, glmesh *mesh, u32 *fixup_table )
360 {
361 mdl_array *arr_vertices = mdl_find_array( mdl, "mdl_vert" );
362 mdl_array *arr_indices = mdl_find_array( mdl, "mdl_indice" );
363
364 if( arr_vertices && arr_indices )
365 {
366 u32 size_verts = vg_align8(sizeof(mdl_vert)*arr_vertices->item_count),
367 size_indices = vg_align8(sizeof(mdl_indice)*arr_indices->item_count),
368 size_hdr = vg_align8(sizeof(struct payload_glmesh_load)),
369 total = size_hdr + size_verts + size_indices;
370
371 vg_async_item *call = vg_async_alloc( total );
372 struct payload_glmesh_load *job = call->payload;
373
374 u8 *payload = call->payload;
375
376 job->mesh = mesh;
377 job->verts = (void*)(payload + size_hdr);
378 job->indices = (void*)(payload + size_hdr + size_verts);
379 job->vertex_count = arr_vertices->item_count;
380 job->indice_count = arr_indices->item_count;
381
382 mdl_load_array_file_buffer( mdl, arr_vertices,
383 job->verts, sizeof(mdl_vert) );
384 mdl_load_array_file_buffer( mdl, arr_indices, job->indices,
385 sizeof(mdl_indice) );
386
387 if( fixup_table )
388 {
389 for( u32 i=0; i<job->vertex_count; i ++ )
390 {
391 mdl_vert *vert = &job->verts[i];
392
393 for( u32 j=0; j<4; j++ )
394 {
395 vert->groups[j] = fixup_table[vert->groups[j]];
396 }
397 }
398 }
399
400 /*
401 * Unpack the indices (if there are meshes)
402 * ---------------------------------------------------------
403 */
404
405 if( mdl_arrcount( &mdl->submeshs ) )
406 {
407 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, 0 );
408 u32 offset = sm->vertex_count;
409
410 for( u32 i=1; i<mdl_arrcount( &mdl->submeshs ); i++ )
411 {
412 mdl_submesh *sm = mdl_arritm( &mdl->submeshs, i );
413 u32 *indices = job->indices + sm->indice_start;
414
415 for( u32 j=0; j<sm->indice_count; j++ )
416 indices[j] += offset;
417
418 offset += sm->vertex_count;
419 }
420 }
421
422 /*
423 * Dispatch
424 * -------------------------
425 */
426
427 vg_async_dispatch( call, _sync_mdl_load_glmesh );
428 }
429 else
430 {
431 vg_fatal_error( "no vertex/indice data\n" );
432 }
433 }
434
435 /* uploads the glmesh, and textures. everything is saved into the mdl_context */
436 void mdl_async_full_load_std( mdl_context *mdl )
437 {
438 mdl_async_load_glmesh( mdl, &mdl->mesh, NULL );
439
440 for( u32 i=0; i<mdl_arrcount( &mdl->textures ); i ++ )
441 {
442 vg_linear_clear( vg_mem.scratch );
443 mdl_texture *tex = mdl_arritm( &mdl->textures, i );
444
445 void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size );
446 mdl_fread_pack_file( mdl, &tex->file, data );
447
448 vg_tex2d_load_qoi_async( data, tex->file.pack_size,
449 VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, &tex->glname );
450 }
451 }