Debian Codegraph @

third_party/cgltf/cgltf_write.h

raw
1 /**
2 * cgltf_write - a single-file glTF 2.0 writer written in C99.
3 *
4 * Version: 1.10
5 *
6 * Website: https://github.com/jkuhlmann/cgltf
7 *
8 * Distributed under the MIT License, see notice at the end of this file.
9 *
10 * Building:
11 * Include this file where you need the struct and function
12 * declarations. Have exactly one source file where you define
13 * `CGLTF_WRITE_IMPLEMENTATION` before including this file to get the
14 * function definitions.
15 *
16 * Reference:
17 * `cgltf_result cgltf_write_file(const cgltf_options* options, const char*
18 * path, const cgltf_data* data)` writes JSON to the given file path. Buffer
19 * files and external images are not written out. `data` is not deallocated.
20 *
21 * `cgltf_size cgltf_write(const cgltf_options* options, char* buffer,
22 * cgltf_size size, const cgltf_data* data)` writes JSON into the given memory
23 * buffer. Returns the number of bytes written to `buffer`, including a null
24 * terminator. If buffer is null, returns the number of bytes that would have
25 * been written. `data` is not deallocated.
26 *
27 * To write custom JSON into the `extras` field, aggregate all the custom JSON
28 * into a single buffer, then set `file_data` to this buffer. By supplying
29 * start_offset and end_offset values for various objects, you can select a
30 * range of characters within the aggregated buffer.
31 */
32 #ifndef CGLTF_WRITE_H_INCLUDED__
33 #define CGLTF_WRITE_H_INCLUDED__
34
35 #include "cgltf.h"
36
37 #include <stddef.h>
38 #include <stdbool.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data);
45 cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data);
46
47 #ifdef __cplusplus
48 }
49 #endif
50
51 #endif /* #ifndef CGLTF_WRITE_H_INCLUDED__ */
52
53 /*
54 *
55 * Stop now, if you are only interested in the API.
56 * Below, you find the implementation.
57 *
58 */
59
60 #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
61 /* This makes MSVC/CLion intellisense work. */
62 #define CGLTF_IMPLEMENTATION
63 #endif
64
65 #ifdef CGLTF_WRITE_IMPLEMENTATION
66
67 #include <stdio.h>
68 #include <stdint.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <float.h>
72
73 #define CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM (1 << 0)
74 #define CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT (1 << 1)
75 #define CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS (1 << 2)
76 #define CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL (1 << 3)
77 #define CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION (1 << 4)
78 #define CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT (1 << 5)
79 #define CGLTF_EXTENSION_FLAG_MATERIALS_IOR (1 << 6)
80 #define CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR (1 << 7)
81 #define CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION (1 << 8)
82 #define CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN (1 << 9)
83 #define CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS (1 << 10)
84 #define CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME (1 << 11)
85
86 typedef struct {
87 char* buffer;
88 cgltf_size buffer_size;
89 cgltf_size remaining;
90 char* cursor;
91 cgltf_size tmp;
92 cgltf_size chars_written;
93 const cgltf_data* data;
94 int depth;
95 const char* indent;
96 int needs_comma;
97 uint32_t extension_flags;
98 uint32_t required_extension_flags;
99 } cgltf_write_context;
100
101 #define CGLTF_MIN(a, b) (a < b ? a : b)
102
103 #ifdef FLT_DECIMAL_DIG
104 // FLT_DECIMAL_DIG is C11
105 #define CGLTF_DECIMAL_DIG (FLT_DECIMAL_DIG)
106 #else
107 #define CGLTF_DECIMAL_DIG 9
108 #endif
109
110 #define CGLTF_SPRINTF(...) { \
111 context->tmp = snprintf ( context->cursor, context->remaining, __VA_ARGS__ ); \
112 context->chars_written += context->tmp; \
113 if (context->cursor) { \
114 context->cursor += context->tmp; \
115 context->remaining -= context->tmp; \
116 } }
117
118 #define CGLTF_SNPRINTF(length, ...) { \
119 context->tmp = snprintf ( context->cursor, CGLTF_MIN(length + 1, context->remaining), __VA_ARGS__ ); \
120 context->chars_written += length; \
121 if (context->cursor) { \
122 context->cursor += length; \
123 context->remaining -= length; \
124 } }
125
126 #define CGLTF_WRITE_IDXPROP(label, val, start) if (val) { \
127 cgltf_write_indent(context); \
128 CGLTF_SPRINTF("\"%s\": %d", label, (int) (val - start)); \
129 context->needs_comma = 1; }
130
131 #define CGLTF_WRITE_IDXARRPROP(label, dim, vals, start) if (vals) { \
132 cgltf_write_indent(context); \
133 CGLTF_SPRINTF("\"%s\": [", label); \
134 for (int i = 0; i < (int)(dim); ++i) { \
135 int idx = (int) (vals[i] - start); \
136 if (i != 0) CGLTF_SPRINTF(","); \
137 CGLTF_SPRINTF(" %d", idx); \
138 } \
139 CGLTF_SPRINTF(" ]"); \
140 context->needs_comma = 1; }
141
142 #define CGLTF_WRITE_TEXTURE_INFO(label, info) if (info.texture) { \
143 cgltf_write_line(context, "\"" label "\": {"); \
144 CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
145 cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
146 cgltf_write_floatprop(context, "scale", info.scale, 1.0f); \
147 if (info.has_transform) { \
148 context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
149 cgltf_write_texture_transform(context, &info.transform); \
150 } \
151 cgltf_write_extras(context, &info.extras); \
152 cgltf_write_line(context, "}"); }
153
154 static void cgltf_write_indent(cgltf_write_context* context)
155 {
156 if (context->needs_comma)
157 {
158 CGLTF_SPRINTF(",\n");
159 context->needs_comma = 0;
160 }
161 else
162 {
163 CGLTF_SPRINTF("\n");
164 }
165 for (int i = 0; i < context->depth; ++i)
166 {
167 CGLTF_SPRINTF("%s", context->indent);
168 }
169 }
170
171 static void cgltf_write_line(cgltf_write_context* context, const char* line)
172 {
173 if (line[0] == ']' || line[0] == '}')
174 {
175 --context->depth;
176 context->needs_comma = 0;
177 }
178 cgltf_write_indent(context);
179 CGLTF_SPRINTF("%s", line);
180 cgltf_size last = (cgltf_size)(strlen(line) - 1);
181 if (line[0] == ']' || line[0] == '}')
182 {
183 context->needs_comma = 1;
184 }
185 if (line[last] == '[' || line[last] == '{')
186 {
187 ++context->depth;
188 context->needs_comma = 0;
189 }
190 }
191
192 static void cgltf_write_strprop(cgltf_write_context* context, const char* label, const char* val)
193 {
194 if (val)
195 {
196 cgltf_write_indent(context);
197 CGLTF_SPRINTF("\"%s\": \"%s\"", label, val);
198 context->needs_comma = 1;
199 }
200 }
201
202 static void cgltf_write_extras(cgltf_write_context* context, const cgltf_extras* extras)
203 {
204 cgltf_size length = extras->end_offset - extras->start_offset;
205 if (length > 0 && context->data->file_data)
206 {
207 char* json_string = ((char*) context->data->file_data) + extras->start_offset;
208 cgltf_write_indent(context);
209 CGLTF_SPRINTF("%s", "\"extras\": ");
210 CGLTF_SNPRINTF(length, "%.*s", (int)(extras->end_offset - extras->start_offset), json_string);
211 context->needs_comma = 1;
212 }
213 }
214
215 static void cgltf_write_stritem(cgltf_write_context* context, const char* item)
216 {
217 cgltf_write_indent(context);
218 CGLTF_SPRINTF("\"%s\"", item);
219 context->needs_comma = 1;
220 }
221
222 static void cgltf_write_intprop(cgltf_write_context* context, const char* label, int val, int def)
223 {
224 if (val != def)
225 {
226 cgltf_write_indent(context);
227 CGLTF_SPRINTF("\"%s\": %d", label, val);
228 context->needs_comma = 1;
229 }
230 }
231
232 static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, float val, float def)
233 {
234 if (val != def)
235 {
236 cgltf_write_indent(context);
237 CGLTF_SPRINTF("\"%s\": ", label);
238 CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, val);
239 context->needs_comma = 1;
240
241 if (context->cursor)
242 {
243 char *decimal_comma = strchr(context->cursor - context->tmp, ',');
244 if (decimal_comma)
245 {
246 *decimal_comma = '.';
247 }
248 }
249 }
250 }
251
252 static void cgltf_write_boolprop_optional(cgltf_write_context* context, const char* label, bool val, bool def)
253 {
254 if (val != def)
255 {
256 cgltf_write_indent(context);
257 CGLTF_SPRINTF("\"%s\": %s", label, val ? "true" : "false");
258 context->needs_comma = 1;
259 }
260 }
261
262 static void cgltf_write_floatarrayprop(cgltf_write_context* context, const char* label, const cgltf_float* vals, cgltf_size dim)
263 {
264 cgltf_write_indent(context);
265 CGLTF_SPRINTF("\"%s\": [", label);
266 for (cgltf_size i = 0; i < dim; ++i)
267 {
268 if (i != 0)
269 {
270 CGLTF_SPRINTF(", %.*g", CGLTF_DECIMAL_DIG, vals[i]);
271 }
272 else
273 {
274 CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, vals[i]);
275 }
276 }
277 CGLTF_SPRINTF("]");
278 context->needs_comma = 1;
279 }
280
281 static bool cgltf_check_floatarray(const float* vals, int dim, float val) {
282 while (dim--)
283 {
284 if (vals[dim] != val)
285 {
286 return true;
287 }
288 }
289 return false;
290 }
291
292 static int cgltf_int_from_component_type(cgltf_component_type ctype)
293 {
294 switch (ctype)
295 {
296 case cgltf_component_type_r_8: return 5120;
297 case cgltf_component_type_r_8u: return 5121;
298 case cgltf_component_type_r_16: return 5122;
299 case cgltf_component_type_r_16u: return 5123;
300 case cgltf_component_type_r_32u: return 5125;
301 case cgltf_component_type_r_32f: return 5126;
302 default: return 0;
303 }
304 }
305
306 static const char* cgltf_str_from_alpha_mode(cgltf_alpha_mode alpha_mode)
307 {
308 switch (alpha_mode)
309 {
310 case cgltf_alpha_mode_mask: return "MASK";
311 case cgltf_alpha_mode_blend: return "BLEND";
312 default: return NULL;
313 }
314 }
315
316 static const char* cgltf_str_from_type(cgltf_type type)
317 {
318 switch (type)
319 {
320 case cgltf_type_scalar: return "SCALAR";
321 case cgltf_type_vec2: return "VEC2";
322 case cgltf_type_vec3: return "VEC3";
323 case cgltf_type_vec4: return "VEC4";
324 case cgltf_type_mat2: return "MAT2";
325 case cgltf_type_mat3: return "MAT3";
326 case cgltf_type_mat4: return "MAT4";
327 default: return NULL;
328 }
329 }
330
331 static cgltf_size cgltf_dim_from_type(cgltf_type type)
332 {
333 switch (type)
334 {
335 case cgltf_type_scalar: return 1;
336 case cgltf_type_vec2: return 2;
337 case cgltf_type_vec3: return 3;
338 case cgltf_type_vec4: return 4;
339 case cgltf_type_mat2: return 4;
340 case cgltf_type_mat3: return 9;
341 case cgltf_type_mat4: return 16;
342 default: return 0;
343 }
344 }
345
346 static const char* cgltf_str_from_camera_type(cgltf_camera_type camera_type)
347 {
348 switch (camera_type)
349 {
350 case cgltf_camera_type_perspective: return "perspective";
351 case cgltf_camera_type_orthographic: return "orthographic";
352 default: return NULL;
353 }
354 }
355
356 static const char* cgltf_str_from_light_type(cgltf_light_type light_type)
357 {
358 switch (light_type)
359 {
360 case cgltf_light_type_directional: return "directional";
361 case cgltf_light_type_point: return "point";
362 case cgltf_light_type_spot: return "spot";
363 default: return NULL;
364 }
365 }
366
367 static void cgltf_write_texture_transform(cgltf_write_context* context, const cgltf_texture_transform* transform)
368 {
369 cgltf_write_line(context, "\"extensions\": {");
370 cgltf_write_line(context, "\"KHR_texture_transform\": {");
371 if (cgltf_check_floatarray(transform->offset, 2, 0.0f))
372 {
373 cgltf_write_floatarrayprop(context, "offset", transform->offset, 2);
374 }
375 cgltf_write_floatprop(context, "rotation", transform->rotation, 0.0f);
376 if (cgltf_check_floatarray(transform->scale, 2, 1.0f))
377 {
378 cgltf_write_floatarrayprop(context, "scale", transform->scale, 2);
379 }
380 cgltf_write_intprop(context, "texCoord", transform->texcoord, 0);
381 cgltf_write_line(context, "}");
382 cgltf_write_line(context, "}");
383 }
384
385 static void cgltf_write_asset(cgltf_write_context* context, const cgltf_asset* asset)
386 {
387 cgltf_write_line(context, "\"asset\": {");
388 cgltf_write_strprop(context, "copyright", asset->copyright);
389 cgltf_write_strprop(context, "generator", asset->generator);
390 cgltf_write_strprop(context, "version", asset->version);
391 cgltf_write_strprop(context, "min_version", asset->min_version);
392 cgltf_write_extras(context, &asset->extras);
393 cgltf_write_line(context, "}");
394 }
395
396 static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_primitive* prim)
397 {
398 cgltf_write_intprop(context, "mode", (int) prim->type, 4);
399 CGLTF_WRITE_IDXPROP("indices", prim->indices, context->data->accessors);
400 CGLTF_WRITE_IDXPROP("material", prim->material, context->data->materials);
401 cgltf_write_line(context, "\"attributes\": {");
402 for (cgltf_size i = 0; i < prim->attributes_count; ++i)
403 {
404 const cgltf_attribute* attr = prim->attributes + i;
405 CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
406 }
407 cgltf_write_line(context, "}");
408
409 if (prim->targets_count)
410 {
411 cgltf_write_line(context, "\"targets\": [");
412 for (cgltf_size i = 0; i < prim->targets_count; ++i)
413 {
414 cgltf_write_line(context, "{");
415 for (cgltf_size j = 0; j < prim->targets[i].attributes_count; ++j)
416 {
417 const cgltf_attribute* attr = prim->targets[i].attributes + j;
418 CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
419 }
420 cgltf_write_line(context, "}");
421 }
422 cgltf_write_line(context, "]");
423 }
424 cgltf_write_extras(context, &prim->extras);
425
426 if (prim->has_draco_mesh_compression || prim->mappings_count > 0)
427 {
428 cgltf_write_line(context, "\"extensions\": {");
429
430 if (prim->has_draco_mesh_compression)
431 {
432 context->extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
433 if (prim->attributes_count == 0 || prim->indices == 0)
434 {
435 context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
436 }
437
438 cgltf_write_line(context, "\"KHR_draco_mesh_compression\": {");
439 CGLTF_WRITE_IDXPROP("bufferView", prim->draco_mesh_compression.buffer_view, context->data->buffer_views);
440 cgltf_write_line(context, "\"attributes\": {");
441 for (cgltf_size i = 0; i < prim->draco_mesh_compression.attributes_count; ++i)
442 {
443 const cgltf_attribute* attr = prim->draco_mesh_compression.attributes + i;
444 CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
445 }
446 cgltf_write_line(context, "}");
447 cgltf_write_line(context, "}");
448 }
449
450 if (prim->mappings_count > 0)
451 {
452 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
453 cgltf_write_line(context, "\"KHR_materials_variants\": {");
454 cgltf_write_line(context, "\"mappings\": [");
455 for (cgltf_size i = 0; i < prim->mappings_count; ++i)
456 {
457 const cgltf_material_mapping* map = prim->mappings + i;
458 cgltf_write_line(context, "{");
459 CGLTF_WRITE_IDXPROP("material", map->material, context->data->materials);
460
461 cgltf_write_indent(context);
462 CGLTF_SPRINTF("\"variants\": [%d]", (int)map->variant);
463 context->needs_comma = 1;
464
465 cgltf_write_extras(context, &map->extras);
466 cgltf_write_line(context, "}");
467 }
468 cgltf_write_line(context, "]");
469 cgltf_write_line(context, "}");
470 }
471
472 cgltf_write_line(context, "}");
473 }
474 }
475
476 static void cgltf_write_mesh(cgltf_write_context* context, const cgltf_mesh* mesh)
477 {
478 cgltf_write_line(context, "{");
479 cgltf_write_strprop(context, "name", mesh->name);
480
481 cgltf_write_line(context, "\"primitives\": [");
482 for (cgltf_size i = 0; i < mesh->primitives_count; ++i)
483 {
484 cgltf_write_line(context, "{");
485 cgltf_write_primitive(context, mesh->primitives + i);
486 cgltf_write_line(context, "}");
487 }
488 cgltf_write_line(context, "]");
489
490 if (mesh->weights_count > 0)
491 {
492 cgltf_write_floatarrayprop(context, "weights", mesh->weights, mesh->weights_count);
493 }
494 cgltf_write_extras(context, &mesh->extras);
495 cgltf_write_line(context, "}");
496 }
497
498 static void cgltf_write_buffer_view(cgltf_write_context* context, const cgltf_buffer_view* view)
499 {
500 cgltf_write_line(context, "{");
501 cgltf_write_strprop(context, "name", view->name);
502 CGLTF_WRITE_IDXPROP("buffer", view->buffer, context->data->buffers);
503 cgltf_write_intprop(context, "byteLength", (int)view->size, -1);
504 cgltf_write_intprop(context, "byteOffset", (int)view->offset, 0);
505 cgltf_write_intprop(context, "byteStride", (int)view->stride, 0);
506 // NOTE: We skip writing "target" because the spec says its usage can be inferred.
507 cgltf_write_extras(context, &view->extras);
508 cgltf_write_line(context, "}");
509 }
510
511
512 static void cgltf_write_buffer(cgltf_write_context* context, const cgltf_buffer* buffer)
513 {
514 cgltf_write_line(context, "{");
515 cgltf_write_strprop(context, "name", buffer->name);
516 cgltf_write_strprop(context, "uri", buffer->uri);
517 cgltf_write_intprop(context, "byteLength", (int)buffer->size, -1);
518 cgltf_write_extras(context, &buffer->extras);
519 cgltf_write_line(context, "}");
520 }
521
522 static void cgltf_write_material(cgltf_write_context* context, const cgltf_material* material)
523 {
524 cgltf_write_line(context, "{");
525 cgltf_write_strprop(context, "name", material->name);
526 cgltf_write_floatprop(context, "alphaCutoff", material->alpha_cutoff, 0.5f);
527 cgltf_write_boolprop_optional(context, "doubleSided", material->double_sided, false);
528 // cgltf_write_boolprop_optional(context, "unlit", material->unlit, false);
529
530 if (material->unlit)
531 {
532 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT;
533 }
534
535 if (material->has_pbr_specular_glossiness)
536 {
537 context->extension_flags |= CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS;
538 }
539
540 if (material->has_clearcoat)
541 {
542 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT;
543 }
544
545 if (material->has_transmission)
546 {
547 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION;
548 }
549
550 if (material->has_volume)
551 {
552 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME;
553 }
554
555 if (material->has_ior)
556 {
557 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_IOR;
558 }
559
560 if (material->has_specular)
561 {
562 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR;
563 }
564
565 if (material->has_sheen)
566 {
567 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN;
568 }
569
570 if (material->has_pbr_metallic_roughness)
571 {
572 const cgltf_pbr_metallic_roughness* params = &material->pbr_metallic_roughness;
573 cgltf_write_line(context, "\"pbrMetallicRoughness\": {");
574 CGLTF_WRITE_TEXTURE_INFO("baseColorTexture", params->base_color_texture);
575 CGLTF_WRITE_TEXTURE_INFO("metallicRoughnessTexture", params->metallic_roughness_texture);
576 cgltf_write_floatprop(context, "metallicFactor", params->metallic_factor, 1.0f);
577 cgltf_write_floatprop(context, "roughnessFactor", params->roughness_factor, 1.0f);
578 if (cgltf_check_floatarray(params->base_color_factor, 4, 1.0f))
579 {
580 cgltf_write_floatarrayprop(context, "baseColorFactor", params->base_color_factor, 4);
581 }
582 cgltf_write_extras(context, &params->extras);
583 cgltf_write_line(context, "}");
584 }
585
586 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)
587 {
588 cgltf_write_line(context, "\"extensions\": {");
589 if (material->has_clearcoat)
590 {
591 const cgltf_clearcoat* params = &material->clearcoat;
592 cgltf_write_line(context, "\"KHR_materials_clearcoat\": {");
593 CGLTF_WRITE_TEXTURE_INFO("clearcoatTexture", params->clearcoat_texture);
594 CGLTF_WRITE_TEXTURE_INFO("clearcoatRoughnessTexture", params->clearcoat_roughness_texture);
595 CGLTF_WRITE_TEXTURE_INFO("clearcoatNormalTexture", params->clearcoat_normal_texture);
596 cgltf_write_floatprop(context, "clearcoatFactor", params->clearcoat_factor, 0.0f);
597 cgltf_write_floatprop(context, "clearcoatRoughnessFactor", params->clearcoat_roughness_factor, 0.0f);
598 cgltf_write_line(context, "}");
599 }
600 if (material->has_ior)
601 {
602 const cgltf_ior* params = &material->ior;
603 cgltf_write_line(context, "\"KHR_materials_ior\": {");
604 cgltf_write_floatprop(context, "ior", params->ior, 1.5f);
605 cgltf_write_line(context, "}");
606 }
607 if (material->has_specular)
608 {
609 const cgltf_specular* params = &material->specular;
610 cgltf_write_line(context, "\"KHR_materials_specular\": {");
611 CGLTF_WRITE_TEXTURE_INFO("specularTexture", params->specular_texture);
612 CGLTF_WRITE_TEXTURE_INFO("specularColorTexture", params->specular_color_texture);
613 cgltf_write_floatprop(context, "specularFactor", params->specular_factor, 1.0f);
614 if (cgltf_check_floatarray(params->specular_color_factor, 3, 1.0f))
615 {
616 cgltf_write_floatarrayprop(context, "specularColorFactor", params->specular_color_factor, 3);
617 }
618 cgltf_write_line(context, "}");
619 }
620 if (material->has_transmission)
621 {
622 const cgltf_transmission* params = &material->transmission;
623 cgltf_write_line(context, "\"KHR_materials_transmission\": {");
624 CGLTF_WRITE_TEXTURE_INFO("transmissionTexture", params->transmission_texture);
625 cgltf_write_floatprop(context, "transmissionFactor", params->transmission_factor, 0.0f);
626 cgltf_write_line(context, "}");
627 }
628 if (material->has_volume)
629 {
630 const cgltf_volume* params = &material->volume;
631 cgltf_write_line(context, "\"KHR_materials_volume\": {");
632 CGLTF_WRITE_TEXTURE_INFO("thicknessTexture", params->thickness_texture);
633 cgltf_write_floatprop(context, "thicknessFactor", params->thickness_factor, 0.0f);
634 if (cgltf_check_floatarray(params->attenuation_color, 3, 1.0f))
635 {
636 cgltf_write_floatarrayprop(context, "attenuationColor", params->attenuation_color, 3);
637 }
638 if (params->attenuation_distance < FLT_MAX)
639 {
640 cgltf_write_floatprop(context, "attenuationDistance", params->attenuation_distance, FLT_MAX);
641 }
642 cgltf_write_line(context, "}");
643 }
644 if (material->has_sheen)
645 {
646 const cgltf_sheen* params = &material->sheen;
647 cgltf_write_line(context, "\"KHR_materials_sheen\": {");
648 CGLTF_WRITE_TEXTURE_INFO("sheenColorTexture", params->sheen_color_texture);
649 CGLTF_WRITE_TEXTURE_INFO("sheenRoughnessTexture", params->sheen_roughness_texture);
650 if (cgltf_check_floatarray(params->sheen_color_factor, 3, 0.0f))
651 {
652 cgltf_write_floatarrayprop(context, "sheenColorFactor", params->sheen_color_factor, 3);
653 }
654 cgltf_write_floatprop(context, "sheenRoughnessFactor", params->sheen_roughness_factor, 0.0f);
655 cgltf_write_line(context, "}");
656 }
657 if (material->has_pbr_specular_glossiness)
658 {
659 const cgltf_pbr_specular_glossiness* params = &material->pbr_specular_glossiness;
660 cgltf_write_line(context, "\"KHR_materials_pbrSpecularGlossiness\": {");
661 CGLTF_WRITE_TEXTURE_INFO("diffuseTexture", params->diffuse_texture);
662 CGLTF_WRITE_TEXTURE_INFO("specularGlossinessTexture", params->specular_glossiness_texture);
663 if (cgltf_check_floatarray(params->diffuse_factor, 4, 1.0f))
664 {
665 cgltf_write_floatarrayprop(context, "dffuseFactor", params->diffuse_factor, 4);
666 }
667 if (cgltf_check_floatarray(params->specular_factor, 3, 1.0f))
668 {
669 cgltf_write_floatarrayprop(context, "specularFactor", params->specular_factor, 3);
670 }
671 cgltf_write_floatprop(context, "glossinessFactor", params->glossiness_factor, 1.0f);
672 cgltf_write_line(context, "}");
673 }
674 if (material->unlit)
675 {
676 cgltf_write_line(context, "\"KHR_materials_unlit\": {}");
677 }
678 cgltf_write_line(context, "}");
679 }
680
681 CGLTF_WRITE_TEXTURE_INFO("normalTexture", material->normal_texture);
682 CGLTF_WRITE_TEXTURE_INFO("occlusionTexture", material->occlusion_texture);
683 CGLTF_WRITE_TEXTURE_INFO("emissiveTexture", material->emissive_texture);
684 if (cgltf_check_floatarray(material->emissive_factor, 3, 0.0f))
685 {
686 cgltf_write_floatarrayprop(context, "emissiveFactor", material->emissive_factor, 3);
687 }
688 cgltf_write_strprop(context, "alphaMode", cgltf_str_from_alpha_mode(material->alpha_mode));
689 cgltf_write_extras(context, &material->extras);
690 cgltf_write_line(context, "}");
691 }
692
693 static void cgltf_write_image(cgltf_write_context* context, const cgltf_image* image)
694 {
695 cgltf_write_line(context, "{");
696 cgltf_write_strprop(context, "name", image->name);
697 cgltf_write_strprop(context, "uri", image->uri);
698 CGLTF_WRITE_IDXPROP("bufferView", image->buffer_view, context->data->buffer_views);
699 cgltf_write_strprop(context, "mimeType", image->mime_type);
700 cgltf_write_extras(context, &image->extras);
701 cgltf_write_line(context, "}");
702 }
703
704 static void cgltf_write_texture(cgltf_write_context* context, const cgltf_texture* texture)
705 {
706 cgltf_write_line(context, "{");
707 cgltf_write_strprop(context, "name", texture->name);
708 CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images);
709 CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers);
710 cgltf_write_extras(context, &texture->extras);
711 cgltf_write_line(context, "}");
712 }
713
714 static void cgltf_write_skin(cgltf_write_context* context, const cgltf_skin* skin)
715 {
716 cgltf_write_line(context, "{");
717 CGLTF_WRITE_IDXPROP("skeleton", skin->skeleton, context->data->nodes);
718 CGLTF_WRITE_IDXPROP("inverseBindMatrices", skin->inverse_bind_matrices, context->data->accessors);
719 CGLTF_WRITE_IDXARRPROP("joints", skin->joints_count, skin->joints, context->data->nodes);
720 cgltf_write_strprop(context, "name", skin->name);
721 cgltf_write_extras(context, &skin->extras);
722 cgltf_write_line(context, "}");
723 }
724
725 static const char* cgltf_write_str_path_type(cgltf_animation_path_type path_type)
726 {
727 switch (path_type)
728 {
729 case cgltf_animation_path_type_translation:
730 return "translation";
731 case cgltf_animation_path_type_rotation:
732 return "rotation";
733 case cgltf_animation_path_type_scale:
734 return "scale";
735 case cgltf_animation_path_type_weights:
736 return "weights";
737 case cgltf_animation_path_type_invalid:
738 break;
739 }
740 return "invalid";
741 }
742
743 static const char* cgltf_write_str_interpolation_type(cgltf_interpolation_type interpolation_type)
744 {
745 switch (interpolation_type)
746 {
747 case cgltf_interpolation_type_linear:
748 return "LINEAR";
749 case cgltf_interpolation_type_step:
750 return "STEP";
751 case cgltf_interpolation_type_cubic_spline:
752 return "CUBICSPLINE";
753 }
754 return "invalid";
755 }
756
757 static void cgltf_write_path_type(cgltf_write_context* context, const char *label, cgltf_animation_path_type path_type)
758 {
759 cgltf_write_strprop(context, label, cgltf_write_str_path_type(path_type));
760 }
761
762 static void cgltf_write_interpolation_type(cgltf_write_context* context, const char *label, cgltf_interpolation_type interpolation_type)
763 {
764 cgltf_write_strprop(context, label, cgltf_write_str_interpolation_type(interpolation_type));
765 }
766
767 static void cgltf_write_animation_sampler(cgltf_write_context* context, const cgltf_animation_sampler* animation_sampler)
768 {
769 cgltf_write_line(context, "{");
770 cgltf_write_interpolation_type(context, "interpolation", animation_sampler->interpolation);
771 CGLTF_WRITE_IDXPROP("input", animation_sampler->input, context->data->accessors);
772 CGLTF_WRITE_IDXPROP("output", animation_sampler->output, context->data->accessors);
773 cgltf_write_extras(context, &animation_sampler->extras);
774 cgltf_write_line(context, "}");
775 }
776
777 static void cgltf_write_animation_channel(cgltf_write_context* context, const cgltf_animation* animation, const cgltf_animation_channel* animation_channel)
778 {
779 cgltf_write_line(context, "{");
780 CGLTF_WRITE_IDXPROP("sampler", animation_channel->sampler, animation->samplers);
781 cgltf_write_line(context, "\"target\": {");
782 CGLTF_WRITE_IDXPROP("node", animation_channel->target_node, context->data->nodes);
783 cgltf_write_path_type(context, "path", animation_channel->target_path);
784 cgltf_write_line(context, "}");
785 cgltf_write_extras(context, &animation_channel->extras);
786 cgltf_write_line(context, "}");
787 }
788
789 static void cgltf_write_animation(cgltf_write_context* context, const cgltf_animation* animation)
790 {
791 cgltf_write_line(context, "{");
792 cgltf_write_strprop(context, "name", animation->name);
793
794 if (animation->samplers_count > 0)
795 {
796 cgltf_write_line(context, "\"samplers\": [");
797 for (cgltf_size i = 0; i < animation->samplers_count; ++i)
798 {
799 cgltf_write_animation_sampler(context, animation->samplers + i);
800 }
801 cgltf_write_line(context, "]");
802 }
803 if (animation->channels_count > 0)
804 {
805 cgltf_write_line(context, "\"channels\": [");
806 for (cgltf_size i = 0; i < animation->channels_count; ++i)
807 {
808 cgltf_write_animation_channel(context, animation, animation->channels + i);
809 }
810 cgltf_write_line(context, "]");
811 }
812 cgltf_write_extras(context, &animation->extras);
813 cgltf_write_line(context, "}");
814 }
815
816 static void cgltf_write_sampler(cgltf_write_context* context, const cgltf_sampler* sampler)
817 {
818 cgltf_write_line(context, "{");
819 cgltf_write_strprop(context, "name", sampler->name);
820 cgltf_write_intprop(context, "magFilter", sampler->mag_filter, 0);
821 cgltf_write_intprop(context, "minFilter", sampler->min_filter, 0);
822 cgltf_write_intprop(context, "wrapS", sampler->wrap_s, 10497);
823 cgltf_write_intprop(context, "wrapT", sampler->wrap_t, 10497);
824 cgltf_write_extras(context, &sampler->extras);
825 cgltf_write_line(context, "}");
826 }
827
828 static void cgltf_write_node(cgltf_write_context* context, const cgltf_node* node)
829 {
830 cgltf_write_line(context, "{");
831 CGLTF_WRITE_IDXARRPROP("children", node->children_count, node->children, context->data->nodes);
832 CGLTF_WRITE_IDXPROP("mesh", node->mesh, context->data->meshes);
833 cgltf_write_strprop(context, "name", node->name);
834 if (node->has_matrix)
835 {
836 cgltf_write_floatarrayprop(context, "matrix", node->matrix, 16);
837 }
838 if (node->has_translation)
839 {
840 cgltf_write_floatarrayprop(context, "translation", node->translation, 3);
841 }
842 if (node->has_rotation)
843 {
844 cgltf_write_floatarrayprop(context, "rotation", node->rotation, 4);
845 }
846 if (node->has_scale)
847 {
848 cgltf_write_floatarrayprop(context, "scale", node->scale, 3);
849 }
850 if (node->skin)
851 {
852 CGLTF_WRITE_IDXPROP("skin", node->skin, context->data->skins);
853 }
854
855 if (node->light)
856 {
857 context->extension_flags |= CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL;
858 cgltf_write_line(context, "\"extensions\": {");
859 cgltf_write_line(context, "\"KHR_lights_punctual\": {");
860 CGLTF_WRITE_IDXPROP("light", node->light, context->data->lights);
861 cgltf_write_line(context, "}");
862 cgltf_write_line(context, "}");
863 }
864
865 if (node->weights_count > 0)
866 {
867 cgltf_write_floatarrayprop(context, "weights", node->weights, node->weights_count);
868 }
869
870 if (node->camera)
871 {
872 CGLTF_WRITE_IDXPROP("camera", node->camera, context->data->cameras);
873 }
874
875 cgltf_write_extras(context, &node->extras);
876 cgltf_write_line(context, "}");
877 }
878
879 static void cgltf_write_scene(cgltf_write_context* context, const cgltf_scene* scene)
880 {
881 cgltf_write_line(context, "{");
882 cgltf_write_strprop(context, "name", scene->name);
883 CGLTF_WRITE_IDXARRPROP("nodes", scene->nodes_count, scene->nodes, context->data->nodes);
884 cgltf_write_extras(context, &scene->extras);
885 cgltf_write_line(context, "}");
886 }
887
888 static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_accessor* accessor)
889 {
890 cgltf_write_line(context, "{");
891 cgltf_write_strprop(context, "name", accessor->name);
892 CGLTF_WRITE_IDXPROP("bufferView", accessor->buffer_view, context->data->buffer_views);
893 cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->component_type), 0);
894 cgltf_write_strprop(context, "type", cgltf_str_from_type(accessor->type));
895 cgltf_size dim = cgltf_dim_from_type(accessor->type);
896 cgltf_write_boolprop_optional(context, "normalized", accessor->normalized, false);
897 cgltf_write_intprop(context, "byteOffset", (int)accessor->offset, 0);
898 cgltf_write_intprop(context, "count", (int)accessor->count, -1);
899 if (accessor->has_min)
900 {
901 cgltf_write_floatarrayprop(context, "min", accessor->min, dim);
902 }
903 if (accessor->has_max)
904 {
905 cgltf_write_floatarrayprop(context, "max", accessor->max, dim);
906 }
907 if (accessor->is_sparse)
908 {
909 cgltf_write_line(context, "\"sparse\": {");
910 cgltf_write_intprop(context, "count", (int)accessor->sparse.count, 0);
911 cgltf_write_line(context, "\"indices\": {");
912 cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.indices_byte_offset, 0);
913 CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.indices_buffer_view, context->data->buffer_views);
914 cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->sparse.indices_component_type), 0);
915 cgltf_write_extras(context, &accessor->sparse.indices_extras);
916 cgltf_write_line(context, "}");
917 cgltf_write_line(context, "\"values\": {");
918 cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.values_byte_offset, 0);
919 CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.values_buffer_view, context->data->buffer_views);
920 cgltf_write_extras(context, &accessor->sparse.values_extras);
921 cgltf_write_line(context, "}");
922 cgltf_write_extras(context, &accessor->sparse.extras);
923 cgltf_write_line(context, "}");
924 }
925 cgltf_write_extras(context, &accessor->extras);
926 cgltf_write_line(context, "}");
927 }
928
929 static void cgltf_write_camera(cgltf_write_context* context, const cgltf_camera* camera)
930 {
931 cgltf_write_line(context, "{");
932 cgltf_write_strprop(context, "type", cgltf_str_from_camera_type(camera->type));
933 if (camera->name)
934 {
935 cgltf_write_strprop(context, "name", camera->name);
936 }
937
938 if (camera->type == cgltf_camera_type_orthographic)
939 {
940 cgltf_write_line(context, "\"orthographic\": {");
941 cgltf_write_floatprop(context, "xmag", camera->data.orthographic.xmag, -1.0f);
942 cgltf_write_floatprop(context, "ymag", camera->data.orthographic.ymag, -1.0f);
943 cgltf_write_floatprop(context, "zfar", camera->data.orthographic.zfar, -1.0f);
944 cgltf_write_floatprop(context, "znear", camera->data.orthographic.znear, -1.0f);
945 cgltf_write_extras(context, &camera->data.orthographic.extras);
946 cgltf_write_line(context, "}");
947 }
948 else if (camera->type == cgltf_camera_type_perspective)
949 {
950 cgltf_write_line(context, "\"perspective\": {");
951
952 if (camera->data.perspective.has_aspect_ratio) {
953 cgltf_write_floatprop(context, "aspectRatio", camera->data.perspective.aspect_ratio, -1.0f);
954 }
955
956 cgltf_write_floatprop(context, "yfov", camera->data.perspective.yfov, -1.0f);
957
958 if (camera->data.perspective.has_zfar) {
959 cgltf_write_floatprop(context, "zfar", camera->data.perspective.zfar, -1.0f);
960 }
961
962 cgltf_write_floatprop(context, "znear", camera->data.perspective.znear, -1.0f);
963 cgltf_write_extras(context, &camera->data.perspective.extras);
964 cgltf_write_line(context, "}");
965 }
966 cgltf_write_extras(context, &camera->extras);
967 cgltf_write_line(context, "}");
968 }
969
970 static void cgltf_write_light(cgltf_write_context* context, const cgltf_light* light)
971 {
972 context->extension_flags |= CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL;
973
974 cgltf_write_line(context, "{");
975 cgltf_write_strprop(context, "type", cgltf_str_from_light_type(light->type));
976 if (light->name)
977 {
978 cgltf_write_strprop(context, "name", light->name);
979 }
980 if (cgltf_check_floatarray(light->color, 3, 1.0f))
981 {
982 cgltf_write_floatarrayprop(context, "color", light->color, 3);
983 }
984 cgltf_write_floatprop(context, "intensity", light->intensity, 1.0f);
985 cgltf_write_floatprop(context, "range", light->range, 0.0f);
986
987 if (light->type == cgltf_light_type_spot)
988 {
989 cgltf_write_line(context, "\"spot\": {");
990 cgltf_write_floatprop(context, "innerConeAngle", light->spot_inner_cone_angle, 0.0f);
991 cgltf_write_floatprop(context, "outerConeAngle", light->spot_outer_cone_angle, 3.14159265358979323846f/4.0f);
992 cgltf_write_line(context, "}");
993 }
994 cgltf_write_line(context, "}");
995 }
996
997 static void cgltf_write_variant(cgltf_write_context* context, const cgltf_material_variant* variant)
998 {
999 context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
1000
1001 cgltf_write_line(context, "{");
1002 cgltf_write_strprop(context, "name", variant->name);
1003 cgltf_write_extras(context, &variant->extras);
1004 cgltf_write_line(context, "}");
1005 }
1006
1007 cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data)
1008 {
1009 cgltf_size expected = cgltf_write(options, NULL, 0, data);
1010 char* buffer = (char*) malloc(expected);
1011 cgltf_size actual = cgltf_write(options, buffer, expected, data);
1012 if (expected != actual) {
1013 fprintf(stderr, "Error: expected %zu bytes but wrote %zu bytes.\n", expected, actual);
1014 }
1015 FILE* file = fopen(path, "wt");
1016 if (!file)
1017 {
1018 return cgltf_result_file_not_found;
1019 }
1020 // Note that cgltf_write() includes a null terminator, which we omit from the file content.
1021 fwrite(buffer, actual - 1, 1, file);
1022 fclose(file);
1023 free(buffer);
1024 return cgltf_result_success;
1025 }
1026
1027 static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extension_flags)
1028 {
1029 if (extension_flags & CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM) {
1030 cgltf_write_stritem(context, "KHR_texture_transform");
1031 }
1032 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT) {
1033 cgltf_write_stritem(context, "KHR_materials_unlit");
1034 }
1035 if (extension_flags & CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS) {
1036 cgltf_write_stritem(context, "KHR_materials_pbrSpecularGlossiness");
1037 }
1038 if (extension_flags & CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL) {
1039 cgltf_write_stritem(context, "KHR_lights_punctual");
1040 }
1041 if (extension_flags & CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION) {
1042 cgltf_write_stritem(context, "KHR_draco_mesh_compression");
1043 }
1044 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT) {
1045 cgltf_write_stritem(context, "KHR_materials_clearcoat");
1046 }
1047 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_IOR) {
1048 cgltf_write_stritem(context, "KHR_materials_ior");
1049 }
1050 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR) {
1051 cgltf_write_stritem(context, "KHR_materials_specular");
1052 }
1053 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION) {
1054 cgltf_write_stritem(context, "KHR_materials_transmission");
1055 }
1056 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN) {
1057 cgltf_write_stritem(context, "KHR_materials_sheen");
1058 }
1059 if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS) {
1060 cgltf_write_stritem(context, "KHR_materials_variants");
1061 }
1062 }
1063
1064 cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data)
1065 {
1066 (void)options;
1067 cgltf_write_context ctx;
1068 ctx.buffer = buffer;
1069 ctx.buffer_size = size;
1070 ctx.remaining = size;
1071 ctx.cursor = buffer;
1072 ctx.chars_written = 0;
1073 ctx.data = data;
1074 ctx.depth = 1;
1075 ctx.indent = " ";
1076 ctx.needs_comma = 0;
1077 ctx.extension_flags = 0;
1078 ctx.required_extension_flags = 0;
1079
1080 cgltf_write_context* context = &ctx;
1081
1082 CGLTF_SPRINTF("{");
1083
1084 if (data->accessors_count > 0)
1085 {
1086 cgltf_write_line(context, "\"accessors\": [");
1087 for (cgltf_size i = 0; i < data->accessors_count; ++i)
1088 {
1089 cgltf_write_accessor(context, data->accessors + i);
1090 }
1091 cgltf_write_line(context, "]");
1092 }
1093
1094 cgltf_write_asset(context, &data->asset);
1095
1096 if (data->buffer_views_count > 0)
1097 {
1098 cgltf_write_line(context, "\"bufferViews\": [");
1099 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1100 {
1101 cgltf_write_buffer_view(context, data->buffer_views + i);
1102 }
1103 cgltf_write_line(context, "]");
1104 }
1105
1106 if (data->buffers_count > 0)
1107 {
1108 cgltf_write_line(context, "\"buffers\": [");
1109 for (cgltf_size i = 0; i < data->buffers_count; ++i)
1110 {
1111 cgltf_write_buffer(context, data->buffers + i);
1112 }
1113 cgltf_write_line(context, "]");
1114 }
1115
1116 if (data->images_count > 0)
1117 {
1118 cgltf_write_line(context, "\"images\": [");
1119 for (cgltf_size i = 0; i < data->images_count; ++i)
1120 {
1121 cgltf_write_image(context, data->images + i);
1122 }
1123 cgltf_write_line(context, "]");
1124 }
1125
1126 if (data->meshes_count > 0)
1127 {
1128 cgltf_write_line(context, "\"meshes\": [");
1129 for (cgltf_size i = 0; i < data->meshes_count; ++i)
1130 {
1131 cgltf_write_mesh(context, data->meshes + i);
1132 }
1133 cgltf_write_line(context, "]");
1134 }
1135
1136 if (data->materials_count > 0)
1137 {
1138 cgltf_write_line(context, "\"materials\": [");
1139 for (cgltf_size i = 0; i < data->materials_count; ++i)
1140 {
1141 cgltf_write_material(context, data->materials + i);
1142 }
1143 cgltf_write_line(context, "]");
1144 }
1145
1146 if (data->nodes_count > 0)
1147 {
1148 cgltf_write_line(context, "\"nodes\": [");
1149 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1150 {
1151 cgltf_write_node(context, data->nodes + i);
1152 }
1153 cgltf_write_line(context, "]");
1154 }
1155
1156 if (data->samplers_count > 0)
1157 {
1158 cgltf_write_line(context, "\"samplers\": [");
1159 for (cgltf_size i = 0; i < data->samplers_count; ++i)
1160 {
1161 cgltf_write_sampler(context, data->samplers + i);
1162 }
1163 cgltf_write_line(context, "]");
1164 }
1165
1166 CGLTF_WRITE_IDXPROP("scene", data->scene, data->scenes);
1167
1168 if (data->scenes_count > 0)
1169 {
1170 cgltf_write_line(context, "\"scenes\": [");
1171 for (cgltf_size i = 0; i < data->scenes_count; ++i)
1172 {
1173 cgltf_write_scene(context, data->scenes + i);
1174 }
1175 cgltf_write_line(context, "]");
1176 }
1177
1178 if (data->textures_count > 0)
1179 {
1180 cgltf_write_line(context, "\"textures\": [");
1181 for (cgltf_size i = 0; i < data->textures_count; ++i)
1182 {
1183 cgltf_write_texture(context, data->textures + i);
1184 }
1185 cgltf_write_line(context, "]");
1186 }
1187
1188 if (data->skins_count > 0)
1189 {
1190 cgltf_write_line(context, "\"skins\": [");
1191 for (cgltf_size i = 0; i < data->skins_count; ++i)
1192 {
1193 cgltf_write_skin(context, data->skins + i);
1194 }
1195 cgltf_write_line(context, "]");
1196 }
1197
1198 if (data->animations_count > 0)
1199 {
1200 cgltf_write_line(context, "\"animations\": [");
1201 for (cgltf_size i = 0; i < data->animations_count; ++i)
1202 {
1203 cgltf_write_animation(context, data->animations + i);
1204 }
1205 cgltf_write_line(context, "]");
1206 }
1207
1208 if (data->cameras_count > 0)
1209 {
1210 cgltf_write_line(context, "\"cameras\": [");
1211 for (cgltf_size i = 0; i < data->cameras_count; ++i)
1212 {
1213 cgltf_write_camera(context, data->cameras + i);
1214 }
1215 cgltf_write_line(context, "]");
1216 }
1217
1218 if (data->lights_count > 0 || data->variants_count > 0)
1219 {
1220 cgltf_write_line(context, "\"extensions\": {");
1221
1222 if (data->lights_count > 0)
1223 {
1224 cgltf_write_line(context, "\"KHR_lights_punctual\": {");
1225 cgltf_write_line(context, "\"lights\": [");
1226 for (cgltf_size i = 0; i < data->lights_count; ++i)
1227 {
1228 cgltf_write_light(context, data->lights + i);
1229 }
1230 cgltf_write_line(context, "]");
1231 cgltf_write_line(context, "}");
1232 }
1233
1234 if (data->variants_count)
1235 {
1236 cgltf_write_line(context, "\"KHR_materials_variants\": {");
1237 cgltf_write_line(context, "\"variants\": [");
1238 for (cgltf_size i = 0; i < data->variants_count; ++i)
1239 {
1240 cgltf_write_variant(context, data->variants + i);
1241 }
1242 cgltf_write_line(context, "]");
1243 cgltf_write_line(context, "}");
1244 }
1245
1246 cgltf_write_line(context, "}");
1247 }
1248
1249 if (context->extension_flags != 0)
1250 {
1251 cgltf_write_line(context, "\"extensionsUsed\": [");
1252 cgltf_write_extensions(context, context->extension_flags);
1253 cgltf_write_line(context, "]");
1254 }
1255
1256 if (context->required_extension_flags != 0)
1257 {
1258 cgltf_write_line(context, "\"extensionsRequired\": [");
1259 cgltf_write_extensions(context, context->required_extension_flags);
1260 cgltf_write_line(context, "]");
1261 }
1262
1263 cgltf_write_extras(context, &data->extras);
1264
1265 CGLTF_SPRINTF("\n}\n");
1266
1267 // snprintf does not include the null terminator in its return value, so be sure to include it
1268 // in the returned byte count.
1269 return 1 + ctx.chars_written;
1270 }
1271
1272 #endif /* #ifdef CGLTF_WRITE_IMPLEMENTATION */
1273
1274 /* cgltf is distributed under MIT license:
1275 *
1276 * Copyright (c) 2019 Philip Rideout
1277
1278 * Permission is hereby granted, free of charge, to any person obtaining a copy
1279 * of this software and associated documentation files (the "Software"), to deal
1280 * in the Software without restriction, including without limitation the rights
1281 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1282 * copies of the Software, and to permit persons to whom the Software is
1283 * furnished to do so, subject to the following conditions:
1284
1285 * The above copyright notice and this permission notice shall be included in all
1286 * copies or substantial portions of the Software.
1287
1288 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1289 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1290 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1291 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1292 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1293 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1294 * SOFTWARE.
1295 */