VTK  9.6.20260429
vtkGLTFDocumentLoader.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
27
28#ifndef vtkGLTFDocumentLoader_h
29#define vtkGLTFDocumentLoader_h
30
31#include "GLTFSampler.h" // For "Sampler"
32#include "vtkIOGeometryModule.h" // For export macro
33#include "vtkObject.h"
34#include "vtkResourceStream.h" // For "vtkResourceStream"
35#include "vtkSmartPointer.h" // For "vtkSmartPointer"
36#include "vtkURILoader.h" // For "vtkURILoader"
37
38#include <map> // For std::map
39#include <memory> // For std::shared_ptr
40#include <set> // For std::set
41#include <string> // For std::string
42#include <vector> // For std::vector
43
44VTK_ABI_NAMESPACE_BEGIN
45class vtkCellArray;
46class vtkDataArray;
47class vtkFloatArray;
48class vtkImageData;
49class vtkMatrix4x4;
50class vtkPoints;
51class vtkPolyData;
53
54class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
55{
56public:
59 void PrintSelf(ostream& os, vtkIndent indent) override;
60
64 enum class Target : unsigned short
65 {
66 ARRAY_BUFFER = 34962,
68 };
69
74 enum class AccessorType : unsigned char
75 {
84 };
85
90 enum class ComponentType : unsigned short
91 {
92 BYTE = 5120,
94 SHORT = 5122,
97 FLOAT = 5126
98 };
99
100 /* The following structs help deserialize a glTF document, representing each object. As such,
101 * their members mostly match with the specification. Default values and boundaries are set
102 * according to the specification.
103 * Most of these structs contain a name property, which is optional, and, while being loaded, is
104 * not currently exploited by the loader.
105 * They are mostly root-level properties, and once created, are stored into vectors in the Model
106 * structure.
107 */
108
114 {
120 std::string Name;
121 };
122
157
165 {
166 // accessor indices from the .gltf file, the map's keys correspond to attribute names
167 std::map<std::string, int> AttributeIndices;
168 // attribute values
169 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
170 };
171
180 {
181 // accessor indices from the .glTF file, the map's keys correspond to attribute names
182 std::map<std::string, int> AttributeIndices;
185
186 // attribute values from buffer data
187 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
188
190
191 std::vector<MorphTarget> Targets;
192
194 int Mode;
195 int CellSize; // 1, 2 or 3, depending on draw mode
196
197 // Primitive-specific extension metadata
199 {
200 // KHR_draco_mesh_compression extension
201 // Only metadata are read (decoding and modifying the internal model is not done yet)
203 {
204 int BufferView = -1;
205 std::map<std::string, int> AttributeIndices;
206 };
208 };
210 };
211
218 struct Node
219 {
220 std::vector<int> Children;
222 int Mesh;
223 int Skin;
224
227
229
231
232 std::vector<float> InitialRotation;
233 std::vector<float> InitialTranslation;
234 std::vector<float> InitialScale;
235 std::vector<float> InitialWeights;
236 std::vector<float> Rotation;
237 std::vector<float> Translation;
238 std::vector<float> Scale;
239 std::vector<float> Weights;
240
241 // Object-specific extension metadata
243 {
244 // KHR_lights_punctual extension
246 {
247 int Light = -1;
248 };
250 };
252
253 std::string Name;
254
256 };
257
262 struct Mesh
263 {
264 std::vector<struct Primitive> Primitives;
265 std::vector<float> Weights;
266 std::string Name;
267 };
268
275 {
276 int Index = -1;
277 int TexCoord = -1;
278 std::vector<double> Offset;
279 std::vector<double> Scale;
280 double Rotation = 0.0;
281 };
282
287 struct Image
288 {
290 std::string MimeType;
291 std::string Uri;
292
294
295 std::string Name;
296 };
297
304 struct Material
305 {
306 enum class AlphaModeType : unsigned char
307 {
311 };
312
322
324
330 std::vector<double> EmissiveFactor;
331
334
336
337 std::string Name;
338
339 // extension KHR_materials_unlit
340 bool Unlit;
341
342 // extension KHR_materials_ior
343 double IOR;
344 };
345
350 struct Texture
351 {
354 std::string Name;
355 };
356
361 struct Sampler : public GLTFSampler
362 {
363 std::string Name;
364 };
365
371 struct Scene
372 {
373 std::vector<unsigned int> Nodes;
374 std::string Name;
375 };
376
382 struct Skin
383 {
384 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
385 std::vector<int> Joints;
388 std::string Name;
390 };
391
399 {
400 struct Sampler
401 {
402 enum class InterpolationMode : unsigned char
403 {
407 };
409 unsigned int Input;
410 unsigned int Output;
412
415
419 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
420 bool forceStep = false, bool isRotation = false) const;
421 };
422
423 struct Channel
424 {
425 enum class PathType : unsigned char
426 {
431 };
435 };
436
437 float Duration; // In seconds
438 std::set<float> AllTimestamps; // In seconds
439 std::vector<Animation::Channel> Channels;
440 std::vector<Animation::Sampler> Samplers;
441 std::string Name;
442 };
443
449 struct Camera
450 {
451 // common properties
452 double Znear;
453 double Zfar;
454 bool IsPerspective; // if not, camera mode is orthographic
455 // perspective
456 double Xmag;
457 double Ymag;
458 // orthographic
459 double Yfov;
461 std::string Name;
462 };
463
471 {
472 // KHR_lights_punctual extension
474 {
475 struct Light
476 {
477 enum class LightType : unsigned char
478 {
482 };
484
485 std::vector<double> Color;
486 double Intensity;
487 double Range;
488
489 // Type-specific parameters
492
493 std::string Name;
494 };
495 std::vector<Light> Lights;
496 };
498 };
499
503 struct Model
504 {
505 std::vector<Accessor> Accessors;
506 std::vector<Animation> Animations;
507 std::vector<std::vector<char>> Buffers;
508 std::vector<BufferView> BufferViews;
509 std::vector<Camera> Cameras;
510 std::vector<Image> Images;
511 std::vector<Material> Materials;
512 std::vector<Mesh> Meshes;
513 std::vector<Node> Nodes;
514 std::vector<Sampler> Samplers;
515 std::vector<Scene> Scenes;
516 std::vector<Skin> Skins;
517 std::vector<Texture> Textures;
518
520
521 std::string BufferMetaData;
523 std::string FileName;
526 };
527
532 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
533
537 void ResetAnimation(int animationId);
538
540
545 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
546 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
548
550
558 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
560 vtkResourceStream* stream, vtkURILoader* loader = nullptr, bool quiet = false);
562
566 bool LoadModelData(const std::vector<char>& glbBuffer);
567
572
576 std::shared_ptr<Model> GetInternalModel();
577
582
586 virtual std::vector<std::string> GetSupportedExtensions();
587
591 const std::vector<std::string>& GetUsedExtensions();
592
597
604 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
605
610
614 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
615 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
616
623 virtual void PrepareData() {}
624
626
631 vtkSetMacro(GLBStart, vtkTypeInt64);
632 vtkGetMacro(GLBStart, vtkTypeInt64);
634
636
641 vtkSetMacro(LoadAnimation, bool);
642 vtkGetMacro(LoadAnimation, bool);
643 vtkBooleanMacro(LoadAnimation, bool);
645
647
652 vtkSetMacro(LoadImages, bool);
653 vtkGetMacro(LoadImages, bool);
654 vtkBooleanMacro(LoadImages, bool);
656
658
663 vtkSetMacro(LoadSkinMatrix, bool);
664 vtkGetMacro(LoadSkinMatrix, bool);
665 vtkBooleanMacro(LoadSkinMatrix, bool);
667
668protected:
670 ~vtkGLTFDocumentLoader() override = default;
671
672private:
673 struct AccessorLoadingWorker;
674
675 struct SparseAccessorLoadingWorker;
676
677 template <typename Type>
678 struct BufferDataExtractionWorker;
679
681 void operator=(const vtkGLTFDocumentLoader&) = delete;
682
686 bool LoadSkinMatrixData();
687
692 bool ExtractPrimitiveAttributes(Primitive& primitive);
693
700 bool ExtractPrimitiveAccessorData(Primitive& primitive);
701
706 bool BuildPolyDataFromPrimitive(Primitive& primitive);
707
711 bool BuildPolyDataFromSkin(Skin& skin);
712
716 bool LoadAnimationData();
717
721 bool LoadImageData();
722
723 std::shared_ptr<Model> InternalModel;
724
725 static const std::vector<std::string> SupportedExtensions;
726 std::vector<std::string> UsedExtensions;
727 bool IsBinary = false;
728 vtkTypeInt64 GLBStart = 0;
729
733 bool LoadAnimation = true;
734 bool LoadImages = true;
735 bool LoadSkinMatrix = true;
736};
737
738VTK_ABI_NAMESPACE_END
739#endif
object to represent cell connectivity
dynamic, self-adjusting array of float
AccessorType
Defines an accessor's type.
bool LoadModelMetaDataFromFile(const std::string &FileName)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
void ResetAnimation(int animationId)
Restore the transforms that were modified by an animation to their initial state.
virtual std::vector< std::string > GetSupportedExtensions()
Get the list of extensions that are supported by this loader.
bool LoadModelData(const std::vector< char > &glbBuffer)
Load buffer data into the internal Model.
void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer< vtkMatrix4x4 > parentTransform)
Concatenate the current node's local transform to its parent's global transform, storing the resultin...
static unsigned int GetNumberOfComponentsForType(vtkGLTFDocumentLoader::AccessorType type)
Returns the number of components for a given accessor type.
static void ComputeJointMatrices(const Model &model, const Skin &skin, Node &node, std::vector< vtkSmartPointer< vtkMatrix4x4 > > &jointMats)
Compute all joint matrices of the skin of a specific node.
bool LoadModelMetaDataFromStream(vtkResourceStream *stream, vtkURILoader *loader=nullptr, bool quiet=false)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
virtual void PrepareData()
Some extensions require a preparation on the model before building VTK objects.
bool GetIsBinary()
Get if the current model contains binary data.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Target
Define an openGL draw target.
static vtkGLTFDocumentLoader * New()
bool ApplyAnimation(float t, int animationId, bool forceStep=false)
Apply the specified animation, at the specified time value t, to the internal Model.
const std::vector< std::string > & GetUsedExtensions()
Get the list of extensions that are used by the current model.
~vtkGLTFDocumentLoader() override=default
vtkGLTFDocumentLoader()=default
bool LoadStreamBuffer(vtkResourceStream *stream, std::vector< char > &glbBuffer)
Load the binary part of a binary glTF (.glb) file.
ComponentType
Define a type for different data components.
bool LoadFileBuffer(const std::string &fileName, std::vector< char > &glbBuffer)
Load the binary part of a binary glTF (.glb) file.
std::shared_ptr< Model > GetInternalModel()
Get the internal Model.
bool BuildModelVTKGeometry()
Converts the internal Model's loaded data into more convenient vtk objects.
void BuildGlobalTransforms()
Build all global transforms.
topologically and geometrically regular array of data
a simple class to control print indentation
Definition vtkIndent.h:108
represent and manipulate 4x4 transformation matrices
represent and manipulate 3D points
Definition vtkPoints.h:140
concrete dataset represents vertices, lines, polygons, and triangle strips
Abstract class used for custom streams.
Hold a reference to a vtkObjectBase instance.
Helper class for readers and importer that need to load more than one resource.
dynamic, self-adjusting array of unsigned short
This struct describes a glTF sampler object.
Definition GLTFSampler.h:16
This struct describes an accessor.sparse glTF object.
This struct describes an accessor glTF object.
vtkSmartPointer< vtkFloatArray > OutputData
vtkSmartPointer< vtkFloatArray > InputData
void GetInterpolatedData(float t, size_t numberOfComponents, std::vector< float > *output, bool forceStep=false, bool isRotation=false) const
Get the interpolated animation output at time t.
This struct describes a glTF animation object.
std::vector< Animation::Channel > Channels
std::vector< Animation::Sampler > Samplers
This struct describes a glTF bufferView object.
This struct describes a glTF camera object.
This struct contains extension metadata.
This struct describes a glTF image object.
vtkSmartPointer< vtkImageData > ImageData
This struct describes a glTF material object.
This struct describes a glTF mesh object.
std::vector< struct Primitive > Primitives
This struct contains all data from a gltf asset.
std::vector< std::vector< char > > Buffers
std::vector< BufferView > BufferViews
vtkSmartPointer< vtkResourceStream > Stream
std::vector< Animation > Animations
std::vector< Material > Materials
std::vector< Accessor > Accessors
vtkSmartPointer< vtkURILoader > URILoader
This struct describes a glTF Morph Target object.
std::map< std::string, vtkSmartPointer< vtkFloatArray > > AttributeValues
std::map< std::string, int > AttributeIndices
Node::Extensions::KHRLightsPunctual KHRLightsPunctualMetaData
This struct describes a glTF node object.
vtkSmartPointer< vtkMatrix4x4 > GlobalTransform
vtkSmartPointer< vtkMatrix4x4 > Matrix
vtkSmartPointer< vtkMatrix4x4 > Transform
std::vector< float > InitialTranslation
Primitive::Extensions::KHRDracoMeshCompression KHRDracoMetaData
This struct describes a glTF primitive object.
vtkSmartPointer< vtkCellArray > Indices
std::map< std::string, int > AttributeIndices
vtkSmartPointer< vtkPolyData > Geometry
std::map< std::string, vtkSmartPointer< vtkDataArray > > AttributeValues
This struct describes a glTF sampler object.
This struct describes a glTF scene object.
std::vector< unsigned int > Nodes
This struct describes a glTF asset.
std::vector< vtkSmartPointer< vtkMatrix4x4 > > InverseBindMatrices
vtkSmartPointer< vtkPolyData > Armature
This struct describes a glTF textureInfo object, mostly used in material descriptions They contain tw...
This struct describes a glTF texture object.
#define vtkDataArray
#define VTK_FILEPATH