23#ifndef VCL_IO_MESH_GLTF_LOAD_H
24#define VCL_IO_MESH_GLTF_LOAD_H
26#include <vclib/io/file_info.h>
27#include <vclib/io/mesh/settings.h>
29#include "detail/info.h"
30#include "detail/load_mesh.h"
36template<Matrix44Concept MatrixType, LoggerConcept LogType>
37void loadGltfMeshesWhileTraversingNodes(
38 const tinygltf::Model& model,
41 MatrixType currentMatrix,
43 const LoadSettings& settings,
47 currentMatrix * gltfCurrentNodeMatrix<MatrixType>(model, currentNode);
49 if (model.nodes[currentNode].mesh >= 0) {
50 int meshid = model.nodes[currentNode].mesh;
64 for (
int c : model.nodes[currentNode].children) {
67 loadGltfMeshesWhileTraversingNodes(
79template<MeshConcept MeshType, LoggerConcept LogType>
81 tinygltf::Model& model,
82 std::vector<MeshType>& meshes,
83 std::vector<MeshInfo>& infos,
84 const LoadSettings& settings = LoadSettings(),
87 using ScalarType = MeshType::ScalarType;
89 using Matrix44m = std::conditional_t<
90 HasTransformMatrix<MeshType>,
91 typename MeshType::TransformMatrixType,
92 Matrix44<ScalarType>>;
94 uint nMeshes = gltfNumberMeshes(model);
96 Matrix44m identityMatrix = Matrix44m::Identity();
98 meshes.resize(nMeshes);
99 infos.resize(nMeshes);
101 log.startProgress(
"Reading meshes", nMeshes);
103 auto mit = meshes.begin();
104 auto iit = infos.begin();
106 for (
unsigned int s = 0; s < model.scenes.size(); ++s) {
107 const tinygltf::Scene& scene = model.scenes[s];
108 for (
unsigned int n = 0; n < scene.nodes.size(); ++n) {
109 loadGltfMeshesWhileTraversingNodes(
110 model, mit, iit, identityMatrix, scene.nodes[n], settings, log);
112 log.progress(s * scene.nodes.size() + n);
121template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
123 std::vector<MeshType>& m,
124 const std::string& filename,
125 std::vector<MeshInfo>& loadedInfo,
126 const LoadSettings& settings = LoadSettings(),
129 tinygltf::TinyGLTF loader;
130 tinygltf::Model model;
140 if (ext ==
".gltf") {
141 ret = loader.LoadASCIIFromFile(&model, &err, &warn, filename);
143 else if (ext ==
".glb") {
144 ret = loader.LoadBinaryFromFile(&model, &err, &warn, filename);
147 throw UnknownFileFormatException(ext);
151 throw CannotOpenFileException(
152 "Failed to load glTF file: " + filename +
"\n" + err);
157 "Warnings while loading glTF file: " + warn,
158 LogType::LogLevel::WARNING_LOG);
161 detail::loadGltf(model, m, loadedInfo, settings, log);
164template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
167 const std::string& filename,
168 MeshInfo& loadedInfo,
169 const LoadSettings& settings = LoadSettings(),
172 std::vector<MeshType> meshes;
173 std::vector<MeshInfo> infos;
174 loadGltf(meshes, filename, infos, settings, log);
176 m = std::move(meshes.front());
177 loadedInfo = std::move(infos.front());
178 for (std::size_t i = 1; i < meshes.size(); ++i) {
179 m.append(std::move(meshes[i]));
static std::string extension(const std::string &filename)
Get the extension of a file.
Definition file_info.h:260
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:123