:diamond_shape_with_a_dot_inside: Single-file glTF 2.0 loader and writer written in C99
Single-file/stb-style C glTF loader and writer
Used in: bgfx, Filament, gltfpack, raylib, and more!
Loading from file: ```c
cgltfoptions options = {0}; cgltfdata* data = NULL; cgltfresult result = cgltfparsefile(&options, "scene.gltf", &data); if (result == cgltfresultsuccess) { /* TODO make awesome stuff */ cgltffree(data); } ```
Loading from memory: ```c
void* buf; /* Pointer to glb or gltf file data / size_t size; / Size of the file data */
cgltfoptions options = {0}; cgltfdata* data = NULL; cgltfresult result = cgltfparse(&options, buf, size, &data); if (result == cgltfresultsuccess) { /* TODO make awesome stuff */ cgltf_free(data); } ```
Note that cgltf does not load the contents of extra files such as buffers or images into memory by default. You'll need to read these files yourself using URIs from
data.buffers[]or
data.images[]respectively. For buffer data, you can alternatively call
cgltf_load_buffers, which will use
FILE*APIs to open and read buffer files.
For more in-depth documentation and a description of the public interface refer to the top of the
cgltf.hfile.
When writing glTF data, you need a valid
cgltf_datastructure that represents a valid glTF document. You can construct such a structure yourself or load it using the loader functions described above. The writer functions do not deallocate any memory. So, you either have to do it manually or call
cgltf_free()if you got the data by loading it from a glTF document.
Writing to file: ```c
cgltfoptions options = {0}; cgltfdata* data = /* TODO must be valid data /; cgltfresult result = cgltfwritefile(&options, "out.gltf", data); if (result != cgltfresult_success) { / TODO handle error */ } ```
Writing to memory: ```c
cgltfoptions options = {0}; cgltfdata* data = /* TODO must be valid data */;
cgltfsize size = cgltfwrite(&options, NULL, 0, data);
char* buf = malloc(size);
cgltfsize written = cgltfwrite(&options, buf, size, data); if (written != size) { /* TODO handle error */ } ```
Note that cgltf does not write the contents of extra files such as buffers or images. You'll need to write this data yourself.
Writing does not yet support "extras" data.
For more in-depth documentation and a description of the public interface refer to the top of the
cgltf_write.hfile.
cgltf supports core glTF 2.0: - glb (binary files) and gltf (JSON files) - meshes (including accessors, buffer views, buffers) - materials (including textures, samplers, images) - scenes and nodes - skins - animations - cameras - morph targets - extras data
cgltf also supports some glTF extensions: - EXTmeshoptcompression - KHRdracomeshcompression (requires a library like Google's Draco for decompression though) - KHRlightspunctual - KHRmaterialsclearcoat - KHRmaterialsior - KHRmaterialspbrSpecularGlossiness - KHRmaterialssheen - KHRmaterialsspecular - KHRmaterialstransmission - KHRmaterialsunlit - KHRmaterialsvariants - KHRmaterialsvolume - KHRtexture_transform
cgltf does not yet support unlisted extensions. However, unlisted extensions can be accessed via "extensions" member on objects.
The easiest approach is to integrate the
cgltf.hheader file into your project. If you are unfamiliar with single-file C libraries (also known as stb-style libraries), this is how it goes:
cgltf.hwhere you need the functionality.
CGLTF_IMPLEMENTATIONbefore including
cgltf.h.
Support for writing can be found in a separate file called
cgltf_write.h(which includes
cgltf.h). Building it works analogously using the
CGLTF_WRITE_IMPLEMENTATIONdefine.
Everyone is welcome to contribute to the library. If you find any problems, you can submit them using GitHub's issue system. If you want to contribute code, you should fork the project and then send a pull request.
None.
C headers being used by implementation: ```
Note, this library has a copy of the [JSMN JSON parser](https://github.com/zserge/jsmn) embedded in its source.Testing
There is a Python script in the
test/
folder that retrieves the glTF 2.0 sample files from the glTF-Sample-Models repository (https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0) and runs the library against all gltf and glb files.Here's one way to build and run the test:
cd test ; mkdir build ; cd build ; cmake .. -DCMAKE_BUILD_TYPE=Debug make -j cd .. ./test_all.py
There is also a llvm-fuzz test in
fuzz/
. See http://llvm.org/docs/LibFuzzer.html for more information.