VTK  9.3.0
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
28#ifndef vtkGLTFDocumentLoader_h
29#define vtkGLTFDocumentLoader_h
30
31#include "vtkIOGeometryModule.h" // For export macro
32#include "vtkObject.h"
33#include "vtkResourceStream.h" // For "vtkResourceStream"
34#include "vtkSmartPointer.h" // For "vtkSmartPointer"
35#include "vtkURILoader.h" // For "vtkURILoader"
36
37#include <map> // For std::map
38#include <memory> // For std::shared_ptr
39#include <string> // For std::string
40#include <vector> // For std::vector
41
42VTK_ABI_NAMESPACE_BEGIN
43class vtkCellArray;
44class vtkDataArray;
45class vtkFloatArray;
46class vtkImageData;
47class vtkMatrix4x4;
48class vtkPoints;
49class vtkPolyData;
51
52class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
53{
54public:
57 void PrintSelf(ostream& os, vtkIndent indent) override;
58
62 enum class Target : unsigned short
63 {
64 ARRAY_BUFFER = 34962,
66 };
67
72 enum class AccessorType : unsigned char
73 {
74 SCALAR,
75 VEC2,
76 VEC3,
77 VEC4,
78 MAT2,
79 MAT3,
80 MAT4,
81 INVALID
82 };
83
88 enum class ComponentType : unsigned short
89 {
90 BYTE = 5120,
91 UNSIGNED_BYTE = 5121,
92 SHORT = 5122,
93 UNSIGNED_SHORT = 5123,
94 UNSIGNED_INT = 5125,
95 FLOAT = 5126
96 };
97
98 /* The following structs help deserialize a glTF document, representing each object. As such,
99 * their members mostly match with the specification. Default values and boundaries are set
100 * according to the specification.
101 * Most of these structs contain a name property, which is optional, and, while being loaded, is
102 * not currently exploited by the loader.
103 * They are mostly root-level properties, and once created, are stored into vectors in the Model
104 * structure.
105 */
106
112 {
118 std::string Name;
119 };
120
155
163 {
164 // accessor indices from the .gltf file, the map's keys correspond to attribute names
165 std::map<std::string, int> AttributeIndices;
166 // attribute values
167 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
168 };
169
178 {
179 // accessor indices from the .glTF file, the map's keys correspond to attribute names
180 std::map<std::string, int> AttributeIndices;
183
184 // attribute values from buffer data
185 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
186
188
189 std::vector<MorphTarget> Targets;
190
192 int Mode;
193 int CellSize; // 1, 2 or 3, depending on draw mode
194 };
195
202 struct Node
203 {
204 std::vector<int> Children;
206 int Mesh;
207 int Skin;
208
211
213
215
216 std::vector<float> InitialRotation;
217 std::vector<float> InitialTranslation;
218 std::vector<float> InitialScale;
219 std::vector<float> InitialWeights;
220 std::vector<float> Rotation;
221 std::vector<float> Translation;
222 std::vector<float> Scale;
223 std::vector<float> Weights;
224
225 // Object-specific extension metadata
227 {
228 // KHR_lights_punctual extension
230 {
231 int Light = -1;
232 };
234 };
236
237 std::string Name;
238
240 };
241
246 struct Mesh
247 {
248 std::vector<struct Primitive> Primitives;
249 std::vector<float> Weights;
250 std::string Name;
251 };
252
259 {
260 int Index = -1;
262 };
263
268 struct Image
269 {
271 std::string MimeType;
272 std::string Uri;
273
275
276 std::string Name;
277 };
278
285 struct Material
286 {
287 enum class AlphaModeType : unsigned char
288 {
289 OPAQUE,
290 MASK,
291 BLEND
292 };
293
303
305
311 std::vector<double> EmissiveFactor;
312
315
317
318 std::string Name;
319
320 // extension KHR_materials_unlit
321 bool Unlit;
322 };
323
328 struct Texture
329 {
332 std::string Name;
333 };
334
339 struct Sampler
340 {
341 enum FilterType : unsigned short
342 {
343 NEAREST = 9728,
344 LINEAR = 9729,
345 NEAREST_MIPMAP_NEAREST = 9984,
346 LINEAR_MIPMAP_NEAREST = 9985,
347 NEAREST_MIPMAP_LINEAR = 9986,
348 LINEAR_MIPMAP_LINEAR = 9987
349 };
350 enum WrapType : unsigned short
351 {
352 CLAMP_TO_EDGE = 33071,
353 MIRRORED_REPEAT = 33648,
354 REPEAT = 10497
355 };
360 std::string Name;
361 };
362
368 struct Scene
369 {
370 std::vector<unsigned int> Nodes;
371 std::string Name;
372 };
373
379 struct Skin
380 {
381 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
382 std::vector<int> Joints;
385 std::string Name;
386 };
387
395 {
396 struct Sampler
397 {
398 enum class InterpolationMode : unsigned char
399 {
400 LINEAR,
401 STEP,
402 CUBICSPLINE
403 };
405 unsigned int Input;
406 unsigned int Output;
408
411
415 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
416 bool forceStep = false, bool isRotation = false) const;
417 };
418
419 struct Channel
420 {
421 enum class PathType : unsigned char
422 {
423 ROTATION,
424 TRANSLATION,
425 SCALE,
426 WEIGHTS
427 };
431 };
432
433 float Duration; // In seconds
434 std::vector<Animation::Channel> Channels;
435 std::vector<Animation::Sampler> Samplers;
436 std::string Name;
437 };
438
444 struct Camera
445 {
446 // common properties
447 double Znear;
448 double Zfar;
449 bool IsPerspective; // if not, camera mode is orthographic
450 // perspective
451 double Xmag;
452 double Ymag;
453 // orthographic
454 double Yfov;
456 std::string Name;
457 };
458
466 {
467 // KHR_lights_punctual extension
469 {
470 struct Light
471 {
472 enum class LightType : unsigned char
473 {
474 DIRECTIONAL,
475 POINT,
476 SPOT
477 };
479
480 std::vector<double> Color;
481 double Intensity;
482 double Range;
483
484 // Type-specific parameters
487
488 std::string Name;
489 };
490 std::vector<Light> Lights;
491 };
493 };
494
498 struct Model
499 {
500 std::vector<Accessor> Accessors;
501 std::vector<Animation> Animations;
502 std::vector<std::vector<char>> Buffers;
503 std::vector<BufferView> BufferViews;
504 std::vector<Camera> Cameras;
505 std::vector<Image> Images;
506 std::vector<Material> Materials;
507 std::vector<Mesh> Meshes;
508 std::vector<Node> Nodes;
509 std::vector<Sampler> Samplers;
510 std::vector<Scene> Scenes;
511 std::vector<Skin> Skins;
512 std::vector<Texture> Textures;
513
515
516 std::string BufferMetaData;
518 std::string FileName;
521 };
522
527 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
528
532 void ResetAnimation(int animationId);
533
535
540 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
541 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
543
545
553 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
556
560 bool LoadModelData(const std::vector<char>& glbBuffer);
561
566
570 std::shared_ptr<Model> GetInternalModel();
571
576
580 const std::vector<std::string>& GetSupportedExtensions();
581
585 const std::vector<std::string>& GetUsedExtensions();
586
593 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
594
599
603 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
604 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
605
606protected:
608 ~vtkGLTFDocumentLoader() override = default;
609
610private:
611 struct AccessorLoadingWorker;
612
613 struct SparseAccessorLoadingWorker;
614
615 template <typename Type>
616 struct BufferDataExtractionWorker;
617
619 void operator=(const vtkGLTFDocumentLoader&) = delete;
620
624 bool LoadSkinMatrixData();
625
630 bool ExtractPrimitiveAttributes(Primitive& primitive);
631
638 bool ExtractPrimitiveAccessorData(Primitive& primitive);
639
644 bool BuildPolyDataFromPrimitive(Primitive& primitive);
645
649 bool LoadAnimationData();
650
654 bool LoadImageData();
655
656 std::shared_ptr<Model> InternalModel;
657
658 static const std::vector<std::string> SupportedExtensions;
659 std::vector<std::string> UsedExtensions;
660};
661
662VTK_ABI_NAMESPACE_END
663#endif
object to represent cell connectivity
abstract superclass for arrays of numeric data
dynamic, self-adjusting array of float
Deserialize a GLTF model file.
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.
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...
const std::vector< std::string > & GetSupportedExtensions()
Get the list of extensions that are supported by this loader.
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.
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
abstract base class for most VTK objects
Definition vtkObject.h:52
represent and manipulate 3D points
Definition vtkPoints.h:29
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition vtkPolyData.h:80
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 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
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
This struct describes a glTF textureInfo object, mostly used in material descriptions They contain tw...
This struct describes a glTF texture object.
#define ARRAY_BUFFER
#define ELEMENT_ARRAY_BUFFER
#define VTK_FILEPATH