GL Transmission Format, binary container: C++11/STL parsing library

glTF is a format for distribution of 3D models optimized for being used in software

File extension

glb

KS implementation details

License: MIT

References

This page hosts a formal specification of GL Transmission Format, binary container using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++11/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.glb", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    gltf_binary_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.header() // => get header

C++11/STL source code to parse GL Transmission Format, binary container

gltf_binary.h

#pragma once

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <memory>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * glTF is a format for distribution of 3D models optimized for being used in software
 * \sa https://github.com/KhronosGroup/glTF/tree/2354846/specification/2.0#binary-gltf-layout Source
 */

class gltf_binary_t : public kaitai::kstruct {

public:
    class header_t;
    class chunk_t;
    class json_t;
    class bin_t;

    enum chunk_type_t {
        CHUNK_TYPE_BIN = 5130562,
        CHUNK_TYPE_JSON = 1313821514
    };

    gltf_binary_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, gltf_binary_t* p__root = nullptr);

private:
    void _read();
    void _clean_up();

public:
    ~gltf_binary_t();

    class header_t : public kaitai::kstruct {

    public:

        header_t(kaitai::kstream* p__io, gltf_binary_t* p__parent = nullptr, gltf_binary_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~header_t();

    private:
        std::string m_magic;
        uint32_t m_version;
        uint32_t m_length;
        gltf_binary_t* m__root;
        gltf_binary_t* m__parent;

    public:
        std::string magic() const { return m_magic; }

        /**
         * Indicates the version of the Binary glTF container format.
         * For this specification, should be set to 2.
         */
        uint32_t version() const { return m_version; }

        /**
         * Total length of the Binary glTF, including Header and all Chunks, in bytes.
         */
        uint32_t length() const { return m_length; }
        gltf_binary_t* _root() const { return m__root; }
        gltf_binary_t* _parent() const { return m__parent; }
    };

    class chunk_t : public kaitai::kstruct {

    public:

        chunk_t(kaitai::kstream* p__io, gltf_binary_t* p__parent = nullptr, gltf_binary_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~chunk_t();

    private:
        uint32_t m_len_data;
        chunk_type_t m_type;
        std::unique_ptr<kaitai::kstruct> m_data;
        bool n_data;

    public:
        bool _is_null_data() { data(); return n_data; };

    private:
        gltf_binary_t* m__root;
        gltf_binary_t* m__parent;
        std::string m__raw_data;
        std::unique_ptr<kaitai::kstream> m__io__raw_data;

    public:
        uint32_t len_data() const { return m_len_data; }
        chunk_type_t type() const { return m_type; }
        kaitai::kstruct* data() const { return m_data.get(); }
        gltf_binary_t* _root() const { return m__root; }
        gltf_binary_t* _parent() const { return m__parent; }
        std::string _raw_data() const { return m__raw_data; }
        kaitai::kstream* _io__raw_data() const { return m__io__raw_data.get(); }
    };

    class json_t : public kaitai::kstruct {

    public:

        json_t(kaitai::kstream* p__io, gltf_binary_t::chunk_t* p__parent = nullptr, gltf_binary_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~json_t();

    private:
        std::string m_data;
        gltf_binary_t* m__root;
        gltf_binary_t::chunk_t* m__parent;

    public:

        /**
         * This is where GLB deviates from being an elegant format.
         * To parse the rest of the file, you have to parse the JSON first.
         */
        std::string data() const { return m_data; }
        gltf_binary_t* _root() const { return m__root; }
        gltf_binary_t::chunk_t* _parent() const { return m__parent; }
    };

    class bin_t : public kaitai::kstruct {

    public:

        bin_t(kaitai::kstream* p__io, gltf_binary_t::chunk_t* p__parent = nullptr, gltf_binary_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~bin_t();

    private:
        std::string m_data;
        gltf_binary_t* m__root;
        gltf_binary_t::chunk_t* m__parent;

    public:
        std::string data() const { return m_data; }
        gltf_binary_t* _root() const { return m__root; }
        gltf_binary_t::chunk_t* _parent() const { return m__parent; }
    };

private:
    std::unique_ptr<header_t> m_header;
    std::unique_ptr<std::vector<std::unique_ptr<chunk_t>>> m_chunks;
    gltf_binary_t* m__root;
    kaitai::kstruct* m__parent;

public:
    header_t* header() const { return m_header.get(); }
    std::vector<std::unique_ptr<chunk_t>>* chunks() const { return m_chunks.get(); }
    gltf_binary_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

gltf_binary.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "gltf_binary.h"
#include "kaitai/exceptions.h"

gltf_binary_t::gltf_binary_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, gltf_binary_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_header = nullptr;
    m_chunks = nullptr;
    _read();
}

void gltf_binary_t::_read() {
    m_header = std::unique_ptr<header_t>(new header_t(m__io, this, m__root));
    m_chunks = std::unique_ptr<std::vector<std::unique_ptr<chunk_t>>>(new std::vector<std::unique_ptr<chunk_t>>());
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_chunks->push_back(std::move(std::unique_ptr<chunk_t>(new chunk_t(m__io, this, m__root))));
            i++;
        }
    }
}

gltf_binary_t::~gltf_binary_t() {
    _clean_up();
}

void gltf_binary_t::_clean_up() {
}

gltf_binary_t::header_t::header_t(kaitai::kstream* p__io, gltf_binary_t* p__parent, gltf_binary_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void gltf_binary_t::header_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x67\x6C\x54\x46", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x67\x6C\x54\x46", 4), magic(), _io(), std::string("/types/header/seq/0"));
    }
    m_version = m__io->read_u4le();
    m_length = m__io->read_u4le();
}

gltf_binary_t::header_t::~header_t() {
    _clean_up();
}

void gltf_binary_t::header_t::_clean_up() {
}

gltf_binary_t::chunk_t::chunk_t(kaitai::kstream* p__io, gltf_binary_t* p__parent, gltf_binary_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m__io__raw_data = nullptr;
    _read();
}

void gltf_binary_t::chunk_t::_read() {
    m_len_data = m__io->read_u4le();
    m_type = static_cast<gltf_binary_t::chunk_type_t>(m__io->read_u4le());
    n_data = true;
    switch (type()) {
    case gltf_binary_t::CHUNK_TYPE_JSON: {
        n_data = false;
        m__raw_data = m__io->read_bytes(len_data());
        m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
        m_data = std::unique_ptr<json_t>(new json_t(m__io__raw_data.get(), this, m__root));
        break;
    }
    case gltf_binary_t::CHUNK_TYPE_BIN: {
        n_data = false;
        m__raw_data = m__io->read_bytes(len_data());
        m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
        m_data = std::unique_ptr<bin_t>(new bin_t(m__io__raw_data.get(), this, m__root));
        break;
    }
    default: {
        m__raw_data = m__io->read_bytes(len_data());
        break;
    }
    }
}

gltf_binary_t::chunk_t::~chunk_t() {
    _clean_up();
}

void gltf_binary_t::chunk_t::_clean_up() {
    if (!n_data) {
    }
}

gltf_binary_t::json_t::json_t(kaitai::kstream* p__io, gltf_binary_t::chunk_t* p__parent, gltf_binary_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void gltf_binary_t::json_t::_read() {
    m_data = kaitai::kstream::bytes_to_str(m__io->read_bytes_full(), std::string("UTF-8"));
}

gltf_binary_t::json_t::~json_t() {
    _clean_up();
}

void gltf_binary_t::json_t::_clean_up() {
}

gltf_binary_t::bin_t::bin_t(kaitai::kstream* p__io, gltf_binary_t::chunk_t* p__parent, gltf_binary_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void gltf_binary_t::bin_t::_read() {
    m_data = m__io->read_bytes_full();
}

gltf_binary_t::bin_t::~bin_t() {
    _clean_up();
}

void gltf_binary_t::bin_t::_clean_up() {
}