----------------------------------------------------------------------------
| 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])); |
nothing calls this directly
no test coverage detected