import/gltf.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/import/gltf.h" | ||
| 3 | |||
| 4 | #include <memory> | ||
| 5 | #include <string> | ||
| 6 | |||
| 7 | #include <cgltf.h> | ||
| 8 | |||
| 9 | #include "chrono_batch_render/util/error.h" | ||
| 10 | #include "chrono_batch_render/util/log.h" | ||
| 11 | |||
| 12 | namespace chrono::batch_render { | ||
| 13 | namespace { | ||
| 14 | |||
| 15 | using GltfData = std::unique_ptr<cgltf_data, decltype(&cgltf_free)>; | ||
| 16 | |||
| 17 | ✗ | GltfData Parse(const ImportSource& source) { | |
| 18 | ✗ | const FileBytes bytes = source.ReadEntry(); | |
| 19 | ✗ | const cgltf_options options{}; | |
| 20 | ✗ | cgltf_data* data = nullptr; | |
| 21 | ✗ | if (const cgltf_result result = cgltf_parse(&options, bytes->data(), bytes->size(), &data); | |
| 22 | result != cgltf_result_success) { | ||
| 23 | ✗ | throw Error("Reading glTF \"{}\": cgltf couldn't parse it (result {})", | |
| 24 | ✗ | source.file.string(), int(result)); | |
| 25 | } | ||
| 26 | ✗ | return {data, cgltf_free}; | |
| 27 | ✗ | } | |
| 28 | |||
| 29 | void LoadBuffers(const cgltf_data&, const ImportSource&) {} | ||
| 30 | |||
| 31 | void DefineMaterials(ImportPlan&, std::string_view, const cgltf_data&) {} | ||
| 32 | |||
| 33 | void DefineMeshes(ImportPlan&, std::string_view, const cgltf_data&) {} | ||
| 34 | |||
| 35 | void DefineNodes(ImportPlan&, std::string_view, const cgltf_data&) {} | ||
| 36 | |||
| 37 | void DefineCameras(ImportPlan&, std::string_view, const cgltf_data&) {} | ||
| 38 | |||
| 39 | void DefineLights(ImportPlan&, std::string_view, const cgltf_data&) {} | ||
| 40 | |||
| 41 | 3 | class GltfImporter final : public Importer { | |
| 42 | public: | ||
| 43 | ✗ | std::string Name() const override { | |
| 44 | ✗ | return "glTF"; | |
| 45 | } | ||
| 46 | |||
| 47 | 3 | std::vector<std::string> Extensions() const override { | |
| 48 | 3 | return {".gltf", ".glb"}; | |
| 49 | } | ||
| 50 | |||
| 51 | ✗ | ImportPlan Read(std::string_view path, const ImportSource& source, | |
| 52 | ImportContext& context) override { | ||
| 53 | ✗ | context.Report("parsing", 0, 1); | |
| 54 | ✗ | const GltfData data = Parse(source); | |
| 55 | ✗ | LoadBuffers(*data, source); | |
| 56 | ✗ | context.Report("parsing", 1, 1); | |
| 57 | |||
| 58 | ✗ | ImportPlan plan; | |
| 59 | ✗ | plan.Define<Xform>(std::string(path)); | |
| 60 | ✗ | DefineMaterials(plan, path, *data); | |
| 61 | ✗ | DefineMeshes(plan, path, *data); | |
| 62 | ✗ | DefineNodes(plan, path, *data); | |
| 63 | ✗ | DefineCameras(plan, path, *data); | |
| 64 | ✗ | DefineLights(plan, path, *data); | |
| 65 | ✗ | Log(LogLevel::Warning, | |
| 66 | "the glTF importer is a placeholder, so \"{}\" imports as an empty Xform", | ||
| 67 | ✗ | source.file.string()); | |
| 68 | ✗ | return plan; | |
| 69 | ✗ | } | |
| 70 | }; | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | 3 | std::shared_ptr<Importer> CreateGltfImporter() { | |
| 75 | 3 | return std::make_shared<GltfImporter>(); | |
| 76 | } | ||
| 77 | |||
| 78 | } | ||
| 79 |