Added support for unsigned int accessors (#245)

* Added Unsigned Int to CreateAccessors()

* Added UnsignedIntAccessor class

* Added UnsignedIntAccessor class function implementations

* Oops

* Update src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp

Co-authored-by: Jan <Laupetin@users.noreply.github.com>

* Fixed ReadElement() using incorrect elementSize

---------

Co-authored-by: Jan <Laupetin@users.noreply.github.com>
This commit is contained in:
Electro Games 2024-09-06 09:46:21 -05:00 committed by GitHub
parent 8bab112afe
commit e0b28075a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 133 additions and 1 deletions

View File

@ -650,6 +650,8 @@ namespace
m_accessors.emplace_back(std::make_unique<UnsignedByteAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_SHORT)
m_accessors.emplace_back(std::make_unique<UnsignedShortAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_INT)
m_accessors.emplace_back(std::make_unique<UnsignedIntAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
else
throw GltfLoadException(std::format("Accessor has unsupported component type {}", static_cast<unsigned>(jAccessor.componentType)));
}

View File

@ -425,7 +425,115 @@ 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<unsigned>(temp[0]);
out[1] = static_cast<unsigned>(temp[1]);
out[2] = static_cast<unsigned>(temp[2]);
out[3] = static_cast<unsigned>(temp[3]);
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<JsonAccessorType> UnsignedIntAccessor::GetType() const
{
return m_type;
}
std::optional<JsonAccessorComponentType> 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<float>(temp[0]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[1] = static_cast<float>(temp[1]) / static_cast<float>(std::numeric_limits<uint32_t>::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<float>(temp[0]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[1] = static_cast<float>(temp[1]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[2] = static_cast<float>(temp[2]) / static_cast<float>(std::numeric_limits<uint32_t>::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<float>(temp[0]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[1] = static_cast<float>(temp[1]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[2] = static_cast<float>(temp[2]) / static_cast<float>(std::numeric_limits<uint32_t>::max());
out[3] = static_cast<float>(temp[3]) / static_cast<float>(std::numeric_limits<uint32_t>::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(uint32_t[4]), m_byte_offset))
return false;
out[0] = static_cast<unsigned>(temp[0]);

View File

@ -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<JsonAccessorType> GetType() const override;
[[nodiscard]] std::optional<JsonAccessorComponentType> 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