GLB reading and writing

1 minute read

gltf

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 /3d/3dmodels/azure/catalogue/cloud/gltf/hololens/unity/unity3d/2017/11/21/holograms-catalogueloading-models.html 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:

output

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

Comments