tinygltf

repository·release·Indexed 25 days ago

https://github.com/syoyo/tinygltf

A header-only C++ library for loading and saving glTF 2.0 assets. It offers a stable C++ version (v2) and an experimental, high-performance C version (v3) designed for low-overhead and easy language binding.

Tokens
11.9K
Snippets
33
Records
65
Agent score
81%

What's inside tinygltf

  1. What is GLM (OpenGL Mathematics)

    release

    GLM is a header-only C++ mathematics library designed for graphics software. It is based on the OpenGL Shading Language (GLSL) specifications, meaning it uses the same naming conventions and functionality as GLSL. This makes it highly intuitive for developers familiar with GLSL to use in C++.

    Key features include:

    • GLSL Compatibility: Classes and functions mirror GLSL.
    • Extension System: Provides extended capabilities like matrix transformations, quaternions, data packing, random numbers, and noise using GLSL-style extension conventions.
    • Versatility: While optimized for OpenGL, it is suitable for software rendering (raytracing/rasterisation), image processing, and physics simulations.
    • Zero Dependencies: A platform-independent, header-only library.
  2. Include GLM stable extensions

    release
    GLM provides stable extensions for specialized math operations. To use an extension, include its dedicated header file. Once included, the features are added to the glm namespace. Including an extension also automatically includes all its dependent core functionalities and other extensions.
  3. Understand NanoSG core concepts: Node and Scene

    release

    NanoSG uses two primary abstractions to manage a scene graph:

    • Node: Represents a node in the scene graph. It can be a transformation node (created by passing nullptr to the Node constructor) or a Mesh (shape) node. Nodes can contain multiple children.
    • Scene: Acts as the container for root nodes and provides the interface for ray intersection testing.
  4. Implement required Mesh and Intersection classes

    release

    To use NanoSG in a user application, you must implement specific classes that the library expects for traversal:

    Mesh class

    The example assumes meshes are composed of triangles. You must implement the following method for Scene::Traversal:

    /// Get the geometric normal and the shading normal at `face_idx' th face.
    ///
    template<typename T>
    void GetNormal(T Ng[3], T Ns[3], const unsigned int face_idx, const T u, const T v) const;

    Intersection class

    You must provide a class that represents intersection (hit) information.

    ///
    /// Get the geometric normal and the shading normal at `face_idx' th face.
    ///
    template<typename T>
    void GetNormal(T Ng[3], T Ns[3], const unsigned int face_idx, const T u, const T v) const;
  5. Use GLM precision qualifiers

    release

    GLM supports GLSL-style precision qualifiers (lowp, mediump, highp) using a prefix syntax on the type names. This allows trading precision for performance. By default, all types use high precision.

    To use these, prefix the type name (e.g., lowp_vec4, mediump_vec4, highp_vec4).

    #include <glm/glm.hpp>
    
    ivec3 foo(const vec4 & v)
    {
        highp_vec4 a = v;
        mediump_vec4 b = a;
        lowp_ivec3 c = glm::ivec3(b);
        return c;
    }
  6. Manage NanoSG memory and transformations

    release

    Memory Management

    Scene and Node do not create copies of asset data (such as vertices or indices). The user is responsible for managing the lifetime and memory of all scene assets.

    Transformations

    Transformations are calculated using the following hierarchy: M' = parent_xform x local_xform x local_pivot

  7. Accessing extension property values in v2

    release

    In the ExtensionMap for v2, JSON number values are stored as tinygltf::Value objects.

    • IsNumber() returns true for both integer and floating-point values.
    • To retrieve a floating-point value, use the GetNumberAsDouble() method.
  8. Run TinyGLTF WASM examples with wasmtime

    release
    After building a .wasi executable (such as loader_example.wasi), you can run it using wasmtime. If your application needs to access local files (like .gltf models), you must grant directory access using the --dir flag.
  9. Best practices for using GLM

    release

    When integrating GLM into your project, follow these best practices to avoid common issues:

    • Avoid using namespace glm;: GLM is based on GLSL, which uses common tokens for types and functions. Using the namespace globally can cause name collisions with other libraries or the standard library.
    • Define NOMINMAX on Windows: If you are using Windows headers, define NOMINMAX before including them. This prevents the Windows min and max macros from interfering with GLM.
    • Use Radians for Angles: Since GLM 0.9.6, all angular functions (like rotate or perspective) expect angles in radians, not degrees.
    • Handle Domain Errors: Be aware that certain functions (like glm::normalize) may crash if passed invalid input (e.g., a zero vector), as this is treated as a domain error similar to the C++ standard library.
  10. Build the glview OpenGL viewer on MacOSX and Linux

    release

    The glview example is a simple OpenGL viewer for glTF geometry. To build it on MacOSX or Linux, you need premake5 (alpha12 or later), GLEW, and glfw3.

    If glfw3 is not found by pkg-config, you must manually set the PKG_CONFIG_PATH before running the build commands.

    # optional. set pkg-config path to find glfw3
    $ export PKG_CONFIG_PATH=/path/to/pkgconfig
    
    > premake4 gmake
    $ make