VTK  9.6.2
vtkGLTFDocumentLoader.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
24
25#ifndef vtkGLTFDocumentLoader_h
26#define vtkGLTFDocumentLoader_h
27
28#include "GLTFSampler.h" // For "Sampler"
29#include "vtkIOGeometryModule.h" // For export macro
30#include "vtkObject.h"
31#include "vtkResourceStream.h" // For "vtkResourceStream"
32#include "vtkSmartPointer.h" // For "vtkSmartPointer"
33#include "vtkURILoader.h" // For "vtkURILoader"
34
35#include <map> // For std::map
36#include <memory> // For std::shared_ptr
37#include <set> // For std::set
38#include <string> // For std::string
39#include <vector> // For std::vector
40
41VTK_ABI_NAMESPACE_BEGIN
42class vtkCellArray;
43class vtkDataArray;
44class vtkFloatArray;
45class vtkImageData;
46class vtkMatrix4x4;
47class vtkPoints;
48class vtkPolyData;
50
51class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
52{
53public:
56 void PrintSelf(ostream& os, vtkIndent indent) override;
57
61 enum class Target : unsigned short
62 {
63 ARRAY_BUFFER = 34962,
65 };
66
71 enum class AccessorType : unsigned char
72 {
73 SCALAR,
74 VEC2,
75 VEC3,
76 VEC4,
77 MAT2,
78 MAT3,
79 MAT4,
80 INVALID
81 };
82
87 enum class ComponentType : unsigned short
88 {
89 BYTE = 5120,
90 UNSIGNED_BYTE = 5121,
91 SHORT = 5122,
92 UNSIGNED_SHORT = 5123,
93 UNSIGNED_INT = 5125,
94 FLOAT = 5126
95 };
96
97 /* The following structs help deserialize a glTF document, representing each object. As such,
98 * their members mostly match with the specification. Default values and boundaries are set
99 * according to the specification.
100 * Most of these structs contain a name property, which is optional, and, while being loaded, is
101 * not currently exploited by the loader.
102 * They are mostly root-level properties, and once created, are stored into vectors in the Model
103 * structure.
104 */
105
111 {
117 std::string Name;
118 };
119
154
162 {
163 // accessor indices from the .gltf file, the map's keys correspond to attribute names
164 std::map<std::string, int> AttributeIndices;
165 // attribute values
166 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
167 };
168
177 {
178 // accessor indices from the .glTF file, the map's keys correspond to attribute names
179 std::map<std::string, int> AttributeIndices;
182
183 // attribute values from buffer data
184 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
185
187
188 std::vector<MorphTarget> Targets;
189
191 int Mode;
192 int CellSize; // 1, 2 or 3, depending on draw mode
193
194 // Primitive-specific extension metadata
196 {
197 // KHR_draco_mesh_compression extension
198 // Only metadata are read (decoding and modifying the internal model is not done yet)
200 {
201 int BufferView = -1;
202 std::map<std::string, int> AttributeIndices;
203 };
205 };
207 };
208
215 struct Node
216 {
217 std::vector<int> Children;
219 int Mesh;
220 int Skin;
221
224
226
228
229 std::vector<float> InitialRotation;
230 std::vector<float> InitialTranslation;
231 std::vector<float> InitialScale;
232 std::vector<float> InitialWeights;
233 std::vector<float> Rotation;
234 std::vector<float> Translation;
235 std::vector<float> Scale;
236 std::vector<float> Weights;
237
238 // Object-specific extension metadata
240 {
241 // KHR_lights_punctual extension
243 {
244 int Light = -1;
245 };
247 };
249
250 std::string Name;
251
253 };
254
259 struct Mesh
260 {
261 std::vector<struct Primitive> Primitives;
262 std::vector<float> Weights;
263 std::string Name;
264 };
265
272 {
273 int Index = -1;
274 int TexCoord = -1;
275 std::vector<double> Offset;
276 std::vector<double> Scale;
277 double Rotation = 0.0;
278 };
279
284 struct Image
285 {
287 std::string MimeType;
288 std::string Uri;
289
291
292 std::string Name;
293 };
294
339
344 struct Texture
345 {
348 std::string Name;
349 };
350
355 struct Sampler : public GLTFSampler
356 {
357 std::string Name;
358 };
359
365 struct Scene
366 {
367 std::vector<unsigned int> Nodes;
368 std::string Name;
369 };
370
376 struct Skin
377 {
378 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
379 std::vector<int> Joints;
382 std::string Name;
384 };
385
393 {
394 struct Sampler
395 {
396 enum class InterpolationMode : unsigned char
397 {
398 LINEAR,
399 STEP,
400 CUBICSPLINE
401 };
403 unsigned int Input;
404 unsigned int Output;
406
409
413 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
414 bool forceStep = false, bool isRotation = false) const;
415 };
416
417 struct Channel
418 {
419 enum class PathType : unsigned char
420 {
421 ROTATION,
422 TRANSLATION,
423 SCALE,
424 WEIGHTS
425 };
429 };
430
431 float Duration; // In seconds
432 std::set<float> AllTimestamps; // In seconds
433 std::vector<Animation::Channel> Channels;
434 std::vector<Animation::Sampler> Samplers;
435 std::string Name;
436 };
437
443 struct Camera
444 {
445 // common properties
446 double Znear;
447 double Zfar;
448 bool IsPerspective; // if not, camera mode is orthographic
449 // perspective
450 double Xmag;
451 double Ymag;
452 // orthographic
453 double Yfov;
455 std::string Name;
456 };
457
465 {
466 // KHR_lights_punctual extension
468 {
469 struct Light
470 {
471 enum class LightType : unsigned char
472 {
473 DIRECTIONAL,
475 SPOT
476 };
478
479 std::vector<double> Color;
480 double Intensity;
481 double Range;
482
483 // Type-specific parameters
486
487 std::string Name;
488 };
489 std::vector<Light> Lights;
490 };
492 };
493
497 struct Model
498 {
499 std::vector<Accessor> Accessors;
500 std::vector<Animation> Animations;
501 std::vector<std::vector<char>> Buffers;
502 std::vector<BufferView> BufferViews;
503 std::vector<Camera> Cameras;
504 std::vector<Image> Images;
505 std::vector<Material> Materials;
506 std::vector<Mesh> Meshes;
507 std::vector<Node> Nodes;
508 std::vector<Sampler> Samplers;
509 std::vector<Scene> Scenes;
510 std::vector<Skin> Skins;
511 std::vector<Texture> Textures;
512
514
515 std::string BufferMetaData;
517 std::string FileName;
520 };
521
526 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
527
531 void ResetAnimation(int animationId);
532
534
539 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
540 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
542
544
552 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
555
559 bool LoadModelData(const std::vector<char>& glbBuffer);
560
565
569 std::shared_ptr<Model> GetInternalModel();
570
575
579 virtual std::vector<std::string> GetSupportedExtensions();
580
584 const std::vector<std::string>& GetUsedExtensions();
585
592 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
593
598
602 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
603 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
604
611 virtual void PrepareData() {}
612
614
619 vtkSetMacro(GLBStart, vtkTypeInt64);
620 vtkGetMacro(GLBStart, vtkTypeInt64);
622
624
629 vtkSetMacro(LoadAnimation, bool);
630 vtkGetMacro(LoadAnimation, bool);
631 vtkBooleanMacro(LoadAnimation, bool);
633
635
640 vtkSetMacro(LoadImages, bool);
641 vtkGetMacro(LoadImages, bool);
642 vtkBooleanMacro(LoadImages, bool);
644
646
651 vtkSetMacro(LoadSkinMatrix, bool);
652 vtkGetMacro(LoadSkinMatrix, bool);
653 vtkBooleanMacro(LoadSkinMatrix, bool);
655
656protected:
658 ~vtkGLTFDocumentLoader() override = default;
659
660private:
661 struct AccessorLoadingWorker;
662
663 struct SparseAccessorLoadingWorker;
664
665 template <typename Type>
666 struct BufferDataExtractionWorker;
667
669 void operator=(const vtkGLTFDocumentLoader&) = delete;
670
674 bool LoadSkinMatrixData();
675
680 bool ExtractPrimitiveAttributes(Primitive& primitive);
681
688 bool ExtractPrimitiveAccessorData(Primitive& primitive);
689
694 bool BuildPolyDataFromPrimitive(Primitive& primitive);
695
699 bool BuildPolyDataFromSkin(Skin& skin);
700
704 bool LoadAnimationData();
705
709 bool LoadImageData();
710
711 std::shared_ptr<Model> InternalModel;
712
713 static const std::vector<std::string> SupportedExtensions;
714 std::vector<std::string> UsedExtensions;
715 vtkTypeInt64 GLBStart = 0;
716
720 bool LoadAnimation = true;
721 bool LoadImages = true;
722 bool LoadSkinMatrix = true;
723};
724
725VTK_ABI_NAMESPACE_END
726#endif
object to represent cell connectivity
dynamic, self-adjusting array of float
AccessorType
Defines an accessor's type.
bool LoadModelMetaDataFromFile(const std::string &FileName)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
void ResetAnimation(int animationId)
Restore the transforms that were modified by an animation to their initial state.
bool LoadModelMetaDataFromStream(vtkResourceStream *stream, vtkURILoader *loader=nullptr)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
virtual std::vector< std::string > GetSupportedExtensions()
Get the list of extensions that are supported by this loader.
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.
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.
virtual void PrepareData()
Some extensions require a preparation on the model before building VTK objects.
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.
static vtkGLTFDocumentLoader * New()
bool ApplyAnimation(float t, int animationId, bool forceStep=false)
Apply the specified animation, at the specified time value t, to the internal Model.
const std::vector< std::string > & GetUsedExtensions()
Get the list of extensions that are used by the current model.
~vtkGLTFDocumentLoader() override=default
vtkGLTFDocumentLoader()=default
bool LoadStreamBuffer(vtkResourceStream *stream, std::vector< char > &glbBuffer)
Load the binary part of a binary glTF (.glb) file.
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.
std::shared_ptr< Model > GetInternalModel()
Get the internal Model.
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
a simple class to control print indentation
Definition vtkIndent.h:29
represent and manipulate 4x4 transformation matrices
represent and manipulate 3D points
Definition vtkPoints.h:30
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition vtkPolyData.h:72
Abstract class used for custom streams.
Hold a reference to a vtkObjectBase instance.
Helper class for readers and importer that need to load more than one resource.
dynamic, self-adjusting array of unsigned short
This struct describes a glTF sampler object.
Definition GLTFSampler.h:16
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
vtkSmartPointer< vtkResourceStream > Stream
std::vector< Animation > Animations
std::vector< Material > Materials
std::vector< Accessor > Accessors
vtkSmartPointer< vtkURILoader > URILoader
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 > InitialTranslation
Primitive::Extensions::KHRDracoMeshCompression KHRDracoMetaData
This struct describes a glTF primitive object.
vtkSmartPointer< vtkCellArray > Indices
std::map< std::string, int > AttributeIndices
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
vtkSmartPointer< vtkPolyData > Armature
This struct describes a glTF textureInfo object, mostly used in material descriptions They contain tw...
This struct describes a glTF texture object.
#define vtkDataArray
#define ARRAY_BUFFER
#define ELEMENT_ARRAY_BUFFER
@ POINT
The selection data provided is point-data.
#define VTK_FILEPATH