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 
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;
59 class vtkTransform;
61 
62 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
63 {
64 public:
65  static vtkGLTFDocumentLoader* New();
67  void PrintSelf(ostream& os, vtkIndent indent) override;
68 
72  enum class Target : unsigned short
73  {
74  ARRAY_BUFFER = 34962,
75  ELEMENT_ARRAY_BUFFER = 34963
76  };
77 
82  enum class AccessorType : unsigned char
83  {
84  SCALAR,
85  VEC2,
86  VEC3,
87  VEC4,
88  MAT2,
89  MAT3,
90  MAT4,
91  INVALID
92  };
93 
98  enum class ComponentType : unsigned short
99  {
100  BYTE = 5120,
101  UNSIGNED_BYTE = 5121,
102  SHORT = 5122,
103  UNSIGNED_SHORT = 5123,
104  UNSIGNED_INT = 5125,
105  FLOAT = 5126
106  };
107 
108  /* The following structs help deserialize a glTF document, representing each object. As such,
109  * their members mostly match with the specification. Default values and boundaries are set
110  * according to the specification.
111  * Most of these structs contain a name property, which is optional, and, while being loaded, is
112  * not currently exploited by the loader.
113  * They are mostly root-level properties, and once created, are stored into vectors in the Model
114  * structure.
115  */
116 
121  struct BufferView
122  {
123  int Buffer;
127  int Target;
129  };
130 
137  struct Accessor
138  {
143  struct Sparse
144  {
145  int Count;
151  };
156  int Count;
157  unsigned int NumberOfComponents;
159  std::vector<double> Max;
160  std::vector<double> Min;
161  bool IsSparse;
164  };
165 
172  struct MorphTarget
173  {
174  // accessor indices from the .gltf file, the map's keys correspond to attribute names
175  std::map<std::string, int> AttributeIndices;
176  // attribute values
177  std::map<std::string, vtkSmartPointer<vtkFloatArray> > AttributeValues;
178  };
179 
187  struct Primitive
188  {
189  // accessor indices from the .glTF file, the map's keys correspond to attribute names
190  std::map<std::string, int> AttributeIndices;
193 
194  // attribute values from buffer data
195  std::map<std::string, vtkSmartPointer<vtkDataArray> > AttributeValues;
196 
198 
199  std::vector<MorphTarget> Targets;
200 
201  int Material;
202  int Mode;
203  int CellSize; // 1, 2 or 3, depending on draw mode
204  };
205 
212  struct Node
213  {
214  std::vector<int> Children;
215  int Camera;
216  int Mesh;
217  int Skin;
218 
221 
222  bool TRSLoaded;
223 
225 
226  std::vector<float> InitialRotation;
227  std::vector<float> InitialTranslation;
228  std::vector<float> InitialScale;
229  std::vector<float> InitialWeights;
230  std::vector<float> Rotation;
231  std::vector<float> Translation;
232  std::vector<float> Scale;
233  std::vector<float> Weights;
234 
235  // Object-specific extension metadata
236  struct Extensions
237  {
238  // KHR_lights_punctual extension
240  {
241  int Light = -1;
242  };
244  };
246 
248 
249  void UpdateTransform();
250  };
251 
256  struct Mesh
257  {
258  std::vector<struct Primitive> Primitives;
259  std::vector<float> Weights;
261  };
262 
268  struct TextureInfo
269  {
270  int Index = -1;
271  int TexCoord;
272  };
273 
278  struct Image
279  {
283 
285 
287  };
288 
295  struct Material
296  {
297  enum class AlphaModeType : unsigned char
298  {
299  OPAQUE,
300  MASK,
301  BLEND
302  };
303 
305  {
307  std::vector<double> BaseColorFactor;
308 
312  };
313 
315 
321  std::vector<double> EmissiveFactor;
322 
324  double AlphaCutoff;
325 
327 
329  };
330 
335  struct Texture
336  {
337  int Sampler;
338  int Source;
340  };
341 
346  struct Sampler
347  {
348  enum FilterType : unsigned short
349  {
350  NEAREST = 9728,
351  LINEAR = 9729,
352  NEAREST_MIPMAP_NEAREST = 9984,
353  LINEAR_MIPMAP_NEAREST = 9985,
354  NEAREST_MIPMAP_LINEAR = 9986,
355  LINEAR_MIPMAP_LINEAR = 9987
356  };
357  enum WrapType : unsigned short
358  {
359  CLAMP_TO_EDGE = 33071,
360  MIRRORED_REPEAT = 33648,
361  REPEAT = 10497
362  };
368  };
369 
375  struct Scene
376  {
377  std::vector<unsigned int> Nodes;
379  };
380 
386  struct Skin
387  {
388  std::vector<vtkSmartPointer<vtkMatrix4x4> > InverseBindMatrices;
389  std::vector<int> Joints;
391  int Skeleton;
393  };
394 
401  struct Animation
402  {
403  struct Sampler
404  {
405  enum class InterpolationMode : unsigned char
406  {
407  LINEAR,
408  STEP,
409  CUBICSPLINE
410  };
412  unsigned int Input;
413  unsigned int Output;
415 
418 
422  void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
423  bool forceStep = false, bool isRotation = false) const;
424  };
425 
426  struct Channel
427  {
428  enum class PathType : unsigned char
429  {
430  ROTATION,
431  TRANSLATION,
432  SCALE,
433  WEIGHTS
434  };
435  int Sampler;
438  };
439 
440  float Duration; // In seconds
441  std::vector<Animation::Channel> Channels;
442  std::vector<Animation::Sampler> Samplers;
444  };
445 
451  struct Camera
452  {
453  // common properties
454  double Znear;
455  double Zfar;
456  bool IsPerspective; // if not, camera mode is orthographic
457  // perspective
458  double Xmag;
459  double Ymag;
460  // orthographic
461  double Yfov;
462  double AspectRatio;
464  };
465 
472  struct Extensions
473  {
474  // KHR_lights_punctual extension
476  {
477  struct Light
478  {
479  enum class LightType : unsigned char
480  {
481  DIRECTIONAL,
482  POINT,
483  SPOT
484  };
486 
487  std::vector<double> Color;
488  double Intensity;
489  double Range;
490 
491  // Type-specific parameters
494 
496  };
497  std::vector<Light> Lights;
498  };
500  };
501 
505  struct Model
506  {
507  std::vector<Accessor> Accessors;
508  std::vector<Animation> Animations;
509  std::vector<std::vector<char> > Buffers;
510  std::vector<BufferView> BufferViews;
511  std::vector<Camera> Cameras;
512  std::vector<Image> Images;
513  std::vector<Material> Materials;
514  std::vector<Mesh> Meshes;
515  std::vector<Node> Nodes;
516  std::vector<Sampler> Samplers;
517  std::vector<Scene> Scenes;
518  std::vector<Skin> Skins;
519  std::vector<Texture> Textures;
520 
522 
526  };
527 
532  bool ApplyAnimation(float t, int animationId, bool forceStep = false);
533 
537  void ResetAnimation(int animationId);
538 
543  bool LoadFileBuffer(const std::string& fileName, std::vector<char>& glbBuffer);
544 
549  bool LoadModelMetaDataFromFile(std::string FileName);
550 
554  bool LoadModelData(const std::vector<char>& glbBuffer);
555 
559  bool BuildModelVTKGeometry();
560 
564  std::shared_ptr<Model> GetInternalModel();
565 
569  static unsigned int GetNumberOfComponentsForType(vtkGLTFDocumentLoader::AccessorType type);
570 
574  const std::vector<std::string>& GetSupportedExtensions();
575 
579  const std::vector<std::string>& GetUsedExtensions();
580 
581 protected:
582  vtkGLTFDocumentLoader() = default;
583  ~vtkGLTFDocumentLoader() override = default;
584 
585 private:
586  struct AccessorLoadingWorker;
587 
588  struct SparseAccessorLoadingWorker;
589 
590  template <typename Type>
591  struct BufferDataExtractionWorker;
592 
594  void operator=(const vtkGLTFDocumentLoader&) = delete;
595 
599  bool LoadSkinMatrixData();
600 
605  bool ExtractPrimitiveAttributes(Primitive& primitive);
606 
613  bool ExtractPrimitiveAccessorData(Primitive& primitive);
614 
619  bool BuildPolyDataFromPrimitive(Primitive& primitive);
620 
624  bool LoadAnimationData();
625 
629  bool LoadImageData();
630 
637  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkTransform> parentTransform);
638 
639  std::shared_ptr<Model> InternalModel;
640 
641  static const std::vector<std::string> SupportedExtensions;
642  std::vector<std::string> UsedExtensions;
643 };
644 
645 #endif
std::vector< float > InitialWeights
This struct contains all data from a gltf asset.
abstract base class for most VTK objects
Definition: vtkObject.h:62
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