VTK  9.3.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 "vtkIOGeometryModule.h" // For export macro
32 #include "vtkObject.h"
33 #include "vtkResourceStream.h" // For "vtkResourceStream"
34 #include "vtkSmartPointer.h" // For "vtkSmartPointer"
35 #include "vtkURILoader.h" // For "vtkURILoader"
36 
37 #include <map> // For std::map
38 #include <memory> // For std::shared_ptr
39 #include <string> // For std::string
40 #include <vector> // For std::vector
41 
42 VTK_ABI_NAMESPACE_BEGIN
43 class vtkCellArray;
44 class vtkDataArray;
45 class vtkFloatArray;
46 class vtkImageData;
47 class vtkMatrix4x4;
48 class vtkPoints;
49 class vtkPolyData;
51 
52 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
53 {
54 public:
55  static vtkGLTFDocumentLoader* New();
57  void PrintSelf(ostream& os, vtkIndent indent) override;
58 
62  enum class Target : unsigned short
63  {
64  ARRAY_BUFFER = 34962,
65  ELEMENT_ARRAY_BUFFER = 34963
66  };
67 
72  enum class AccessorType : unsigned char
73  {
74  SCALAR,
75  VEC2,
76  VEC3,
77  VEC4,
78  MAT2,
79  MAT3,
80  MAT4,
81  INVALID
82  };
83 
88  enum class ComponentType : unsigned short
89  {
90  BYTE = 5120,
91  UNSIGNED_BYTE = 5121,
92  SHORT = 5122,
93  UNSIGNED_SHORT = 5123,
94  UNSIGNED_INT = 5125,
95  FLOAT = 5126
96  };
97 
98  /* The following structs help deserialize a glTF document, representing each object. As such,
99  * their members mostly match with the specification. Default values and boundaries are set
100  * according to the specification.
101  * Most of these structs contain a name property, which is optional, and, while being loaded, is
102  * not currently exploited by the loader.
103  * They are mostly root-level properties, and once created, are stored into vectors in the Model
104  * structure.
105  */
106 
111  struct BufferView
112  {
113  int Buffer;
117  int Target;
119  };
120 
127  struct Accessor
128  {
133  struct Sparse
134  {
135  int Count;
141  };
146  int Count;
147  unsigned int NumberOfComponents;
149  std::vector<double> Max;
150  std::vector<double> Min;
151  bool IsSparse;
154  };
155 
162  struct MorphTarget
163  {
164  // accessor indices from the .gltf file, the map's keys correspond to attribute names
165  std::map<std::string, int> AttributeIndices;
166  // attribute values
167  std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
168  };
169 
177  struct Primitive
178  {
179  // accessor indices from the .glTF file, the map's keys correspond to attribute names
180  std::map<std::string, int> AttributeIndices;
183 
184  // attribute values from buffer data
185  std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
186 
188 
189  std::vector<MorphTarget> Targets;
190 
191  int Material;
192  int Mode;
193  int CellSize; // 1, 2 or 3, depending on draw mode
194  };
195 
202  struct Node
203  {
204  std::vector<int> Children;
205  int Camera;
206  int Mesh;
207  int Skin;
208 
211 
212  bool TRSLoaded;
213 
215 
216  std::vector<float> InitialRotation;
217  std::vector<float> InitialTranslation;
218  std::vector<float> InitialScale;
219  std::vector<float> InitialWeights;
220  std::vector<float> Rotation;
221  std::vector<float> Translation;
222  std::vector<float> Scale;
223  std::vector<float> Weights;
224 
225  // Object-specific extension metadata
226  struct Extensions
227  {
228  // KHR_lights_punctual extension
230  {
231  int Light = -1;
232  };
234  };
236 
238 
239  void UpdateTransform();
240  };
241 
246  struct Mesh
247  {
248  std::vector<struct Primitive> Primitives;
249  std::vector<float> Weights;
251  };
252 
258  struct TextureInfo
259  {
260  int Index = -1;
261  int TexCoord;
262  };
263 
268  struct Image
269  {
273 
275 
277  };
278 
285  struct Material
286  {
287  enum class AlphaModeType : unsigned char
288  {
289  OPAQUE,
290  MASK,
291  BLEND
292  };
293 
295  {
297  std::vector<double> BaseColorFactor;
298 
302  };
303 
305 
311  std::vector<double> EmissiveFactor;
312 
314  double AlphaCutoff;
315 
317 
319 
320  // extension KHR_materials_unlit
321  bool Unlit;
322  };
323 
328  struct Texture
329  {
330  int Sampler;
331  int Source;
333  };
334 
339  struct Sampler
340  {
341  enum FilterType : unsigned short
342  {
343  NEAREST = 9728,
344  LINEAR = 9729,
345  NEAREST_MIPMAP_NEAREST = 9984,
346  LINEAR_MIPMAP_NEAREST = 9985,
347  NEAREST_MIPMAP_LINEAR = 9986,
348  LINEAR_MIPMAP_LINEAR = 9987
349  };
350  enum WrapType : unsigned short
351  {
352  CLAMP_TO_EDGE = 33071,
353  MIRRORED_REPEAT = 33648,
354  REPEAT = 10497
355  };
361  };
362 
368  struct Scene
369  {
370  std::vector<unsigned int> Nodes;
372  };
373 
379  struct Skin
380  {
381  std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
382  std::vector<int> Joints;
384  int Skeleton;
386  };
387 
394  struct Animation
395  {
396  struct Sampler
397  {
398  enum class InterpolationMode : unsigned char
399  {
400  LINEAR,
401  STEP,
402  CUBICSPLINE
403  };
405  unsigned int Input;
406  unsigned int Output;
408 
411 
415  void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
416  bool forceStep = false, bool isRotation = false) const;
417  };
418 
419  struct Channel
420  {
421  enum class PathType : unsigned char
422  {
423  ROTATION,
424  TRANSLATION,
425  SCALE,
426  WEIGHTS
427  };
428  int Sampler;
431  };
432 
433  float Duration; // In seconds
434  std::vector<Animation::Channel> Channels;
435  std::vector<Animation::Sampler> Samplers;
437  };
438 
444  struct Camera
445  {
446  // common properties
447  double Znear;
448  double Zfar;
449  bool IsPerspective; // if not, camera mode is orthographic
450  // perspective
451  double Xmag;
452  double Ymag;
453  // orthographic
454  double Yfov;
455  double AspectRatio;
457  };
458 
465  struct Extensions
466  {
467  // KHR_lights_punctual extension
469  {
470  struct Light
471  {
472  enum class LightType : unsigned char
473  {
474  DIRECTIONAL,
475  POINT,
476  SPOT
477  };
479 
480  std::vector<double> Color;
481  double Intensity;
482  double Range;
483 
484  // Type-specific parameters
487 
489  };
490  std::vector<Light> Lights;
491  };
493  };
494 
498  struct Model
499  {
500  std::vector<Accessor> Accessors;
501  std::vector<Animation> Animations;
502  std::vector<std::vector<char>> Buffers;
503  std::vector<BufferView> BufferViews;
504  std::vector<Camera> Cameras;
505  std::vector<Image> Images;
506  std::vector<Material> Materials;
507  std::vector<Mesh> Meshes;
508  std::vector<Node> Nodes;
509  std::vector<Sampler> Samplers;
510  std::vector<Scene> Scenes;
511  std::vector<Skin> Skins;
512  std::vector<Texture> Textures;
513 
515 
521  };
522 
527  bool ApplyAnimation(float t, int animationId, bool forceStep = false);
528 
532  void ResetAnimation(int animationId);
533 
535 
540  bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
541  bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
543 
545 
553  bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
554  bool LoadModelMetaDataFromStream(vtkResourceStream* stream, vtkURILoader* loader = nullptr);
556 
560  bool LoadModelData(const std::vector<char>& glbBuffer);
561 
565  bool BuildModelVTKGeometry();
566 
570  std::shared_ptr<Model> GetInternalModel();
571 
575  static unsigned int GetNumberOfComponentsForType(vtkGLTFDocumentLoader::AccessorType type);
576 
580  const std::vector<std::string>& GetSupportedExtensions();
581 
585  const std::vector<std::string>& GetUsedExtensions();
586 
593  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
594 
598  void BuildGlobalTransforms();
599 
603  static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
605 
606 protected:
607  vtkGLTFDocumentLoader() = default;
608  ~vtkGLTFDocumentLoader() override = default;
609 
610 private:
611  struct AccessorLoadingWorker;
612 
613  struct SparseAccessorLoadingWorker;
614 
615  template <typename Type>
616  struct BufferDataExtractionWorker;
617 
619  void operator=(const vtkGLTFDocumentLoader&) = delete;
620 
624  bool LoadSkinMatrixData();
625 
630  bool ExtractPrimitiveAttributes(Primitive& primitive);
631 
638  bool ExtractPrimitiveAccessorData(Primitive& primitive);
639 
644  bool BuildPolyDataFromPrimitive(Primitive& primitive);
645 
649  bool LoadAnimationData();
650 
654  bool LoadImageData();
655 
656  std::shared_ptr<Model> InternalModel;
657 
658  static const std::vector<std::string> SupportedExtensions;
659  std::vector<std::string> UsedExtensions;
660 };
661 
662 VTK_ABI_NAMESPACE_END
663 #endif
std::vector< float > InitialWeights
This struct contains all data from a gltf asset.
abstract base class for most VTK objects
Definition: vtkObject.h:51
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:30
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
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:30
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:79
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
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:42
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:175
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.
This struct describes a glTF mesh object.
represent and manipulate 3D points
Definition: vtkPoints.h:28
ComponentType
Define a type for different data components.
This struct describes a glTF Morph Target object.
dynamic, self-adjusting array of unsigned short
std::vector< Animation::Sampler > Samplers