forked from jkuhlmann/cgltf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgltf_write.h
1543 lines (1392 loc) · 50.9 KB
/
cgltf_write.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* cgltf_write - a single-file glTF 2.0 writer written in C99.
*
* Version: 1.14
*
* Website: https://github.com/jkuhlmann/cgltf
*
* Distributed under the MIT License, see notice at the end of this file.
*
* Building:
* Include this file where you need the struct and function
* declarations. Have exactly one source file where you define
* `CGLTF_WRITE_IMPLEMENTATION` before including this file to get the
* function definitions.
*
* Reference:
* `cgltf_result cgltf_write_file(const cgltf_options* options, const char*
* path, const cgltf_data* data)` writes a glTF data to the given file path.
* If `options->type` is `cgltf_file_type_glb`, both JSON content and binary
* buffer of the given glTF data will be written in a GLB format.
* Otherwise, only the JSON part will be written.
* External buffers and images are not written out. `data` is not deallocated.
*
* `cgltf_size cgltf_write(const cgltf_options* options, char* buffer,
* cgltf_size size, const cgltf_data* data)` writes JSON into the given memory
* buffer. Returns the number of bytes written to `buffer`, including a null
* terminator. If buffer is null, returns the number of bytes that would have
* been written. `data` is not deallocated.
*/
#ifndef CGLTF_WRITE_H_INCLUDED__
#define CGLTF_WRITE_H_INCLUDED__
#include "cgltf.h"
#include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data);
cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef CGLTF_WRITE_H_INCLUDED__ */
/*
*
* Stop now, if you are only interested in the API.
* Below, you find the implementation.
*
*/
#if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
/* This makes MSVC/CLion intellisense work. */
#define CGLTF_WRITE_IMPLEMENTATION
#endif
#ifdef CGLTF_WRITE_IMPLEMENTATION
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#define CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM (1 << 0)
#define CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT (1 << 1)
#define CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS (1 << 2)
#define CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL (1 << 3)
#define CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION (1 << 4)
#define CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT (1 << 5)
#define CGLTF_EXTENSION_FLAG_MATERIALS_IOR (1 << 6)
#define CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR (1 << 7)
#define CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION (1 << 8)
#define CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN (1 << 9)
#define CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS (1 << 10)
#define CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME (1 << 11)
#define CGLTF_EXTENSION_FLAG_TEXTURE_BASISU (1 << 12)
#define CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH (1 << 13)
#define CGLTF_EXTENSION_FLAG_MESH_GPU_INSTANCING (1 << 14)
#define CGLTF_EXTENSION_FLAG_MATERIALS_IRIDESCENCE (1 << 15)
#define CGLTF_EXTENSION_FLAG_MATERIALS_ANISOTROPY (1 << 16)
#define CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION (1 << 17)
#define CGLTF_EXTENSION_FLAG_TEXTURE_WEBP (1 << 18)
typedef struct {
char* buffer;
cgltf_size buffer_size;
cgltf_size remaining;
char* cursor;
cgltf_size tmp;
cgltf_size chars_written;
const cgltf_data* data;
int depth;
const char* indent;
int needs_comma;
uint32_t extension_flags;
uint32_t required_extension_flags;
} cgltf_write_context;
#define CGLTF_MIN(a, b) (a < b ? a : b)
#ifdef FLT_DECIMAL_DIG
// FLT_DECIMAL_DIG is C11
#define CGLTF_DECIMAL_DIG (FLT_DECIMAL_DIG)
#else
#define CGLTF_DECIMAL_DIG 9
#endif
#define CGLTF_SPRINTF(...) { \
assert(context->cursor || (!context->cursor && context->remaining == 0)); \
context->tmp = snprintf ( context->cursor, context->remaining, __VA_ARGS__ ); \
context->chars_written += context->tmp; \
if (context->cursor) { \
context->cursor += context->tmp; \
context->remaining -= context->tmp; \
} }
#define CGLTF_SNPRINTF(length, ...) { \
assert(context->cursor || (!context->cursor && context->remaining == 0)); \
context->tmp = snprintf ( context->cursor, CGLTF_MIN(length + 1, context->remaining), __VA_ARGS__ ); \
context->chars_written += length; \
if (context->cursor) { \
context->cursor += length; \
context->remaining -= length; \
} }
#define CGLTF_WRITE_IDXPROP(label, val, start) if (val) { \
cgltf_write_indent(context); \
CGLTF_SPRINTF("\"%s\": %d", label, (int) (val - start)); \
context->needs_comma = 1; }
#define CGLTF_WRITE_IDXARRPROP(label, dim, vals, start) if (vals) { \
cgltf_write_indent(context); \
CGLTF_SPRINTF("\"%s\": [", label); \
for (int i = 0; i < (int)(dim); ++i) { \
int idx = (int) (vals[i] - start); \
if (i != 0) CGLTF_SPRINTF(","); \
CGLTF_SPRINTF(" %d", idx); \
} \
CGLTF_SPRINTF(" ]"); \
context->needs_comma = 1; }
#define CGLTF_WRITE_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
if (info.has_transform) { \
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
cgltf_write_texture_transform(context, &info.transform); \
} \
cgltf_write_line(context, "}"); }
#define CGLTF_WRITE_NORMAL_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
cgltf_write_floatprop(context, "scale", info.scale, 1.0f); \
if (info.has_transform) { \
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
cgltf_write_texture_transform(context, &info.transform); \
} \
cgltf_write_line(context, "}"); }
#define CGLTF_WRITE_OCCLUSION_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
cgltf_write_floatprop(context, "strength", info.scale, 1.0f); \
if (info.has_transform) { \
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
cgltf_write_texture_transform(context, &info.transform); \
} \
cgltf_write_line(context, "}"); }
#ifndef CGLTF_CONSTS
#define GlbHeaderSize 12
#define GlbChunkHeaderSize 8
static const uint32_t GlbVersion = 2;
static const uint32_t GlbMagic = 0x46546C67;
static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
static const uint32_t GlbMagicBinChunk = 0x004E4942;
#define CGLTF_CONSTS
#endif
static void cgltf_write_indent(cgltf_write_context* context)
{
if (context->needs_comma)
{
CGLTF_SPRINTF(",\n");
context->needs_comma = 0;
}
else
{
CGLTF_SPRINTF("\n");
}
for (int i = 0; i < context->depth; ++i)
{
CGLTF_SPRINTF("%s", context->indent);
}
}
static void cgltf_write_line(cgltf_write_context* context, const char* line)
{
if (line[0] == ']' || line[0] == '}')
{
--context->depth;
context->needs_comma = 0;
}
cgltf_write_indent(context);
CGLTF_SPRINTF("%s", line);
cgltf_size last = (cgltf_size)(strlen(line) - 1);
if (line[0] == ']' || line[0] == '}')
{
context->needs_comma = 1;
}
if (line[last] == '[' || line[last] == '{')
{
++context->depth;
context->needs_comma = 0;
}
}
static void cgltf_write_strprop(cgltf_write_context* context, const char* label, const char* val)
{
if (val)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": \"%s\"", label, val);
context->needs_comma = 1;
}
}
static void cgltf_write_extras(cgltf_write_context* context, const cgltf_extras* extras)
{
if (extras->data)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"extras\": %s", extras->data);
context->needs_comma = 1;
}
else
{
cgltf_size length = extras->end_offset - extras->start_offset;
if (length > 0 && context->data->json)
{
char* json_string = ((char*) context->data->json) + extras->start_offset;
cgltf_write_indent(context);
CGLTF_SPRINTF("%s", "\"extras\": ");
CGLTF_SNPRINTF(length, "%.*s", (int)(extras->end_offset - extras->start_offset), json_string);
context->needs_comma = 1;
}
}
}
static void cgltf_write_stritem(cgltf_write_context* context, const char* item)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\"", item);
context->needs_comma = 1;
}
static void cgltf_write_intprop(cgltf_write_context* context, const char* label, int val, int def)
{
if (val != def)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": %d", label, val);
context->needs_comma = 1;
}
}
static void cgltf_write_sizeprop(cgltf_write_context* context, const char* label, cgltf_size val, cgltf_size def)
{
if (val != def)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": %zu", label, val);
context->needs_comma = 1;
}
}
static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, float val, float def)
{
if (val != def)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": ", label);
CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, val);
context->needs_comma = 1;
if (context->cursor)
{
char *decimal_comma = strchr(context->cursor - context->tmp, ',');
if (decimal_comma)
{
*decimal_comma = '.';
}
}
}
}
static void cgltf_write_boolprop_optional(cgltf_write_context* context, const char* label, bool val, bool def)
{
if (val != def)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": %s", label, val ? "true" : "false");
context->needs_comma = 1;
}
}
static void cgltf_write_floatarrayprop(cgltf_write_context* context, const char* label, const cgltf_float* vals, cgltf_size dim)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": [", label);
for (cgltf_size i = 0; i < dim; ++i)
{
if (i != 0)
{
CGLTF_SPRINTF(", %.*g", CGLTF_DECIMAL_DIG, vals[i]);
}
else
{
CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, vals[i]);
}
}
CGLTF_SPRINTF("]");
context->needs_comma = 1;
}
static bool cgltf_check_floatarray(const float* vals, int dim, float val) {
while (dim--)
{
if (vals[dim] != val)
{
return true;
}
}
return false;
}
static int cgltf_int_from_component_type(cgltf_component_type ctype)
{
switch (ctype)
{
case cgltf_component_type_r_8: return 5120;
case cgltf_component_type_r_8u: return 5121;
case cgltf_component_type_r_16: return 5122;
case cgltf_component_type_r_16u: return 5123;
case cgltf_component_type_r_32u: return 5125;
case cgltf_component_type_r_32f: return 5126;
default: return 0;
}
}
static int cgltf_int_from_primitive_type(cgltf_primitive_type ctype)
{
switch (ctype)
{
case cgltf_primitive_type_points: return 0;
case cgltf_primitive_type_lines: return 1;
case cgltf_primitive_type_line_loop: return 2;
case cgltf_primitive_type_line_strip: return 3;
case cgltf_primitive_type_triangles: return 4;
case cgltf_primitive_type_triangle_strip: return 5;
case cgltf_primitive_type_triangle_fan: return 6;
default: return -1;
}
}
static const char* cgltf_str_from_alpha_mode(cgltf_alpha_mode alpha_mode)
{
switch (alpha_mode)
{
case cgltf_alpha_mode_mask: return "MASK";
case cgltf_alpha_mode_blend: return "BLEND";
default: return NULL;
}
}
static const char* cgltf_str_from_type(cgltf_type type)
{
switch (type)
{
case cgltf_type_scalar: return "SCALAR";
case cgltf_type_vec2: return "VEC2";
case cgltf_type_vec3: return "VEC3";
case cgltf_type_vec4: return "VEC4";
case cgltf_type_mat2: return "MAT2";
case cgltf_type_mat3: return "MAT3";
case cgltf_type_mat4: return "MAT4";
default: return NULL;
}
}
static cgltf_size cgltf_dim_from_type(cgltf_type type)
{
switch (type)
{
case cgltf_type_scalar: return 1;
case cgltf_type_vec2: return 2;
case cgltf_type_vec3: return 3;
case cgltf_type_vec4: return 4;
case cgltf_type_mat2: return 4;
case cgltf_type_mat3: return 9;
case cgltf_type_mat4: return 16;
default: return 0;
}
}
static const char* cgltf_str_from_camera_type(cgltf_camera_type camera_type)
{
switch (camera_type)
{
case cgltf_camera_type_perspective: return "perspective";
case cgltf_camera_type_orthographic: return "orthographic";
default: return NULL;
}
}
static const char* cgltf_str_from_light_type(cgltf_light_type light_type)
{
switch (light_type)
{
case cgltf_light_type_directional: return "directional";
case cgltf_light_type_point: return "point";
case cgltf_light_type_spot: return "spot";
default: return NULL;
}
}
static void cgltf_write_texture_transform(cgltf_write_context* context, const cgltf_texture_transform* transform)
{
cgltf_write_line(context, "\"extensions\": {");
cgltf_write_line(context, "\"KHR_texture_transform\": {");
if (cgltf_check_floatarray(transform->offset, 2, 0.0f))
{
cgltf_write_floatarrayprop(context, "offset", transform->offset, 2);
}
cgltf_write_floatprop(context, "rotation", transform->rotation, 0.0f);
if (cgltf_check_floatarray(transform->scale, 2, 1.0f))
{
cgltf_write_floatarrayprop(context, "scale", transform->scale, 2);
}
if (transform->has_texcoord)
{
cgltf_write_intprop(context, "texCoord", transform->texcoord, -1);
}
cgltf_write_line(context, "}");
cgltf_write_line(context, "}");
}
static void cgltf_write_asset(cgltf_write_context* context, const cgltf_asset* asset)
{
cgltf_write_line(context, "\"asset\": {");
cgltf_write_strprop(context, "copyright", asset->copyright);
cgltf_write_strprop(context, "generator", asset->generator);
cgltf_write_strprop(context, "version", asset->version);
cgltf_write_strprop(context, "min_version", asset->min_version);
cgltf_write_extras(context, &asset->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_primitive* prim)
{
cgltf_write_intprop(context, "mode", cgltf_int_from_primitive_type(prim->type), 4);
CGLTF_WRITE_IDXPROP("indices", prim->indices, context->data->accessors);
CGLTF_WRITE_IDXPROP("material", prim->material, context->data->materials);
cgltf_write_line(context, "\"attributes\": {");
for (cgltf_size i = 0; i < prim->attributes_count; ++i)
{
const cgltf_attribute* attr = prim->attributes + i;
CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
}
cgltf_write_line(context, "}");
if (prim->targets_count)
{
cgltf_write_line(context, "\"targets\": [");
for (cgltf_size i = 0; i < prim->targets_count; ++i)
{
cgltf_write_line(context, "{");
for (cgltf_size j = 0; j < prim->targets[i].attributes_count; ++j)
{
const cgltf_attribute* attr = prim->targets[i].attributes + j;
CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
}
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "]");
}
cgltf_write_extras(context, &prim->extras);
if (prim->has_draco_mesh_compression || prim->mappings_count > 0)
{
cgltf_write_line(context, "\"extensions\": {");
if (prim->has_draco_mesh_compression)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
if (prim->attributes_count == 0 || prim->indices == 0)
{
context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
}
cgltf_write_line(context, "\"KHR_draco_mesh_compression\": {");
CGLTF_WRITE_IDXPROP("bufferView", prim->draco_mesh_compression.buffer_view, context->data->buffer_views);
cgltf_write_line(context, "\"attributes\": {");
for (cgltf_size i = 0; i < prim->draco_mesh_compression.attributes_count; ++i)
{
const cgltf_attribute* attr = prim->draco_mesh_compression.attributes + i;
CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
}
cgltf_write_line(context, "}");
cgltf_write_line(context, "}");
}
if (prim->mappings_count > 0)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
cgltf_write_line(context, "\"KHR_materials_variants\": {");
cgltf_write_line(context, "\"mappings\": [");
for (cgltf_size i = 0; i < prim->mappings_count; ++i)
{
const cgltf_material_mapping* map = prim->mappings + i;
cgltf_write_line(context, "{");
CGLTF_WRITE_IDXPROP("material", map->material, context->data->materials);
cgltf_write_indent(context);
CGLTF_SPRINTF("\"variants\": [%d]", (int)map->variant);
context->needs_comma = 1;
cgltf_write_extras(context, &map->extras);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "]");
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "}");
}
}
static void cgltf_write_mesh(cgltf_write_context* context, const cgltf_mesh* mesh)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", mesh->name);
cgltf_write_line(context, "\"primitives\": [");
for (cgltf_size i = 0; i < mesh->primitives_count; ++i)
{
cgltf_write_line(context, "{");
cgltf_write_primitive(context, mesh->primitives + i);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "]");
if (mesh->weights_count > 0)
{
cgltf_write_floatarrayprop(context, "weights", mesh->weights, mesh->weights_count);
}
cgltf_write_extras(context, &mesh->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_buffer_view(cgltf_write_context* context, const cgltf_buffer_view* view)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", view->name);
CGLTF_WRITE_IDXPROP("buffer", view->buffer, context->data->buffers);
cgltf_write_sizeprop(context, "byteLength", view->size, (cgltf_size)-1);
cgltf_write_sizeprop(context, "byteOffset", view->offset, 0);
cgltf_write_sizeprop(context, "byteStride", view->stride, 0);
// NOTE: We skip writing "target" because the spec says its usage can be inferred.
cgltf_write_extras(context, &view->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_buffer(cgltf_write_context* context, const cgltf_buffer* buffer)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", buffer->name);
cgltf_write_strprop(context, "uri", buffer->uri);
cgltf_write_sizeprop(context, "byteLength", buffer->size, (cgltf_size)-1);
cgltf_write_extras(context, &buffer->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_material(cgltf_write_context* context, const cgltf_material* material)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", material->name);
if (material->alpha_mode == cgltf_alpha_mode_mask)
{
cgltf_write_floatprop(context, "alphaCutoff", material->alpha_cutoff, 0.5f);
}
cgltf_write_boolprop_optional(context, "doubleSided", (bool)material->double_sided, false);
// cgltf_write_boolprop_optional(context, "unlit", material->unlit, false);
if (material->unlit)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT;
}
if (material->has_pbr_specular_glossiness)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS;
}
if (material->has_clearcoat)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT;
}
if (material->has_transmission)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION;
}
if (material->has_volume)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME;
}
if (material->has_ior)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_IOR;
}
if (material->has_specular)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR;
}
if (material->has_sheen)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN;
}
if (material->has_emissive_strength)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH;
}
if (material->has_iridescence)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_IRIDESCENCE;
}
if (material->has_anisotropy)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_ANISOTROPY;
}
if (material->has_dispersion)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION;
}
if (material->has_pbr_metallic_roughness)
{
const cgltf_pbr_metallic_roughness* params = &material->pbr_metallic_roughness;
cgltf_write_line(context, "\"pbrMetallicRoughness\": {");
CGLTF_WRITE_TEXTURE_INFO("baseColorTexture", params->base_color_texture);
CGLTF_WRITE_TEXTURE_INFO("metallicRoughnessTexture", params->metallic_roughness_texture);
cgltf_write_floatprop(context, "metallicFactor", params->metallic_factor, 1.0f);
cgltf_write_floatprop(context, "roughnessFactor", params->roughness_factor, 1.0f);
if (cgltf_check_floatarray(params->base_color_factor, 4, 1.0f))
{
cgltf_write_floatarrayprop(context, "baseColorFactor", params->base_color_factor, 4);
}
cgltf_write_line(context, "}");
}
if (material->unlit || material->has_pbr_specular_glossiness || material->has_clearcoat || material->has_ior || material->has_specular || material->has_transmission || material->has_sheen || material->has_volume || material->has_emissive_strength || material->has_iridescence || material->has_anisotropy || material->has_dispersion)
{
cgltf_write_line(context, "\"extensions\": {");
if (material->has_clearcoat)
{
const cgltf_clearcoat* params = &material->clearcoat;
cgltf_write_line(context, "\"KHR_materials_clearcoat\": {");
CGLTF_WRITE_TEXTURE_INFO("clearcoatTexture", params->clearcoat_texture);
CGLTF_WRITE_TEXTURE_INFO("clearcoatRoughnessTexture", params->clearcoat_roughness_texture);
CGLTF_WRITE_NORMAL_TEXTURE_INFO("clearcoatNormalTexture", params->clearcoat_normal_texture);
cgltf_write_floatprop(context, "clearcoatFactor", params->clearcoat_factor, 0.0f);
cgltf_write_floatprop(context, "clearcoatRoughnessFactor", params->clearcoat_roughness_factor, 0.0f);
cgltf_write_line(context, "}");
}
if (material->has_ior)
{
const cgltf_ior* params = &material->ior;
cgltf_write_line(context, "\"KHR_materials_ior\": {");
cgltf_write_floatprop(context, "ior", params->ior, 1.5f);
cgltf_write_line(context, "}");
}
if (material->has_specular)
{
const cgltf_specular* params = &material->specular;
cgltf_write_line(context, "\"KHR_materials_specular\": {");
CGLTF_WRITE_TEXTURE_INFO("specularTexture", params->specular_texture);
CGLTF_WRITE_TEXTURE_INFO("specularColorTexture", params->specular_color_texture);
cgltf_write_floatprop(context, "specularFactor", params->specular_factor, 1.0f);
if (cgltf_check_floatarray(params->specular_color_factor, 3, 1.0f))
{
cgltf_write_floatarrayprop(context, "specularColorFactor", params->specular_color_factor, 3);
}
cgltf_write_line(context, "}");
}
if (material->has_transmission)
{
const cgltf_transmission* params = &material->transmission;
cgltf_write_line(context, "\"KHR_materials_transmission\": {");
CGLTF_WRITE_TEXTURE_INFO("transmissionTexture", params->transmission_texture);
cgltf_write_floatprop(context, "transmissionFactor", params->transmission_factor, 0.0f);
cgltf_write_line(context, "}");
}
if (material->has_volume)
{
const cgltf_volume* params = &material->volume;
cgltf_write_line(context, "\"KHR_materials_volume\": {");
CGLTF_WRITE_TEXTURE_INFO("thicknessTexture", params->thickness_texture);
cgltf_write_floatprop(context, "thicknessFactor", params->thickness_factor, 0.0f);
if (cgltf_check_floatarray(params->attenuation_color, 3, 1.0f))
{
cgltf_write_floatarrayprop(context, "attenuationColor", params->attenuation_color, 3);
}
if (params->attenuation_distance < FLT_MAX)
{
cgltf_write_floatprop(context, "attenuationDistance", params->attenuation_distance, FLT_MAX);
}
cgltf_write_line(context, "}");
}
if (material->has_sheen)
{
const cgltf_sheen* params = &material->sheen;
cgltf_write_line(context, "\"KHR_materials_sheen\": {");
CGLTF_WRITE_TEXTURE_INFO("sheenColorTexture", params->sheen_color_texture);
CGLTF_WRITE_TEXTURE_INFO("sheenRoughnessTexture", params->sheen_roughness_texture);
if (cgltf_check_floatarray(params->sheen_color_factor, 3, 0.0f))
{
cgltf_write_floatarrayprop(context, "sheenColorFactor", params->sheen_color_factor, 3);
}
cgltf_write_floatprop(context, "sheenRoughnessFactor", params->sheen_roughness_factor, 0.0f);
cgltf_write_line(context, "}");
}
if (material->has_pbr_specular_glossiness)
{
const cgltf_pbr_specular_glossiness* params = &material->pbr_specular_glossiness;
cgltf_write_line(context, "\"KHR_materials_pbrSpecularGlossiness\": {");
CGLTF_WRITE_TEXTURE_INFO("diffuseTexture", params->diffuse_texture);
CGLTF_WRITE_TEXTURE_INFO("specularGlossinessTexture", params->specular_glossiness_texture);
if (cgltf_check_floatarray(params->diffuse_factor, 4, 1.0f))
{
cgltf_write_floatarrayprop(context, "diffuseFactor", params->diffuse_factor, 4);
}
if (cgltf_check_floatarray(params->specular_factor, 3, 1.0f))
{
cgltf_write_floatarrayprop(context, "specularFactor", params->specular_factor, 3);
}
cgltf_write_floatprop(context, "glossinessFactor", params->glossiness_factor, 1.0f);
cgltf_write_line(context, "}");
}
if (material->unlit)
{
cgltf_write_line(context, "\"KHR_materials_unlit\": {}");
}
if (material->has_emissive_strength)
{
cgltf_write_line(context, "\"KHR_materials_emissive_strength\": {");
const cgltf_emissive_strength* params = &material->emissive_strength;
cgltf_write_floatprop(context, "emissiveStrength", params->emissive_strength, 1.f);
cgltf_write_line(context, "}");
}
if (material->has_iridescence)
{
cgltf_write_line(context, "\"KHR_materials_iridescence\": {");
const cgltf_iridescence* params = &material->iridescence;
cgltf_write_floatprop(context, "iridescenceFactor", params->iridescence_factor, 0.f);
CGLTF_WRITE_TEXTURE_INFO("iridescenceTexture", params->iridescence_texture);
cgltf_write_floatprop(context, "iridescenceIor", params->iridescence_ior, 1.3f);
cgltf_write_floatprop(context, "iridescenceThicknessMinimum", params->iridescence_thickness_min, 100.f);
cgltf_write_floatprop(context, "iridescenceThicknessMaximum", params->iridescence_thickness_max, 400.f);
CGLTF_WRITE_TEXTURE_INFO("iridescenceThicknessTexture", params->iridescence_thickness_texture);
cgltf_write_line(context, "}");
}
if (material->has_anisotropy)
{
cgltf_write_line(context, "\"KHR_materials_anisotropy\": {");
const cgltf_anisotropy* params = &material->anisotropy;
cgltf_write_floatprop(context, "anisotropyFactor", params->anisotropy_strength, 0.f);
cgltf_write_floatprop(context, "anisotropyRotation", params->anisotropy_rotation, 0.f);
CGLTF_WRITE_TEXTURE_INFO("anisotropyTexture", params->anisotropy_texture);
cgltf_write_line(context, "}");
}
if (material->has_dispersion)
{
cgltf_write_line(context, "\"KHR_materials_dispersion\": {");
const cgltf_dispersion* params = &material->dispersion;
cgltf_write_floatprop(context, "dispersion", params->dispersion, 0.f);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "}");
}
CGLTF_WRITE_NORMAL_TEXTURE_INFO("normalTexture", material->normal_texture);
CGLTF_WRITE_OCCLUSION_TEXTURE_INFO("occlusionTexture", material->occlusion_texture);
CGLTF_WRITE_TEXTURE_INFO("emissiveTexture", material->emissive_texture);
if (cgltf_check_floatarray(material->emissive_factor, 3, 0.0f))
{
cgltf_write_floatarrayprop(context, "emissiveFactor", material->emissive_factor, 3);
}
cgltf_write_strprop(context, "alphaMode", cgltf_str_from_alpha_mode(material->alpha_mode));
cgltf_write_extras(context, &material->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_image(cgltf_write_context* context, const cgltf_image* image)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", image->name);
cgltf_write_strprop(context, "uri", image->uri);
CGLTF_WRITE_IDXPROP("bufferView", image->buffer_view, context->data->buffer_views);
cgltf_write_strprop(context, "mimeType", image->mime_type);
cgltf_write_extras(context, &image->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_texture(cgltf_write_context* context, const cgltf_texture* texture)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", texture->name);
CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images);
CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers);
if (texture->has_basisu || texture->has_webp)
{
cgltf_write_line(context, "\"extensions\": {");
if (texture->has_basisu)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_BASISU;
cgltf_write_line(context, "\"KHR_texture_basisu\": {");
CGLTF_WRITE_IDXPROP("source", texture->basisu_image, context->data->images);
cgltf_write_line(context, "}");
}
if (texture->has_webp)
{
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_WEBP;
cgltf_write_line(context, "\"EXT_texture_webp\": {");
CGLTF_WRITE_IDXPROP("source", texture->webp_image, context->data->images);
cgltf_write_line(context, "}");
}
cgltf_write_line(context, "}");
}
cgltf_write_extras(context, &texture->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_skin(cgltf_write_context* context, const cgltf_skin* skin)
{
cgltf_write_line(context, "{");
CGLTF_WRITE_IDXPROP("skeleton", skin->skeleton, context->data->nodes);
CGLTF_WRITE_IDXPROP("inverseBindMatrices", skin->inverse_bind_matrices, context->data->accessors);
CGLTF_WRITE_IDXARRPROP("joints", skin->joints_count, skin->joints, context->data->nodes);
cgltf_write_strprop(context, "name", skin->name);
cgltf_write_extras(context, &skin->extras);
cgltf_write_line(context, "}");
}
static const char* cgltf_write_str_path_type(cgltf_animation_path_type path_type)
{
switch (path_type)
{
case cgltf_animation_path_type_translation:
return "translation";
case cgltf_animation_path_type_rotation:
return "rotation";
case cgltf_animation_path_type_scale:
return "scale";
case cgltf_animation_path_type_weights:
return "weights";
default:
break;
}
return "invalid";
}
static const char* cgltf_write_str_interpolation_type(cgltf_interpolation_type interpolation_type)
{
switch (interpolation_type)
{
case cgltf_interpolation_type_linear:
return "LINEAR";
case cgltf_interpolation_type_step:
return "STEP";
case cgltf_interpolation_type_cubic_spline:
return "CUBICSPLINE";
default:
break;
}
return "invalid";
}
static void cgltf_write_path_type(cgltf_write_context* context, const char *label, cgltf_animation_path_type path_type)
{
cgltf_write_strprop(context, label, cgltf_write_str_path_type(path_type));
}
static void cgltf_write_interpolation_type(cgltf_write_context* context, const char *label, cgltf_interpolation_type interpolation_type)
{
cgltf_write_strprop(context, label, cgltf_write_str_interpolation_type(interpolation_type));
}
static void cgltf_write_animation_sampler(cgltf_write_context* context, const cgltf_animation_sampler* animation_sampler)
{
cgltf_write_line(context, "{");
cgltf_write_interpolation_type(context, "interpolation", animation_sampler->interpolation);
CGLTF_WRITE_IDXPROP("input", animation_sampler->input, context->data->accessors);
CGLTF_WRITE_IDXPROP("output", animation_sampler->output, context->data->accessors);
cgltf_write_extras(context, &animation_sampler->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_animation_channel(cgltf_write_context* context, const cgltf_animation* animation, const cgltf_animation_channel* animation_channel)
{
cgltf_write_line(context, "{");
CGLTF_WRITE_IDXPROP("sampler", animation_channel->sampler, animation->samplers);
cgltf_write_line(context, "\"target\": {");
CGLTF_WRITE_IDXPROP("node", animation_channel->target_node, context->data->nodes);
cgltf_write_path_type(context, "path", animation_channel->target_path);
cgltf_write_line(context, "}");
cgltf_write_extras(context, &animation_channel->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_animation(cgltf_write_context* context, const cgltf_animation* animation)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", animation->name);
if (animation->samplers_count > 0)
{
cgltf_write_line(context, "\"samplers\": [");
for (cgltf_size i = 0; i < animation->samplers_count; ++i)
{
cgltf_write_animation_sampler(context, animation->samplers + i);
}
cgltf_write_line(context, "]");
}
if (animation->channels_count > 0)
{
cgltf_write_line(context, "\"channels\": [");
for (cgltf_size i = 0; i < animation->channels_count; ++i)
{
cgltf_write_animation_channel(context, animation, animation->channels + i);
}
cgltf_write_line(context, "]");
}
cgltf_write_extras(context, &animation->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_sampler(cgltf_write_context* context, const cgltf_sampler* sampler)
{
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", sampler->name);
cgltf_write_intprop(context, "magFilter", sampler->mag_filter, 0);
cgltf_write_intprop(context, "minFilter", sampler->min_filter, 0);
cgltf_write_intprop(context, "wrapS", sampler->wrap_s, 10497);
cgltf_write_intprop(context, "wrapT", sampler->wrap_t, 10497);
cgltf_write_extras(context, &sampler->extras);
cgltf_write_line(context, "}");
}
static void cgltf_write_node(cgltf_write_context* context, const cgltf_node* node)
{
cgltf_write_line(context, "{");
CGLTF_WRITE_IDXARRPROP("children", node->children_count, node->children, context->data->nodes);
CGLTF_WRITE_IDXPROP("mesh", node->mesh, context->data->meshes);
cgltf_write_strprop(context, "name", node->name);
if (node->has_matrix)
{
cgltf_write_floatarrayprop(context, "matrix", node->matrix, 16);
}
if (node->has_translation)
{
cgltf_write_floatarrayprop(context, "translation", node->translation, 3);
}
if (node->has_rotation)
{
cgltf_write_floatarrayprop(context, "rotation", node->rotation, 4);
}