diff --git a/CMakeLists.txt b/CMakeLists.txt index 975fd3237..3e4b5ecf6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ option(FASTGLTF_ENABLE_TESTS "Enables test targets for fastlgtf" OFF) option(FASTGLTF_ENABLE_EXAMPLES "Enables example targets for fastgltf" OFF) option(FASTGLTF_ENABLE_DOCS "Enables the configuration of targets that build/generate documentation" OFF) option(FASTGLTF_ENABLE_GLTF_RS "Enables the benchmark usage of gltf-rs" OFF) +option(FASTGLTF_ENABLE_ASSIMP "Enables the benchmark usage of assimp" OFF) option(FASTGLTF_ENABLE_DEPRECATED_EXT "Enables support for deprecated extensions" OFF) option(FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL "Disables the memory allocation algorithm based on polymorphic resources" OFF) option(FASTGLTF_USE_64BIT_FLOAT "Default to 64-bit double precision floats for everything" OFF) diff --git a/include/fastgltf/tools.hpp b/include/fastgltf/tools.hpp index f556a8f23..c4598eda5 100644 --- a/include/fastgltf/tools.hpp +++ b/include/fastgltf/tools.hpp @@ -123,10 +123,8 @@ concept Element = std::is_arithmetic_v::comp namespace internal { -template -constexpr DestType convertComponent(const std::byte* bytes, bool normalized) { - auto source = reinterpret_cast(bytes)[Index]; - +template +constexpr DestType convertComponent(const SourceType& source, bool normalized) { if (normalized) { if constexpr (std::is_floating_point_v && std::is_integral_v) { // float -> int conversion @@ -150,6 +148,11 @@ constexpr DestType convertComponent(const std::byte* bytes, bool normalized) { return static_cast(source); } +template +constexpr DestType convertComponent(const std::byte* bytes, bool normalized) { + return convertComponent(reinterpret_cast(bytes)[Index], normalized); +} + template #if FASTGLTF_HAS_CONCEPTS requires Element diff --git a/src/fastgltf.cpp b/src/fastgltf.cpp index 7ed873538..6823dbe4f 100644 --- a/src/fastgltf.cpp +++ b/src/fastgltf.cpp @@ -734,7 +734,7 @@ fg::Error fg::Parser::generateMeshIndices(fastgltf::Asset& asset) const { fastgltf::span indices { reinterpret_cast(generatedIndices.bytes.data()), generatedIndices.bytes.size() / sizeof(std::uint32_t) }; for (std::size_t i = 0; i < positionAccessor.count; ++i) { - indices[i] = i; + indices[i] = static_cast(i); } auto bufferIdx = asset.buffers.size(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 09086a3d5..f1113d517 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,7 +48,7 @@ if (FASTGLTF_ENABLE_GLTF_RS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf-rs/src/ target_compile_definitions(fastgltf_tests PRIVATE HAS_GLTFRS=1) endif() -if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf_loaders/assimp") +if(FASTGLTF_ENABLE_ASSIMP AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf_loaders/assimp") message(STATUS "fastgltf: Found assimp") # Only enable glTF importer set(ASSIMP_NO_EXPORT ON CACHE BOOL "") diff --git a/tests/accessor_tests.cpp b/tests/accessor_tests.cpp index c8c2b0b0f..5dfe67f39 100644 --- a/tests/accessor_tests.cpp +++ b/tests/accessor_tests.cpp @@ -1,6 +1,8 @@ #include #include +#include +#include #include #include @@ -25,6 +27,30 @@ static const std::byte* getBufferData(const fastgltf::Buffer& buffer) { return result; } +TEST_CASE("Test data type conversion", "[gltf-tools]") { + // normalized int-to-float and normalized float-to-int + for (auto i = std::numeric_limits::min(); i < std::numeric_limits::max(); ++i) { + auto converted = fastgltf::internal::convertComponent(i, true); + REQUIRE(glm::epsilonEqual(converted, fastgltf::max(i / 127.0f, -1), glm::epsilon())); + REQUIRE(fastgltf::internal::convertComponent(converted, true) == std::round(converted * 127.0f)); + } + for (auto i = std::numeric_limits::min(); i < std::numeric_limits::max(); ++i) { + auto converted = fastgltf::internal::convertComponent(i, true); + REQUIRE(glm::epsilonEqual(converted, i / 255.0f, glm::epsilon())); + REQUIRE(fastgltf::internal::convertComponent(converted, true) == std::round(converted * 255.0f)); + } + for (auto i = std::numeric_limits::min(); i < std::numeric_limits::max(); ++i) { + auto converted = fastgltf::internal::convertComponent(i, true); + REQUIRE(glm::epsilonEqual(converted, fastgltf::max(i / 32767.0f, -1), glm::epsilon())); + REQUIRE(fastgltf::internal::convertComponent(converted, true) == std::round(converted * 32767.0f)); + } + for (auto i = std::numeric_limits::min(); i < std::numeric_limits::max(); ++i) { + auto converted = fastgltf::internal::convertComponent(i, true); + REQUIRE(glm::epsilonEqual(converted, i / 65535.0f, glm::epsilon())); + REQUIRE(fastgltf::internal::convertComponent(converted, true) == std::round(converted * 65535.0f)); + } +} + TEST_CASE("Test accessor", "[gltf-tools]") { auto lightsLamp = sampleModels / "2.0" / "LightsPunctualLamp" / "glTF"; fastgltf::GltfDataBuffer jsonData;