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 
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;
57 
58 class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
59 {
60 public:
63  void PrintSelf(ostream& os, vtkIndent indent) override;
64 
68  enum class Target : unsigned short
69  {
70  ARRAY_BUFFER = 34962,
71  ELEMENT_ARRAY_BUFFER = 34963
72  };
73 
78  enum class AccessorType : unsigned char
79  {
80  SCALAR,
81  VEC2,
82  VEC3,
83  VEC4,
84  MAT2,
85  MAT3,
86  MAT4,
87  INVALID
88  };
89 
94  enum class ComponentType : unsigned short
95  {
96  BYTE = 5120,
97  UNSIGNED_BYTE = 5121,
98  SHORT = 5122,
99  UNSIGNED_SHORT = 5123,
100  UNSIGNED_INT = 5125,
101  FLOAT = 5126
102  };
103 
104  /* The following structs help deserialize a glTF document, representing each object. As such,
105  * their members mostly match with the specification. Default values and boundaries are set
106  * according to the specification.
107  * Most of these structs contain a name property, which is optional, and, while being loaded, is
108  * not currently exploited by the loader.
109  * They are mostly root-level properties, and once created, are stored into vectors in the Model
110  * structure.
111  */
112 
117  struct BufferView
118  {
119  int Buffer;
123  int Target;
125  };
126 
133  struct Accessor
134  {
139  struct Sparse
140  {
141  int Count;
147  };
152  int Count;
153  unsigned int NumberOfComponents;
155  std::vector<double> Max;
156  std::vector<double> Min;
157  bool IsSparse;
160  };
161 
168  struct MorphTarget
169  {
170  // accessor indices from the .gltf file, the map's keys correspond to attribute names
171  std::map<std::string, int> AttributeIndices;
172  // attribute values
173  std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
174  };
175 
183  struct Primitive
184  {
185  // accessor indices from the .glTF file, the map's keys correspond to attribute names
186  std::map<std::string, int> AttributeIndices;
189 
190  // attribute values from buffer data
191  std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
192 
194 
195  std::vector<MorphTarget> Targets;
196 
197  int Material;
198  int Mode;
199  int CellSize; // 1, 2 or 3, depending on draw mode
200  };
201 
208  struct Node
209  {
210  std::vector<int> Children;
211  int Camera;
212  int Mesh;
213  int Skin;
214 
217 
218  bool TRSLoaded;
219 
221 
222  std::vector<float> InitialRotation;
223  std::vector<float> InitialTranslation;
224  std::vector<float> InitialScale;
225  std::vector<float> InitialWeights;
226  std::vector<float> Rotation;
227  std::vector<float> Translation;
228  std::vector<float> Scale;
229  std::vector<float> Weights;
230 
231  // Object-specific extension metadata
232  struct Extensions
233  {
234  // KHR_lights_punctual extension
236  {
237  int Light = -1;
238  };
240  };
242 
244 
246  };
247 
252  struct Mesh
253  {
254  std::vector<struct Primitive> Primitives;
255  std::vector<float> Weights;
257  };
258 
264  struct TextureInfo
265  {
266  int Index = -1;
267  int TexCoord;
268  };
269 
274  struct Image
275  {
279 
281 
283  };
284 
291  struct Material
292  {
293  enum class AlphaModeType : unsigned char
294  {
295  OPAQUE,
296  MASK,
297  BLEND
298  };
299 
301  {
303  std::vector<double> BaseColorFactor;
304 
308  };
309 
311 
317  std::vector<double> EmissiveFactor;
318 
320  double AlphaCutoff;
321 
323 
325 
326  // extension KHR_materials_unlit
327  bool Unlit;
328  };
329 
334  struct Texture
335  {
336  int Sampler;
337  int Source;
339  };
340 
345  struct Sampler
346  {
347  enum FilterType : unsigned short
348  {
349  NEAREST = 9728,
350  LINEAR = 9729,
351  NEAREST_MIPMAP_NEAREST = 9984,
352  LINEAR_MIPMAP_NEAREST = 9985,
353  NEAREST_MIPMAP_LINEAR = 9986,
354  LINEAR_MIPMAP_LINEAR = 9987
355  };
356  enum WrapType : unsigned short
357  {
358  CLAMP_TO_EDGE = 33071,
359  MIRRORED_REPEAT = 33648,
360  REPEAT = 10497
361  };
367  };
368 
374  struct Scene
375  {
376  std::vector<unsigned int> Nodes;
378  };
379 
385  struct Skin
386  {
387  std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
388  std::vector<int> Joints;
390  int Skeleton;
392  };
393 
400  struct Animation
401  {
402  struct Sampler
403  {
404  enum class InterpolationMode : unsigned char
405  {
406  LINEAR,
407  STEP,
408  CUBICSPLINE
409  };
411  unsigned int Input;
412  unsigned int Output;
414 
417 
421  void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
422  bool forceStep = false, bool isRotation = false) const;
423  };
424 
425  struct Channel
426  {
427  enum class PathType : unsigned char
428  {
429  ROTATION,
430  TRANSLATION,
431  SCALE,
432  WEIGHTS
433  };
434  int Sampler;
437  };
438 
439  float Duration; // In seconds
440  std::vector<Animation::Channel> Channels;
441  std::vector<Animation::Sampler> Samplers;
443  };
444 
450  struct Camera
451  {
452  // common properties
453  double Znear;
454  double Zfar;
455  bool IsPerspective; // if not, camera mode is orthographic
456  // perspective
457  double Xmag;
458  double Ymag;
459  // orthographic
460  double Yfov;
461  double AspectRatio;
463  };
464 
471  struct Extensions
472  {
473  // KHR_lights_punctual extension
475  {
476  struct Light
477  {
478  enum class LightType : unsigned char
479  {
480  DIRECTIONAL,
481  POINT,
482  SPOT
483  };
485 
486  std::vector<double> Color;
487  double Intensity;
488  double Range;
489 
490  // Type-specific parameters
493 
495  };
496  std::vector<Light> Lights;
497  };
499  };
500 
504  struct Model
505  {
506  std::vector<Accessor> Accessors;
507  std::vector<Animation> Animations;
508  std::vector<std::vector<char>> Buffers;
509  std::vector<BufferView> BufferViews;
510  std::vector<Camera> Cameras;
511  std::vector<Image> Images;
512  std::vector<Material> Materials;
513  std::vector<Mesh> Meshes;
514  std::vector<Node> Nodes;
515  std::vector<Sampler> Samplers;
516  std::vector<Scene> Scenes;
517  std::vector<Skin> Skins;
518  std::vector<Texture> Textures;
519 
521 
525  };
526 
531  bool ApplyAnimation(float t, int animationId, bool forceStep = false);
532 
536  void ResetAnimation(int animationId);
537 
542  bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
543 
549 
553  bool LoadModelData(const std::vector<char>& glbBuffer);
554 
559 
563  std::shared_ptr<Model> GetInternalModel();
564 
569 
573  const std::vector<std::string>& GetSupportedExtensions();
574 
578  const std::vector<std::string>& GetUsedExtensions();
579 
586  void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
587 
592 
596  static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
598 
599 protected:
601  ~vtkGLTFDocumentLoader() override = default;
602 
603 private:
604  struct AccessorLoadingWorker;
605 
606  struct SparseAccessorLoadingWorker;
607 
608  template <typename Type>
609  struct BufferDataExtractionWorker;
610 
612  void operator=(const vtkGLTFDocumentLoader&) = delete;
613 
617  bool LoadSkinMatrixData();
618 
623  bool ExtractPrimitiveAttributes(Primitive& primitive);
624 
631  bool ExtractPrimitiveAccessorData(Primitive& primitive);
632 
637  bool BuildPolyDataFromPrimitive(Primitive& primitive);
638 
642  bool LoadAnimationData();
643 
647  bool LoadImageData();
648 
649  std::shared_ptr<Model> InternalModel;
650 
651  static const std::vector<std::string> SupportedExtensions;
652  std::vector<std::string> UsedExtensions;
653 };
654 
655 #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:54
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