VTK  9.1.0
vtkGLTFDocumentLoader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkGLTFDocumentLoader.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
40 #ifndef vtkGLTFDocumentLoader_h
41 #define vtkGLTFDocumentLoader_h
42 
43 #include "vtkIOGeometryModule.h" // For export macro
44 #include "vtkObject.h"
45 #include "vtkSmartPointer.h" // For SmartPointer
46 
47 #include <map> // For std::map
48 #include <memory> // For std::shared_ptr
49 #include <string> // For std::string
50 #include <vector> // For std::vector
51 
52 class vtkCellArray;
53 class vtkDataArray;
54 class vtkFloatArray;
55 class vtkImageData;
56 class vtkMatrix4x4;
57 class vtkPoints;
58 class vtkPolyData;
60 
61 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
62 {
63 public:
66  void PrintSelf(ostream& os, vtkIndent indent) override;
67 
71  enum class Target : unsigned short
72  {
73  ARRAY_BUFFER = 34962,
74  ELEMENT_ARRAY_BUFFER = 34963
75  };
76 
81  enum class AccessorType : unsigned char
82  {
83  SCALAR,
84  VEC2,
85  VEC3,
86  VEC4,
87  MAT2,
88  MAT3,
89  MAT4,
90  INVALID
91  };
92 
97  enum class ComponentType : unsigned short
98  {
99  BYTE = 5120,
100  UNSIGNED_BYTE = 5121,
101  SHORT = 5122,
102  UNSIGNED_SHORT = 5123,
103  UNSIGNED_INT = 5125,
104  FLOAT = 5126
105  };
106 
107  /* The following structs help deserialize a glTF document, representing each object. As such,
108  * their members mostly match with the specification. Default values and boundaries are set
109  * according to the specification.
110  * Most of these structs contain a name property, which is optional, and, while being loaded, is
111  * not currently exploited by the loader.
112  * They are mostly root-level properties, and once created, are stored into vectors in the Model
113  * structure.
114  */
115 
120  struct BufferView
121  {
122  int Buffer;
126  int Target;
128  };
129 
136  struct Accessor
137  {
142  struct Sparse
143  {
144  int Count;
150  };
155  int Count;
156  unsigned int NumberOfComponents;
158  std::vector<double> Max;
159  std::vector<double> Min;
160  bool IsSparse;
163  };
164 
171  struct MorphTarget
172  {
173  // accessor indices from the .gltf file, the map's keys correspond to attribute names
174  std::map<std::string, int> AttributeIndices;
175  // attribute values
176  std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
177  };
178 
186  struct Primitive
187  {
188  // accessor indices from the .glTF file, the map's keys correspond to attribute names
189  std::map<std::string, int> AttributeIndices;
192 
193  // attribute values from buffer data
194  std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
195 
197 
198  std::vector<MorphTarget> Targets;
199 
200  int Material;
201  int Mode;
202  int CellSize; // 1, 2 or 3, depending on draw mode
203  };
204 
211  struct Node
212  {
213  std::vector<int> Children;
214  int Camera;
215  int Mesh;
216  int Skin;
217 
220 
221  bool TRSLoaded;
222 
224 
225  std::vector<float> InitialRotation;
226  std::vector<float> InitialTranslation;
227  std::vector<float> InitialScale;
228  std::vector<float> InitialWeights;
229  std::vector<float> Rotation;
230  std::vector<float> Translation;
231  std::vector<float> Scale;
232  std::vector<float> Weights;
233 
234  // Object-specific extension metadata
235  struct Extensions
236  {
237  // KHR_lights_punctual extension
239  {
240  int Light = -1;
241  };
243  };
245 
247 
249  };
250 
255  struct Mesh
256  {
257  std::vector<struct Primitive> Primitives;
258  std::vector<float> Weights;
260  };
261 
267  struct TextureInfo
268  {
269  int Index = -1;
270  int TexCoord;
271  };
272 
277  struct Image
278  {
282 
284 
286  };
287 
294  struct Material
295  {
296  enum class AlphaModeType : unsigned char
297  {
298  OPAQUE,
299  MASK,
300  BLEND
301  };
302 
304  {
306  std::vector<double> BaseColorFactor;
307 
311  };
312 
314 
320  std::vector<double> EmissiveFactor;
321 
323  double AlphaCutoff;
324 
326 
328 
329  // extension KHR_materials_unlit
330  bool Unlit;
331  };
332 
337  struct Texture
338  {
339  int Sampler;
340  int Source;
342  };
343 
348  struct Sampler
349  {
350  enum FilterType : unsigned short
351  {
352  NEAREST = 9728,
353  LINEAR = 9729,
354  NEAREST_MIPMAP_NEAREST = 9984,
355  LINEAR_MIPMAP_NEAREST = 9985,
356  NEAREST_MIPMAP_LINEAR = 9986,
357  LINEAR_MIPMAP_LINEAR = 9987
358  };
359  enum WrapType : unsigned short
360  {
361  CLAMP_TO_EDGE = 33071,
362  MIRRORED_REPEAT = 33648,
363  REPEAT = 10497
364  };
370  };
371 
377  struct Scene
378  {
379  std::vector<unsigned int> Nodes;
381  };
382 
388  struct Skin
389  {
390  std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
391  std::vector<int> Joints;
393  int Skeleton;
395  };
396 
403  struct Animation
404  {
405  struct Sampler
406  {
407  enum class InterpolationMode : unsigned char
408  {
409  LINEAR,
410  STEP,
411  CUBICSPLINE
412  };
414  unsigned int Input;
415  unsigned int Output;
417 
420 
424  void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
425  bool forceStep = false, bool isRotation = false) const;
426  };
427 
428  struct Channel
429  {
430  enum class PathType : unsigned char
431  {
432  ROTATION,
433  TRANSLATION,
434  SCALE,
435  WEIGHTS
436  };
437  int Sampler;
440  };
441 
442  float Duration; // In seconds
443  std::vector<Animation::Channel> Channels;
444  std::vector<Animation::Sampler> Samplers;
446  };
447 
453  struct Camera
454  {
455  // common properties
456  double Znear;
457  double Zfar;
458  bool IsPerspective; // if not, camera mode is orthographic
459  // perspective
460  double Xmag;
461  double Ymag;
462  // orthographic
463  double Yfov;
464  double AspectRatio;
466  };
467 
474  struct Extensions
475  {
476  // KHR_lights_punctual extension
478  {
479  struct Light
480  {
481  enum class LightType : unsigned char
482  {
483  DIRECTIONAL,
484  POINT,
485  SPOT
486  };
488 
489  std::vector<double> Color;
490  double Intensity;
491  double Range;
492 
493  // Type-specific parameters
496 
498  };
499  std::vector<Light> Lights;
500  };
502  };
503 
507  struct Model
508  {
509  std::vector<Accessor> Accessors;
510  std::vector<Animation> Animations;
511  std::vector<std::vector<char>> Buffers;
512  std::vector<BufferView> BufferViews;
513  std::vector<Camera> Cameras;
514  std::vector<Image> Images;
515  std::vector<Material> Materials;
516  std::vector<Mesh> Meshes;
517  std::vector<Node> Nodes;
518  std::vector<Sampler> Samplers;
519  std::vector<Scene> Scenes;
520  std::vector<Skin> Skins;
521  std::vector<Texture> Textures;
522 
524 
528  };
529 
534  bool ApplyAnimation(float t, int animationId, bool forceStep = false);
535 
539  void ResetAnimation(int animationId);
540 
545  bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
546 
552 
556  bool LoadModelData(const std::vector<char>& glbBuffer);
557 
562 
566  std::shared_ptr<Model> GetInternalModel();
567 
572 
576  const std::vector<std::string>& GetSupportedExtensions();
577 
581  const std::vector<std::string>& GetUsedExtensions();
582 
589  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
590 
595 
599  static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
601 
602 protected:
604  ~vtkGLTFDocumentLoader() override = default;
605 
606 private:
607  struct AccessorLoadingWorker;
608 
609  struct SparseAccessorLoadingWorker;
610 
611  template <typename Type>
612  struct BufferDataExtractionWorker;
613 
615  void operator=(const vtkGLTFDocumentLoader&) = delete;
616 
620  bool LoadSkinMatrixData();
621 
626  bool ExtractPrimitiveAttributes(Primitive& primitive);
627 
634  bool ExtractPrimitiveAccessorData(Primitive& primitive);
635 
640  bool BuildPolyDataFromPrimitive(Primitive& primitive);
641 
645  bool LoadAnimationData();
646 
650  bool LoadImageData();
651 
652  std::shared_ptr<Model> InternalModel;
653 
654  static const std::vector<std::string> SupportedExtensions;
655  std::vector<std::string> UsedExtensions;
656 };
657 
658 #endif
object to represent cell connectivity
Definition: vtkCellArray.h:181
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:50
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:36
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 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, 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.
ComponentType
Define a type for different data components.
bool LoadModelMetaDataFromFile(std::string FileName)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
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:48
a simple class to control print indentation
Definition: vtkIndent.h:34
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:36
abstract base class for most VTK objects
Definition: vtkObject.h:63
represent and manipulate 3D points
Definition: vtkPoints.h:34
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:86
dynamic, self-adjusting array of unsigned short
@ vector
Definition: vtkX3D.h:243
@ type
Definition: vtkX3D.h:522
@ string
Definition: vtkX3D.h:496
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
std::vector< Animation > Animations
std::vector< Material > Materials
std::vector< Accessor > Accessors
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 VTK_FILEPATH