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
40#ifndef vtkGLTFDocumentLoader_h
41#define vtkGLTFDocumentLoader_h
42
43#include "vtkIOGeometryModule.h" // For export macro
44#include "vtkObject.h"
45#include "vtkSmartPointer.h" // For SmartPointer
46
47#include <map> // For std::map
48#include <memory> // For std::shared_ptr
49#include <string> // For std::string
50#include <vector> // For std::vector
51
52class vtkCellArray;
53class vtkDataArray;
54class vtkFloatArray;
55class vtkImageData;
56class vtkMatrix4x4;
57class vtkPoints;
58class vtkPolyData;
60
61class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
62{
63public:
66 void PrintSelf(ostream& os, vtkIndent indent) override;
67
71 enum class Target : unsigned short
72 {
73 ARRAY_BUFFER = 34962,
74 ELEMENT_ARRAY_BUFFER = 34963
75 };
76
81 enum class AccessorType : unsigned char
82 {
83 SCALAR,
84 VEC2,
85 VEC3,
86 VEC4,
87 MAT2,
88 MAT3,
89 MAT4,
90 INVALID
91 };
92
97 enum class ComponentType : unsigned short
98 {
99 BYTE = 5120,
100 UNSIGNED_BYTE = 5121,
101 SHORT = 5122,
102 UNSIGNED_SHORT = 5123,
103 UNSIGNED_INT = 5125,
104 FLOAT = 5126
105 };
106
107 /* The following structs help deserialize a glTF document, representing each object. As such,
108 * their members mostly match with the specification. Default values and boundaries are set
109 * according to the specification.
110 * Most of these structs contain a name property, which is optional, and, while being loaded, is
111 * not currently exploited by the loader.
112 * They are mostly root-level properties, and once created, are stored into vectors in the Model
113 * structure.
114 */
115
121 {
127 std::string Name;
128 };
129
136 struct Accessor
137 {
142 struct Sparse
143 {
144 int Count;
150 };
155 int Count;
156 unsigned int NumberOfComponents;
158 std::vector<double> Max;
159 std::vector<double> Min;
162 std::string Name;
163 };
164
172 {
173 // accessor indices from the .gltf file, the map's keys correspond to attribute names
174 std::map<std::string, int> AttributeIndices;
175 // attribute values
176 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
177 };
178
187 {
188 // accessor indices from the .glTF file, the map's keys correspond to attribute names
189 std::map<std::string, int> AttributeIndices;
192
193 // attribute values from buffer data
194 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
195
197
198 std::vector<MorphTarget> Targets;
199
201 int Mode;
202 int CellSize; // 1, 2 or 3, depending on draw mode
203 };
204
211 struct Node
212 {
213 std::vector<int> Children;
215 int Mesh;
216 int Skin;
217
220
222
224
225 std::vector<float> InitialRotation;
226 std::vector<float> InitialTranslation;
227 std::vector<float> InitialScale;
228 std::vector<float> InitialWeights;
229 std::vector<float> Rotation;
230 std::vector<float> Translation;
231 std::vector<float> Scale;
232 std::vector<float> Weights;
233
234 // Object-specific extension metadata
236 {
237 // KHR_lights_punctual extension
239 {
240 int Light = -1;
241 };
243 };
245
246 std::string Name;
247
249 };
250
255 struct Mesh
256 {
257 std::vector<struct Primitive> Primitives;
258 std::vector<float> Weights;
259 std::string Name;
260 };
261
268 {
269 int Index = -1;
271 };
272
277 struct Image
278 {
280 std::string MimeType;
281 std::string Uri;
282
284
285 std::string Name;
286 };
287
294 struct Material
295 {
296 enum class AlphaModeType : unsigned char
297 {
298 OPAQUE,
299 MASK,
300 BLEND
301 };
302
304 {
306 std::vector<double> BaseColorFactor;
307
311 };
312
314
320 std::vector<double> EmissiveFactor;
321
324
326
327 std::string Name;
328
329 // extension KHR_materials_unlit
330 bool Unlit;
331 };
332
337 struct Texture
338 {
341 std::string Name;
342 };
343
348 struct Sampler
349 {
350 enum FilterType : unsigned short
351 {
352 NEAREST = 9728,
353 LINEAR = 9729,
354 NEAREST_MIPMAP_NEAREST = 9984,
355 LINEAR_MIPMAP_NEAREST = 9985,
356 NEAREST_MIPMAP_LINEAR = 9986,
357 LINEAR_MIPMAP_LINEAR = 9987
358 };
359 enum WrapType : unsigned short
360 {
361 CLAMP_TO_EDGE = 33071,
362 MIRRORED_REPEAT = 33648,
363 REPEAT = 10497
364 };
369 std::string Name;
370 };
371
377 struct Scene
378 {
379 std::vector<unsigned int> Nodes;
380 std::string Name;
381 };
382
388 struct Skin
389 {
390 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
391 std::vector<int> Joints;
394 std::string Name;
395 };
396
404 {
405 struct Sampler
406 {
407 enum class InterpolationMode : unsigned char
408 {
409 LINEAR,
410 STEP,
411 CUBICSPLINE
412 };
414 unsigned int Input;
415 unsigned int Output;
417
420
424 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
425 bool forceStep = false, bool isRotation = false) const;
426 };
427
428 struct Channel
429 {
430 enum class PathType : unsigned char
431 {
432 ROTATION,
433 TRANSLATION,
434 SCALE,
435 WEIGHTS
436 };
440 };
441
442 float Duration; // In seconds
443 std::vector<Animation::Channel> Channels;
444 std::vector<Animation::Sampler> Samplers;
445 std::string Name;
446 };
447
453 struct Camera
454 {
455 // common properties
456 double Znear;
457 double Zfar;
458 bool IsPerspective; // if not, camera mode is orthographic
459 // perspective
460 double Xmag;
461 double Ymag;
462 // orthographic
463 double Yfov;
465 std::string Name;
466 };
467
475 {
476 // KHR_lights_punctual extension
478 {
479 struct Light
480 {
481 enum class LightType : unsigned char
482 {
483 DIRECTIONAL,
484 POINT,
485 SPOT
486 };
488
489 std::vector<double> Color;
490 double Intensity;
491 double Range;
492
493 // Type-specific parameters
496
497 std::string Name;
498 };
499 std::vector<Light> Lights;
500 };
502 };
503
507 struct Model
508 {
509 std::vector<Accessor> Accessors;
510 std::vector<Animation> Animations;
511 std::vector<std::vector<char>> Buffers;
512 std::vector<BufferView> BufferViews;
513 std::vector<Camera> Cameras;
514 std::vector<Image> Images;
515 std::vector<Material> Materials;
516 std::vector<Mesh> Meshes;
517 std::vector<Node> Nodes;
518 std::vector<Sampler> Samplers;
519 std::vector<Scene> Scenes;
520 std::vector<Skin> Skins;
521 std::vector<Texture> Textures;
522
524
525 std::string BufferMetaData;
527 std::string FileName;
528 };
529
534 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
535
539 void ResetAnimation(int animationId);
540
545 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
546
551 bool LoadModelMetaDataFromFile(std::string FileName);
552
556 bool LoadModelData(const std::vector<char>& glbBuffer);
557
562
566 std::shared_ptr<Model> GetInternalModel();
567
572
576 const std::vector<std::string>& GetSupportedExtensions();
577
581 const std::vector<std::string>& GetUsedExtensions();
582
589 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
590
595
599 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
600 std::vector<vtkSmartPointer<vtkMatrix4x4>>& jointMats);
601
602protected:
604 ~vtkGLTFDocumentLoader() override = default;
605
606private:
607 struct AccessorLoadingWorker;
608
609 struct SparseAccessorLoadingWorker;
610
611 template <typename Type>
612 struct BufferDataExtractionWorker;
613
615 void operator=(const vtkGLTFDocumentLoader&) = delete;
616
620 bool LoadSkinMatrixData();
621
626 bool ExtractPrimitiveAttributes(Primitive& primitive);
627
634 bool ExtractPrimitiveAccessorData(Primitive& primitive);
635
640 bool BuildPolyDataFromPrimitive(Primitive& primitive);
641
645 bool LoadAnimationData();
646
650 bool LoadImageData();
651
652 std::shared_ptr<Model> InternalModel;
653
654 static const std::vector<std::string> SupportedExtensions;
655 std::vector<std::string> UsedExtensions;
656};
657
658#endif
object to represent cell connectivity
Definition: vtkCellArray.h:290
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:159
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 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, 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
ComponentType
Define a type for different data components.
std::shared_ptr< Model > GetInternalModel()
Get the internal Model.
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:157
a simple class to control print indentation
Definition: vtkIndent.h:113
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:145
abstract base class for most VTK objects
Definition: vtkObject.h:82
represent and manipulate 3D points
Definition: vtkPoints.h:143
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:195
Hold a reference to a vtkObjectBase instance.
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
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