14#include <gsl/span_ext>
18inline int32_t
getComponentSize(
const fx::gltf::Accessor::ComponentType& componentType) {
19 switch (componentType) {
20 case fx::gltf::Accessor::ComponentType::Byte:
21 case fx::gltf::Accessor::ComponentType::UnsignedByte:
23 case fx::gltf::Accessor::ComponentType::Short:
24 case fx::gltf::Accessor::ComponentType::UnsignedShort:
27 case fx::gltf::Accessor::ComponentType::UnsignedInt:
28 case fx::gltf::Accessor::ComponentType::Float:
35inline int32_t
getTypeSize(
const fx::gltf::Accessor::Type& ty) {
37 case fx::gltf::Accessor::Type::Scalar:
39 case fx::gltf::Accessor::Type::Vec2:
41 case fx::gltf::Accessor::Type::Vec3:
43 case fx::gltf::Accessor::Type::Vec4:
44 case fx::gltf::Accessor::Type::Mat2:
46 case fx::gltf::Accessor::Type::Mat3:
48 case fx::gltf::Accessor::Type::Mat4:
57 const auto& accessor = model.accessors[id];
61 const auto elsize = tsize * csize;
63 if (elsize !=
sizeof(T)) {
65 "[{}] Accessor type mismatch. Expect to read {} bytes, but got type of {} bytes with {} components",
73 const auto& view = model.bufferViews[accessor.bufferView];
74 const auto stride = view.byteStride == 0 ? elsize : view.byteStride;
76 const auto& buf = model.buffers[view.buffer];
77 auto bytes = std::as_bytes(std::span<const uint8_t>(buf.data));
78 bytes = bytes.subspan(view.byteOffset, view.byteLength);
79 bytes = bytes.subspan(accessor.byteOffset);
81 std::vector<T> r(accessor.count);
82 for (
size_t i = 0; i < accessor.count; i++) {
83 std::memcpy(
static_cast<void*
>(&r[i]), &bytes[i * stride], elsize);
91 const auto& accessor = model.accessors[id];
92 if (accessor.count == 0) {
93 MT_LOGW(
"Accessor count zero.");
99 const auto elsize = tsize * csize;
101 if (elsize !=
sizeof(T)) {
103 "[{}] Accessor type mismatch. Expect to read {} bytes, but got type of {} bytes with {} components",
111 const auto& view = model.bufferViews[accessor.bufferView];
112 const auto stride = view.byteStride == 0 ? elsize : view.byteStride;
114 const auto& buf = model.buffers[view.buffer];
115 auto bytes = std::as_bytes(std::span<const uint8_t>(buf.data));
116 bytes = bytes.subspan(view.byteOffset, view.byteLength);
117 bytes = bytes.subspan(accessor.byteOffset);
119 std::vector<T> r(accessor.count);
120 for (
size_t i = 0; i < accessor.count; i++) {
121 std::memcpy(
static_cast<void*
>(&r[i]), &bytes[i * stride], elsize);
130 MT_THROW(
"Unsupported data type {}",
typeid(T).name());
136 fx::gltf::Document& model,
138 const bool align =
false,
139 const bool normalized =
false) {
145 if (model.buffers.empty()) {
146 model.buffers.resize(1);
150 const size_t bufferIdx = 0;
151 auto& buffer = model.buffers.front();
154 const size_t bufferDataStart = buffer.data.size();
155 const auto elementSize =
sizeof(T);
156 auto alignedElementSize = elementSize;
157 auto dataSize = data.size() * alignedElementSize;
160 if (align && alignedElementSize % 4 != 0) {
162 alignedElementSize = ((elementSize / 4) + 1) * 4;
163 dataSize = data.size() * alignedElementSize;
164 buffer.data.resize(bufferDataStart + dataSize);
166 for (
size_t e = 0; e < data.size(); e++) {
167 std::memcpy(&buffer.data[bufferDataStart + alignedElementSize * e], &data[e], elementSize);
169 buffer.byteLength =
static_cast<uint32_t
>(buffer.data.size());
172 buffer.data.resize(bufferDataStart + dataSize);
173 std::memcpy(&buffer.data[bufferDataStart], data.data(), dataSize);
174 buffer.byteLength =
static_cast<uint32_t
>(buffer.data.size());
178 const size_t bufferViewIdx = model.bufferViews.size();
179 model.bufferViews.emplace_back();
180 auto& bufferView = model.bufferViews.back();
182 bufferView.buffer =
static_cast<int32_t
>(bufferIdx);
183 bufferView.byteLength =
static_cast<uint32_t
>(dataSize);
184 bufferView.byteOffset =
static_cast<uint32_t
>(bufferDataStart);
185 if (align && elementSize % 4 != 0) {
186 bufferView.byteStride =
static_cast<uint32_t
>(alignedElementSize);
188 bufferView.byteStride = 0;
192 const size_t accessorIdx = model.accessors.size();
193 model.accessors.emplace_back();
194 auto& accessor = model.accessors.back();
198 accessor.bufferView =
static_cast<int32_t
>(bufferViewIdx);
199 accessor.byteOffset = 0;
200 accessor.count =
static_cast<uint32_t
>(data.size());
201 accessor.normalized = normalized;
203 return static_cast<int32_t
>(accessorIdx);
208 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedInt;
209 accessor.type = fx::gltf::Accessor::Type::Scalar;
214 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
215 accessor.type = fx::gltf::Accessor::Type::Scalar;
220 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
221 accessor.type = fx::gltf::Accessor::Type::Vec2;
226 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
227 accessor.type = fx::gltf::Accessor::Type::Vec3;
232 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
233 accessor.type = fx::gltf::Accessor::Type::Vec4;
238 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedByte;
239 accessor.type = fx::gltf::Accessor::Type::Vec3;
244 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedInt;
245 accessor.type = fx::gltf::Accessor::Type::Vec3;
250 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedShort;
251 accessor.type = fx::gltf::Accessor::Type::Vec4;
256 accessor.componentType = fx::gltf::Accessor::ComponentType::UnsignedByte;
257 accessor.type = fx::gltf::Accessor::Type::Vec4;
262 accessor.componentType = fx::gltf::Accessor::ComponentType::Float;
263 accessor.type = fx::gltf::Accessor::Type::Mat4;
270 switch (interpolation) {
272 return fx::gltf::Animation::Sampler::Type::Step;
274 return fx::gltf::Animation::Sampler::Type::Linear;
276 return fx::gltf::Animation::Sampler::Type::Step;
281 fx::gltf::Document& model,
282 fx::gltf::Animation& anim,
284 const std::span<float>& timestamps,
287 data.size() == timestamps.size(),
"data: {}, timestmaps: {}", data.size(), timestamps.size());
290 const auto index =
static_cast<int32_t
>(anim.samplers.size());
291 anim.samplers.emplace_back();
292 auto& sampler = anim.samplers.back();
303 fx::gltf::Document& model,
304 fx::gltf::Animation& anim,
306 const int32_t timestampAccessor,
309 const auto index =
static_cast<int32_t
>(anim.samplers.size());
310 anim.samplers.emplace_back();
311 auto& sampler = anim.samplers.back();
313 sampler.input = timestampAccessor;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
void setAccessorType< const Vector4b >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector4f >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3i >(fx::gltf::Accessor &accessor)
int32_t getTypeSize(const fx::gltf::Accessor::Type &ty)
int32_t createAccessorBuffer(fx::gltf::Document &model, std::span< T > data, const bool align=false, const bool normalized=false)
std::vector< T > copyAlignedAccessorBuffer(const fx::gltf::Document &model, int32_t id)
GltfAnimationInterpolation
How animation samplers written to a GLTF file interpolate between their keys.
@ Linear
Interpolate between keys (GLTF "LINEAR"; slerp for rotations).
@ Step
Hold each key until the next one (GLTF "STEP").
std::vector< T > copyAccessorBuffer(const fx::gltf::Document &model, int32_t id)
void setAccessorType< const Matrix4f >(fx::gltf::Accessor &accessor)
void setAccessorType< const int32_t >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector2f >(fx::gltf::Accessor &accessor)
int32_t getComponentSize(const fx::gltf::Accessor::ComponentType &componentType)
void setAccessorType(fx::gltf::Accessor &)
void setAccessorType< const Vector4s >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3b >(fx::gltf::Accessor &accessor)
fx::gltf::Animation::Sampler::Type toGltfSamplerType(GltfAnimationInterpolation interpolation)
Converts the save option to the GLTF sampler interpolation type.
void setAccessorType< const float >(fx::gltf::Accessor &accessor)
void setAccessorType< const Vector3f >(fx::gltf::Accessor &accessor)
int32_t createSampler(fx::gltf::Document &model, fx::gltf::Animation &anim, std::span< T > data, const std::span< float > ×tamps, GltfAnimationInterpolation interpolation)