Skip to content

Commit

Permalink
Fix: Allow the GLB BIN chunk to be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Mar 2, 2024
1 parent d133ae9 commit 4d32070
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3967,33 +3967,39 @@ fg::Expected<fg::Asset> fg::Parser::loadGltfBinary(GltfDataBuffer* buffer, fs::p
return Expected<Asset>(Error::InvalidGLB);
}

if (hasBit(options, Options::LoadGLBBuffers)) {
if (config.mapCallback != nullptr) {
auto info = config.mapCallback(binaryChunk.chunkLength, config.userPointer);
if (info.mappedMemory != nullptr) {
read(info.mappedMemory, binaryChunk.chunkLength);
if (config.unmapCallback != nullptr) {
config.unmapCallback(&info, config.userPointer);
}
glbBuffer = sources::CustomBuffer { info.customId, MimeType::None };
}
} else {
StaticVector<std::uint8_t> binaryData(binaryChunk.chunkLength);
read(binaryData.data(), binaryChunk.chunkLength);

sources::Array vectorData = {
std::move(binaryData),
MimeType::GltfBuffer,
};
glbBuffer = std::move(vectorData);
}
} else {
const span<const std::byte> glbBytes(reinterpret_cast<std::byte*>(buffer->bufferPointer + offset), binaryChunk.chunkLength);
sources::ByteView glbByteView = {};
glbByteView.bytes = glbBytes;
glbByteView.mimeType = MimeType::GltfBuffer;
glbBuffer = glbByteView;
}
// The binary chunk is allowed to be empty:
// When the binary buffer is empty or when it is stored by other means,
// this chunk SHOULD be omitted.
if (binaryChunk.chunkLength != 0) {
if (hasBit(options, Options::LoadGLBBuffers)) {
if (config.mapCallback != nullptr) {
auto info = config.mapCallback(binaryChunk.chunkLength, config.userPointer);
if (info.mappedMemory != nullptr) {
read(info.mappedMemory, binaryChunk.chunkLength);
if (config.unmapCallback != nullptr) {
config.unmapCallback(&info, config.userPointer);
}
glbBuffer = sources::CustomBuffer{info.customId, MimeType::None};
}
} else {
StaticVector<std::uint8_t> binaryData(binaryChunk.chunkLength);
read(binaryData.data(), binaryChunk.chunkLength);

sources::Array vectorData = {
std::move(binaryData),
MimeType::GltfBuffer,
};
glbBuffer = std::move(vectorData);
}
} else {
const span<const std::byte> glbBytes(reinterpret_cast<std::byte *>(buffer->bufferPointer + offset),
binaryChunk.chunkLength);
sources::ByteView glbByteView = {};
glbByteView.bytes = glbBytes;
glbByteView.mimeType = MimeType::GltfBuffer;
glbBuffer = glbByteView;
}
}
}

return parse(root, categories);
Expand Down

0 comments on commit 4d32070

Please sign in to comment.