VTK  9.5.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 <string> // For std::string
38#include <vector> // For std::vector
39
40VTK_ABI_NAMESPACE_BEGIN
41class vtkCellArray;
42class vtkDataArray;
43class vtkFloatArray;
44class vtkImageData;
45class vtkMatrix4x4;
46class vtkPoints;
47class vtkPolyData;
49
50class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
51{
52public:
55 void PrintSelf(ostream& os, vtkIndent indent) override;
56
60 enum class Target : unsigned short
61 {
62 ARRAY_BUFFER = 34962,
64 };
65
70 enum class AccessorType : unsigned char
71 {
72 SCALAR,
73 VEC2,
74 VEC3,
75 VEC4,
76 MAT2,
77 MAT3,
78 MAT4,
79 INVALID
80 };
81
86 enum class ComponentType : unsigned short
87 {
88 BYTE = 5120,
89 UNSIGNED_BYTE = 5121,
90 SHORT = 5122,
91 UNSIGNED_SHORT = 5123,
92 UNSIGNED_INT = 5125,
93 FLOAT = 5126
94 };
95
96 /* The following structs help deserialize a glTF document, representing each object. As such,
97 * their members mostly match with the specification. Default values and boundaries are set
98 * according to the specification.
99 * Most of these structs contain a name property, which is optional, and, while being loaded, is
100 * not currently exploited by the loader.
101 * They are mostly root-level properties, and once created, are stored into vectors in the Model
102 * structure.
103 */
104
110 {
116 std::string Name;
117 };
118
153
161 {
162 // accessor indices from the .gltf file, the map's keys correspond to attribute names
163 std::map<std::string, int> AttributeIndices;
164 // attribute values
165 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
166 };
167
176 {
177 // accessor indices from the .glTF file, the map's keys correspond to attribute names
178 std::map<std::string, int> AttributeIndices;
181
182 // attribute values from buffer data
183 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
184
186
187 std::vector<MorphTarget> Targets;
188
190 int Mode;
191 int CellSize; // 1, 2 or 3, depending on draw mode
192
193 // Primitive-specific extension metadata
195 {
196 // KHR_draco_mesh_compression extension
197 // Only metadata are read (decoding and modifying the internal model is not done yet)
199 {
200 int BufferView = -1;
201 std::map<std::string, int> AttributeIndices;
202 };
204 };
206 };
207
214 struct Node
215 {
216 std::vector<int> Children;
218 int Mesh;
219 int Skin;
220
223
225
227
228 std::vector<float> InitialRotation;
229 std::vector<float> InitialTranslation;
230 std::vector<float> InitialScale;
231 std::vector<float> InitialWeights;
232 std::vector<float> Rotation;
233 std::vector<float> Translation;
234 std::vector<float> Scale;
235 std::vector<float> Weights;
236
237 // Object-specific extension metadata
239 {
240 // KHR_lights_punctual extension
242 {
243 int Light = -1;
244 };
246 };
248
249 std::string Name;
250
252 };
253
258 struct Mesh
259 {
260 std::vector<struct Primitive> Primitives;
261 std::vector<float> Weights;
262 std::string Name;
263 };
264
271 {
272 int Index = -1;
273 int TexCoord = -1;
274 };
275
280 struct Image
281 {
283 std::string MimeType;
284 std::string Uri;
285
287
288 std::string Name;
289 };
290
335
340 struct Texture
341 {
344 std::string Name;
345 };
346
351 struct Sampler : public GLTFSampler
352 {
353 std::string Name;
354 };
355
361 struct Scene
362 {
363 std::vector<unsigned int> Nodes;
364 std::string Name;
365 };
366
372 struct Skin
373 {
374 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
375 std::vector<int> Joints;
378 std::string Name;
380 };
381
389 {
390 struct Sampler
391 {
392 enum class InterpolationMode : unsigned char
393 {
394 LINEAR,
395 STEP,
396 CUBICSPLINE
397 };
399 unsigned int Input;
400 unsigned int Output;
402
405
409 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
410 bool forceStep = false, bool isRotation = false) const;
411 };
412
413 struct Channel
414 {
415 enum class PathType : unsigned char
416 {
417 ROTATION,
418 TRANSLATION,
419 SCALE,
420 WEIGHTS
421 };
425 };
426
427 float Duration; // In seconds
428 std::vector<Animation::Channel> Channels;
429 std::vector<Animation::Sampler> Samplers;
430 std::string Name;
431 };
432
438 struct Camera
439 {
440 // common properties
441 double Znear;
442 double Zfar;
443 bool IsPerspective; // if not, camera mode is orthographic
444 // perspective
445 double Xmag;
446 double Ymag;
447 // orthographic
448 double Yfov;
450 std::string Name;
451 };
452
460 {
461 // KHR_lights_punctual extension
463 {
464 struct Light
465 {
466 enum class LightType : unsigned char
467 {
468 DIRECTIONAL,
470 SPOT
471 };
473
474 std::vector<double> Color;
475 double Intensity;
476 double Range;
477
478 // Type-specific parameters
481
482 std::string Name;
483 };
484 std::vector<Light> Lights;
485 };
487 };
488
492 struct Model
493 {
494 std::vector<Accessor> Accessors;
495 std::vector<Animation> Animations;
496 std::vector<std::vector<char>> Buffers;
497 std::vector<BufferView> BufferViews;
498 std::vector<Camera> Cameras;
499 std::vector<Image> Images;
500 std::vector<Material> Materials;
501 std::vector<Mesh> Meshes;
502 std::vector<Node> Nodes;
503 std::vector<Sampler> Samplers;
504 std::vector<Scene> Scenes;
505 std::vector<Skin> Skins;
506 std::vector<Texture> Textures;
507
509
510 std::string BufferMetaData;
512 std::string FileName;
515 };
516
521 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
522
526 void ResetAnimation(int animationId);
527
529
534 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
535 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
537
539
547 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
550
554 bool LoadModelData(const std::vector<char>& glbBuffer);
555
560
564 std::shared_ptr<Model> GetInternalModel();
565
570
574 virtual std::vector<std::string> GetSupportedExtensions();
575
579 const std::vector<std::string>& GetUsedExtensions();
580
587 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
588
593
597 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
598 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
599
606 virtual void PrepareData() {}
607
609
614 vtkSetMacro(GLBStart, vtkTypeInt64);
615 vtkGetMacro(GLBStart, vtkTypeInt64);
617
619
624 vtkSetMacro(LoadAnimation, bool);
625 vtkGetMacro(LoadAnimation, bool);
626 vtkBooleanMacro(LoadAnimation, bool);
628
630
635 vtkSetMacro(LoadImages, bool);
636 vtkGetMacro(LoadImages, bool);
637 vtkBooleanMacro(LoadImages, bool);
639
641
646 vtkSetMacro(LoadSkinMatrix, bool);
647 vtkGetMacro(LoadSkinMatrix, bool);
648 vtkBooleanMacro(LoadSkinMatrix, bool);
650
651protected:
653 ~vtkGLTFDocumentLoader() override = default;
654
655private:
656 struct AccessorLoadingWorker;
657
658 struct SparseAccessorLoadingWorker;
659
660 template <typename Type>
661 struct BufferDataExtractionWorker;
662
664 void operator=(const vtkGLTFDocumentLoader&) = delete;
665
669 bool LoadSkinMatrixData();
670
675 bool ExtractPrimitiveAttributes(Primitive& primitive);
676
683 bool ExtractPrimitiveAccessorData(Primitive& primitive);
684
689 bool BuildPolyDataFromPrimitive(Primitive& primitive);
690
694 bool BuildPolyDataFromSkin(Skin& skin);
695
699 bool LoadAnimationData();
700
704 bool LoadImageData();
705
706 std::shared_ptr<Model> InternalModel;
707
708 static const std::vector<std::string> SupportedExtensions;
709 std::vector<std::string> UsedExtensions;
710 vtkTypeInt64 GLBStart = 0;
711
715 bool LoadAnimation = true;
716 bool LoadImages = true;
717 bool LoadSkinMatrix = true;
718};
719
720VTK_ABI_NAMESPACE_END
721#endif
object to represent cell connectivity
dynamic, self-adjusting array of float
AccessorType
Defines an accessor's type.
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 LoadModelMetaDataFromFile(VTK_FILEPATH const std::string &FileName)
Reset internal Model struct, and serialize glTF metadata (all json information) into it.
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.
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