VTK  9.5.20251216
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
342
347 struct Texture
348 {
351 std::string Name;
352 };
353
358 struct Sampler : public GLTFSampler
359 {
360 std::string Name;
361 };
362
368 struct Scene
369 {
370 std::vector<unsigned int> Nodes;
371 std::string Name;
372 };
373
379 struct Skin
380 {
381 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
382 std::vector<int> Joints;
385 std::string Name;
387 };
388
396 {
397 struct Sampler
398 {
399 enum class InterpolationMode : unsigned char
400 {
404 };
406 unsigned int Input;
407 unsigned int Output;
409
412
416 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
417 bool forceStep = false, bool isRotation = false) const;
418 };
419
420 struct Channel
421 {
422 enum class PathType : unsigned char
423 {
428 };
432 };
433
434 float Duration; // In seconds
435 std::set<float> AllTimestamps; // In seconds
436 std::vector<Animation::Channel> Channels;
437 std::vector<Animation::Sampler> Samplers;
438 std::string Name;
439 };
440
446 struct Camera
447 {
448 // common properties
449 double Znear;
450 double Zfar;
451 bool IsPerspective; // if not, camera mode is orthographic
452 // perspective
453 double Xmag;
454 double Ymag;
455 // orthographic
456 double Yfov;
458 std::string Name;
459 };
460
468 {
469 // KHR_lights_punctual extension
471 {
472 struct Light
473 {
474 enum class LightType : unsigned char
475 {
479 };
481
482 std::vector<double> Color;
483 double Intensity;
484 double Range;
485
486 // Type-specific parameters
489
490 std::string Name;
491 };
492 std::vector<Light> Lights;
493 };
495 };
496
500 struct Model
501 {
502 std::vector<Accessor> Accessors;
503 std::vector<Animation> Animations;
504 std::vector<std::vector<char>> Buffers;
505 std::vector<BufferView> BufferViews;
506 std::vector<Camera> Cameras;
507 std::vector<Image> Images;
508 std::vector<Material> Materials;
509 std::vector<Mesh> Meshes;
510 std::vector<Node> Nodes;
511 std::vector<Sampler> Samplers;
512 std::vector<Scene> Scenes;
513 std::vector<Skin> Skins;
514 std::vector<Texture> Textures;
515
517
518 std::string BufferMetaData;
520 std::string FileName;
523 };
524
529 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
530
534 void ResetAnimation(int animationId);
535
537
542 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
543 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
545
547
555 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
558
562 bool LoadModelData(const std::vector<char>& glbBuffer);
563
568
572 std::shared_ptr<Model> GetInternalModel();
573
578
582 virtual std::vector<std::string> GetSupportedExtensions();
583
587 const std::vector<std::string>& GetUsedExtensions();
588
595 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
596
601
605 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
606 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
607
614 virtual void PrepareData() {}
615
617
622 vtkSetMacro(GLBStart, vtkTypeInt64);
623 vtkGetMacro(GLBStart, vtkTypeInt64);
625
627
632 vtkSetMacro(LoadAnimation, bool);
633 vtkGetMacro(LoadAnimation, bool);
634 vtkBooleanMacro(LoadAnimation, bool);
636
638
643 vtkSetMacro(LoadImages, bool);
644 vtkGetMacro(LoadImages, bool);
645 vtkBooleanMacro(LoadImages, bool);
647
649
654 vtkSetMacro(LoadSkinMatrix, bool);
655 vtkGetMacro(LoadSkinMatrix, bool);
656 vtkBooleanMacro(LoadSkinMatrix, bool);
658
659protected:
661 ~vtkGLTFDocumentLoader() override = default;
662
663private:
664 struct AccessorLoadingWorker;
665
666 struct SparseAccessorLoadingWorker;
667
668 template <typename Type>
669 struct BufferDataExtractionWorker;
670
672 void operator=(const vtkGLTFDocumentLoader&) = delete;
673
677 bool LoadSkinMatrixData();
678
683 bool ExtractPrimitiveAttributes(Primitive& primitive);
684
691 bool ExtractPrimitiveAccessorData(Primitive& primitive);
692
697 bool BuildPolyDataFromPrimitive(Primitive& primitive);
698
702 bool BuildPolyDataFromSkin(Skin& skin);
703
707 bool LoadAnimationData();
708
712 bool LoadImageData();
713
714 std::shared_ptr<Model> InternalModel;
715
716 static const std::vector<std::string> SupportedExtensions;
717 std::vector<std::string> UsedExtensions;
718 vtkTypeInt64 GLBStart = 0;
719
723 bool LoadAnimation = true;
724 bool LoadImages = true;
725 bool LoadSkinMatrix = true;
726};
727
728VTK_ABI_NAMESPACE_END
729#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.
bool LoadModelMetaDataFromStream(vtkResourceStream *stream, vtkURILoader *loader=nullptr)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
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.
virtual void PrepareData()
Some extensions require a preparation on the model before building VTK objects.
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:139
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