Debian Codegraph @

libs/picomodel/pm_gltf.c

raw symbols SCIP
1 /* -----------------------------------------------------------------------------
2
3 PicoModel Library - glTF 2.0 support via cgltf
4
5 Supports both .gltf (JSON) and .glb (binary) files.
6
7 Uses cgltf single-header library: https://github.com/jkuhlmann/cgltf
8
9 ----------------------------------------------------------------------------- */
10
11 /* dependencies */
12 #define CGLTF_IMPLEMENTATION
13 #include "cgltf.h"
14 #include "picointernal.h"
15
16 /* _gltf_canload:
17 * validates a glTF/GLB model file.
18 */
19 static int _gltf_canload( PM_PARAMS_CANLOAD ){
20 /* check data length */
21 if ( bufSize < 12 ) {
22 return PICO_PMV_ERROR_SIZE;
23 }
24
25 /* check file extension */
26 if ( _pico_stristr( fileName, ".glb" ) != NULL ) {
27 /* binary glTF - validate magic bytes */
28 const unsigned char *b = (const unsigned char *)buffer;
29 if ( b[0] == 'g' && b[1] == 'l' && b[2] == 'T' && b[3] == 'F' ) {
30 return PICO_PMV_OK;
31 }
32 return PICO_PMV_ERROR_IDENT;
33 }
34
35 if ( _pico_stristr( fileName, ".gltf" ) != NULL ) {
36 return PICO_PMV_OK;
37 }
38
39 return PICO_PMV_ERROR;
40 }
41
42 /* cgltf file read callback that uses picomodel's VFS */
43 static cgltf_result _gltf_file_read( const struct cgltf_memory_options *memory_options,
44 const struct cgltf_file_options *file_options,
45 const char *path, cgltf_size *size, void **data ){
46 unsigned char *buffer = NULL;
47 int bufSize = 0;
48
49 (void)memory_options;
50 (void)file_options;
51
52 _pico_load_file( path, &buffer, &bufSize );
53 if ( buffer == NULL || bufSize < 0 ) {
54 return cgltf_result_file_not_found;
55 }
56
57 *data = buffer;
58 *size = (cgltf_size)bufSize;
59 return cgltf_result_success;
60 }
61
62 /* cgltf file release callback */
63 static void _gltf_file_release( const struct cgltf_memory_options *memory_options,
64 const struct cgltf_file_options *file_options,
65 void *data ){
66 (void)memory_options;
67 (void)file_options;
68
69 if ( data != NULL ) {
70 _pico_free_file( data );
71 }
72 }
73
74 /* _gltf_extract_image:
75 * Extracts an embedded glTF image (data URI or buffer_view) to a file
76 * next to the model. Returns a pointer to an allocated string with the
77 * VFS-relative path on success, or NULL on failure.
78 * The caller must free the returned string with _pico_free().
79 */
80 static char *_gltf_extract_image( const cgltf_options *options,
81 const char *modelFileName, const cgltf_image *img, int imageIndex ){
82 const unsigned char *imageData = NULL;
83 cgltf_size imageSize = 0;
84 void *decodedData = NULL;
85 const char *ext = ".png";
86 char outPath[1024];
87 char modelDir[1024];
88 char baseName[256];
89 const char *p;
90
91 /* determine file extension from mime type */
92 if ( img->mime_type != NULL ) {
93 if ( _pico_stristr( img->mime_type, "jpeg" ) != NULL ||
94 _pico_stristr( img->mime_type, "jpg" ) != NULL ) {
95 ext = ".jpg";
96 }
97 }
98
99 /* try to get image data from data URI */
100 if ( img->uri != NULL && strncmp( img->uri, "data:", 5 ) == 0 ) {
101 const char *comma = strchr( img->uri, ',' );
102 if ( comma != NULL && comma - img->uri >= 7 &&
103 strncmp( comma - 7, ";base64", 7 ) == 0 ) {
104 /* detect extension from data URI mime type (e.g. data:image/jpeg;base64) */
105 if ( _pico_stristr( img->uri, "image/jpeg" ) != NULL ) {
106 ext = ".jpg";
107 }
108 const char *base64Start = comma + 1;
109 cgltf_size encodedLen = strlen( base64Start );
110 /* calculate exact decoded size accounting for padding */
111 int padding = 0;
112 if ( encodedLen > 0 && base64Start[encodedLen - 1] == '=' ) {
113 padding++;
114 if ( encodedLen > 1 && base64Start[encodedLen - 2] == '=' ) {
115 padding++;
116 }
117 }
118 cgltf_size decodedSize = ( encodedLen / 4 ) * 3 - padding;
119
120 if ( decodedSize > 0 &&
121 cgltf_load_buffer_base64( options, decodedSize, base64Start, &decodedData ) == cgltf_result_success ) {
122 imageData = (const unsigned char *)decodedData;
123 imageSize = decodedSize;
124
125 /* detect actual format from magic bytes */
126 if ( imageSize >= 8 && imageData[0] == 0x89 && imageData[1] == 'P' &&
127 imageData[2] == 'N' && imageData[3] == 'G' ) {
128 ext = ".png";
129 }
130 else if ( imageSize >= 2 && imageData[0] == 0xFF && imageData[1] == 0xD8 ) {
131 ext = ".jpg";
132 }
133 }
134 }
135 }
136
137 /* try to get image data from buffer_view (GLB embedded) */
138 if ( imageData == NULL && img->buffer_view != NULL ) {
139 if ( img->buffer_view->buffer != NULL &&
140 img->buffer_view->buffer->data != NULL ) {
141 imageData = (const unsigned char *)img->buffer_view->buffer->data +
142 img->buffer_view->offset;
143 imageSize = img->buffer_view->size;
144 }
145 }
146
147 if ( imageData == NULL || imageSize == 0 ) {
148 if ( decodedData != NULL ) {
149 _pico_free( decodedData );
150 }
151 return NULL;
152 }
153
154 /* build output path: same directory as model, with image name or index */
155 _pico_nofname( modelFileName, modelDir, sizeof( modelDir ) );
156
157 if ( img->name != NULL && img->name[0] != '\0' ) {
158 snprintf( baseName, sizeof( baseName ), "%s", img->name );
159 /* strip any existing extension */
160 _pico_setfext( baseName, NULL );
161 }
162 else {
163 /* extract model base name for unique naming */
164 p = _pico_nopath( modelFileName );
165 snprintf( baseName, sizeof( baseName ), "%s", p );
166 _pico_setfext( baseName, NULL );
167 /* append image index */
168 {
169 char suffix[32];
170 snprintf( suffix, sizeof( suffix ), "_img%d", imageIndex );
171 if ( strlen( baseName ) + strlen( suffix ) < sizeof( baseName ) ) {
172 strcat( baseName, suffix );
173 }
174 }
175 }
176
177 snprintf( outPath, sizeof( outPath ), "%s%s%s", modelDir, baseName, ext );
178
179 /* write the file via the save callback */
180 if ( _pico_save_file( outPath, imageData, (int)imageSize ) ) {
181 char *result_path;
182 if ( decodedData != NULL ) {
183 _pico_free( decodedData );
184 }
185 result_path = _pico_clone_alloc( outPath );
186 return result_path;
187 }
188
189 if ( decodedData != NULL ) {
190 _pico_free( decodedData );
191 }
192 return NULL;
193 }
194
195 /* _gltf_load:
196 * loads a glTF/GLB model file.
197 */
198 static picoModel_t *_gltf_load( PM_PARAMS_LOAD ){
199 cgltf_options options;
200 cgltf_data *data = NULL;
201 cgltf_result result;
202 picoModel_t *model;
203 cgltf_size mi, pi, ni;
204 int surfNum = 0;
205
206 /* initialize cgltf options with VFS callbacks */
207 memset( &options, 0, sizeof( options ) );
208 options.file.read = _gltf_file_read;
209 options.file.release = _gltf_file_release;
210
211 /* parse the glTF/GLB data from the buffer picomodel already loaded */
212 result = cgltf_parse( &options, buffer, (cgltf_size)bufSize, &data );
213 if ( result != cgltf_result_success ) {
214 _pico_printf( PICO_ERROR, "glTF: failed to parse '%s' (error %d)", fileName, (int)result );
215 return NULL;
216 }
217
218 /* load external buffer data (.bin files for .gltf, or embedded in .glb) */
219 result = cgltf_load_buffers( &options, data, fileName );
220 if ( result != cgltf_result_success ) {
221 _pico_printf( PICO_ERROR, "glTF: failed to load buffers for '%s' (error %d)", fileName, (int)result );
222 cgltf_free( data );
223 return NULL;
224 }
225
226 /* validate the parsed data */
227 result = cgltf_validate( data );
228 if ( result != cgltf_result_success ) {
229 _pico_printf( PICO_WARNING, "glTF: validation warnings for '%s' (error %d)", fileName, (int)result );
230 /* continue anyway - many files have minor validation issues */
231 }
232
233 /* create a new pico model */
234 model = PicoNewModel();
235 if ( model == NULL ) {
236 _pico_printf( PICO_ERROR, "glTF: failed to allocate model" );
237 cgltf_free( data );
238 return NULL;
239 }
240
241 /* set model name */
242 PicoSetModelName( model, fileName );
243 PicoSetModelFileName( model, fileName );
244
245 /* iterate all nodes to process meshes with their transforms */
246 for ( ni = 0; ni < data->nodes_count; ni++ )
247 {
248 cgltf_node *node = &data->nodes[ni];
249 cgltf_mesh *mesh;
250 float worldMatrix[16];
251
252 if ( node->mesh == NULL ) {
253 continue;
254 }
255
256 mesh = node->mesh;
257
258 /* compute the world transform for this node */
259 cgltf_node_transform_world( node, worldMatrix );
260
261 /* iterate primitives in this mesh */
262 for ( pi = 0; pi < mesh->primitives_count; pi++ )
263 {
264 cgltf_primitive *prim = &mesh->primitives[pi];
265 const cgltf_accessor *posAccessor = NULL;
266 const cgltf_accessor *normAccessor = NULL;
267 const cgltf_accessor *texAccessor = NULL;
268 const cgltf_accessor *colorAccessor = NULL;
269 picoSurface_t *surface;
270 picoShader_t *shader;
271 cgltf_size v, idx;
272 int hasTransform;
273
274 /* only handle triangle primitives */
275 if ( prim->type != cgltf_primitive_type_triangles ) {
276 continue;
277 }
278
279 /* find vertex attributes */
280 for ( mi = 0; mi < prim->attributes_count; mi++ )
281 {
282 cgltf_attribute *attr = &prim->attributes[mi];
283 switch ( attr->type )
284 {
285 case cgltf_attribute_type_position:
286 posAccessor = attr->data;
287 break;
288 case cgltf_attribute_type_normal:
289 normAccessor = attr->data;
290 break;
291 case cgltf_attribute_type_texcoord:
292 if ( attr->index == 0 ) {
293 texAccessor = attr->data;
294 }
295 break;
296 case cgltf_attribute_type_color:
297 if ( attr->index == 0 ) {
298 colorAccessor = attr->data;
299 }
300 break;
301 default:
302 break;
303 }
304 }
305
306 /* position is required */
307 if ( posAccessor == NULL ) {
308 _pico_printf( PICO_WARNING, "glTF: skipping primitive without POSITION attribute in '%s'", fileName );
309 continue;
310 }
311
312 /* create a new surface */
313 surface = PicoNewSurface( model );
314 if ( surface == NULL ) {
315 _pico_printf( PICO_ERROR, "glTF: failed to allocate surface" );
316 PicoFreeModel( model );
317 cgltf_free( data );
318 return NULL;
319 }
320
321 PicoSetSurfaceType( surface, PICO_TRIANGLES );
322
323 /* set surface name */
324 if ( mesh->name != NULL ) {
325 char surfName[128];
326 if ( mesh->primitives_count > 1 ) {
327 snprintf( surfName, sizeof( surfName ), "%s_%d", mesh->name, (int)pi );
328 }
329 else {
330 snprintf( surfName, sizeof( surfName ), "%s", mesh->name );
331 }
332 PicoSetSurfaceName( surface, surfName );
333 }
334 else {
335 char surfName[64];
336 snprintf( surfName, sizeof( surfName ), "surface_%d", surfNum );
337 PicoSetSurfaceName( surface, surfName );
338 }
339
340 /* set up shader/material */
341 if ( prim->material != NULL && prim->material->name != NULL ) {
342 shader = PicoNewShader( model );
343 if ( shader != NULL ) {
344 PicoSetShaderName( shader, prim->material->name );
345
346 /* try to get the base color texture name for the shader path */
347 if ( prim->material->has_pbr_metallic_roughness &&
348 prim->material->pbr_metallic_roughness.base_color_texture.texture != NULL &&
349 prim->material->pbr_metallic_roughness.base_color_texture.texture->image != NULL ) {
350 cgltf_image *img = prim->material->pbr_metallic_roughness.base_color_texture.texture->image;
351 if ( img->uri != NULL && strncmp( img->uri, "data:", 5 ) != 0 ) {
352 PicoSetShaderMapName( shader, img->uri );
353 }
354 else {
355 /* try to extract embedded image to disk */
356 int imgIdx = (int)( img - data->images );
357 char *extractedPath = _gltf_extract_image( &options, fileName, img, imgIdx );
358 if ( extractedPath != NULL ) {
359 PicoSetShaderMapName( shader, extractedPath );
360 _pico_free( extractedPath );
361 }
362 else if ( img->name != NULL ) {
363 PicoSetShaderMapName( shader, img->name );
364 }
365 }
366 }
367
368 PicoSetSurfaceShader( surface, shader );
369 }
370 }
371
372 /* check if the node has a non-identity transform */
373 hasTransform = node->has_translation || node->has_rotation ||
374 node->has_scale || node->has_matrix;
375
376 /* read vertices */
377 for ( v = 0; v < posAccessor->count; v++ )
378 {
379 picoVec3_t xyz;
380 cgltf_float pos[3] = { 0, 0, 0 };
381
382 cgltf_accessor_read_float( posAccessor, v, pos, 3 );
383
384 /* glTF is right-handed Y-up, Quake is right-handed Z-up */
385 /* transform: X stays, Y becomes Z, Z becomes -Y */
386 if ( hasTransform ) {
387 /* apply node transform first, then coordinate system conversion */
388 cgltf_float transformed[3];
389 transformed[0] = worldMatrix[0] * pos[0] + worldMatrix[4] * pos[1] + worldMatrix[8] * pos[2] + worldMatrix[12];
390 transformed[1] = worldMatrix[1] * pos[0] + worldMatrix[5] * pos[1] + worldMatrix[9] * pos[2] + worldMatrix[13];
391 transformed[2] = worldMatrix[2] * pos[0] + worldMatrix[6] * pos[1] + worldMatrix[10] * pos[2] + worldMatrix[14];
392 xyz[0] = transformed[0];
393 xyz[1] = -transformed[2];
394 xyz[2] = transformed[1];
395 }
396 else {
397 xyz[0] = pos[0];
398 xyz[1] = -pos[2];
399 xyz[2] = pos[1];
400 }
401
402 PicoSetSurfaceXYZ( surface, (int)v, xyz );
403
404 /* normals */
405 if ( normAccessor != NULL && v < normAccessor->count ) {
406 picoVec3_t normal;
407 cgltf_float n[3] = { 0, 0, 1 };
408
409 cgltf_accessor_read_float( normAccessor, v, n, 3 );
410
411 if ( hasTransform ) {
412 cgltf_float tn[3];
413 /* transform normal by the 3x3 rotation part of the world matrix */
414 tn[0] = worldMatrix[0] * n[0] + worldMatrix[4] * n[1] + worldMatrix[8] * n[2];
415 tn[1] = worldMatrix[1] * n[0] + worldMatrix[5] * n[1] + worldMatrix[9] * n[2];
416 tn[2] = worldMatrix[2] * n[0] + worldMatrix[6] * n[1] + worldMatrix[10] * n[2];
417 normal[0] = tn[0];
418 normal[1] = -tn[2];
419 normal[2] = tn[1];
420 }
421 else {
422 normal[0] = n[0];
423 normal[1] = -n[2];
424 normal[2] = n[1];
425 }
426
427 PicoSetSurfaceNormal( surface, (int)v, normal );
428 }
429
430 /* texture coordinates */
431 if ( texAccessor != NULL && v < texAccessor->count ) {
432 picoVec2_t st;
433 cgltf_float uv[2] = { 0, 0 };
434
435 cgltf_accessor_read_float( texAccessor, v, uv, 2 );
436
437 st[0] = uv[0];
438 st[1] = uv[1];
439 PicoSetSurfaceST( surface, 0, (int)v, st );
440 }
441
442 /* vertex colors */
443 if ( colorAccessor != NULL && v < colorAccessor->count ) {
444 picoColor_t color;
445 cgltf_float rgba[4] = { 1, 1, 1, 1 };
446 int numComponents;
447
448 numComponents = (int)cgltf_num_components( colorAccessor->type );
449 cgltf_accessor_read_float( colorAccessor, v, rgba, numComponents < 4 ? numComponents : 4 );
450
451 color[0] = (picoByte_t)( rgba[0] * 255.0f + 0.5f );
452 color[1] = (picoByte_t)( rgba[1] * 255.0f + 0.5f );
453 color[2] = (picoByte_t)( rgba[2] * 255.0f + 0.5f );
454 color[3] = (picoByte_t)( rgba[3] * 255.0f + 0.5f );
455 PicoSetSurfaceColor( surface, 0, (int)v, color );
456 }
457 else {
458 picoColor_t color;
459 _pico_set_color( color, 255, 255, 255, 255 );
460 PicoSetSurfaceColor( surface, 0, (int)v, color );
461 }
462 }
463
464 /* read indices - reverse winding order because the Y-up to Z-up
465 coordinate conversion flips handedness */
466 if ( prim->indices != NULL ) {
467 for ( idx = 0; idx + 2 < prim->indices->count; idx += 3 )
468 {
469 cgltf_size i0 = cgltf_accessor_read_index( prim->indices, idx );
470 cgltf_size i1 = cgltf_accessor_read_index( prim->indices, idx + 1 );
471 cgltf_size i2 = cgltf_accessor_read_index( prim->indices, idx + 2 );
472 PicoSetSurfaceIndex( surface, (int)idx, (picoIndex_t)i0 );
473 PicoSetSurfaceIndex( surface, (int)idx + 1, (picoIndex_t)i2 );
474 PicoSetSurfaceIndex( surface, (int)idx + 2, (picoIndex_t)i1 );
475 }
476 }
477 else {
478 /* non-indexed geometry: generate sequential indices with reversed winding */
479 for ( idx = 0; idx + 2 < posAccessor->count; idx += 3 )
480 {
481 PicoSetSurfaceIndex( surface, (int)idx, (picoIndex_t)idx );
482 PicoSetSurfaceIndex( surface, (int)idx + 1, (picoIndex_t)( idx + 2 ) );
483 PicoSetSurfaceIndex( surface, (int)idx + 2, (picoIndex_t)( idx + 1 ) );
484 }
485 }
486
487 surfNum++;
488 }
489 }
490
491 /* if no nodes reference meshes (orphaned meshes), load them without transforms */
492 if ( surfNum == 0 ) {
493 for ( mi = 0; mi < data->meshes_count; mi++ )
494 {
495 cgltf_mesh *mesh = &data->meshes[mi];
496
497 for ( pi = 0; pi < mesh->primitives_count; pi++ )
498 {
499 cgltf_primitive *prim = &mesh->primitives[pi];
500 const cgltf_accessor *posAccessor = NULL;
501 const cgltf_accessor *normAccessor = NULL;
502 const cgltf_accessor *texAccessor = NULL;
503 const cgltf_accessor *colorAccessor = NULL;
504 picoSurface_t *surface;
505 picoShader_t *shader;
506 cgltf_size v, idx, ai;
507
508 if ( prim->type != cgltf_primitive_type_triangles ) {
509 continue;
510 }
511
512 for ( ai = 0; ai < prim->attributes_count; ai++ )
513 {
514 cgltf_attribute *attr = &prim->attributes[ai];
515 switch ( attr->type )
516 {
517 case cgltf_attribute_type_position:
518 posAccessor = attr->data;
519 break;
520 case cgltf_attribute_type_normal:
521 normAccessor = attr->data;
522 break;
523 case cgltf_attribute_type_texcoord:
524 if ( attr->index == 0 ) {
525 texAccessor = attr->data;
526 }
527 break;
528 case cgltf_attribute_type_color:
529 if ( attr->index == 0 ) {
530 colorAccessor = attr->data;
531 }
532 break;
533 default:
534 break;
535 }
536 }
537
538 if ( posAccessor == NULL ) {
539 continue;
540 }
541
542 surface = PicoNewSurface( model );
543 if ( surface == NULL ) {
544 PicoFreeModel( model );
545 cgltf_free( data );
546 return NULL;
547 }
548
549 PicoSetSurfaceType( surface, PICO_TRIANGLES );
550
551 if ( mesh->name != NULL ) {
552 char surfName[128];
553 if ( mesh->primitives_count > 1 ) {
554 snprintf( surfName, sizeof( surfName ), "%s_%d", mesh->name, (int)pi );
555 }
556 else {
557 snprintf( surfName, sizeof( surfName ), "%s", mesh->name );
558 }
559 PicoSetSurfaceName( surface, surfName );
560 }
561 else {
562 char surfName[64];
563 snprintf( surfName, sizeof( surfName ), "surface_%d", surfNum );
564 PicoSetSurfaceName( surface, surfName );
565 }
566
567 if ( prim->material != NULL && prim->material->name != NULL ) {
568 shader = PicoNewShader( model );
569 if ( shader != NULL ) {
570 PicoSetShaderName( shader, prim->material->name );
571
572 if ( prim->material->has_pbr_metallic_roughness &&
573 prim->material->pbr_metallic_roughness.base_color_texture.texture != NULL &&
574 prim->material->pbr_metallic_roughness.base_color_texture.texture->image != NULL ) {
575 cgltf_image *img = prim->material->pbr_metallic_roughness.base_color_texture.texture->image;
576 if ( img->uri != NULL && strncmp( img->uri, "data:", 5 ) != 0 ) {
577 PicoSetShaderMapName( shader, img->uri );
578 }
579 else {
580 /* try to extract embedded image to disk */
581 int imgIdx = (int)( img - data->images );
582 char *extractedPath = _gltf_extract_image( &options, fileName, img, imgIdx );
583 if ( extractedPath != NULL ) {
584 PicoSetShaderMapName( shader, extractedPath );
585 _pico_free( extractedPath );
586 }
587 else if ( img->name != NULL ) {
588 PicoSetShaderMapName( shader, img->name );
589 }
590 }
591 }
592
593 PicoSetSurfaceShader( surface, shader );
594 }
595 }
596
597 for ( v = 0; v < posAccessor->count; v++ )
598 {
599 picoVec3_t xyz;
600 cgltf_float pos[3] = { 0, 0, 0 };
601
602 cgltf_accessor_read_float( posAccessor, v, pos, 3 );
603
604 /* Y-up to Z-up conversion */
605 xyz[0] = pos[0];
606 xyz[1] = -pos[2];
607 xyz[2] = pos[1];
608 PicoSetSurfaceXYZ( surface, (int)v, xyz );
609
610 if ( normAccessor != NULL && v < normAccessor->count ) {
611 picoVec3_t normal;
612 cgltf_float n[3] = { 0, 0, 1 };
613 cgltf_accessor_read_float( normAccessor, v, n, 3 );
614
615 normal[0] = n[0];
616 normal[1] = -n[2];
617 normal[2] = n[1];
618 PicoSetSurfaceNormal( surface, (int)v, normal );
619 }
620
621 if ( texAccessor != NULL && v < texAccessor->count ) {
622 picoVec2_t st;
623 cgltf_float uv[2] = { 0, 0 };
624 cgltf_accessor_read_float( texAccessor, v, uv, 2 );
625 st[0] = uv[0];
626 st[1] = uv[1];
627 PicoSetSurfaceST( surface, 0, (int)v, st );
628 }
629
630 if ( colorAccessor != NULL && v < colorAccessor->count ) {
631 picoColor_t color;
632 cgltf_float rgba[4] = { 1, 1, 1, 1 };
633 int numComponents = (int)cgltf_num_components( colorAccessor->type );
634 cgltf_accessor_read_float( colorAccessor, v, rgba, numComponents < 4 ? numComponents : 4 );
635 color[0] = (picoByte_t)( rgba[0] * 255.0f + 0.5f );
636 color[1] = (picoByte_t)( rgba[1] * 255.0f + 0.5f );
637 color[2] = (picoByte_t)( rgba[2] * 255.0f + 0.5f );
638 color[3] = (picoByte_t)( rgba[3] * 255.0f + 0.5f );
639 PicoSetSurfaceColor( surface, 0, (int)v, color );
640 }
641 else {
642 picoColor_t color;
643 _pico_set_color( color, 255, 255, 255, 255 );
644 PicoSetSurfaceColor( surface, 0, (int)v, color );
645 }
646 }
647
648 /* reverse winding order because the Y-up to Z-up
649 coordinate conversion flips handedness */
650 if ( prim->indices != NULL ) {
651 for ( idx = 0; idx + 2 < prim->indices->count; idx += 3 )
652 {
653 cgltf_size i0 = cgltf_accessor_read_index( prim->indices, idx );
654 cgltf_size i1 = cgltf_accessor_read_index( prim->indices, idx + 1 );
655 cgltf_size i2 = cgltf_accessor_read_index( prim->indices, idx + 2 );
656 PicoSetSurfaceIndex( surface, (int)idx, (picoIndex_t)i0 );
657 PicoSetSurfaceIndex( surface, (int)idx + 1, (picoIndex_t)i2 );
658 PicoSetSurfaceIndex( surface, (int)idx + 2, (picoIndex_t)i1 );
659 }
660 }
661 else {
662 for ( idx = 0; idx + 2 < posAccessor->count; idx += 3 )
663 {
664 PicoSetSurfaceIndex( surface, (int)idx, (picoIndex_t)idx );
665 PicoSetSurfaceIndex( surface, (int)idx + 1, (picoIndex_t)( idx + 2 ) );
666 PicoSetSurfaceIndex( surface, (int)idx + 2, (picoIndex_t)( idx + 1 ) );
667 }
668 }
669
670 surfNum++;
671 }
672 }
673 }
674
675 if ( surfNum == 0 ) {
676 _pico_printf( PICO_WARNING, "glTF: no triangle surfaces found in '%s'", fileName );
677 }
678
679 cgltf_free( data );
680 return model;
681 }
682
683 /* pico file format module definition */
684 const picoModule_t picoModuleGLTF =
685 {
686 "1.0", /* module version string */
687 "glTF 2.0", /* module display name */
688 "cgltf/picomodel", /* author's name */
689 "2026 Martin Gerhardy", /* module copyright */
690 {
691 "gltf","glb",NULL,NULL /* default extensions to use */
692 },
693 _gltf_canload, /* validation routine */
694 _gltf_load, /* load routine */
695 NULL, /* save validation routine */
696 NULL /* save routine */
697 };