MCPcopy Create free account
hub / github.com/Kitware/VTK / TryLoadGLTFModel

Function TryLoadGLTFModel

Rendering/OpenXR/vtkOpenXRModel.cxx:42–216  ·  view source on GitHub ↗

----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

40
41 //----------------------------------------------------------------------------
42 bool TryLoadGLTFModel(const std::string& assetPath)
43 {
44 if (assetPath.empty())
45 {
46 vtkDebugWithObjectMacro(nullptr, << "No asset path for model");
47 return false;
48 }
49
50 vtkNew<vtkURILoader> uriLoader;
51 uriLoader->SetBaseDirectory(vtksys::SystemTools::GetFilenamePath(assetPath));
52 vtkNew<vtkFileResourceStream> fileStream;
53
54 if (!fileStream->Open(assetPath.c_str()))
55 {
56 vtkErrorWithObjectMacro(nullptr, << "Unable to open file: " << assetPath.c_str());
57 return false;
58 }
59
60 vtkNew<vtkGLTFReader> reader;
61 reader->SetStream(fileStream);
62 reader->SetURILoader(uriLoader);
63 reader->Update();
64
65 // GLTF reader produces multiblock dataset, and here we expect the first
66 // data object to be a polydata representing the model.
67 vtkMultiBlockDataSet* mbds = reader->GetOutput();
68 vtkCompositeDataIterator* iter = mbds->NewIterator();
69 iter->InitTraversal();
70 vtkPolyData* pd = vtkPolyData::SafeDownCast(iter->GetCurrentDataObject());
71 iter->Delete();
72
73 if (!pd)
74 {
75 vtkErrorWithObjectMacro(nullptr, "Unable to retrieve polydata from reader");
76 return false;
77 }
78
79 // Flip the texture coordinates
80 vtkNew<vtkTransformTextureCoords> texTransform;
81 texTransform->SetInputDataObject(pd);
82 texTransform->SetFlipR(true);
83 texTransform->Update();
84
85 vtkPolyData* polyData = vtkPolyData::SafeDownCast(texTransform->GetOutput());
86 vtkPointData* pointData = polyData->GetPointData();
87 vtkDataArray* tCoordsArray = pointData->GetTCoords();
88
89 int numPoints = polyData->GetNumberOfPoints();
90 double position[3];
91 double tcoord[2];
92
93 // Store the polydata position and tcoords in our vertex attributes buffer
94 for (int ptIdx = 0; ptIdx < numPoints; ++ptIdx)
95 {
96 polyData->GetPoint(ptIdx, position);
97 tCoordsArray->GetTuple(ptIdx, tcoord);
98 this->ModelVBOData.push_back(static_cast<float>(position[0]));
99 this->ModelVBOData.push_back(static_cast<float>(position[1]));

Callers

nothing calls this directly

Calls 15

SetBaseDirectoryMethod · 0.80
GetScalarIndexMethod · 0.80
DeleteMethod · 0.65
emptyMethod · 0.45
OpenMethod · 0.45
c_strMethod · 0.45
SetStreamMethod · 0.45
UpdateMethod · 0.45
GetOutputMethod · 0.45
NewIteratorMethod · 0.45
InitTraversalMethod · 0.45
GetCurrentDataObjectMethod · 0.45

Tested by

no test coverage detected