Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Unsigned Int accessors #245

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ObjLoading/XModel/Gltf/GltfLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
110 changes: 109 additions & 1 deletion src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
22 changes: 22 additions & 0 deletions src/ObjLoading/XModel/Gltf/Internal/GltfAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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