VTK  9.0.3
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:
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 
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 
550 
554  bool LoadModelData(const std::vector<char>& glbBuffer);
555 
560 
564  std::shared_ptr<Model> GetInternalModel();
565 
570 
574  const std::vector<std::string>& GetSupportedExtensions();
575 
579  const std::vector<std::string>& GetUsedExtensions();
580 
581 protected:
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
object to represent cell connectivity
Definition: vtkCellArray.h:180
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 LoadModelData(const std::vector< char > &glbBuffer)
Load buffer data into the internal Model.
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.
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 LoadFileBuffer(const std::string &fileName, std::vector< char > &glbBuffer)
Load the binary part of a binary glTF (.glb) file.
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.
topologically and geometrically regular array of data
Definition: vtkImageData.h:42
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:85
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:55
dynamic, self-adjusting array of unsigned short
@ 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< vtkTransform > GlobalTransform
vtkSmartPointer< vtkMatrix4x4 > Matrix
std::vector< float > InitialRotation
vtkSmartPointer< vtkTransform > Transform
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.