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
25#ifndef vtkGLTFDocumentLoader_h
26#define vtkGLTFDocumentLoader_h
27
28#include "vtkIOGeometryModule.h" // For export macro
29#include "vtkObject.h"
30#include "vtkResourceStream.h" // For "vtkResourceStream"
31#include "vtkSmartPointer.h" // For "vtkSmartPointer"
32#include "vtkURILoader.h" // For "vtkURILoader"
33
34#include <map> // For std::map
35#include <memory> // For std::shared_ptr
36#include <string> // For std::string
37#include <vector> // For std::vector
38
39VTK_ABI_NAMESPACE_BEGIN
40class vtkCellArray;
41class vtkDataArray;
42class vtkFloatArray;
43class vtkImageData;
44class vtkMatrix4x4;
45class vtkPoints;
46class vtkPolyData;
48
49class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
50{
51public:
54 void PrintSelf(ostream& os, vtkIndent indent) override;
55
59 enum class Target : unsigned short
60 {
61 ARRAY_BUFFER = 34962,
63 };
64
69 enum class AccessorType : unsigned char
70 {
71 SCALAR,
72 VEC2,
73 VEC3,
74 VEC4,
75 MAT2,
76 MAT3,
77 MAT4,
78 INVALID
79 };
80
85 enum class ComponentType : unsigned short
86 {
87 BYTE = 5120,
88 UNSIGNED_BYTE = 5121,
89 SHORT = 5122,
90 UNSIGNED_SHORT = 5123,
91 UNSIGNED_INT = 5125,
92 FLOAT = 5126
93 };
94
95 /* The following structs help deserialize a glTF document, representing each object. As such,
96 * their members mostly match with the specification. Default values and boundaries are set
97 * according to the specification.
98 * Most of these structs contain a name property, which is optional, and, while being loaded, is
99 * not currently exploited by the loader.
100 * They are mostly root-level properties, and once created, are stored into vectors in the Model
101 * structure.
102 */
103
109 {
115 std::string Name;
116 };
117
124 struct Accessor
125 {
130 struct Sparse
131 {
132 int Count;
138 };
143 int Count;
144 unsigned int NumberOfComponents;
146 std::vector<double> Max;
147 std::vector<double> Min;
150 std::string Name;
151 };
152
160 {
161 // accessor indices from the .gltf file, the map's keys correspond to attribute names
162 std::map<std::string, int> AttributeIndices;
163 // attribute values
164 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
165 };
166
175 {
176 // accessor indices from the .glTF file, the map's keys correspond to attribute names
177 std::map<std::string, int> AttributeIndices;
180
181 // attribute values from buffer data
182 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
183
185
186 std::vector<MorphTarget> Targets;
187
189 int Mode;
190 int CellSize; // 1, 2 or 3, depending on draw mode
191 };
192
199 struct Node
200 {
201 std::vector<int> Children;
203 int Mesh;
204 int Skin;
205
208
210
212
213 std::vector<float> InitialRotation;
214 std::vector<float> InitialTranslation;
215 std::vector<float> InitialScale;
216 std::vector<float> InitialWeights;
217 std::vector<float> Rotation;
218 std::vector<float> Translation;
219 std::vector<float> Scale;
220 std::vector<float> Weights;
221
222 // Object-specific extension metadata
224 {
225 // KHR_lights_punctual extension
227 {
228 int Light = -1;
229 };
231 };
233
234 std::string Name;
235
237 };
238
243 struct Mesh
244 {
245 std::vector<struct Primitive> Primitives;
246 std::vector<float> Weights;
247 std::string Name;
248 };
249
256 {
257 int Index = -1;
259 };
260
265 struct Image
266 {
268 std::string MimeType;
269 std::string Uri;
270
272
273 std::string Name;
274 };
275
282 struct Material
283 {
284 enum class AlphaModeType : unsigned char
285 {
286 OPAQUE,
287 MASK,
288 BLEND
289 };
290
292 {
294 std::vector<double> BaseColorFactor;
295
299 };
300
302
308 std::vector<double> EmissiveFactor;
309
312
314
315 std::string Name;
316
317 // extension KHR_materials_unlit
318 bool Unlit;
319 };
320
325 struct Texture
326 {
329 std::string Name;
330 };
331
336 struct Sampler
337 {
338 enum FilterType : unsigned short
339 {
340 NEAREST = 9728,
341 LINEAR = 9729,
342 NEAREST_MIPMAP_NEAREST = 9984,
343 LINEAR_MIPMAP_NEAREST = 9985,
344 NEAREST_MIPMAP_LINEAR = 9986,
345 LINEAR_MIPMAP_LINEAR = 9987
346 };
347 enum WrapType : unsigned short
348 {
349 CLAMP_TO_EDGE = 33071,
350 MIRRORED_REPEAT = 33648,
351 REPEAT = 10497
352 };
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;
383 };
384
392 {
393 struct Sampler
394 {
395 enum class InterpolationMode : unsigned char
396 {
397 LINEAR,
398 STEP,
399 CUBICSPLINE
400 };
402 unsigned int Input;
403 unsigned int Output;
405
408
412 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
413 bool forceStep = false, bool isRotation = false) const;
414 };
415
416 struct Channel
417 {
418 enum class PathType : unsigned char
419 {
420 ROTATION,
421 TRANSLATION,
422 SCALE,
423 WEIGHTS
424 };
428 };
429
430 float Duration; // In seconds
431 std::vector<Animation::Channel> Channels;
432 std::vector<Animation::Sampler> Samplers;
433 std::string Name;
434 };
435
441 struct Camera
442 {
443 // common properties
444 double Znear;
445 double Zfar;
446 bool IsPerspective; // if not, camera mode is orthographic
447 // perspective
448 double Xmag;
449 double Ymag;
450 // orthographic
451 double Yfov;
453 std::string Name;
454 };
455
463 {
464 // KHR_lights_punctual extension
466 {
467 struct Light
468 {
469 enum class LightType : unsigned char
470 {
471 DIRECTIONAL,
472 POINT,
473 SPOT
474 };
476
477 std::vector<double> Color;
478 double Intensity;
479 double Range;
480
481 // Type-specific parameters
484
485 std::string Name;
486 };
487 std::vector<Light> Lights;
488 };
490 };
491
495 struct Model
496 {
497 std::vector<Accessor> Accessors;
498 std::vector<Animation> Animations;
499 std::vector<std::vector<char>> Buffers;
500 std::vector<BufferView> BufferViews;
501 std::vector<Camera> Cameras;
502 std::vector<Image> Images;
503 std::vector<Material> Materials;
504 std::vector<Mesh> Meshes;
505 std::vector<Node> Nodes;
506 std::vector<Sampler> Samplers;
507 std::vector<Scene> Scenes;
508 std::vector<Skin> Skins;
509 std::vector<Texture> Textures;
510
512
513 std::string BufferMetaData;
515 std::string FileName;
518 };
519
524 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
525
529 void ResetAnimation(int animationId);
530
532
537 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
538 bool LoadStreamBuffer(vtkResourceStream* stream, std::vector<char>& glbBuffer);
540
542
550 bool LoadModelMetaDataFromFile(VTK_FILEPATH const std::string& FileName);
553
557 bool LoadModelData(const std::vector<char>& glbBuffer);
558
563
567 std::shared_ptr<Model> GetInternalModel();
568
573
577 const std::vector<std::string>& GetSupportedExtensions();
578
582 const std::vector<std::string>& GetUsedExtensions();
583
590 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
591
596
600 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
601 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
602
603protected:
605 ~vtkGLTFDocumentLoader() override = default;
606
607private:
608 struct AccessorLoadingWorker;
609
610 struct SparseAccessorLoadingWorker;
611
612 template <typename Type>
613 struct BufferDataExtractionWorker;
614
616 void operator=(const vtkGLTFDocumentLoader&) = delete;
617
621 bool LoadSkinMatrixData();
622
627 bool ExtractPrimitiveAttributes(Primitive& primitive);
628
635 bool ExtractPrimitiveAccessorData(Primitive& primitive);
636
641 bool BuildPolyDataFromPrimitive(Primitive& primitive);
642
646 bool LoadAnimationData();
647
651 bool LoadImageData();
652
653 std::shared_ptr<Model> InternalModel;
654
655 static const std::vector<std::string> SupportedExtensions;
656 std::vector<std::string> UsedExtensions;
657};
658
659VTK_ABI_NAMESPACE_END
660#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:49
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