3D, c++

GLB reading and writing


Just a quick post as I am looking at a useful Nuget package Microsoft.GLTF.cpp and wanted to note down my quick experiment with it. For an unrelated project I wrote a c++ parser for the GLB format (binary version of GLTF) and wanted to replace it with something better. If you are unfamiliar with the format I reference it here http://peted.azurewebsites.net/holograms-catalogueloading-models/ where I used a c# GLTF loader within Unity and also you can find the GLTF spec here https://github.com/KhronosGroup/glTF/tree/master/specification/.

To cut a long story short if you are working in the 3D industry you will most-likely have winced when people from other sectors bring up their industries open standard file format. GLTF is the answer to that and I suspect we should all get behind it.

Anyway, back to the loader:

nuget I loaded the Nuget package via Visual Studio’s integration and looked in it’s README file to find a sample project there which shows how to save/load a glb file. I extended that a little and added the facility to print out the scene hierarchy with node names and vertex counts for each mesh. The result looks like this:


when loading in the sample GLTF Lantern file.

There are some reference models here https://github.com/KhronosGroup/glTF-Sample-Models where you can find the Lantern and others.

The code is very simple and with the clutter removed looks a bit like this; first creating a GLTFDocument:

Then, using the document to iterate and access the data as described by the spec above, mostly by looking up objects using their index into a list of objects, such as meshes, nodes and scenes, etc. The following shows a function that recurses through the scene graph  Note, how the meshes are looked up using the mesh ID.

The sample code is here https://github.com/peted70/glb-commandline-sample

Tagged , , ,

2 thoughts on “GLB reading and writing

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.