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,8 +167,10 @@ namespace glTF2 {
if (bv.byteStride != 0) { if (bv.byteStride != 0) {
obj.AddMember("byteStride", bv.byteStride, w.mAl); obj.AddMember("byteStride", bv.byteStride, w.mAl);
} }
if (bv.target != 0) {
obj.AddMember("target", int(bv.target), w.mAl); 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,6 +179,11 @@ namespace glTF2 {
inline void Write(Value& obj, Image& img, AssetWriter& w) inline void Write(Value& obj, Image& img, AssetWriter& w)
{ {
if (img.bufferView) {
obj.AddMember("bufferView", img.bufferView->index, w.mAl);
obj.AddMember("mimeType", Value(img.mimeType, w.mAl).Move(), w.mAl);
}
else {
std::string uri; std::string uri;
if (img.HasData()) { if (img.HasData()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType); uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
@ -189,6 +196,7 @@ namespace glTF2 {
obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl); obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl);
} }
}
namespace { namespace {
inline void SetTexBasic(TextureInfo t, Value& tex, MemoryPoolAllocator<>& al) inline void SetTexBasic(TextureInfo t, Value& tex, MemoryPoolAllocator<>& al)
@ -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,11 +620,9 @@ 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;
@ -624,14 +634,13 @@ namespace glTF2 {
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!");
} }
} }
}
// //
// Header // Header

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();