scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
gltf_io.h File Reference

Write scenes to glTF 2.0 (.gltf + .bin) or binary glTF (.glb). More...

#include <scimesh/scene.h>
#include <scimesh/camera.h>
#include <scimesh/mesh.h>
#include <scimesh/normals.h>
#include <string>
#include <vector>
#include <cstdint>
#include <cctype>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
Include dependency graph for gltf_io.h:

Go to the source code of this file.

Classes

struct  scimesh::gltf_io::detail::PackedMesh
 One mesh, packed into glTF-ready flat arrays. More...
 
struct  scimesh::gltf_io::detail::GltfOutput
 Serialized glTF scene: the JSON document plus the binary buffer. More...
 

Namespaces

namespace  scimesh
 
namespace  scimesh::gltf_io
 
namespace  scimesh::gltf_io::detail
 

Functions

std::string scimesh::gltf_io::detail::json_escape (const std::string &s)
 
std::string scimesh::gltf_io::detail::fmt (float v)
 Print a float compactly with enough digits to round-trip a float32.
 
std::string scimesh::gltf_io::detail::join_impl (const std::vector< std::string > &v)
 
void scimesh::gltf_io::detail::append_vertex (PackedMesh &pm, const Vec3 &v, const std::vector< Vec3 > *normals, uint32_t idx, const Color *color)
 
PackedMesh scimesh::gltf_io::detail::pack_mesh (const Mesh &mesh)
 
void scimesh::gltf_io::detail::pad_bin (std::vector< uint8_t > &bin, size_t align=4)
 
GltfOutput scimesh::gltf_io::detail::build (const Scene &scene, const Camera *camera)
 
std::string scimesh::gltf_io::detail::basename (const std::string &path)
 
std::string scimesh::gltf_io::detail::dirname (const std::string &path)
 
std::string scimesh::gltf_io::detail::stem (const std::string &path)
 
void scimesh::gltf_io::detail::write_bytes (const std::string &path, const std::vector< uint8_t > &data)
 
void scimesh::gltf_io::write_gltf (const std::string &path, const Scene &scene, const Camera *camera=nullptr)
 Write a scene as glTF 2.0 (JSON document + external .bin file).
 
void scimesh::gltf_io::write_glb (const std::string &path, const Scene &scene, const Camera *camera=nullptr)
 Write a scene as a single binary glTF file (.glb).
 
void scimesh::gltf_io::write (const std::string &path, const Scene &scene, const Camera *camera=nullptr)
 Write a scene as glTF, choosing the format from the file extension.
 

Detailed Description

Write scenes to glTF 2.0 (.gltf + .bin) or binary glTF (.glb).

This is a one-way export: after a scene is written to a glTF file, the browser/GPU side is out of scimesh's scope. The library stays a CPU mesh renderer and simply emits a standard, shareable scene file.

What is exported

  • Geometry: vertex positions (FLOAT32), normals (FLOAT32) when present (computed if missing, matching what the renderer does), indices (UINT32).
  • Vertex colors mapped to the glTF COLOR_0 attribute (normalized UNSIGNED BYTE RGBA). Per-face colors have no direct glTF equivalent, so they are exported by splitting vertices (each triangle gets its own three vertices), increasing geometry ~3x.
  • Per-mesh placement transforms as node matrix (column-major, matching what the renderer applies) and node names.
  • An optional perspective camera (when a Camera* is supplied).
  • Diffuse materials (metallicFactor 0, roughnessFactor 1): scimesh is not physically-based, so exports default to fully diffuse so they are not rendered pitch black by PBR viewers.

Renderer-specific settings (shading mode, fog, SSAO, ...) are deliberately NOT exported — they are not part of the glTF standard.

Usage

Scene scene;
scene.add(sphere);
scene.add("cube", cube, glm::translate(Mat4(1.0f), Vec3(2, 0, 0)));
gltf_io::write_gltf("out/model.gltf", scene); // .gltf + .bin
gltf_io::write_glb("out/model.glb", scene); // single binary file
See also
Scene, Scene::add(), write_stl()

Definition in file gltf_io.h.