Git Product home page Git Product logo

gltf.c3's Introduction

gltf.c3

GLTF parser for C3

Functional features

  • GLTF files
  • GLB files
  • Morph Animations
  • Skin animations

How to use

Open and parse gltf file

Gltf gltf_data = gltf::loadFile("examples/gltf/assets/boxes.glb")!;
defer gltf_data.free();

Loop over meshes and load vertices / indices

    VertexData vertices;
    defer vertices.free();
    
    IndexData indices;
    defer indices.free();

   foreach (mesh : gltf_data.meshes) {
        foreach (index, primitive: mesh.primitives) {
            Accessor pos_accessor = gltf_data.accessors[primitive.attributes["POSITION"]!!];
            Accessor tex_accessor = gltf_data.accessors[primitive.attributes["TEX_CORD"]!!];
            Accessor normal_accessor = gltf_data.accessors[primitive.attributes["NORMAL"]!!];
            Accessor index_accessor = gltf_data.accessors[primitive.attributes["index"]!!];

            for (uint i = 0; i < pos_accessor.count ; i += 1) {
                vertices.push(Vertex {
                    gltf_data.@castBuffer(pos_accessor, i, Vec3f),
                    gltf_data.@castBuffer(tex_accessor, i, Vec2f),
                    gltf_data.@castBuffer(normal_accessor, i, Vec3f)
                });
            }

            for (uint i = 0; i < index_accessor.count ; i += 1) {
                indices.push(gltf_data.@castBuffer(index_accessor, i, ushort));
            }
        }
    }

Load textures

// Create texture-images from gltf buffer
foreach (gltf_texture : gltf_data.textures) {
    Format image_format = vk::FORMAT_R8G8B8A8_UNORM;
    TextureData texture;

    gltf::Image image = gltf_data.images[gltf_texture.source];
    gltf::Sampler gltf_sampler = gltf_data.samplers[gltf_texture.sampler];
    gltf::BufferView buffer_view = gltf_data.bufferViews[image.bufferView];
    gltf::Buffer buffer = gltf_data.buffers[buffer_view.buffer];

    stb::Image! image_data = stb::loadFromBuffer(buffer.data[buffer_view.offset..], buffer_view.byteLength, stb::Channel.STBI_RGB_ALPHA);

    if (catch err = image_data) {
        io::printfn("Failed loading image from buffer");
        return;
    }
}

gltf.c3's People

Contributors

tonis2 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

lukyguylucky

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.