VTK  9.3.0
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:
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 
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 
556 
560  bool LoadModelData(const std::vector<char>& glbBuffer);
561 
566 
570  std::shared_ptr<Model> GetInternalModel();
571 
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 
599 
603  static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
605 
606 protected:
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
object to represent cell connectivity
Definition: vtkCellArray.h:176
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:45
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:31
Deserialize a GLTF model file.
AccessorType
Defines an accessor's type.
const std::vector< std::string > & GetSupportedExtensions()
Get the list of extensions that are supported by this loader.
static vtkGLTFDocumentLoader * New()
void ResetAnimation(int animationId)
Restore the transforms that were modified by an animation to their initial state.
bool LoadFileBuffer(VTK_FILEPATH const std::string &fileName, std::vector< char > &glbBuffer)
Load the binary part of a binary glTF (.glb) file.
bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string &FileName)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
bool LoadModelMetaDataFromStream(vtkResourceStream *stream, vtkURILoader *loader=nullptr)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
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.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
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.
Target
Define an openGL draw target.
bool ApplyAnimation(float t, int animationId, bool forceStep=false)
Apply the specified animation, at the specified time value t, to the internal Model.
~vtkGLTFDocumentLoader() override=default
vtkGLTFDocumentLoader()=default
std::shared_ptr< Model > GetInternalModel()
Get the internal Model.
const std::vector< std::string > & GetUsedExtensions()
Get the list of extensions that are used by the current model.
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 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
Definition: vtkImageData.h:43
a simple class to control print indentation
Definition: vtkIndent.h:29
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:31
abstract base class for most VTK objects
Definition: vtkObject.h:52
represent and manipulate 3D points
Definition: vtkPoints.h:29
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:80
Abstract class used for custom streams.
Helper class for readers and importer that need to load more than one resource.
Definition: vtkURILoader.h:100
dynamic, self-adjusting array of unsigned short
@ vector
Definition: vtkX3D.h:237
@ type
Definition: vtkX3D.h:516
@ string
Definition: vtkX3D.h:490
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 > InitialRotation
std::vector< float > InitialWeights
std::vector< float > InitialTranslation
This struct describes a glTF primitive object.
vtkSmartPointer< vtkCellArray > Indices
std::map< std::string, int > AttributeIndices
std::vector< MorphTarget > Targets
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
This struct describes a glTF textureInfo object, mostly used in material descriptions They contain tw...
This struct describes a glTF texture object.
#define ARRAY_BUFFER
#define ELEMENT_ARRAY_BUFFER
#define VTK_FILEPATH