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
37#ifndef vtkGLTFDocumentLoader_h
38#define vtkGLTFDocumentLoader_h
39
40#include "vtkIOGeometryModule.h" // For export macro
41#include "vtkObject.h"
42#include "vtkSmartPointer.h" // For SmartPointer
43
44#include <map> // For std::map
45#include <memory> // For std::shared_ptr
46#include <string> // For std::string
47#include <vector> // For std::vector
48
49class vtkCellArray;
50class vtkDataArray;
51class vtkFloatArray;
52class vtkImageData;
53class vtkMatrix4x4;
54class vtkPoints;
55class vtkPolyData;
57
58class VTKIOGEOMETRY_EXPORT vtkGLTFDocumentLoader : public vtkObject
59{
60public:
63 void PrintSelf(ostream& os, vtkIndent indent) override;
64
68 enum class Target : unsigned short
69 {
70 ARRAY_BUFFER = 34962,
71 ELEMENT_ARRAY_BUFFER = 34963
72 };
73
78 enum class AccessorType : unsigned char
79 {
80 SCALAR,
81 VEC2,
82 VEC3,
83 VEC4,
84 MAT2,
85 MAT3,
86 MAT4,
87 INVALID
88 };
89
94 enum class ComponentType : unsigned short
95 {
96 BYTE = 5120,
97 UNSIGNED_BYTE = 5121,
98 SHORT = 5122,
99 UNSIGNED_SHORT = 5123,
100 UNSIGNED_INT = 5125,
101 FLOAT = 5126
102 };
103
104 /* The following structs help deserialize a glTF document, representing each object. As such,
105 * their members mostly match with the specification. Default values and boundaries are set
106 * according to the specification.
107 * Most of these structs contain a name property, which is optional, and, while being loaded, is
108 * not currently exploited by the loader.
109 * They are mostly root-level properties, and once created, are stored into vectors in the Model
110 * structure.
111 */
112
118 {
125 };
126
133 struct Accessor
134 {
139 struct Sparse
140 {
141 int Count;
147 };
152 int Count;
153 unsigned int NumberOfComponents;
155 std::vector<double> Max;
156 std::vector<double> Min;
160 };
161
169 {
170 // accessor indices from the .gltf file, the map's keys correspond to attribute names
171 std::map<std::string, int> AttributeIndices;
172 // attribute values
173 std::map<std::string, vtkSmartPointer<vtkFloatArray>> AttributeValues;
174 };
175
184 {
185 // accessor indices from the .glTF file, the map's keys correspond to attribute names
186 std::map<std::string, int> AttributeIndices;
189
190 // attribute values from buffer data
191 std::map<std::string, vtkSmartPointer<vtkDataArray>> AttributeValues;
192
194
195 std::vector<MorphTarget> Targets;
196
198 int Mode;
199 int CellSize; // 1, 2 or 3, depending on draw mode
200 };
201
208 struct Node
209 {
210 std::vector<int> Children;
212 int Mesh;
213 int Skin;
214
217
219
221
222 std::vector<float> InitialRotation;
223 std::vector<float> InitialTranslation;
224 std::vector<float> InitialScale;
225 std::vector<float> InitialWeights;
226 std::vector<float> Rotation;
227 std::vector<float> Translation;
228 std::vector<float> Scale;
229 std::vector<float> Weights;
230
231 // Object-specific extension metadata
233 {
234 // KHR_lights_punctual extension
236 {
237 int Light = -1;
238 };
240 };
242
244
246 };
247
252 struct Mesh
253 {
254 std::vector<struct Primitive> Primitives;
255 std::vector<float> Weights;
257 };
258
265 {
266 int Index = -1;
268 };
269
274 struct Image
275 {
279
281
283 };
284
291 struct Material
292 {
293 enum class AlphaModeType : unsigned char
294 {
295 OPAQUE,
296 MASK,
297 BLEND
298 };
299
301 {
303 std::vector<double> BaseColorFactor;
304
308 };
309
311
317 std::vector<double> EmissiveFactor;
318
321
323
325
326 // extension KHR_materials_unlit
327 bool Unlit;
328 };
329
334 struct Texture
335 {
339 };
340
345 struct Sampler
346 {
347 enum FilterType : unsigned short
348 {
349 NEAREST = 9728,
350 LINEAR = 9729,
351 NEAREST_MIPMAP_NEAREST = 9984,
352 LINEAR_MIPMAP_NEAREST = 9985,
353 NEAREST_MIPMAP_LINEAR = 9986,
354 LINEAR_MIPMAP_LINEAR = 9987
355 };
356 enum WrapType : unsigned short
357 {
358 CLAMP_TO_EDGE = 33071,
359 MIRRORED_REPEAT = 33648,
360 REPEAT = 10497
361 };
367 };
368
374 struct Scene
375 {
376 std::vector<unsigned int> Nodes;
378 };
379
385 struct Skin
386 {
387 std::vector<vtkSmartPointer<vtkMatrix4x4>> InverseBindMatrices;
388 std::vector<int> Joints;
392 };
393
401 {
402 struct Sampler
403 {
404 enum class InterpolationMode : unsigned char
405 {
406 LINEAR,
407 STEP,
408 CUBICSPLINE
409 };
411 unsigned int Input;
412 unsigned int Output;
414
417
421 void GetInterpolatedData(float t, size_t numberOfComponents, std::vector<float>* output,
422 bool forceStep = false, bool isRotation = false) const;
423 };
424
425 struct Channel
426 {
427 enum class PathType : unsigned char
428 {
429 ROTATION,
430 TRANSLATION,
431 SCALE,
432 WEIGHTS
433 };
437 };
438
439 float Duration; // In seconds
440 std::vector<Animation::Channel> Channels;
441 std::vector<Animation::Sampler> Samplers;
443 };
444
450 struct Camera
451 {
452 // common properties
453 double Znear;
454 double Zfar;
455 bool IsPerspective; // if not, camera mode is orthographic
456 // perspective
457 double Xmag;
458 double Ymag;
459 // orthographic
460 double Yfov;
463 };
464
472 {
473 // KHR_lights_punctual extension
475 {
476 struct Light
477 {
478 enum class LightType : unsigned char
479 {
480 DIRECTIONAL,
481 POINT,
482 SPOT
483 };
485
486 std::vector<double> Color;
487 double Intensity;
488 double Range;
489
490 // Type-specific parameters
493
495 };
496 std::vector<Light> Lights;
497 };
499 };
500
504 struct Model
505 {
506 std::vector<Accessor> Accessors;
507 std::vector<Animation> Animations;
508 std::vector<std::vector<char>> Buffers;
509 std::vector<BufferView> BufferViews;
510 std::vector<Camera> Cameras;
511 std::vector<Image> Images;
512 std::vector<Material> Materials;
513 std::vector<Mesh> Meshes;
514 std::vector<Node> Nodes;
515 std::vector<Sampler> Samplers;
516 std::vector<Scene> Scenes;
517 std::vector<Skin> Skins;
518 std::vector<Texture> Textures;
519
521
525 };
526
531 bool ApplyAnimation(float t, int animationId, bool forceStep = false);
532
536 void ResetAnimation(int animationId);
537
542 bool LoadFileBuffer(VTK_FILEPATH const std::string& fileName, std::vector<char>& glbBuffer);
543
549
553 bool LoadModelData(const std::vector<char>& glbBuffer);
554
559
563 std::shared_ptr<Model> GetInternalModel();
564
569
573 const std::vector<std::string>& GetSupportedExtensions();
574
578 const std::vector<std::string>& GetUsedExtensions();
579
586 void BuildGlobalTransforms(unsigned int nodeIndex, vtkSmartPointer<vtkMatrix4x4> parentTransform);
587
592
596 static void ComputeJointMatrices(const Model& model, const Skin& skin, Node& node,
598
599protected:
601 ~vtkGLTFDocumentLoader() override = default;
602
603private:
604 struct AccessorLoadingWorker;
605
606 struct SparseAccessorLoadingWorker;
607
608 template <typename Type>
609 struct BufferDataExtractionWorker;
610
612 void operator=(const vtkGLTFDocumentLoader&) = delete;
613
617 bool LoadSkinMatrixData();
618
623 bool ExtractPrimitiveAttributes(Primitive& primitive);
624
631 bool ExtractPrimitiveAccessorData(Primitive& primitive);
632
637 bool BuildPolyDataFromPrimitive(Primitive& primitive);
638
642 bool LoadAnimationData();
643
647 bool LoadImageData();
648
649 std::shared_ptr<Model> InternalModel;
650
651 static const std::vector<std::string> SupportedExtensions;
652 std::vector<std::string> UsedExtensions;
653};
654
655#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:73
represent and manipulate 3D points
Definition: vtkPoints.h:143
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:195
dynamic, self-adjusting array of unsigned short
@ vector
Definition: vtkX3D.h:243
@ type
Definition: vtkX3D.h:522
@ string
Definition: vtkX3D.h:496
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