vsgXchange: include/vsgXchange/gltf.h Source File

vsgXchange 1.1.9 VulkanSceneGraph 3rd party data integration library

Loading...
Searching...
No Matches
gltf.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2025 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shimages be included in images
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24</editor-fold> */
25
26#include <vsg/animation/AnimationGroup.h>
27#include <vsg/animation/JointSampler.h>
28#include <vsg/app/Camera.h>
29#include <vsg/io/JSONParser.h>
30#include <vsg/io/ReaderWriter.h>
31#include <vsg/lighting/Light.h>
32#include <vsg/utils/GraphicsPipelineConfigurator.h>
33#include <vsgXchange/Version.h>
34
35namespace vsgXchange
36{
37
39 class VSGXCHANGE_DECLSPEC gltf : public vsg::Inherit<vsg::ReaderWriter, gltf>
40 {
41 public:
42 gltf();
43
44 vsg::ref_ptr<vsg::Object> read(const vsg::Path&, vsg::ref_ptr<const vsg::Options>) const override;
45 vsg::ref_ptr<vsg::Object> read(std::istream&, vsg::ref_ptr<const vsg::Options>) const override;
46 vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;
47
48 vsg::ref_ptr<vsg::Object> read_gltf(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
49 vsg::ref_ptr<vsg::Object> read_glb(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
50
51 vsg::Logger::Level level = vsg::Logger::LOGGER_WARN;
52
53 bool supportedExtension(const vsg::Path& ext) const;
54
55 bool getFeatures(Features& features) const override;
56
57 static constexpr const char* report = "report";
58 static constexpr const char* culling = "culling";
59 static constexpr const char* disable_gltf = "disable_gltf";
60 static constexpr const char* clone_accessors = "clone_accessors";
61 static constexpr const char* maxAnisotropy = "maxAnisotropy";
62
63 bool readOptions(vsg::Options& options, vsg::CommandLine& arguments) const override;
64
65 public:
66 struct glTFid
67 {
68 static const uint32_t invalid_value = std::numeric_limits<uint32_t>::max();
69
70 uint32_t value = invalid_value;
71
72 bool valid() const { return value != invalid_value; }
73
74 explicit operator bool() const noexcept { return valid(); }
75 };
76
77 struct VSGXCHANGE_DECLSPEC Extensions : public vsg::Inherit<vsg::JSONtoMetaDataSchema, Extensions>
78 {
79 std::map<std::string, vsg::ref_ptr<vsg::JSONParser::Schema>> values;
80
81 void report(vsg::LogOutput& output);
82
83 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
84 };
85
86 using Extras = vsg::JSONtoMetaDataSchema;
87
88 struct VSGXCHANGE_DECLSPEC ExtensionsExtras : public vsg::Inherit<vsg::JSONParser::Schema, ExtensionsExtras>
89 {
90 vsg::ref_ptr<Extensions> extensions;
91 vsg::ref_ptr<Extras> extras;
92
93 virtual bool requireMetaData() const { return extras.valid(); }
94
95 void report(vsg::LogOutput& output);
96
97 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
98
99 template<class T>
100 vsg::ref_ptr<T> extension(const char* name) const
101 {
102 if (!extensions) return {};
103
104 auto itr = extensions->values.find(name);
105 if (itr != extensions->values.end())
106 return itr->second.cast<T>();
107 else
108 return {};
109 }
110 };
111
112 struct VSGXCHANGE_DECLSPEC NameExtensionsExtras : public vsg::Inherit<ExtensionsExtras, NameExtensionsExtras>
113 {
114 std::string name;
115
116 bool requireMetaData() const override { return !name.empty() || extras.valid(); }
117
118 void report(vsg::LogOutput& output);
119
120 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
121 };
122
123 struct VSGXCHANGE_DECLSPEC SparseIndices : public vsg::Inherit<NameExtensionsExtras, SparseIndices>
124 {
125 glTFid bufferView;
126 uint32_t byteOffset = 0;
127 uint32_t componentType = 0;
128
129 void report(vsg::LogOutput& output);
130 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
131 };
132
133 struct VSGXCHANGE_DECLSPEC SparseValues : public vsg::Inherit<NameExtensionsExtras, SparseValues>
134 {
135 glTFid bufferView;
136 uint32_t byteOffset = 0;
137
138 void report(vsg::LogOutput& output);
139 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
140 };
141
142 struct VSGXCHANGE_DECLSPEC Sparse : public vsg::Inherit<NameExtensionsExtras, Sparse>
143 {
144 uint32_t count = 0;
145 vsg::ref_ptr<SparseIndices> indices;
146 vsg::ref_ptr<SparseValues> values;
147
148 void report(vsg::LogOutput& output);
149 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
150 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
151 };
152
153 enum ComponentType : uint32_t
154 {
155 COMPONENT_TYPE_UNDEFINED = 0,
156 COMPONENT_TYPE_BYTE = 5120,
157 COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
158 COMPONENT_TYPE_SHORT = 5122,
159 COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
160 COMPONENT_TYPE_INT = 5124,
161 COMPONENT_TYPE_UNSIGNED_INT = 5125,
162 COMPONENT_TYPE_FLOAT = 5126,
163 COMPONENT_TYPE_DOUBLE = 5130
164 };
165
166 struct DataProperties
167 {
168 uint32_t componentType = 0;
169 uint32_t componentSize = 0;
170 uint32_t componentCount = 0;
171 };
172
173 struct VSGXCHANGE_DECLSPEC Accessor : public vsg::Inherit<NameExtensionsExtras, Accessor>
174 {
175 glTFid bufferView;
176 uint32_t byteOffset = 0;
177 uint32_t componentType = 0;
178 bool normalized = false;
179 uint32_t count = 0;
180 std::string type;
181 vsg::ValuesSchema<double> max;
182 vsg::ValuesSchema<double> min;
183 vsg::ref_ptr<Sparse> sparse;
184
185 DataProperties getDataProperties() const;
186
187 void report(vsg::LogOutput& output);
188 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
189 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
190 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
191 void read_bool(vsg::JSONParser& parser, const std::string_view& property, bool value) override;
192 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
193 };
194
195 struct VSGXCHANGE_DECLSPEC Asset : public vsg::Inherit<ExtensionsExtras, Asset>
196 {
197 std::string copyright;
198 std::string version;
199 std::string generator;
200 std::string minVersion;
201
202 void report(vsg::LogOutput& output);
203 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
204 };
205
206 struct VSGXCHANGE_DECLSPEC BufferView : public vsg::Inherit<NameExtensionsExtras, BufferView>
207 {
208 glTFid buffer;
209 uint32_t byteOffset = 0;
210 uint32_t byteLength = 0;
211 uint32_t byteStride = 1;
212 uint32_t target = 0;
213
214 void report(vsg::LogOutput& output);
215 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
216 };
217
218 struct VSGXCHANGE_DECLSPEC Buffer : public vsg::Inherit<NameExtensionsExtras, Buffer>
219 {
220 std::string_view uri;
221 uint32_t byteLength = 0;
222
223 // loaded from uri
224 vsg::ref_ptr<vsg::Data> data;
225
226 void report(vsg::LogOutput& output);
227 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
228 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
229 };
230
231 struct VSGXCHANGE_DECLSPEC Image : public vsg::Inherit<NameExtensionsExtras, Image>
232 {
233 std::string_view uri;
234 std::string mimeType;
235 glTFid bufferView;
236
237 // loaded from uri
238 vsg::ref_ptr<vsg::Data> data;
239
240 void report(vsg::LogOutput& output);
241 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
242 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
243 };
244
245 struct VSGXCHANGE_DECLSPEC TextureInfo : public vsg::Inherit<ExtensionsExtras, TextureInfo>
246 {
247 glTFid index;
248 uint32_t texCoord = 0;
249
250 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
251 };
252
253 // https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform
254 struct VSGXCHANGE_DECLSPEC KHR_texture_transform : public vsg::Inherit<vsg::JSONParser::Schema, KHR_texture_transform>
255 {
256 vsg::ValuesSchema<float> offset;
257 float rotation = 0.0f;
258 vsg::ValuesSchema<float> scale;
259 uint32_t texCoord = 0;
260
261 // extention prototype will be cloned when it's used.
262 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_texture_transform::create(*this); }
263
264 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
265 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
266 };
267
268 struct VSGXCHANGE_DECLSPEC PbrMetallicRoughness : public vsg::Inherit<ExtensionsExtras, PbrMetallicRoughness>
269 {
270 vsg::ValuesSchema<float> baseColorFactor; // default { 1.0, 1.0, 1.0, 1.0 }
271 TextureInfo baseColorTexture;
272 float metallicFactor = 1.0f;
273 float roughnessFactor = 1.0f;
274 TextureInfo metallicRoughnessTexture;
275
276 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
277 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
278 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
279 };
280
281 struct VSGXCHANGE_DECLSPEC NormalTextureInfo : public vsg::Inherit<TextureInfo, NormalTextureInfo>
282 {
283 float scale = 1.0f;
284
285 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
286 };
287
288 struct VSGXCHANGE_DECLSPEC OcclusionTextureInfo : public vsg::Inherit<TextureInfo, OcclusionTextureInfo>
289 {
290 float strength = 1.0f;
291
292 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
293 };
294
296 struct VSGXCHANGE_DECLSPEC KHR_materials_specular : public vsg::Inherit<vsg::JSONParser::Schema, KHR_materials_specular>
297 {
298 float specularFactor = 1.0f;
299 TextureInfo specularTexture;
300 vsg::ValuesSchema<float> specularColorFactor;
301 TextureInfo specularColorTexture;
302
303 // extention prototype will be cloned when it's used.
304 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_materials_specular::create(*this); }
305
306 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
307 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
308 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
309 };
310
312 struct VSGXCHANGE_DECLSPEC KHR_materials_pbrSpecularGlossiness : public vsg::Inherit<ExtensionsExtras, KHR_materials_pbrSpecularGlossiness>
313 {
314 vsg::ValuesSchema<float> diffuseFactor;
315 TextureInfo diffuseTexture;
316 vsg::ValuesSchema<float> specularFactor;
317 float glossinessFactor = 1.0f;
318 TextureInfo specularGlossinessTexture;
319
320 // extention prototype will be cloned when it's used.
321 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_materials_pbrSpecularGlossiness::create(*this); }
322
323 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
324 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
325 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
326 };
327
328 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_emissive_strength
329 struct VSGXCHANGE_DECLSPEC KHR_materials_emissive_strength : public vsg::Inherit<ExtensionsExtras, KHR_materials_emissive_strength>
330 {
331 float emissiveStrength = 1.0f;
332
333 // extention prototype will be cloned when it's used.
334 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_materials_emissive_strength::create(*this); }
335
336 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
337 };
338
340 struct VSGXCHANGE_DECLSPEC KHR_materials_ior : public vsg::Inherit<vsg::JSONParser::Schema, KHR_materials_ior>
341 {
342 float ior = 1.5f;
343
344 // extention prototype will be cloned when it's used.
345 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_materials_ior::create(*this); }
346
347 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
348 };
349
350 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_unlit
351 struct VSGXCHANGE_DECLSPEC KHR_materials_unlit : public vsg::Inherit<vsg::JSONParser::Schema, KHR_materials_unlit>
352 {
353 // extention prototype will be cloned when it's used.
354 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_materials_unlit::create(*this); }
355 };
356
357 // Extensions used in glTF-Sample-Assets/Models
358 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy
359 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_clearcoat
360 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_diffuse_transmission
361 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_dispersion
362 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_iridescence
363 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_sheen
364 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_transmission
365 // https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_volume
366
367 struct VSGXCHANGE_DECLSPEC Sampler : public vsg::Inherit<NameExtensionsExtras, Sampler>
368 {
369 uint32_t minFilter = 0;
370 uint32_t magFilter = 0;
371 uint32_t wrapS = 10497; // default REPEAT
372 uint32_t wrapT = 10497; // default REPEAT
373 uint32_t wrapR = 10497; // default REPEAT, not part of official glTF spec but occurs in select .gltf/.glb files.
374
375 // extensions
376 // extras
377
378 void report(vsg::LogOutput& output);
379 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
380 };
381
382 struct VSGXCHANGE_DECLSPEC Texture : public vsg::Inherit<NameExtensionsExtras, Texture>
383 {
384 glTFid sampler;
385 glTFid source;
386
387 void report(vsg::LogOutput& output);
388 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
389 };
390
391 struct VSGXCHANGE_DECLSPEC Material : public vsg::Inherit<NameExtensionsExtras, Material>
392 {
393 PbrMetallicRoughness pbrMetallicRoughness;
394 NormalTextureInfo normalTexture;
395 OcclusionTextureInfo occlusionTexture;
396 TextureInfo emissiveTexture;
397 vsg::ValuesSchema<float> emissiveFactor; // default { 0.0, 0.0, 0.0 }
398 std::string alphaMode = "OPAQUE";
399 float alphaCutoff = 0.5f;
400 bool doubleSided = false;
401
402 void report(vsg::LogOutput& output);
403 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
404 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
405 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
406 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
407 void read_bool(vsg::JSONParser& parser, const std::string_view& property, bool value) override;
408 };
409
410 struct VSGXCHANGE_DECLSPEC Attributes : public vsg::Inherit<vsg::JSONParser::Schema, Attributes>
411 {
412 std::map<std::string, glTFid> values;
413
414 void read_number(vsg::JSONParser&, const std::string_view& property, std::istream& input) override;
415 };
416
417 struct VSGXCHANGE_DECLSPEC Primitive : public vsg::Inherit<ExtensionsExtras, Primitive>
418 {
419 Attributes attributes;
420 glTFid indices;
421 glTFid material;
422 uint32_t mode = 4;
423 vsg::ObjectsSchema<Attributes> targets;
424
425 void report(vsg::LogOutput& output);
426 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
427 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
428 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
429 };
430
431 struct VSGXCHANGE_DECLSPEC Mesh : public vsg::Inherit<NameExtensionsExtras, Mesh>
432 {
433 vsg::ObjectsSchema<Primitive> primitives;
434 vsg::ValuesSchema<double> weights;
435
436 void report(vsg::LogOutput& output);
437 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
438 };
439
441 struct VSGXCHANGE_DECLSPEC KHR_draco_mesh_compression : public vsg::Inherit<ExtensionsExtras, KHR_draco_mesh_compression>
442 {
443 glTFid bufferView;
444 Attributes attributes;
445
446 // extention prototype will be cloned when it's used.
447 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_draco_mesh_compression::create(*this); }
448
449 void report(vsg::LogOutput& output);
450 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
451 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
452 };
453
455 struct VSGXCHANGE_DECLSPEC EXT_mesh_gpu_instancing : public vsg::Inherit<ExtensionsExtras, EXT_mesh_gpu_instancing>
456 {
457 vsg::ref_ptr<Attributes> attributes;
458
459 // extention prototype will be cloned when it's used.
460 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return EXT_mesh_gpu_instancing::create(*this); }
461
462 void report(vsg::LogOutput& output);
463 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
464 };
465
466 struct VSGXCHANGE_DECLSPEC Node : public vsg::Inherit<NameExtensionsExtras, Node>
467 {
468 glTFid camera;
469 glTFid skin;
470 glTFid mesh;
471 vsg::ValuesSchema<glTFid> children;
472 vsg::ValuesSchema<double> matrix;
473 vsg::ValuesSchema<double> rotation;
474 vsg::ValuesSchema<double> scale;
475 vsg::ValuesSchema<double> translation;
476 vsg::ValuesSchema<double> weights;
477
478 void report(vsg::LogOutput& output);
479 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
480 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
481 };
482
483 struct VSGXCHANGE_DECLSPEC Scene : public vsg::Inherit<NameExtensionsExtras, Scene>
484 {
485 vsg::ValuesSchema<glTFid> nodes;
486
487 void report(vsg::LogOutput& output);
488 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
489 };
490
491 struct VSGXCHANGE_DECLSPEC AnimationTarget : public vsg::Inherit<ExtensionsExtras, AnimationTarget>
492 {
493 glTFid node;
494 std::string path;
495
496 void report(vsg::LogOutput& output);
497 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
498 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
499 };
500
501 struct VSGXCHANGE_DECLSPEC AnimationChannel : public vsg::Inherit<ExtensionsExtras, AnimationChannel>
502 {
503 glTFid sampler;
504 AnimationTarget target;
505
506 void report(vsg::LogOutput& output);
507 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
508 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
509 };
510
511 struct VSGXCHANGE_DECLSPEC AnimationSampler : public vsg::Inherit<ExtensionsExtras, AnimationSampler>
512 {
513 glTFid input;
514 std::string interpolation;
515 glTFid output;
516
517 void report(vsg::LogOutput& output);
518 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
519 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
520 };
521
522 struct VSGXCHANGE_DECLSPEC Animation : public vsg::Inherit<NameExtensionsExtras, Animation>
523 {
524 vsg::ObjectsSchema<AnimationChannel> channels;
525 vsg::ObjectsSchema<AnimationSampler> samplers;
526
527 void report(vsg::LogOutput& output);
528 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
529 };
530
531 struct VSGXCHANGE_DECLSPEC Skins : public vsg::Inherit<NameExtensionsExtras, Skins>
532 {
533 glTFid inverseBindMatrices;
534 glTFid skeleton;
535 vsg::ValuesSchema<glTFid> joints;
536
537 void report(vsg::LogOutput& output);
538 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
539 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
540 };
541
542 struct VSGXCHANGE_DECLSPEC Orthographic : public vsg::Inherit<ExtensionsExtras, Orthographic>
543 {
544 double xmag = 1.0;
545 double ymag = 1.0;
546 double znear = 1.0;
547 double zfar = 1000.0;
548
549 void report(vsg::LogOutput& output);
550 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
551 };
552
553 struct VSGXCHANGE_DECLSPEC Perspective : public vsg::Inherit<ExtensionsExtras, Perspective>
554 {
555 double aspectRatio = 1.0;
556 double yfov = 1.0;
557 double znear = 1.0;
558 double zfar = 1000.0;
559
560 void report(vsg::LogOutput& output);
561 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
562 };
563
564 struct VSGXCHANGE_DECLSPEC Camera : public vsg::Inherit<NameExtensionsExtras, Camera>
565 {
566 vsg::ref_ptr<Orthographic> orthographic;
567 vsg::ref_ptr<Perspective> perspective;
568 std::string type;
569
570 void report(vsg::LogOutput& output);
571 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
572 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
573 };
574
575 struct VSGXCHANGE_DECLSPEC Spot : public vsg::Inherit<ExtensionsExtras, Spot>
576 {
577 double innerConeAngle = 0.0;
578 double outerConeAngle = 0.7853981633974483;
579
580 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
581 };
582
583 struct VSGXCHANGE_DECLSPEC Light : public vsg::Inherit<NameExtensionsExtras, Light>
584 {
585 vsg::ValuesSchema<float> color;
586 float intensity = 1.0f;
587 vsg::ref_ptr<Spot> spot;
588 std::string type;
589 float range = std::numeric_limits<float>::max();
590
591 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
592 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
593 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
594 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
595 };
596
597 struct VSGXCHANGE_DECLSPEC KHR_lights_punctual : vsg::Inherit<ExtensionsExtras, KHR_lights_punctual>
598 {
599 vsg::ObjectsSchema<Light> lights;
600 glTFid light;
601
602 // extention prototype will be cloned when it's used.
603 vsg::ref_ptr<vsg::Object> clone(const vsg::CopyOp&) const override { return KHR_lights_punctual::create(*this); }
604
605 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
606 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
607 };
608
609 struct VSGXCHANGE_DECLSPEC glTF : public vsg::Inherit<ExtensionsExtras, glTF>
610 {
611 // filename that the glTF was read from.
612 vsg::Path filename;
613
614 // data structures read by the parser
615 vsg::StringsSchema extensionsUsed;
616 vsg::StringsSchema extensionsRequired;
617 vsg::ref_ptr<Asset> asset;
618 vsg::ObjectsSchema<Accessor> accessors;
619 vsg::ObjectsSchema<BufferView> bufferViews;
620 vsg::ObjectsSchema<Buffer> buffers;
621 vsg::ObjectsSchema<Image> images;
622 vsg::ObjectsSchema<Material> materials;
623 vsg::ObjectsSchema<Mesh> meshes;
624 vsg::ObjectsSchema<Node> nodes;
625 vsg::ObjectsSchema<Sampler> samplers;
626 glTFid scene;
627 vsg::ObjectsSchema<Scene> scenes;
628 vsg::ObjectsSchema<Texture> textures;
629 vsg::ObjectsSchema<Animation> animations;
630 vsg::ObjectsSchema<Camera> cameras;
631 vsg::ObjectsSchema<Skins> skins;
632
633 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
634 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
635 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
636
637 void report(vsg::LogOutput& output);
638
639 virtual void resolveURIs(vsg::ref_ptr<const vsg::Options> options);
640 };
641
642 class VSGXCHANGE_DECLSPEC SceneGraphBuilder : public vsg::Inherit<vsg::Object, SceneGraphBuilder>
643 {
644 public:
645 SceneGraphBuilder();
646
647 struct SamplerImage
648 {
649 vsg::ref_ptr<vsg::Sampler> sampler;
650 vsg::ref_ptr<vsg::Data> image;
651 };
652
653 vsg::ref_ptr<const vsg::Options> options;
654 vsg::ref_ptr<vsg::ShaderSet> flatShaderSet;
655 vsg::ref_ptr<vsg::ShaderSet> pbrShaderSet;
656 vsg::ref_ptr<vsg::SharedObjects> sharedObjects;
657
658 vsg::CoordinateConvention source_coordinateConvention = vsg::CoordinateConvention::Y_UP;
659 int instanceNodeHint = vsg::Options::INSTANCE_NONE;
660 bool cloneAccessors = false;
661 float maxAnisotropy = 16.0f;
662
663 vsg::ref_ptr<glTF> model;
664
665 std::vector<vsg::ref_ptr<vsg::Data>> vsg_buffers;
666 std::vector<vsg::ref_ptr<vsg::Data>> vsg_bufferViews;
667 std::vector<vsg::ref_ptr<vsg::Data>> vsg_accessors;
668 std::vector<vsg::ref_ptr<vsg::Camera>> vsg_cameras;
669 std::vector<vsg::ref_ptr<vsg::JointSampler>> vsg_skins;
670 std::vector<vsg::ref_ptr<vsg::Sampler>> vsg_samplers;
671 std::vector<vsg::ref_ptr<vsg::Data>> vsg_images;
672 std::vector<SamplerImage> vsg_textures;
673 std::vector<vsg::ref_ptr<vsg::DescriptorConfigurator>> vsg_materials;
674 std::vector<vsg::ref_ptr<vsg::Node>> vsg_meshes;
675 std::vector<vsg::ref_ptr<vsg::Light>> vsg_lights;
676 std::vector<vsg::ref_ptr<vsg::Node>> vsg_nodes;
677 std::vector<vsg::ref_ptr<vsg::Node>> vsg_scenes;
678 std::vector<bool> vsg_joints;
679 vsg::Animations vsg_animations;
680
681 struct MeshExtras
682 {
683 vsg::ref_ptr<gltf::Attributes> instancedAttributes;
684 vsg::ref_ptr<vsg::JointSampler> jointSampler;
685 };
686
687 vsg::ref_ptr<vsg::DescriptorConfigurator> default_material;
688
689 // map used to map gltf attribute names to ShaderSet vertex attribute names
690 std::map<std::string, std::string> attributeLookup;
691
692 void assign_extras(ExtensionsExtras& src, vsg::Object& dest);
693 void assign_name_extras(NameExtensionsExtras& src, vsg::Object& dest);
694
695 bool decodePrimitiveIfRequired(vsg::ref_ptr<gltf::Primitive> gltf_primitive);
696
697 void flattenTransforms(gltf::Node& node, const vsg::dmat4& transform);
698
699 bool getTransform(gltf::Node& node, vsg::dmat4& transform);
700
701 vsg::ref_ptr<vsg::Data> createBuffer(vsg::ref_ptr<gltf::Buffer> gltf_buffer);
702 vsg::ref_ptr<vsg::Data> createBufferView(vsg::ref_ptr<gltf::BufferView> gltf_bufferView);
703 vsg::ref_ptr<vsg::Data> createArray(const std::string& type, uint32_t componentType, glTFid bufferView, uint32_t offset, uint32_t count);
704 vsg::ref_ptr<vsg::Data> createAccessor(vsg::ref_ptr<gltf::Accessor> gltf_accessor);
705 vsg::ref_ptr<vsg::Camera> createCamera(vsg::ref_ptr<gltf::Camera> gltf_camera);
706 vsg::ref_ptr<vsg::Sampler> createSampler(vsg::ref_ptr<gltf::Sampler> gltf_sampler);
707 vsg::ref_ptr<vsg::Data> createImage(vsg::ref_ptr<gltf::Image> gltf_image);
708 SamplerImage createTexture(vsg::ref_ptr<gltf::Texture> gltf_texture);
709 vsg::ref_ptr<vsg::DescriptorConfigurator> createPbrMaterial(vsg::ref_ptr<gltf::Material> gltf_material);
710 vsg::ref_ptr<vsg::DescriptorConfigurator> createUnlitMaterial(vsg::ref_ptr<gltf::Material> gltf_material);
711 vsg::ref_ptr<vsg::DescriptorConfigurator> createMaterial(vsg::ref_ptr<gltf::Material> gltf_material);
712 vsg::ref_ptr<vsg::Node> createMesh(vsg::ref_ptr<gltf::Mesh> gltf_mesh, const MeshExtras& extras = {});
713 vsg::ref_ptr<vsg::Light> createLight(vsg::ref_ptr<gltf::Light> gltf_light);
714 vsg::ref_ptr<vsg::Node> createNode(vsg::ref_ptr<gltf::Node> gltf_node, bool jointNode);
715 vsg::ref_ptr<vsg::Animation> createAnimation(vsg::ref_ptr<gltf::Animation> gltf_animation);
716 vsg::ref_ptr<vsg::Node> createScene(vsg::ref_ptr<gltf::Scene> gltf_scene, bool requiresRootTransformNode, const vsg::dmat4& matrix);
717
718 vsg::ref_ptr<vsg::ShaderSet> getOrCreatePbrShaderSet();
719 vsg::ref_ptr<vsg::ShaderSet> getOrCreateFlatShaderSet();
720
721 vsg::ref_ptr<vsg::Object> createSceneGraph(vsg::ref_ptr<gltf::glTF> in_model, vsg::ref_ptr<const vsg::Options> in_options);
722 };
723
724 static vsg::Path decodeURI(const std::string_view& uri);
725
727 static bool dataURI(const std::string_view& uri, std::string_view& mimeType, std::string_view& encoding, std::string_view& value);
728
730 static vsg::Path mimeTypeToExtension(const std::string_view& mimeType);
731
732 virtual void assignExtensions(vsg::JSONParser& parser) const;
733 };
734
736 inline std::ostream& operator<<(std::ostream& output, const gltf::glTFid& id)
737 {
738 if (id)
739 output << "glTFid(" << id.value << ")";
740 else
741 output << "glTFid(null)";
742 return output;
743 }
744
746 inline std::istream& operator>>(std::istream& input, gltf::glTFid& id)
747 {
748 input >> id.value;
749 return input;
750 }
751
752} // namespace vsgXchange
753
754EVSG_type_name(vsgXchange::gltf)
vsgXchange::gltf
gltf ReaderWriter
Definition gltf.h:40
vsgXchange::gltf::mimeTypeToExtension
static vsg::Path mimeTypeToExtension(const std::string_view &mimeType)
function for mapping a mimeType to .extension that can be used with vsgXchange's plugins.
vsgXchange::gltf::culling
static constexpr const char * culling
bool, report parsed glTF to console, defaults to false
Definition gltf.h:58
vsgXchange::gltf::clone_accessors
static constexpr const char * clone_accessors
bool, disable vsgXchange::gltf so vsgXchange::assimp will be used instead, defaults to false
Definition gltf.h:60
vsgXchange::gltf::dataURI
static bool dataURI(const std::string_view &uri, std::string_view &mimeType, std::string_view &encoding, std::string_view &value)
function for extracting components of a uri
vsgXchange::gltf::readOptions
bool readOptions(vsg::Options &options, vsg::CommandLine &arguments) const override
float, default setting of vsg::Sampler::maxAnisotropy to use.
vsgXchange::gltf::maxAnisotropy
static constexpr const char * maxAnisotropy
bool, hint to clone the data associated with accessors, defaults to false
Definition gltf.h:61
vsgXchange::gltf::disable_gltf
static constexpr const char * disable_gltf
bool, insert cull nodes, defaults to true
Definition gltf.h:59
vsgXchange::gltf::Accessor
Definition gltf.h:174
vsgXchange::gltf::AnimationChannel
Definition gltf.h:502
vsgXchange::gltf::Animation
Definition gltf.h:523
vsgXchange::gltf::AnimationSampler
Definition gltf.h:512
vsgXchange::gltf::AnimationTarget
Definition gltf.h:492
vsgXchange::gltf::Asset
Definition gltf.h:196
vsgXchange::gltf::Attributes
Definition gltf.h:411
vsgXchange::gltf::Buffer
Definition gltf.h:219
vsgXchange::gltf::BufferView
Definition gltf.h:207
vsgXchange::gltf::Camera
Definition gltf.h:565
vsgXchange::gltf::DataProperties
Definition gltf.h:167
vsgXchange::gltf::EXT_mesh_gpu_instancing
https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing/README....
Definition gltf.h:456
vsgXchange::gltf::ExtensionsExtras
Definition gltf.h:89
vsgXchange::gltf::Extensions
Definition gltf.h:78
vsgXchange::gltf::Image
Definition gltf.h:232
vsgXchange::gltf::KHR_draco_mesh_compression
https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_draco_mesh_compression
Definition gltf.h:442
vsgXchange::gltf::KHR_lights_punctual
Definition gltf.h:598
vsgXchange::gltf::KHR_materials_emissive_strength
Definition gltf.h:330
vsgXchange::gltf::KHR_materials_ior
index of refraction : https://github.com/KhronosGroup/glTF/blob/main/extensions/2....
Definition gltf.h:341
vsgXchange::gltf::KHR_materials_pbrSpecularGlossiness
https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGloss...
Definition gltf.h:313
vsgXchange::gltf::KHR_materials_specular
https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_specular
Definition gltf.h:297
vsgXchange::gltf::KHR_materials_unlit
Definition gltf.h:352
vsgXchange::gltf::KHR_texture_transform
Definition gltf.h:255
vsgXchange::gltf::Light
Definition gltf.h:584
vsgXchange::gltf::Material
Definition gltf.h:392
vsgXchange::gltf::Mesh
Definition gltf.h:432
vsgXchange::gltf::NameExtensionsExtras
Definition gltf.h:113
vsgXchange::gltf::Node
Definition gltf.h:467
vsgXchange::gltf::NormalTextureInfo
Definition gltf.h:282
vsgXchange::gltf::OcclusionTextureInfo
Definition gltf.h:289
vsgXchange::gltf::Orthographic
Definition gltf.h:543
vsgXchange::gltf::PbrMetallicRoughness
Definition gltf.h:269
vsgXchange::gltf::Perspective
Definition gltf.h:554
vsgXchange::gltf::Primitive
Definition gltf.h:418
vsgXchange::gltf::Sampler
Definition gltf.h:368
vsgXchange::gltf::SceneGraphBuilder::MeshExtras
Definition gltf.h:682
vsgXchange::gltf::SceneGraphBuilder::SamplerImage
Definition gltf.h:648
vsgXchange::gltf::Scene
Definition gltf.h:484
vsgXchange::gltf::Skins
Definition gltf.h:532
vsgXchange::gltf::Sparse
Definition gltf.h:143
vsgXchange::gltf::SparseIndices
Definition gltf.h:124
vsgXchange::gltf::SparseValues
Definition gltf.h:134
vsgXchange::gltf::Spot
Definition gltf.h:576
vsgXchange::gltf::Texture
Definition gltf.h:383
vsgXchange::gltf::TextureInfo
Definition gltf.h:246
vsgXchange::gltf::glTF
Definition gltf.h:610
vsgXchange::gltf::glTFid
Definition gltf.h:67