Skip to content

Commit

Permalink
Fix issue #67
Browse files Browse the repository at this point in the history
  • Loading branch information
jessey-git committed May 22, 2020
1 parent d5dc0bd commit 1d6e081
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions include/fx/gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1284,14 +1284,65 @@ namespace gltf
detail::WriteExtensions(json, sparse.extensionsAndExtras);
}

namespace detail
{
template <typename TType>
inline void WriteMinMaxConvert(nlohmann::json & json, Accessor const & accessor)
{
if (!accessor.min.empty())
{
auto & item = json["min"];
for (float v : accessor.min)
item.push_back(static_cast<TType>(v));
}

if (!accessor.max.empty())
{
auto & item = json["max"];
for (float v : accessor.max)
item.push_back(static_cast<TType>(v));
}
}

inline void WriteAccessorConvert(nlohmann::json & json, Accessor const & accessor)
{
switch (accessor.componentType)
{
case Accessor::ComponentType::Byte:
WriteMinMaxConvert<int8_t>(json, accessor);
break;
case Accessor::ComponentType::UnsignedByte:
WriteMinMaxConvert<uint8_t>(json, accessor);
break;
case Accessor::ComponentType::Short:
WriteMinMaxConvert<int16_t>(json, accessor);
break;
case Accessor::ComponentType::UnsignedShort:
WriteMinMaxConvert<uint16_t>(json, accessor);
break;
case Accessor::ComponentType::UnsignedInt:
WriteMinMaxConvert<uint32_t>(json, accessor);
break;
}
}
} // namespace detail

inline void to_json(nlohmann::json & json, Accessor const & accessor)
{
detail::WriteField("bufferView", json, accessor.bufferView, -1);
detail::WriteField("byteOffset", json, accessor.byteOffset, {});
detail::WriteField("componentType", json, accessor.componentType, Accessor::ComponentType::None);
detail::WriteField("count", json, accessor.count, {});
detail::WriteField("max", json, accessor.max);
detail::WriteField("min", json, accessor.min);
if (accessor.componentType == Accessor::ComponentType::Float)
{
detail::WriteField("max", json, accessor.max);
detail::WriteField("min", json, accessor.min);
}
else
{
detail::WriteAccessorConvert(json, accessor);
}

detail::WriteField("name", json, accessor.name);
detail::WriteField("normalized", json, accessor.normalized, false);
detail::WriteField("sparse", json, accessor.sparse);
Expand Down

0 comments on commit 1d6e081

Please sign in to comment.