From d983e187ab7741b5f4ea26b6d9e987b0e6f716f9 Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 02:40:14 -0500 Subject: [PATCH 1/6] Added Unsigned Int to CreateAccessors() --- src/ObjLoading/XModel/Gltf/GltfLoader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ObjLoading/XModel/Gltf/GltfLoader.cpp b/src/ObjLoading/XModel/Gltf/GltfLoader.cpp index 42644a5a..3dd44c64 100644 --- a/src/ObjLoading/XModel/Gltf/GltfLoader.cpp +++ b/src/ObjLoading/XModel/Gltf/GltfLoader.cpp @@ -650,6 +650,8 @@ namespace m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_SHORT) m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); + else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_INT) + m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); else throw GltfLoadException(std::format("Accessor has unsupported component type {}", static_cast(jAccessor.componentType))); } From c527a864181a640d78714c4970a1585031e6406f Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 02:42:50 -0500 Subject: [PATCH 2/6] Added UnsignedIntAccessor class --- .../XModel/Gltf/Internal/GltfAccessor.h | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.h b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.h index 8395f037..fe0ee5c5 100644 --- a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.h +++ b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.h @@ -130,4 +130,26 @@ namespace gltf size_t m_byte_offset; size_t m_count; }; + + class UnsignedIntAccessor final : public Accessor + { + public: + UnsignedIntAccessor(const BufferView* bufferView, JsonAccessorType type, size_t byteOffset, size_t count); + + [[nodiscard]] std::optional GetType() const override; + [[nodiscard]] std::optional GetComponentType() const override; + [[nodiscard]] size_t GetCount() const override; + + [[nodiscard]] bool GetFloatVec2(size_t index, float (&out)[2]) const override; + [[nodiscard]] bool GetFloatVec3(size_t index, float (&out)[3]) const override; + [[nodiscard]] bool GetFloatVec4(size_t index, float (&out)[4]) const override; + [[nodiscard]] bool GetUnsigned(size_t index, unsigned& out) const override; + [[nodiscard]] bool GetUnsignedVec4(size_t index, unsigned (&out)[4]) const override; + + private: + const BufferView* m_buffer_view; + JsonAccessorType m_type; + size_t m_byte_offset; + size_t m_count; + }; } // namespace gltf From dfdf5d10223973fb1f1fd24d3f12e60dd417db46 Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 02:43:15 -0500 Subject: [PATCH 3/6] Added UnsignedIntAccessor class function implementations --- .../XModel/Gltf/Internal/GltfAccessor.cpp | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp index 8bf26355..0b939c60 100644 --- a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp +++ b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp @@ -435,3 +435,111 @@ bool UnsignedShortAccessor::GetUnsignedVec4(const size_t index, unsigned (&out)[ return true; } + +UnsignedIntAccessor::UnsignedIntAccessor(const BufferView* bufferView, const JsonAccessorType type, size_t byteOffset, const size_t count) + : m_buffer_view(bufferView), + m_type(type), + m_byte_offset(byteOffset), + m_count(count) +{ +} + +std::optional UnsignedIntAccessor::GetType() const +{ + return m_type; +} + +std::optional UnsignedIntAccessor::GetComponentType() const +{ + return JsonAccessorComponentType::UNSIGNED_INT; +} + +size_t UnsignedIntAccessor::GetCount() const +{ + return m_count; +} + +bool UnsignedIntAccessor::GetFloatVec2(const size_t index, float (&out)[2]) const +{ + assert(m_type == JsonAccessorType::VEC2 || m_type == JsonAccessorType::VEC3 || m_type == JsonAccessorType::VEC4); + if (index >= m_count) + return false; + + uint32_t temp[2]; + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint32_t[2]), m_byte_offset)) + return false; + + // Return as normalized value between 0 and 1 + out[0] = static_cast(temp[0]) / static_cast(std::numeric_limits::max()); + out[1] = static_cast(temp[1]) / static_cast(std::numeric_limits::max()); + + return true; +} + +bool UnsignedIntAccessor::GetFloatVec3(const size_t index, float (&out)[3]) const +{ + assert(m_type == JsonAccessorType::VEC3 || m_type == JsonAccessorType::VEC4); + if (index >= m_count) + return false; + + uint32_t temp[3]; + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint32_t[3]), m_byte_offset)) + return false; + + // Return as normalized value between 0 and 1 + out[0] = static_cast(temp[0]) / static_cast(std::numeric_limits::max()); + out[1] = static_cast(temp[1]) / static_cast(std::numeric_limits::max()); + out[2] = static_cast(temp[2]) / static_cast(std::numeric_limits::max()); + + return true; +} + +bool UnsignedIntAccessor::GetFloatVec4(const size_t index, float (&out)[4]) const +{ + assert(m_type == JsonAccessorType::VEC4); + if (index >= m_count) + return false; + + uint32_t temp[4]; + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint32_t[4]), m_byte_offset)) + return false; + + // Return as normalized value between 0 and 1 + out[0] = static_cast(temp[0]) / static_cast(std::numeric_limits::max()); + out[1] = static_cast(temp[1]) / static_cast(std::numeric_limits::max()); + out[2] = static_cast(temp[2]) / static_cast(std::numeric_limits::max()); + out[3] = static_cast(temp[3]) / static_cast(std::numeric_limits::max()); + + return true; +} + +bool UnsignedIntAccessor::GetUnsigned(const size_t index, unsigned& out) const +{ + if (index >= m_count) + return false; + + uint32_t temp; + if (!m_buffer_view->ReadElement(&temp, index, sizeof(uint32_t), m_byte_offset)) + return false; + + out = temp; + return true; +} + +bool UnsignedIntAccessor::GetUnsignedVec4(const size_t index, unsigned (&out)[4]) const +{ + assert(m_type == JsonAccessorType::VEC4); + if (index >= m_count) + return false; + + uint32_t temp[4]; + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint8_t[4]), m_byte_offset)) + return false; + + out[0] = static_cast(temp[0]); + out[1] = static_cast(temp[1]); + out[2] = static_cast(temp[2]); + out[3] = static_cast(temp[3]); + + return true; +} From 628c28f7af5ea42d1159bfaf748ef65a9160beb0 Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 02:52:28 -0500 Subject: [PATCH 4/6] Oops --- src/ObjLoading/XModel/Gltf/GltfLoader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ObjLoading/XModel/Gltf/GltfLoader.cpp b/src/ObjLoading/XModel/Gltf/GltfLoader.cpp index 3dd44c64..2fc49975 100644 --- a/src/ObjLoading/XModel/Gltf/GltfLoader.cpp +++ b/src/ObjLoading/XModel/Gltf/GltfLoader.cpp @@ -651,7 +651,7 @@ namespace else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_SHORT) m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_INT) - m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); + m_accessors.emplace_back(std::make_unique(bufferView, jAccessor.type, byteOffset, jAccessor.count)); else throw GltfLoadException(std::format("Accessor has unsupported component type {}", static_cast(jAccessor.componentType))); } From 6889e4d70f21e1a12d23218f53a1be3a4c477bd6 Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 03:15:14 -0500 Subject: [PATCH 5/6] Update src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp Co-authored-by: Jan --- src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp index 0b939c60..df8a34a1 100644 --- a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp +++ b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp @@ -533,7 +533,7 @@ bool UnsignedIntAccessor::GetUnsignedVec4(const size_t index, unsigned (&out)[4] return false; uint32_t temp[4]; - if (!m_buffer_view->ReadElement(temp, index, sizeof(uint8_t[4]), m_byte_offset)) + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint32_t[4]), m_byte_offset)) return false; out[0] = static_cast(temp[0]); From 0c2919d14d31f766e4b412fedbabcb95675ccbba Mon Sep 17 00:00:00 2001 From: Electro Games <2cool4u567@gmail.com> Date: Fri, 6 Sep 2024 03:20:39 -0500 Subject: [PATCH 6/6] Fixed ReadElement() using incorrect elementSize --- src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp index df8a34a1..3f8164f7 100644 --- a/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp +++ b/src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp @@ -425,7 +425,7 @@ bool UnsignedShortAccessor::GetUnsignedVec4(const size_t index, unsigned (&out)[ return false; uint16_t temp[4]; - if (!m_buffer_view->ReadElement(temp, index, sizeof(uint8_t[4]), m_byte_offset)) + if (!m_buffer_view->ReadElement(temp, index, sizeof(uint16_t[4]), m_byte_offset)) return false; out[0] = static_cast(temp[0]);