glb_tests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <fstream>
  2. #include <catch2/catch_test_macros.hpp>
  3. #include <fastgltf/parser.hpp>
  4. #include <fastgltf/types.hpp>
  5. #include "gltf_path.hpp"
  6. TEST_CASE("Load basic GLB file", "[gltf-loader]") {
  7. fastgltf::Parser parser;
  8. auto folder = sampleModels / "2.0" / "Box" / "glTF-Binary";
  9. fastgltf::GltfDataBuffer jsonData;
  10. REQUIRE(jsonData.loadFromFile(folder / "Box.glb"));
  11. SECTION("Load basic Box.glb") {
  12. auto asset = parser.loadBinaryGLTF(&jsonData, folder, fastgltf::Options::None, fastgltf::Category::Buffers);
  13. REQUIRE(asset.error() == fastgltf::Error::None);
  14. REQUIRE(fastgltf::validate(asset.get()) == fastgltf::Error::None);
  15. REQUIRE(asset->buffers.size() == 1);
  16. auto& buffer = asset->buffers.front();
  17. auto* bufferView = std::get_if<fastgltf::sources::ByteView>(&buffer.data);
  18. REQUIRE(bufferView != nullptr);
  19. auto jsonSpan = fastgltf::span<std::byte>(jsonData);
  20. REQUIRE(bufferView->bytes.data() - jsonSpan.data() == 1016);
  21. REQUIRE(jsonSpan.size() == 1664);
  22. }
  23. SECTION("Load basic Box.glb and load buffers") {
  24. auto asset = parser.loadBinaryGLTF(&jsonData, folder, fastgltf::Options::LoadGLBBuffers, fastgltf::Category::Buffers);
  25. REQUIRE(asset.error() == fastgltf::Error::None);
  26. REQUIRE(fastgltf::validate(asset.get()) == fastgltf::Error::None);
  27. REQUIRE(asset->buffers.size() == 1);
  28. auto& buffer = asset->buffers.front();
  29. auto* bufferVector = std::get_if<fastgltf::sources::Vector>(&buffer.data);
  30. REQUIRE(bufferVector != nullptr);
  31. REQUIRE(!bufferVector->bytes.empty());
  32. REQUIRE(static_cast<uint64_t>(bufferVector->bytes.size() - buffer.byteLength) < 3);
  33. }
  34. SECTION("Load GLB by bytes") {
  35. std::ifstream file(folder / "Box.glb", std::ios::binary | std::ios::ate);
  36. auto length = static_cast<size_t>(file.tellg());
  37. file.seekg(0, std::ifstream::beg);
  38. std::vector<uint8_t> bytes(length + fastgltf::getGltfBufferPadding());
  39. file.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(length));
  40. fastgltf::GltfDataBuffer byteBuffer;
  41. REQUIRE(byteBuffer.fromByteView(bytes.data(), length, length + fastgltf::getGltfBufferPadding()));
  42. auto asset = parser.loadBinaryGLTF(&byteBuffer, folder, fastgltf::Options::LoadGLBBuffers, fastgltf::Category::Buffers);
  43. REQUIRE(asset.error() == fastgltf::Error::None);
  44. }
  45. }