cgltf glTF 2.0 Loader and Writer

repository·master·Indexed 42 days ago

https://github.com/warrenm/gltfkit2

A single-file, C-based glTF 2.0 loader and writer using an stb-style integration pattern. It supports core glTF 2.0 features, GLB files, and numerous extensions including KHR_materials_unlit and EXT_meshopt_compression. Provides functions for parsing glTF data from files or memory, automatic buffer loading via cgltf_load_buffers, and writing data back to disk or memory buffers.

Tokens
1.5K
Snippets
5
Records
7
Agent score
4%

What's inside cgltf

  1. Handle external buffers and images in cgltf

    master

    By default, cgltf does not load the contents of external files like buffers or images into memory. You have two options:

    1. Manual Loading: Iterate through data.buffers[] or data.images[] and use the provided URIs to load the data yourself.
    2. Automatic Loading:
      • Use cgltf_load_buffers to automatically open and read buffer files using FILE* APIs. This also handles base64 decoding in buffers.
      • Use cgltf_load_buffer_base64 for base64 encoded data URIs in images.
  2. Integrate cgltf into your C project

    master

    cgltf is a single-file/stb-style C library. To integrate it, follow these steps:

    1. Include cgltf.h in your project files.
    2. In exactly one source file, define CGLTF_IMPLEMENTATION before including the header to generate the implementation.
    3. If you need writing capabilities, you must also define CGLTF_WRITE_IMPLEMENTATION in that same file and include cgltf_write.h (which includes cgltf.h).
    #include "cgltf.h"
    
    #define CGLTF_IMPLEMENTATION
    // Implementation is generated here
    
    #define CGLTF_WRITE_IMPLEMENTATION
    #include "cgltf_write.h"
    // Writing implementation is generated here
  3. Write glTF data to a file

    master

    To write glTF data, you need a valid cgltf_data structure. Use cgltf_write_file to save it to disk. Note that the writer functions do not deallocate memory; you must call cgltf_free() manually if the data was originally loaded via the parser.

    #define CGLTF_IMPLEMENTATION
    #define CGLTF_WRITE_IMPLEMENTATION
    #include "cgltf_write.h"
    
    cgltf_options options = {0};
    cgltf_data* data = /* valid data pointer */;
    cgltf_result result = cgltf_write_file(&options, "out.gltf", data);
    if (result != cgltf_result_success)
    {
    	/* handle error */
    }
  4. Write glTF data to memory

    master

    To write glTF data to a memory buffer, you must perform a two-step process:

    1. Call cgltf_write with a NULL buffer and 0 size to determine the required buffer size (cgltf_size).
    2. Allocate a buffer of that size.
    3. Call cgltf_write again with the allocated buffer to perform the actual write.

    Note: cgltf does not write the contents of extra files (buffers/images); you must handle those separately.

    #define CGLTF_IMPLEMENTATION
    #define CGLTF_WRITE_IMPLEMENTATION
    #include "cgltf_write.h"
    
    cgltf_options options = {0};
    cgltf_data* data = /* valid data pointer */;
    
    // 1. Get required size
    cgltf_size size = cgltf_write(&options, NULL, 0, data);
    
    // 2. Allocate
    char* buf = malloc(size);
    
    // 3. Write
    cgltf_size written = cgltf_write(&options, buf, size, data);
    if (written != size)
    {
    	/* handle error */
    }
  5. Load glTF data from memory

    master

    Use cgltf_parse to load glTF data from an existing memory buffer. This is useful when the file data is already in memory (e.g., loaded via a custom asset manager).

    #define CGLTF_IMPLEMENTATION
    #include "cgltf.h"
    
    void* buf; /* Pointer to glb or gltf file data */
    size_t size; /* Size of the file data */
    
    cgltf_options options = {0};
    cgltf_data* data = NULL;
    cgltf_result result = cgltf_parse(&options, buf, size, &data);
    if (result == cgltf_result_success)
    {
    	/* Use data */
    	cgltf_free(data);
    }
  6. Load glTF data from a file

    master

    Use cgltf_parse_file to load a glTF or GLB file from disk. You must provide a cgltf_options struct and a pointer to a cgltf_data* which will be populated on success. Always call cgltf_free(data) to release the memory when finished.

    #define CGLTF_IMPLEMENTATION
    #include "cgltf.h"
    
    cgltf_options options = {0};
    cgltf_data* data = NULL;
    cgltf_result result = cgltf_parse_file(&options, "scene.gltf", &data);
    if (result == cgltf_result_success)
    {
    	/* Use data */
    	cgltf_free(data);
    }
  7. Supported glTF 2.0 features and extensions

    master

    cgltf supports core glTF 2.0 features including glb/gltf formats, meshes, materials, scenes, nodes, skins, animations, cameras, morph targets, and extras data.

    Supported Extensions:

    • EXT_mesh_gpu_instancing
    • EXT_meshopt_compression
    • EXT_texture_webp
    • KHR_draco_mesh_compression (requires external library like Google Draco)
    • KHR_lights_punctual
    • KHR_materials_anisotropy
    • KHR_materials_clearcoat
    • KHR_materials_diffuse_transmission
    • KHR_materials_dispersion
    • KHR_materials_emissive_strength
    • KHR_materials_ior
    • KHR_materials_iridescence
    • KHR_materials_pbrSpecularGlossiness
    • KHR_materials_sheen
    • KHR_materials_specular
    • KHR_materials_transmission
    • KHR_materials_unlit
    • KHR_materials_variants
    • KHR_materials_volume
    • KHR_texture_basisu (requires external library like Binomial BasisU)
    • KHR_texture_transform

    Unlisted extensions can still be accessed via the extensions member on objects.