VTK  9.0.1
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 
37 #ifndef vtkGLTFDocumentLoader_h
38 #define vtkGLTFDocumentLoader_h
39 
40 #include "vtkIOGeometryModule.h" // For export macro
41 #include "vtkObject.h"
42 #include "vtkSmartPointer.h" // For SmartPointer
43 
44 #include <map> // For std::map
45 #include <memory> // For std::shared_ptr
46 #include <string> // For std::string
47 #include <vector> // For std::vector
48 
49 class vtkCellArray;
50 class vtkDataArray;
51 class vtkFloatArray;
52 class vtkImageData;
53 class vtkMatrix4x4;
54 class vtkPoints;
55 class vtkPolyData;
56 class vtkTransform;
58 
59 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
60 {
61 public:
62  static vtkGLTFDocumentLoader* New();
64  void PrintSelf(ostream& os, vtkIndent indent) override;
65 
69  enum class Target : unsigned short
70  {
71  ARRAY_BUFFER = 34962,
72  ELEMENT_ARRAY_BUFFER = 34963
73  };
74 
79  enum class AccessorType : unsigned char
80  {
81  SCALAR,
82  VEC2,
83  VEC3,
84  VEC4,
85  MAT2,
86  MAT3,
87  MAT4,
88  INVALID
89  };
90 
95  enum class ComponentType : unsigned short
96  {
97  BYTE = 5120,
98  UNSIGNED_BYTE = 5121,
99  SHORT = 5122,
100  UNSIGNED_SHORT = 5123,
101  UNSIGNED_INT = 5125,
102  FLOAT = 5126
103  };
104 
105  /* The following structs help deserialize a glTF document, representing each object. As such,
106  * their members mostly match with the specification. Default values and boundaries are set
107  * according to the specification.
108  * Most of these structs contain a name property, which is optional, and, while being loaded, is
109  * not currently exploited by the loader.
110  * They are mostly root-level properties, and once created, are stored into vectors in the Model
111  * structure.
112  */
113 
118  struct BufferView
119  {
120  int Buffer;
124  int Target;
126  };
127 
134  struct Accessor
135  {
140  struct Sparse
141  {
142  int Count;
148  };
153  int Count;
154  unsigned int NumberOfComponents;
156  std::vector<double> Max;
157  std::vector<double> Min;
158  bool IsSparse;
161  };
162 
169  struct MorphTarget
170  {
171  // accessor indices from the .gltf file, the map's keys correspond to attribute names
172  std::map<std::string, int> AttributeIndices;
173  // attribute values
174  std::map<std::string, vtkSmartPointer<vtkFloatArray> > AttributeValues;
175  };
176 
184  struct Primitive
185  {
186  // accessor indices from the .glTF file, the map's keys correspond to attribute names
187  std::map<std::string, int> AttributeIndices;
190 
191  // attribute values from buffer data
192  std::map<std::string, vtkSmartPointer<vtkDataArray> > AttributeValues;
193 
195 
196  std::vector<MorphTarget> Targets;
197 
198  int Material;
199  int Mode;
200  int CellSize; // 1, 2 or 3, depending on draw mode
201  };
202 
209  struct Node
210  {
211  std::vector<int> Children;
212  int Camera;
213  int Mesh;
214  int Skin;
215 
218 
219  bool TRSLoaded;
220 
222 
223  std::vector<float> InitialRotation;
224  std::vector<float> InitialTranslation;
225  std::vector<float> InitialScale;
226  std::vector<float> InitialWeights;
227  std::vector<float> Rotation;
228  std::vector<float> Translation;
229  std::vector<float> Scale;
230  std::vector<float> Weights;
231 
232  // Object-specific extension metadata
233  struct Extensions
234  {
235  // KHR_lights_punctual extension
237  {
238  int Light = -1;
239  };
241  };
243 
245 
246  void UpdateTransform();
247  };
248 
253  struct Mesh
254  {
255  std::vector<struct Primitive> Primitives;
256  std::vector<float> Weights;
258  };
259 
265  struct TextureInfo
266  {
267  int Index = -1;
268  int TexCoord;
269  };
270 
275  struct Image
276  {
280 
282 
284  };
285 
292  struct Material
293  {
294  enum class AlphaModeType : unsigned char
295  {
296  OPAQUE,
297  MASK,
298  BLEND
299  };
300 
302  {
304  std::vector<double> BaseColorFactor;
305 
309  };
310 
312 
318  std::vector<double> EmissiveFactor;
319 
321  double AlphaCutoff;
322 
324 
326  };
327 
332  struct Texture
333  {
334  int Sampler;
335  int Source;
337  };
338 
343  struct Sampler
344  {
345  enum FilterType : unsigned short
346  {
347  NEAREST = 9728,
348  LINEAR = 9729,
349  NEAREST_MIPMAP_NEAREST = 9984,
350  LINEAR_MIPMAP_NEAREST = 9985,
351  NEAREST_MIPMAP_LINEAR = 9986,
352  LINEAR_MIPMAP_LINEAR = 9987
353  };
354  enum WrapType : unsigned short
355  {
356  CLAMP_TO_EDGE = 33071,
357  MIRRORED_REPEAT = 33648,
358  REPEAT = 10497
359  };
365  };
366 
372  struct Scene
373  {
374  std::vector<unsigned int> Nodes;
376  };
377 
383  struct Skin
384  {
385  std::vector<vtkSmartPointer<vtkMatrix4x4> > InverseBindMatrices;
386  std::vector<int> Joints;
388  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::vector<Animation::Channel> Channels;
439  std::vector<Animation::Sampler> Samplers;
441  };
442 
448  struct Camera
449  {
450  // common properties
451  double Znear;
452  double Zfar;
453  bool IsPerspective; // if not, camera mode is orthographic
454  // perspective
455  double Xmag;
456  double Ymag;
457  // orthographic
458  double Yfov;
459  double AspectRatio;
461  };
462 
469  struct Extensions
470  {
471  // KHR_lights_punctual extension
473  {
474  struct Light
475  {
476  enum class LightType : unsigned char
477  {
478  DIRECTIONAL,
479  POINT,
480  SPOT
481  };
483 
484  std::vector<double> Color;
485  double Intensity;
486  double Range;
487 
488  // Type-specific parameters
491 
493  };
494  std::vector<Light> Lights;
495  };
497  };
498 
502  struct Model
503  {
504  std::vector<Accessor> Accessors;
505  std::vector<Animation> Animations;
506  std::vector<std::vector<char> > Buffers;
507  std::vector<BufferView> BufferViews;
508  std::vector<Camera> Cameras;
509  std::vector<Image> Images;
510  std::vector<Material> Materials;
511  std::vector<Mesh> Meshes;
512  std::vector<Node> Nodes;
513  std::vector<Sampler> Samplers;
514  std::vector<Scene> Scenes;
515  std::vector<Skin> Skins;
516  std::vector<Texture> Textures;
517 
519 
523  };
524 
529  bool ApplyAnimation(float t, int animationId, bool forceStep = false);
530 
534  void ResetAnimation(int animationId);
535 
540  bool LoadFileBuffer(const std::string& fileName, std::vector<char>& glbBuffer);
541 
546  bool LoadModelMetaDataFromFile(std::string FileName);
547 
551  bool LoadModelData(const std::vector<char>& glbBuffer);
552 
556  bool BuildModelVTKGeometry();
557 
561  std::shared_ptr<Model> GetInternalModel();
562 
566  static unsigned int GetNumberOfComponentsForType(vtkGLTFDocumentLoader::AccessorType type);
567 
571  const std::vector<std::string>& GetSupportedExtensions();
572 
576  const std::vector<std::string>& GetUsedExtensions();
577 
578 protected:
579  vtkGLTFDocumentLoader() = default;
580  ~vtkGLTFDocumentLoader() override = default;
581 
582 private:
583  struct AccessorLoadingWorker;
584 
585  struct SparseAccessorLoadingWorker;
586 
587  template <typename Type>
588  struct BufferDataExtractionWorker;
589 
591  void operator=(const vtkGLTFDocumentLoader&) = delete;
592 
596  bool LoadSkinMatrixData();
597 
602  bool ExtractPrimitiveAttributes(Primitive& primitive);
603 
610  bool ExtractPrimitiveAccessorData(Primitive& primitive);
611 
616  bool BuildPolyDataFromPrimitive(Primitive& primitive);
617 
621  bool LoadAnimationData();
622 
626  bool LoadImageData();
627 
634  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkTransform> parentTransform);
635 
636  std::shared_ptr<Model> InternalModel;
637 
638  static const std::vector<std::string> SupportedExtensions;
639  std::vector<std::string> UsedExtensions;
640 };
641 
642 #endif
std::vector< float > InitialWeights
This struct contains all data from a gltf asset.
abstract base class for most VTK objects
Definition: vtkObject.h:53
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:35
AccessorType
Defines an accessor&#39;s type.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
This struct describes a glTF animation object.
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:35
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
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:54
std::map< std::string, vtkSmartPointer< vtkFloatArray > > AttributeValues
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:84
std::vector< Accessor > Accessors
std::vector< MorphTarget > Targets
std::vector< Animation::Channel > Channels
Node::Extensions::KHRLightsPunctual KHRLightsPunctualMetaData
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:33
topologically and geometrically regular array of data
Definition: vtkImageData.h:41
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:49
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
std::vector< BufferView > BufferViews
object to represent cell connectivity
Definition: vtkCellArray.h:179
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.
vtkSmartPointer< vtkTransform > Transform
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:33
ComponentType
Define a type for different data components.
vtkSmartPointer< vtkTransform > GlobalTransform
This struct describes a glTF Morph Target object.
dynamic, self-adjusting array of unsigned short
std::vector< Animation::Sampler > Samplers