jakubtomsu / glTF2

Native Odin implementation of glTF2 loading file format as described by khronos: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why another glTF2 library?

  1. I want to learn Odin because I like philosophy behind it and syntax. I also tried Zig for few weeks, but it's syntax is not compelling to me.
  2. I don't like cgltf implementation in vendor:cgltf and it doesn't work on *Nix based systems (for now), only on Windows.
  3. Odin has built-in many great native packages like core/encoding/json so why not write glTF file format package that uses Odin types and remove C hint fields from cgltf wrapper?
  4. Learning how to implement specification document into working code. In this case it's glTF2
  5. Because it's fun.

Progress

✔️ - fully implemented ➕ - partially implemented ❌ - not implemented

Type Status Details Specification URL
Accessors ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-accessor
Animations ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-animation
Asset ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-asset
Buffers ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-buffer
Buffer Views ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-bufferview
Cameras ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-camera
Images ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-image
Materials ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-material
Meshes Missing mesh primitive targets https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-mesh
Nodes ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-node
Samplers ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-sampler
Scene ✔️ It's just an integer
Scenes ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-scene
Skins ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-skin
Textures ✔️ https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-texture
Extensions ✔️ Represented as JSON.Value https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-extension
Extensions Used ✔️ Array of strings
Extensions Required ✔️ Array of strings
Extras ✔️ Represented as JSON.Value https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-extras

How to use it

  1. Download glTF2 package from github and put in your source folder.
  2. Load entire glTF2 file into memory (simplest way).
  3. You can change floating point precision (by default it's 32bit) to 64bit, by setting flag to Odin compiler like this: -define:GLTF_DOUBLE_PRECISION=true
import "gltf2"

main :: proc() {
    // This procedure sets file format from file extension [gltf/glb]
    data, error := gltf2.load_from_file("file_name.[gltf/glb]")
    switch err in error {
    case gltf2.JSON_Error: // handle json parsing errors
    case gltf2.GLTF_Error: // handle gltf2 parsing errors
    }
    // if there are no errors we want to free memory when we are done with processing gltf/glb file.
    defer gltf2.unload(data)

    // do stuff with 'data'
    ...

    // Iterate over buffer elements using accessor:
    for it := gltf2.buf_iter_make([3]f32, &data.accessors[0], data); it.idx < it.count; it.idx += 1 {
        fmt.printf("Index: %v = %v\n", it.idx, gltf2.buf_iter_elem(&it))
    }
}
  1. Load parts of file into memory and parse itself. It can be handy if you can't load entire file into memory.
import "gltf2"

main :: proc () {
    // Set options for gltf2 parser.
    // is_glb must be set to true if file is in binary format. Most likely it will have "*.glb" suffix. By default it's gltf or JSON file format.
    // delete_content set to true will delete bytes provided in procedure call. This is what 'load_from_file' does.
    options := gltf2.Options{ is_glb = [true/false(default)], delete_content = [true/false(default)] }

    // Load some part of file that is valid JSON object
    data, error := gltf2.parse(bytes, options)
    switch err in error {
    case gltf2.JSON_Error: // Handle JSON parsing errors
    case gltf2.GLTF_Error: // Handle GLTF2 parsing errors
    }
    // If there are no errors we want to free memory when we are done with processing gltf/glb file.
    defer gltf2.unload(data)
}

How You can help.

  1. Implement missing functionality (package is still missing some stuff).
  2. Remove bugs (they are for sure).
  3. Optimize code.

About

Native Odin implementation of glTF2 loading file format as described by khronos: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html

License:MIT License


Languages

Language:Odin 100.0%