Welcome to libgltf’s documentation!

glTF 2.0 docs bestpractices visit-milstones

github-action coveralls

codacy lgtm-alerts lgtm-grade

This project was generated by glTF 2.0 JSON schema and support to load the glTF 2.0 file to a struct SGlTF.

It was used in glTFForUE4.

Features

  • glTF 2.0

  • Load the gltf/embedded/glb file

  • This is a static library

  • Cross platform

  • C++17

  • Supports the Unicode and UTF8

  • Supports some extensions
    • KHR_draco_mesh_compression - Google’s Draco

    • KHR_lights_punctual

    • KHR_materials_clearcoat

    • KHR_materials_emissive_strength

    • KHR_materials_ior

    • KHR_materials_iridescence

    • KHR_materials_sheen

    • KHR_materials_specular

    • KHR_materials_transmission

    • KHR_materials_unlit

    • KHR_materials_variants

    • KHR_materials_volume

    • KHR_texture_transform

    • ADOBE_materials_thin_transparency

    • AGI_articulations

    • AGI_stk_metadata

    • CESIUM_primitive_outline

    • EXT_lights_ies

    • EXT_mesh_gpu_instancing

    • EXT_texture_webp

    • FB_geometry_metadata

    • MSFT_lod

    • MSFT_texture_dds

  • Platforms
    • Windows
      • Win32 (win32)

      • x64 (win64)

    • Linux (linux)

    • macOS (macos)

    • Android
      • armeabi-v7a

      • armeabi-v7a-with-neon

      • arm64-v8a

      • x86

      • x86_64

    • iOS
      • iOS (iphoneos)

      • watchOS (watchos)

      • simulator

Getting Started

  1. Update the submodule

    Run git submodule update --init

  2. Generate the project by [CMake]

    Run cmake -G "[GENERATOR BY YOUR SYSTEM]" [LIBGLTF FOLDER]

  3. Build the project and generate the static library libgltf.(lib/a)

  4. Include libgltf/libgltf.h in your project.

  5. Link the static library libgltf.(lib/a) in your project.

    You have to link the static library draco.lib or draco.a with your project, if you want to support the Google’s Draco. And you can find the draco in the external folder.

Code example:

std::shared_ptr<libgltf::IglTFLoader> gltf_loader = libgltf::IglTFLoader::Create(/*a function to load the file by std::istream*/);
gltf_loader->Execute();
std::shared_ptr<libgltf::SGlTF> loaded_gltf = gltf_loader->glTF().lock();
if (!loaded_gltf)
{
    printf("failed to load your gltf file");
}

Usage

Generate the makefile or ninja or visual c++ project or xcode project by CMake.

It is a static library - libgltf.(lib/a).

How to use

Load the glTF file

You can load the glTF file by the function - libgltf::IglTFLoader::Create, like this:

std::shared_ptr<libgltf::IglTFLoader> gltf_loader = libgltf::IglTFLoader::Create(
  [](const std::string& _path)
  {
      std::filesystem::path file_path;
      if (_path.empty())
          file_path = std::filesystem::path("Monster.gltf");
      else
          file_path = std::filesystem::path("Monster.gltf").parent_path().append(_path);

      std::shared_ptr<std::istream> stream_ptr = nullptr;
      if (!std::filesystem::exists(file_path))
          return stream_ptr;

       stream_ptr = std::make_shared<std::ifstream>(file_path.string(), std::ios::in | std::ios::binary);
       return stream_ptr;
   });
std::shared_ptr<libgltf::SGlTF> loaded_gltf = gltf_loader->glTF().lock();
if (!loaded_gltf)
{
    // the glTF file is valid
    return false;
}

Load the mesh data

And get the mesh data, like this:

// get all indices of the triangle
libgltf::TVertexList<1, size_t> triangle_data;
std::shared_ptr<libgltf::TAccessorStream<libgltf::TVertexList<1, size_t> > > triangle_stream = std::make_shared<libgltf::TAccessorStream<libgltf::TVertexList<1, size_t> > >(triangle_data);
gltf_loader->LoadMeshPrimitiveIndicesData(0, 0, triangle_stream);

// get all points of the triangle
libgltf::TVertexList<3, float> position_data;
std::shared_ptr<libgltf::TAccessorStream<libgltf::TVertexList<3, float> > > position_stream = std::make_shared<libgltf::TAccessorStream<libgltf::TVertexList<3, float> > >(position_data);
gltf_loader->LoadMeshPrimitiveAttributeData(0, 0, L"position", position_stream);

Load the image data

You can get the image (data and type) by libgltf::IglTFLoader::LoadImageData, like this:

std::vector<uint8_t> image0_data;
libgltf::string_t image0_data_type;
gltf_loader->LoadImageData(0, image0_data, image0_data_type);

Advance

Regenerate new code by the glTF schema

Generate the c++11 code:

  • You can update the c++11 source code by jsonschematoc11.

  • You need update and pull the submodule external/glTF

  1. Run update_parser_by_scheme.bat

    • For Windows: cd toolsbatch&& update_parser_by_scheme.bat && cd ....

    • For Linux/MacOS cd tools/batch/ && ./update_parser_by_scheme.sh && cd ../../

  2. Build your version by CMake.

Supports Google’s draco

You can update Google’s draco submodule in external/draco or pull the draco repo by yourself.

Check the LIBGLTF_WITH_GOOGLE_DRACO or set LIBGLTF_WITH_GOOGLE_DRACO is TRUE.

  • Set the GOOGLE_DRACO_PATH_INCLUDE, GOOGLE_DRACO_PATH_BUILD, GOOGLE_DRACO_LIBRARY_DRACO_DEBUG.

  • And compile with the submodule - external/draco.

Download libraries

This project is compiled by GitHub action, and you can download the compiled library with Google’s Draco from the action page or the release page.

In the action page or the release page, libraries was compiled with MultiThreading (/MT or /MTd) for windows.

Donation

Please consider donating to sustain my activities

support-buy-a-cup-of-coffee donation-beome-a-patreon

License

The MIT license.