$ show jkuhlmann/cgltf
cgltf is a single-file, stb-style C library for parsing and writing glTF 2.0 files. It provides a comprehensive set of data structures mirroring the glTF spec, along with functions to parse JSON/GLB, load buffers, validate data, and write back glTF/GLB. The library is self-contained, requires no external dependencies, and includes an embedded copy of the JSMN JSON parser. It supports core glTF 2.0 and many extensions (KHR_materials_*, KHR_lights_punctual, EXT_mesh_gpu_instancing, etc.). The code is well-structured, uses C99, and is designed for easy integration into any C or C++ project. It is used by major projects like bgfx, Filament, and raylib. The writer component (cgltf_write.h) is separate but similarly crafted. The repository includes a comprehensive test suite and fuzz testing setup.
The LLM identified 2 files worth highlighting for reuse:
The core parser header. It defines all glTF data structures, provides cgltf_parse, cgltf_parse_file, cgltf_load_buffers, cgltf_validate, cgltf_free, and various accessor and node utility functions. The implementation is guarded by CGLTF_IMPLEMENTATION and includes an embedded JSMN JSON parser for tokenizing glTF JSON. The code demonstrates careful memory management, pointer fixup for cross-references, efficient sparse accessor handling, and comprehensive validation. It is a model of a single-file C library, self-contained with no external dependencies beyond standard C headers.
The writer header, which includes cgltf.h and provides cgltf_write_file and cgltf_write. It serializes a cgltf_data structure back to JSON or GLB format. It handles all supported extensions, texture transforms, material properties, and emits properly formatted JSON. The writer uses a context struct with a buffer cursor and formatting macros, ensuring correct indentation and comma placement. It demonstrates how to robustly serialize complex nested data structures to a stream with memory constraints.