Skip to content

Adding gltf model support for the object mesh#190

Open
kestr31 wants to merge 2 commits intoericstoneking:masterfrom
kestr31:feature/gltf-model-support
Open

Adding gltf model support for the object mesh#190
kestr31 wants to merge 2 commits intoericstoneking:masterfrom
kestr31:feature/gltf-model-support

Conversation

@kestr31
Copy link
Copy Markdown

@kestr31 kestr31 commented May 5, 2026

Thank you for developing and maintaining 42 — it has been really helpful for preliminary design of our CubeSat flight software.

While working on a CubeSat project, I ran into difficulty using custom meshes for simulation configuration. I needed to convert a STEP assembly into OBJ via FreeCAD, but the direct STEP-to-OBJ export was unreliable. As a workaround, I exported the STEP file to glTF in FreeCAD — which worked without issues — and then imported the glTF into Blender to convert it to OBJ. While this pipeline got the job done, the extra Blender conversion step felt unnecessary: if 42 could load glTF directly, the entire workaround would be eliminated.

The glTF format ("GL Transmission Format") is an open standard by the Khronos Group and is more modern than OBJ, with broader and more reliable support in current CAD tools — FreeCAD, Blender, SolidWorks, Fusion 360, and others all export it natively. Supporting it in 42 would allow users to bring in models from a wider range of tools without needing a manual conversion step to OBJ.

This pull request adds support for loading glTF 2.0 (.gltf/.glb) 3D models alongside the existing OBJ format. The change is fully backward-compatible: all existing OBJ models continue to load through the same code path without any behavioral change. I would like to ask whether this modification could be considered for merging into the mainline codebase.

How it works

A thin LoadMeshFile() dispatcher checks the file extension:

  • .gltf or .glb files are routed to a new LoadGltfFile() loader
  • All other extensions fall through to the existing LoadWingsObjFile()

To use a glTF model, simply name a .gltf or .glb file in the spacecraft configuration (e.g., MyModel.gltf in the "Geometry Input File Name" field). No other configuration changes are needed.

Implementation details

  • Parser: cgltf v1.15, a single-header C99 library (MIT license). It is included as Kit/Include/cgltf.h with no external dependencies.
  • All glTF code is integrated into meshkit.c as static functions, following the existing pattern where all mesh I/O (OBJ loading, KD-tree, octree, etc.) lives in this file.
  • Material mapping: PBR metallic-roughness properties are approximated to the Phong shading model that 42 uses (base color → Kd, metallic → Ks, roughness → Ns, etc.).
  • Supported features: Triangle primitives (indexed and non-indexed), vertex positions, normals, texture coordinates, world transforms from the glTF scene hierarchy, and degenerate polygon filtering.

Commit structure

The PR is split into two commits for ease of review:

  1. refactor(meshkit): extract FinalizeMesh from LoadWingsObjFile — A mechanical extraction of the shared mesh finalization code (bounding box, edge tables, surface force properties, etc.) into a reusable function. This is a pure refactoring with no behavioral change, enabling the new glTF loader to reuse the same finalization logic.
  2. feat(meshkit): add glTF 2.0 model loading support — The actual glTF feature: cgltf vendor header, loader implementation, dispatcher, and call-site updates.

Files changed

File Change
Kit/Include/cgltf.h New vendor header (cgltf v1.15, MIT)
Kit/Include/meshkit.h Add PolyIsDegenerate, FinalizeMesh, LoadMeshFile declarations
Kit/Source/meshkit.c Extract FinalizeMesh; add glTF loader and LoadMeshFile dispatcher
Source/42init.c Route 5 mesh-loading calls through LoadMeshFile
Source/42commlink.c Route 2 antenna mesh-loading calls through LoadMeshFile
Makefile Add cgltf.h dependency to meshkit.o build rule

Testing

  • Builds cleanly with make clean && make on Linux (gcc, -std=gnu11 -Wall -Wshadow, zero warnings)
  • Verified existing OBJ models load and render identically (regression test with default SC_Simple configuration)
  • Verified glTF model loading and rendering with a test satellite model

Images

image image image

The console log for this model's gltf loading was like below:

glTF: Loading Model/cubesat(stowed).gltf
glTF: Counted 407940 vertices, 407940 normals, 0 texcoords, 421830 triangles
glTF: Extracted 407940 vertices, 407940 normals, 0 texcoords, 42114 polys
glTF: M->Nv=407940 M->Nvn=407940 M->Nvt=0 M->Npoly=42114
glTF: FinalizeMesh complete
...

I would be happy to make any adjustments you think are appropriate. Thank you in advance for considering this contribution.

kestr31 added 2 commits May 6, 2026 03:59
- Extract mesh finalization code into standalone FinalizeMesh function
  (bounding box, normal unitization, edge tables, SurfaceForceProps,
  EdgeAndPolyDyads, per-poly bounding sphere)
- Expose PolyIsDegenerate declaration in meshkit.h
- Remove unused variables from LoadWingsObjFile after extraction
- Add cgltf v1.15 header-only C99 parser (MIT license) as vendor header
- Add static GLTF helper functions in meshkit.c (TransformPoint,
  TransformNormal, FindAttribute, UriToPpm, MapGltfMaterial)
- Add static LoadGltfFile for parsing glTF/GLB files with support for
  triangle primitives, world transforms, and PBR-to-Phong material mapping
- Add LoadMeshFile dispatcher routing .gltf/.glb to LoadGltfFile and
  all other extensions to LoadWingsObjFile
- Replace LoadWingsObjFile with LoadMeshFile in 42init.c (5 call sites)
- Replace LoadWingsObjFile with LoadMeshFile in 42commlink.c (2 call sites)
- Add cgltf.h dependency to meshkit.o Makefile rule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant