cgltf_write.h 39 KB

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