glTF2: fix "file size doesn't match" issue when importing GLB in Blender

pull/3246/head
FRICOTEAUX 2020-05-28 11:05:38 +02:00
parent 471f9b23c9
commit 87584eb007
1 changed files with 6 additions and 3 deletions

View File

@ -672,10 +672,13 @@ namespace glTF2 {
// Binary chunk
//
int GLB_Chunk_count = 1;
uint32_t binaryChunkLength = 0;
if (bodyBuffer->byteLength > 0) {
binaryChunkLength = (bodyBuffer->byteLength + 3) & ~3; // Round up to next multiple of 4
auto paddingLength = binaryChunkLength - bodyBuffer->byteLength;
auto curPaddingLength = binaryChunkLength - bodyBuffer->byteLength;
++GLB_Chunk_count;
GLB_Chunk binaryChunk;
binaryChunk.chunkLength = binaryChunkLength;
@ -690,7 +693,7 @@ namespace glTF2 {
if (outfile->Write(bodyBuffer->GetPointer(), 1, bodyBuffer->byteLength) != bodyBuffer->byteLength) {
throw DeadlyExportError("Failed to write body data!");
}
if (paddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) {
if (curPaddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) {
throw DeadlyExportError("Failed to write body data padding!");
}
}
@ -705,7 +708,7 @@ namespace glTF2 {
header.version = 2;
AI_SWAP4(header.version);
header.length = uint32_t(sizeof(GLB_Header) + 2 * sizeof(GLB_Chunk) + jsonChunkLength + binaryChunkLength);
header.length = uint32_t(sizeof(GLB_Header) + GLB_Chunk_count * sizeof(GLB_Chunk) + jsonChunkLength + binaryChunkLength);
AI_SWAP4(header.length);
outfile->Seek(0, aiOrigin_SET);