mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 00:02:55 +00:00
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:
parent
8bab112afe
commit
e0b28075a6
@ -650,6 +650,8 @@ namespace
|
|||||||
m_accessors.emplace_back(std::make_unique<UnsignedByteAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
|
m_accessors.emplace_back(std::make_unique<UnsignedByteAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
|
||||||
else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_SHORT)
|
else if (jAccessor.componentType == JsonAccessorComponentType::UNSIGNED_SHORT)
|
||||||
m_accessors.emplace_back(std::make_unique<UnsignedShortAccessor>(bufferView, jAccessor.type, byteOffset, jAccessor.count));
|
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
|
else
|
||||||
throw GltfLoadException(std::format("Accessor has unsupported component type {}", static_cast<unsigned>(jAccessor.componentType)));
|
throw GltfLoadException(std::format("Accessor has unsupported component type {}", static_cast<unsigned>(jAccessor.componentType)));
|
||||||
}
|
}
|
||||||
|
@ -425,7 +425,115 @@ bool UnsignedShortAccessor::GetUnsignedVec4(const size_t index, unsigned (&out)[
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint16_t temp[4];
|
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;
|
return false;
|
||||||
|
|
||||||
out[0] = static_cast<unsigned>(temp[0]);
|
out[0] = static_cast<unsigned>(temp[0]);
|
||||||
|
@ -130,4 +130,26 @@ namespace gltf
|
|||||||
size_t m_byte_offset;
|
size_t m_byte_offset;
|
||||||
size_t m_count;
|
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
|
} // namespace gltf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user