VTK  9.7.1
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 
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 
44 VTK_ABI_NAMESPACE_BEGIN
45 class vtkCellArray;
46 class vtkDataArray;
47 class vtkFloatArray;
48 class vtkImageData;
49 class vtkMatrix4x4;
50 class vtkPoints;
51 class vtkPolyData;
53 
54 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
55 {
56 public:
57  static vtkGLTFDocumentLoader* New();
59  void PrintSelf(ostream& os, vtkIndent indent) override;
60 
64  enum class Target : unsigned short
65  {
66  ARRAY_BUFFER = 34962,
67  ELEMENT_ARRAY_BUFFER = 34963
68  };
69 
74  enum class AccessorType : unsigned char
75  {
76  SCALAR,
77  VEC2,
78  VEC3,
79  VEC4,
80  MAT2,
81  MAT3,
82  MAT4,
83  INVALID
84  };
85 
90  enum class ComponentType : unsigned short
91  {
92  BYTE = 5120,
93  UNSIGNED_BYTE = 5121,
94  SHORT = 5122,
95  UNSIGNED_SHORT = 5123,
96  UNSIGNED_INT = 5125,
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 
113  struct BufferView
114  {
115  int Buffer;
119  int Target;
121  };
122 
129  struct Accessor
130  {
135  struct Sparse
136  {
137  int Count;
143  };
148  int Count;
149  unsigned int NumberOfComponents;
151  std::vector<double> Max;
152  std::vector<double> Min;
153  bool IsSparse;
156  };
157 
164  struct MorphTarget
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 
179  struct Primitive
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 
193  int Material;
194  int Mode;
195  int CellSize; // 1, 2 or 3, depending on draw mode
196 
197  // Primitive-specific extension metadata
198  struct Extensions
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;
221  int Camera;
222  int Mesh;
223  int Skin;
224 
227 
228  bool TRSLoaded;
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
242  struct Extensions
243  {
244  // KHR_lights_punctual extension
246  {
247  int Light = -1;
248  };
250  };
252 
254 
255  void UpdateTransform();
256  };
257 
262  struct Mesh
263  {
264  std::vector<struct Primitive> Primitives;
265  std::vector<float> Weights;
267  };
268 
274  struct TextureInfo
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  {
292 
294 
296  };
297 
304  struct Material
305  {
306  enum class AlphaModeType : unsigned char
307  {
308  OPAQUE,
309  MASK,
310  BLEND
311  };
312 
314  {
316  std::vector<double> BaseColorFactor;
317 
321  };
322 
324 
330  std::vector<double> EmissiveFactor;
331 
333  double AlphaCutoff;
334 
336 
338 
339  // extension KHR_materials_unlit
340  bool Unlit;
341 
342  // extension KHR_materials_ior
343  double IOR;
344  };
345 
350  struct Texture
351  {
352  int Sampler;
353  int Source;
355  };
356 
361  struct Sampler : public GLTFSampler
362  {
364  };
365 
371  struct Scene
372  {
373  std::vector<unsigned int> Nodes;
375  };
376 
382  struct Skin
383  {
384  std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
385  std::vector<int> Joints;
387  int Skeleton;
390  };
391 
398  struct Animation
399  {
400  struct Sampler
401  {
402  enum class InterpolationMode : unsigned char
403  {
404  LINEAR,
405  STEP,
406  CUBICSPLINE
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  {
427  ROTATION,
428  TRANSLATION,
429  SCALE,
430  WEIGHTS
431  };
432  int Sampler;
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;
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;
460  double AspectRatio;
462  };
463 
470  struct Extensions
471  {
472  // KHR_lights_punctual extension
474  {
475  struct Light
476  {
477  enum class LightType : unsigned char
478  {
479  DIRECTIONAL,
480  POINT,
481  SPOT
482  };
484 
485  std::vector<double> Color;
486  double Intensity;
487  double Range;
488 
489  // Type-specific parameters
492 
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 
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);
559  bool LoadModelMetaDataFromStream(
560  vtkResourceStream* stream, vtkURILoader* loader = nullptr, bool quiet = false);
562 
566  bool LoadModelData(const std::vector<char>& glbBuffer);
567 
571  bool BuildModelVTKGeometry();
572 
576  std::shared_ptr<Model> GetInternalModel();
577 
581  static unsigned int GetNumberOfComponentsForType(vtkGLTFDocumentLoader::AccessorType type);
582 
586  virtual std::vector<std::string> GetSupportedExtensions();
587 
591  const std::vector<std::string>& GetUsedExtensions();
592 
596  bool GetIsBinary();
597 
604  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
605 
609  void BuildGlobalTransforms();
610 
614  static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
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 
668 protected:
669  vtkGLTFDocumentLoader() = default;
670  ~vtkGLTFDocumentLoader() override = default;
671 
672 private:
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 
738 VTK_ABI_NAMESPACE_END
739 #endif
std::vector< float > InitialWeights
This struct contains all data from a gltf asset.
abstract base class for most VTK objects
Definition: vtkObject.h:52
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:31
AccessorType
Defines an accessor's type.
vtkSmartPointer< vtkResourceStream > Stream
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
#define ARRAY_BUFFER
Abstract class used for custom streams.
This struct describes a glTF animation object.
vtkSmartPointer< vtkMatrix4x4 > Transform
vtkSmartPointer< vtkMatrix4x4 > GlobalTransform
vtkSmartPointer< vtkFloatArray > OutputData
vtkSmartPointer< vtkMatrix4x4 > Matrix
This struct describes a glTF textureInfo object, mostly used in material descriptions They contain tw...
std::map< std::string, int > AttributeIndices
vtkSmartPointer< vtkPolyData > Armature
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:31
This struct describes a glTF scene object.
std::vector< struct Primitive > Primitives
Target
Define an openGL draw target.
vtkSmartPointer< vtkPolyData > Geometry
std::vector< vtkSmartPointer< vtkMatrix4x4 > > InverseBindMatrices
std::map< std::string, vtkSmartPointer< vtkFloatArray > > AttributeValues
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:71
std::vector< Accessor > Accessors
std::vector< MorphTarget > Targets
std::vector< Animation::Channel > Channels
Node::Extensions::KHRLightsPunctual KHRLightsPunctualMetaData
vtkSmartPointer< vtkURILoader > URILoader
vtkSmartPointer< vtkCellArray > Indices
vtkSmartPointer< vtkFloatArray > InputData
virtual void PrepareData()
Some extensions require a preparation on the model before building VTK objects.
std::vector< Animation > Animations
This struct describes a glTF texture object.
This struct describes a glTF camera object.
This struct describes a glTF node object.
This struct describes a glTF primitive object.
a simple class to control print indentation
Definition: vtkIndent.h:28
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
std::map< std::string, vtkSmartPointer< vtkDataArray > > AttributeValues
std::map< std::string, int > AttributeIndices
This struct describes an accessor.sparse glTF object.
This struct contains extension metadata.
std::vector< float > InitialTranslation
This struct describes a glTF image object.
This struct describes a glTF material object.
std::vector< float > InitialRotation
#define ELEMENT_ARRAY_BUFFER
std::vector< BufferView > BufferViews
#define VTK_FILEPATH
object to represent cell connectivity
Definition: vtkCellArray.h:177
Helper class for readers and importer that need to load more than one resource.
Definition: vtkURILoader.h:99
std::vector< std::vector< char > > Buffers
std::vector< unsigned int > Nodes
std::vector< Material > Materials
This struct describes a glTF sampler object.
vtkSmartPointer< vtkImageData > ImageData
Deserialize a GLTF model file.
This struct describes an accessor glTF object.
This struct describes a glTF bufferView object.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
This struct describes a glTF asset.
Primitive::Extensions::KHRDracoMeshCompression KHRDracoMetaData
This struct describes a glTF mesh object.
represent and manipulate 3D points
Definition: vtkPoints.h:30
ComponentType
Define a type for different data components.
This struct describes a glTF Morph Target object.
This struct describes a glTF sampler object.
Definition: GLTFSampler.h:15
dynamic, self-adjusting array of unsigned short
std::vector< Animation::Sampler > Samplers