From 87584eb007df4dda029cbac5e328941742129d5d Mon Sep 17 00:00:00 2001 From: FRICOTEAUX Date: Thu, 28 May 2020 11:05:38 +0200 Subject: [PATCH] glTF2: fix "file size doesn't match" issue when importing GLB in Blender --- code/glTF2/glTF2AssetWriter.inl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/code/glTF2/glTF2AssetWriter.inl b/code/glTF2/glTF2AssetWriter.inl index 02c14980d..f9bbb7ef7 100644 --- a/code/glTF2/glTF2AssetWriter.inl +++ b/code/glTF2/glTF2AssetWriter.inl @@ -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);