Merge pull request #1682 from wanadev/glb2-no-bin-file

[glTF2] Correctly export images with bufferView
pull/1685/head
Kim Kulling 2018-01-06 09:18:52 +01:00 committed by GitHub
commit d91d10f694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 33 deletions

View File

@ -167,7 +167,9 @@ namespace glTF2 {
if (bv.byteStride != 0) { if (bv.byteStride != 0) {
obj.AddMember("byteStride", bv.byteStride, w.mAl); obj.AddMember("byteStride", bv.byteStride, w.mAl);
} }
obj.AddMember("target", int(bv.target), w.mAl); if (bv.target != 0) {
obj.AddMember("target", int(bv.target), w.mAl);
}
} }
inline void Write(Value& /*obj*/, Camera& /*c*/, AssetWriter& /*w*/) inline void Write(Value& /*obj*/, Camera& /*c*/, AssetWriter& /*w*/)
@ -177,17 +179,23 @@ namespace glTF2 {
inline void Write(Value& obj, Image& img, AssetWriter& w) inline void Write(Value& obj, Image& img, AssetWriter& w)
{ {
std::string uri; if (img.bufferView) {
if (img.HasData()) { obj.AddMember("bufferView", img.bufferView->index, w.mAl);
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType); obj.AddMember("mimeType", Value(img.mimeType, w.mAl).Move(), w.mAl);
uri += ";base64,";
Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
} }
else { else {
uri = img.uri; std::string uri;
} if (img.HasData()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
uri += ";base64,";
Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
}
else {
uri = img.uri;
}
obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl); obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl);
}
} }
namespace { namespace {
@ -569,13 +577,17 @@ namespace glTF2 {
throw DeadlyExportError("Could not open output file: " + std::string(path)); throw DeadlyExportError("Could not open output file: " + std::string(path));
} }
Ref<Buffer> bodyBuffer = mAsset.GetBodyBuffer();
if (bodyBuffer->byteLength > 0) {
rapidjson::Value glbBodyBuffer;
glbBodyBuffer.SetObject();
glbBodyBuffer.AddMember("byteLength", bodyBuffer->byteLength, mAl);
mDoc["buffers"].PushBack(glbBodyBuffer, mAl);
}
// Padding with spaces as required by the spec // Padding with spaces as required by the spec
uint32_t padding = 0x20202020; uint32_t padding = 0x20202020;
// Adapt JSON so that it is not pointing to an external file,
// as this is required by the GLB spec'.
mDoc["buffers"][0].RemoveMember("uri");
// //
// JSON chunk // JSON chunk
// //
@ -608,28 +620,25 @@ namespace glTF2 {
// //
uint32_t binaryChunkLength = 0; uint32_t binaryChunkLength = 0;
if (mAsset.buffers.Size() > 0) { if (bodyBuffer->byteLength > 0) {
Ref<Buffer> b = mAsset.buffers.Get(0u); binaryChunkLength = (bodyBuffer->byteLength + 3) & ~3; // Round up to next multiple of 4
if (b->byteLength > 0) { auto paddingLength = binaryChunkLength - bodyBuffer->byteLength;
binaryChunkLength = (b->byteLength + 3) & ~3; // Round up to next multiple of 4
auto paddingLength = binaryChunkLength - b->byteLength;
GLB_Chunk binaryChunk; GLB_Chunk binaryChunk;
binaryChunk.chunkLength = binaryChunkLength; binaryChunk.chunkLength = binaryChunkLength;
binaryChunk.chunkType = ChunkType_BIN; binaryChunk.chunkType = ChunkType_BIN;
AI_SWAP4(binaryChunk.chunkLength); AI_SWAP4(binaryChunk.chunkLength);
size_t bodyOffset = sizeof(GLB_Header) + sizeof(GLB_Chunk) + jsonChunk.chunkLength; size_t bodyOffset = sizeof(GLB_Header) + sizeof(GLB_Chunk) + jsonChunk.chunkLength;
outfile->Seek(bodyOffset, aiOrigin_SET); outfile->Seek(bodyOffset, aiOrigin_SET);
if (outfile->Write(&binaryChunk, 1, sizeof(GLB_Chunk)) != sizeof(GLB_Chunk)) { if (outfile->Write(&binaryChunk, 1, sizeof(GLB_Chunk)) != sizeof(GLB_Chunk)) {
throw DeadlyExportError("Failed to write body data header!"); throw DeadlyExportError("Failed to write body data header!");
} }
if (outfile->Write(b->GetPointer(), 1, b->byteLength) != b->byteLength) { if (outfile->Write(bodyBuffer->GetPointer(), 1, bodyBuffer->byteLength) != bodyBuffer->byteLength) {
throw DeadlyExportError("Failed to write body data!"); throw DeadlyExportError("Failed to write body data!");
} }
if (paddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) { if (paddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) {
throw DeadlyExportError("Failed to write body data padding!"); throw DeadlyExportError("Failed to write body data padding!");
}
} }
} }

View File

@ -109,6 +109,10 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
mAsset.reset( new Asset( pIOSystem ) ); mAsset.reset( new Asset( pIOSystem ) );
if (isBinary) {
mAsset->SetAsBinary();
}
ExportMetadata(); ExportMetadata();
ExportMaterials(); ExportMaterials();